[Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/imote2/tos/sensorboards/Common CoarseDecimationM.nc, NONE, 1.1

Robbie Adler radler at users.sourceforge.net
Wed Oct 25 08:03:55 PDT 2006


Update of /cvsroot/tinyos/tinyos-1.x/contrib/imote2/tos/sensorboards/Common
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv27317/sensorboards/Common

Added Files:
	CoarseDecimationM.nc 
Log Message:
initial release of driver framework and basicsensorboard driver

--- NEW FILE: CoarseDecimationM.nc ---
module CoarseDecimationM{
  provides{
    interface SensorData as OutData[uint8_t dataChannel];
    
  }
  uses{
    interface SensorData as InData[uint8_t dataChannel];
  }
}

implementation{
#include "paramtask.h"

#ifndef MAX_COURSE_DECIMATION_CHANNELS
#define MAX_COURSE_DECIMATION_CHANNELS (4)
#endif
  
  void processBuffer(uint32_t arg);
  DEFINE_PARAMTASK(processBuffer);
  
   
  extern _PTR memalign(size_t, size_t) __attribute__((C,spontaneous));
  
  typedef struct {
    uint32_t k;
    bool kValid;
    uint8_t width;
    bool widthValid;
    bool finished;
    int outstandingCount;
  }collectionInfo_t;

  typedef struct{
    uint8_t *buffer;
    uint32_t numSamples;
  } outDataBufferInfo_t;
  

  typedef struct{
    uint8_t *buffer;
    uint64_t timestamp;
    float ADCScale;
    float ADCOffset;
    uint32_t numSamples;
    uint8_t dataChannel;
  } inDataBufferInfo_t;
  
  outDataBufferInfo_t gOutDataInfo[MAX_COURSE_DECIMATION_CHANNELS];
 
  collectionInfo_t gCollectionInfo[MAX_COURSE_DECIMATION_CHANNELS];
  downsampleTempBuffer_t *downsampleTempBuffer[MAX_COURSE_DECIMATION_CHANNELS];
  downsampleStates_t gDownsampleStates[4];

  void processBuffer(uint32_t arg){
    uint8_t *ret;
    inDataBufferInfo_t *pBI;
    int dsRet;
    uint8_t dataChannel;
    
    pBI = (inDataBufferInfo_t *)arg;
    
    if(pBI == NULL){
      trace(DBG_USR1,"FATAL ERROR:  CoarseDecimationM.processBuffer received NULL arg\r\n");
      return;
    }
    
    dataChannel  = pBI->dataChannel;
    
    gCollectionInfo[dataChannel].outstandingCount--;
    if(gCollectionInfo[dataChannel].finished){
      free(pBI->buffer);
      free(pBI);
      return;
    }
    
    if(pBI->numSamples != (gOutDataInfo[dataChannel].numSamples * gCollectionInfo[dataChannel].k)){
      trace(DBG_USR1,"FATAL ERROR:  CoarseDecimationM received %d samples when it expected %d samples\r\n",
	    pBI->numSamples,
	    gOutDataInfo[dataChannel].numSamples*gCollectionInfo[dataChannel].k);
      return;
    }
    
    if((dsRet = downsample(&gDownsampleStates[dataChannel], 
			   (short int *)pBI->buffer, 
			   pBI->numSamples, 
			   (short int *)gOutDataInfo[dataChannel].buffer, 
			   gCollectionInfo[dataChannel].k)) != 1){
      
      trace(DBG_USR1,"FATAL ERROR:  downsampling failed with return code %d\r\n",dsRet);
      return;
    }
    //successful;
    ret = signal OutData.getSensorDataDone[dataChannel](gOutDataInfo[dataChannel].buffer, gOutDataInfo[dataChannel].numSamples, pBI->timestamp, pBI->ADCScale, pBI->ADCOffset);
    if(ret == NULL){
      //we're done!
      free(downsampleTempBuffer[dataChannel]);
      gCollectionInfo[dataChannel].kValid = FALSE;
      gCollectionInfo[dataChannel].widthValid = FALSE;
      gCollectionInfo[dataChannel].finished = TRUE;
    }
    else{
      gOutDataInfo[dataChannel].buffer = ret;
    }
    free(pBI->buffer);
    free(pBI);
    return;
  }
  
  
  command result_t OutData.getOutputUOM[uint8_t dataChannel](uint8_t *UOM){
    if(dataChannel < MAX_COURSE_DECIMATION_CHANNELS){
      return call InData.getOutputUOM[dataChannel](UOM);
    }
    else{
      return FAIL;
    }
  }


  command result_t OutData.setSensorType[uint8_t dataChannel](uint32_t sensorType){
    if(dataChannel < MAX_COURSE_DECIMATION_CHANNELS){
      return call InData.setSensorType[dataChannel](sensorType);
    }
    else{
      return FAIL;
    }
  }

  command result_t OutData.setSamplingRate[uint8_t dataChannel](uint32_t requestedSamplingRate, uint32_t *actualSamplingRate){
    if(dataChannel < MAX_COURSE_DECIMATION_CHANNELS){
      //see what the board will be returning us for this samplingRate
      if(call InData.setSamplingRate[dataChannel](requestedSamplingRate, actualSamplingRate) == SUCCESS){
	if((*actualSamplingRate) >= requestedSamplingRate){
	  gCollectionInfo[dataChannel].k = *actualSamplingRate/requestedSamplingRate;
	  gCollectionInfo[dataChannel].kValid = TRUE;
	  *actualSamplingRate = *actualSamplingRate/gCollectionInfo[dataChannel].k;
	}
	return SUCCESS;
      }
    }
    return FAIL;
  }
  
  command result_t OutData.setSampleWidth[uint8_t dataChannel](uint8_t requestedSampleWidth){
    if(dataChannel < MAX_COURSE_DECIMATION_CHANNELS){
      if(call InData.setSampleWidth[dataChannel](requestedSampleWidth)){
	gCollectionInfo[dataChannel].width = requestedSampleWidth;
	gCollectionInfo[dataChannel].widthValid = TRUE;
	return SUCCESS;
      }
      else{
	gCollectionInfo[dataChannel].widthValid = FALSE;
      }
    }
    return FAIL;
  }
  
  command result_t OutData.getSensorData[uint8_t dataChannel](uint8_t *buffer, uint32_t numSamples){
    uint8_t *newBuffer;
    uint32_t newNumSamples;
    
    if(dataChannel < MAX_COURSE_DECIMATION_CHANNELS){
      if(gCollectionInfo[dataChannel].kValid == FALSE){
	trace(DBG_USR1,"FATAL ERROR:  SensorData.setSamplingRate was not called on data channel %d\r\n",dataChannel);
	return FAIL;
      }
      if(gCollectionInfo[dataChannel].widthValid == FALSE){
	trace(DBG_USR1,"FATAL ERROR:  SensorData.setSampleWidth was not called on data channel %d\r\n",dataChannel);
	return FAIL;
      }
      
      if(gCollectionInfo[dataChannel].outstandingCount != 0){
	trace(DBG_USR1,"FATAL ERROR:  CoarseDecimation found acquisition in progress on channel %d while trying to start a new one: outstandingCount = %d\r\n",dataChannel, gCollectionInfo[dataChannel].outstandingCount);
	return FAIL;
      }
      
      downsampleTempBuffer[dataChannel] = memalign(8,(numSamples*gCollectionInfo[dataChannel].width*gCollectionInfo[dataChannel].k)/4);
      if(downsampleTempBuffer[dataChannel] == NULL){
	trace(DBG_USR1,"FATAL ERROR:  Unable to allocate temporary downsampling buffer\r\n");
	return FAIL;
      }
      
      downsampleInit(&gDownsampleStates[dataChannel],downsampleTempBuffer[dataChannel]); 
      
      gOutDataInfo[dataChannel].buffer = buffer;
      gOutDataInfo[dataChannel].numSamples = numSamples;
      
      newNumSamples = numSamples * gCollectionInfo[dataChannel].k;
      newBuffer = memalign(32, newNumSamples * gCollectionInfo[dataChannel].width);
      
      if(newBuffer){
	gCollectionInfo[dataChannel].finished = FALSE;
	gCollectionInfo[dataChannel].outstandingCount ++;
	return call InData.getSensorData[dataChannel](newBuffer, newNumSamples);
      }
      else{
	free(downsampleTempBuffer[dataChannel]);
	trace(DBG_USR1,"FATAL ERROR:  Unable to allocate downsampling buffer\r\n");
	return FAIL;
      }
    }
    else{
      return FAIL;
    }
  }
  
  event uint8_t *InData.getSensorDataDone[uint8_t dataChannel](uint8_t *buffer, uint32_t numSamples, uint64_t timestamp, float ADCScale, float ADCOffset){
    
    inDataBufferInfo_t *pBI;
    uint8_t * newBuffer;
    
    if(gCollectionInfo[dataChannel].finished){
      //drop samples if we're done.
      gCollectionInfo[dataChannel].outstandingCount--;
      return NULL;
    }
    
    pBI = malloc(sizeof(*pBI));
    
    if(pBI){
      pBI->buffer = buffer;
      pBI->timestamp = timestamp;
      pBI->ADCScale = ADCScale;
      pBI->ADCOffset = ADCOffset;
      pBI->numSamples = numSamples;
      pBI->dataChannel = dataChannel;
      POST_PARAMTASK(processBuffer,pBI);
      
      newBuffer =  memalign(32, numSamples * gCollectionInfo[dataChannel].width);
      
      if(newBuffer == NULL){
	trace(DBG_USR1,"FATAL ERROR:  Unable to allocate downsampling buffer\r\n");
      }
      else{
	gCollectionInfo[dataChannel].outstandingCount++;
      }
      return newBuffer;
    }
    else{
      trace(DBG_USR1,"FATAL ERROR:  Unable to allocate temporary structure\r\n");
      return NULL;
    }
  }

  /****************************
DEFAULT COMMANDS
  *****************************/

  default command result_t InData.getOutputUOM[uint8_t dataChannel](uint8_t *UOM){
    return FAIL;
  }
  

  default command result_t InData.setSensorType[uint8_t dataChannel](uint32_t sensorType){
    return FAIL;
  }

  default command result_t InData.setSamplingRate[uint8_t dataChannel](uint32_t requestedSamplingRate, uint32_t *actualSamplingRate){
    return FAIL;
  }

  default command result_t InData.setSampleWidth[uint8_t dataChannel](uint8_t requestedSampleWidth){
    return FAIL;
  }
  
  default command result_t InData.getSensorData[uint8_t dataChannel](uint8_t *buffer, uint32_t numSamples){
    return FAIL;
  }
  
  default event uint8_t *OutData.getSensorDataDone[uint8_t dataChannel](uint8_t *buffer, uint32_t numSamples, uint64_t timestamp, float ADCScale, float ADCOffset){
    return NULL;
  }
  
}



More information about the Tinyos-contrib-commits mailing list