fuck this
Dependencies: BMP280
Sampling.cpp
- Committer:
- mwthewsey
- Date:
- 2018-01-06
- Revision:
- 9:ac5673cca703
- Parent:
- 8:dbb57b4d5ba4
- Child:
- 10:261f2b69c4c7
- Child:
- 15:e61297f9bae9
File content as of revision 9:ac5673cca703:
#include "Sampling.h" #include "mbed.h" #include "rtos.h" //Thread Sync Tools Mutex tempReadingsLock; Mutex presReadingsLock; Mutex LDRReadingsLock; Mutex timeReadingsLock; //Buffers with zero inital value float tempReadings[BUFFERSIZE] = {}; float presReadings[BUFFERSIZE] = {}; float LDRReadings[BUFFERSIZE] = {}; float timeReadings[BUFFERSIZE] = {}; Thread t1; //Sample Enviromental Sensor Thread t2; //Sample LDR Sensor Ticker sampleRate; Timeout SampleLEDTimeout; bool NewEnvSample; //Is there new data from the envirom sensor to output? bool NewLDRSample; //Is there new data from the LDR to output? //Index unsigned short nextIndex = 0; unsigned short currentIndex = 0; unsigned short oldestIndex = 0; bool firstSample = true; //Hardware BMP280 sensor(D14, D15); AnalogIn LDRSensor(A0); DigitalOut SamplingLED(PB_10); void SampleTimerISR(void) { //Flag Threads t1.signal_set(1); t2.signal_set(1); SamplingLED = 1; SampleLEDTimeout.attach(&FlipSamplingLED,0.1); //To turn LED off } void ConfigThreadsAndIR(void) { NewEnvSample = false; //Reset NewLDRSample = false; //Reset t1.start(&ThreadSampleEnvSensor); t2.start(&ThreadSampleLDR); sampleRate.attach(&SampleTimerISR, 1); //15 second interval } void AddTempSample(float temp) { tempReadingsLock.lock(); //Take the key tempReadings[nextIndex] = temp; //Add the sample after the most recent tempReadingsLock.unlock(); // Release the key } void AddPresSample(float pres) { presReadingsLock.lock(); //Take the key presReadings[nextIndex] = pres; //Add to register presReadingsLock.unlock(); //Release the key } void ThreadSampleEnvSensor(void) { while (true) { Thread::signal_wait(1); //Wait for signal 1 //Get readings float temp = sensor.getTemperature(); float pres = sensor.getPressure(); AddPresSample(pres); //Add value to register AddTempSample(temp); //Add value to register NewEnvSample = true; //Signal to main thread } } void AddLDRSample(float LDRval) { LDRReadingsLock.lock(); //Take the key LDRReadings[nextIndex] = LDRval; //Add the sample after the most recent LDRReadingsLock.unlock(); // Release the key } void ThreadSampleLDR(void) { while (true) { Thread::signal_wait(1); //Wait for signal 1 //get readings float LDRval = LDRSensor; //Read the analogue pin value //get time function AddLDRSample(LDRval); //add time sample NewLDRSample = true; //signal to main thread } } void IncrementIndex(void) { //printf("%d %d %d %2.2f %2.2f %2.2f %2.2f %2.2f %2.2f %2.2f %2.2f\r",nextIndex, currentIndex, oldestIndex, LDRReadings[0], LDRReadings[1], LDRReadings[2], LDRReadings[3], LDRReadings[4], LDRReadings[5], LDRReadings[6], LDRReadings[7]); nextIndex = IndexIncrement(nextIndex); //Increment next index if (firstSample) { firstSample = false; //During first sample, do not increment current or oldest } else { currentIndex = IndexIncrement(currentIndex); if (currentIndex == oldestIndex) { //When current index overflows, start infrementing oldest oldestIndex = IndexIncrement(oldestIndex); } } } unsigned short IndexIncrement(unsigned short thisIndex) { if (thisIndex+1 == BUFFERSIZE) { thisIndex = 0; //When index reached buffersize, reset to 0 } else { thisIndex++; //Else increment } return thisIndex; } void FlipSamplingLED(void) { SamplingLED = 0; }