Pull request for i.a. sensor buffer template

Dependencies:   BLE_API MPU6050 mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MeasurementBufferTemplate.h Source File

MeasurementBufferTemplate.h

00001 #ifndef MEASUREMENTBUFFERTEMPLATE_H
00002 #define MEASUREMENTBUFFERTEMPLATE_H
00003 
00004 #include "AppTime.h"
00005 
00006 #define IncrementInRange(x, range) (((x+1) >= range) ? 0 : x+1)
00007 
00008 
00009 template<typename T_BufferType, unsigned int T_BufferLength, unsigned int T_ReadingsInUpdate, unsigned int T_ReadInterval, T_BufferType (*T_MeasurementFunction)()>
00010 class MeasurementBufferTemplate {
00011 public:
00012     struct BulkUpdate {
00013         uint32_t requestedTime;             //intended time of first sensor-reading, specified by request
00014         uint32_t initialTime;               //provided time of first sensor-reading
00015         uint32_t sensorReadingInterval;     // time difference between sensor-readings (seconds)
00016         uint32_t numberOfReadings;
00017         T_BufferType readings[T_ReadingsInUpdate];
00018 
00019         BulkUpdate(const time_t & requestedTime=0, const time_t & initialTime=0, const time_t & sensorReadingInterval=0) :
00020                 requestedTime(requestedTime),
00021                 initialTime(initialTime),
00022                 sensorReadingInterval(sensorReadingInterval),
00023                 numberOfReadings(0)
00024         {
00025 
00026         }
00027     };
00028 
00029 public:
00030     MeasurementBufferTemplate() : 
00031             buffer(new T_BufferType[T_BufferLength]),
00032             indexOfNewestReading(T_BufferLength-1),
00033             timeOfLastReading(0),
00034             nrSensorReadingsPerformed(0)
00035     {
00036 
00037     }
00038 
00039     virtual ~MeasurementBufferTemplate(){
00040         delete [] buffer;
00041     }
00042 
00043     T_BufferType performMeasurement(){
00044         //Get data
00045         T_BufferType measurementData = T_MeasurementFunction();
00046 
00047         //Update buffer and meta-data
00048         indexOfNewestReading = IncrementInRange(indexOfNewestReading, T_BufferLength);
00049         timeOfLastReading = AppTime::getTime();
00050         buffer[indexOfNewestReading] = measurementData;
00051 
00052         if(nrSensorReadingsPerformed < T_BufferLength){     //doesn't matter what the value is past this point, so only increment when relevant to prevent overflow
00053             nrSensorReadingsPerformed++;
00054         }
00055 
00056         return measurementData;
00057     }
00058 
00059     BulkUpdate getBulkUpdate(const time_t & requestedTime){
00060         if(timeOfLastReading < requestedTime){
00061             //no data available yet for requested time
00062             return BulkUpdate(requestedTime);
00063         }
00064 
00065         //find sensor-reading from where to start copying data
00066         float tDiff = timeOfLastReading - (float)requestedTime;
00067         unsigned int periodsPassed = tDiff / T_ReadInterval;
00068 
00069         //if request is made for data that no longer exists (i.e. it was overwritten already), then just present the oldest data we have
00070         if(periodsPassed >= T_BufferLength){
00071             periodsPassed = T_BufferLength-1;
00072         }
00073 
00074         //If more sensor-readings are requested than have been read: adjust request to all readings that have been read
00075         if(periodsPassed >= nrSensorReadingsPerformed){
00076             periodsPassed = nrSensorReadingsPerformed-1;
00077         }
00078 
00079         //set time of initial sensorReading
00080         float timePassed = periodsPassed * T_ReadInterval;
00081         time_t initialTime = 0;
00082         if(timePassed > timeOfLastReading){
00083             //Shouldn't happen
00084             initialTime = 0;
00085         } else {
00086             initialTime = timeOfLastReading - timePassed;
00087         }
00088 
00089         //find the startIndex of the stored data
00090         int startIndex = (int)indexOfNewestReading - periodsPassed;     // n+1 sensor-readings read for n = periodsPassed
00091         if(startIndex < 0){
00092             //count backwards
00093             startIndex = T_BufferLength - abs(startIndex);
00094                 //startIndex should now be >= 0, because (abs(startIndex) + 1) <= T_BufferLength
00095         }
00096 
00097         //copy data to BulkUpdate
00098         BulkUpdate ret(requestedTime, initialTime, T_ReadInterval);
00099         unsigned int readIndex = startIndex;
00100         for(unsigned int i=0; i < T_ReadingsInUpdate; i++){
00101             //Copy reading
00102             ret.readings[i] = buffer[readIndex];
00103             ret.numberOfReadings++;
00104 
00105             //increment read index
00106             readIndex = IncrementInRange(readIndex, T_BufferLength);
00107             
00108             if(readIndex == IncrementInRange(indexOfNewestReading, T_BufferLength)){
00109                 //if this iteration just read the newest temperature-sensor-reading: stop
00110                 break;
00111             }
00112         }
00113 
00114         return ret;
00115     }
00116 
00117 private:
00118     T_BufferType * buffer;
00119     unsigned int indexOfNewestReading;
00120     float timeOfLastReading;
00121     uint16_t nrSensorReadingsPerformed;
00122 };
00123 
00124 
00125 #endif /* MEASUREMENTBUFFERTEMPLATE_H */