fuck this

Dependencies:   BMP280

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Sampling.cpp Source File

Sampling.cpp

00001 #include "Sampling.h"
00002 #include "TimeInterface.h"
00003 #include "mbed.h"
00004 #include "rtos.h"
00005 
00006 //Thread Sync Tools
00007 Mutex tempReadingsLock;
00008 Mutex presReadingsLock;
00009 Mutex LDRReadingsLock;
00010 Mutex timeReadingsLock;
00011 
00012 //Buffers with zero inital value
00013 float tempReadings[BUFFERSIZE] = {};
00014 float presReadings[BUFFERSIZE] = {};
00015 float LDRReadings[BUFFERSIZE] = {};
00016 time_t timeReadings[BUFFERSIZE] = {};
00017 
00018 unsigned short SAMPLERATE = 15; //Set initial sample rate.
00019 
00020 Thread t1; //Sample Enviromental Sensor
00021 Thread t2; //Sample LDR Sensor
00022 
00023 Ticker sampleRate;          
00024 Timeout SampleLEDTimeout;   
00025 
00026 bool NewEnvSample;  //Is there new data from the envirom sensor to output?
00027 bool NewLDRSample;  //Is there new data from the LDR to output?
00028 
00029 //Index
00030 volatile unsigned short nextIndex = 0;
00031 volatile unsigned short currentIndex = 0;
00032 volatile unsigned short oldestIndex = 0;
00033 
00034 bool firstSample;
00035 
00036 void SampleTimerISR(void)
00037 {
00038     //Flag Threads
00039     t1.signal_set(1);
00040     t2.signal_set(1);
00041     SamplingLED = 1;
00042     SampleLEDTimeout.attach(&FlipSamplingLED,0.1); //To turn LED off
00043 }
00044 
00045 void ConfigThreadsAndIR(void)
00046 {
00047     NewEnvSample = false;  //Reset
00048     NewLDRSample = false;  //Reset
00049 
00050     t1.start(&ThreadSampleEnvSensor); //Start Threads
00051     t2.start(&ThreadSampleLDR);
00052 
00053     Thread t1(osPriorityRealtime);  //Higher priority
00054     Thread t2(osPriorityRealtime);
00055 
00056     sampleRate.attach(&SampleTimerISR, SAMPLERATE); //15 second interval
00057 }
00058 
00059 void AddTempSample(float temp)
00060 {
00061     tempReadingsLock.lock();        //Take the key
00062     tempReadings[nextIndex] = temp; //Add the sample after the most recent
00063     tempReadingsLock.unlock();      // Release the key
00064 }
00065 
00066 void AddPresSample(float pres)
00067 {
00068     presReadingsLock.lock();        //Take the key
00069     presReadings[nextIndex] = pres; //Add to register
00070     presReadingsLock.unlock();      //Release the key
00071 }
00072 
00073 void ThreadSampleEnvSensor(void)
00074 {
00075     while (true) {
00076         Thread::signal_wait(1); //Wait for signal 1
00077         //Get readings
00078         float temp = sensor.getTemperature();
00079         float pres = sensor.getPressure();
00080         AddPresSample(pres);    //Add value to register
00081         AddTempSample(temp);    //Add value to register
00082         NewEnvSample = true;    //Signal to main thread
00083     }
00084 }
00085 
00086 void AddLDRSample(float LDRval)
00087 {
00088     LDRReadingsLock.lock();             //Take the key
00089     LDRReadings[nextIndex] = LDRval;    //Add the sample after the most recent
00090     LDRReadingsLock.unlock();           // Release the key
00091 }
00092 
00093 void AddTimeSample(time_t sampledTime)
00094 {
00095     timeReadingsLock.lock();                //Take the key
00096     timeReadings[nextIndex] = sampledTime;  //Add the sample after the most recent
00097     timeReadingsLock.unlock();              // Release the key
00098 }
00099 
00100 void ThreadSampleLDR(void)
00101 {
00102     while (true) {
00103         Thread::signal_wait(1); //Wait for signal 1
00104         //get readings
00105         float LDRval = LDRSensor; //Read the analogue pin value
00106         time_t currentTime = time(0);   //Get the system time
00107         AddLDRSample(LDRval);
00108 
00109         AddTimeSample(currentTime);
00110         NewLDRSample = true;    //signal to main thread
00111     }
00112 }
00113 
00114 void IncrementIndex(void)
00115 {
00116     nextIndex = IndexIncrement(nextIndex); //Increment next index
00117     if (firstSample) {
00118         firstSample = false; //During first sample, do not increment current or oldest
00119     } else {
00120         currentIndex = IndexIncrement(currentIndex);
00121         if (currentIndex == oldestIndex) { //When current index overflows, start infrementing oldest
00122             oldestIndex = IndexIncrement(oldestIndex);
00123         }
00124     }
00125 }
00126 
00127 unsigned short IndexIncrement(unsigned short thisIndex)
00128 {
00129     if (thisIndex+1 == BUFFERSIZE) {
00130         thisIndex = 0; //When index reached buffersize, reset to 0
00131     } else {
00132         thisIndex++;   //Else increment
00133     }
00134     return thisIndex;
00135 }
00136 
00137 unsigned short IndexDecrement(unsigned short thisIndex)
00138 {
00139     if (thisIndex-1 == -1) { // Wait for underflow
00140         thisIndex = BUFFERSIZE-1; //When index reaches 0 reset to Buffersize-1
00141     } else {
00142         thisIndex--;   //Else decrement
00143     }
00144     return thisIndex;
00145 }
00146 
00147 void Sampling(bool inputState)
00148 {
00149     if (inputState) {
00150         sampleRate.attach(&SampleTimerISR, SAMPLERATE); //Attach ticker
00151     } else {
00152         sampleRate.detach(); //Detach ticker, stops sampling
00153     }
00154 }
00155 
00156 void TakeKeys(bool inputState)
00157 {
00158     if (inputState) {
00159         tempReadingsLock.lock();             //Take the key
00160         presReadingsLock.lock();
00161         LDRReadingsLock.lock();
00162         timeReadingsLock.lock();
00163     } else {
00164         tempReadingsLock.unlock();           //Release the key
00165         presReadingsLock.unlock();
00166         LDRReadingsLock.unlock();
00167         timeReadingsLock.unlock();
00168     }
00169 }
00170 
00171 
00172 void FlipSamplingLED(void)
00173 {
00174     SamplingLED = 0;
00175 }
00176