fuck this
Dependencies: BMP280
Sampling.h
00001 #ifndef __Sampling__ 00002 #define __Sampling__ 00003 /* 00004 * This module relates to the handling and sampling of enviromental data. 00005 * Sampled data is held in 4 arrays. One for each different reading. Each 00006 * of these is protected by a mutex lock, which is taken when data is being 00007 * written to the registers. 00008 * There are common indexes for the positions in the array. Stored is the position 00009 * position of the oldest sample, the most recent sample, and where the next 00010 * sample is to be written to. These operate in such a way that the array will fill 00011 * and when fill will start to overwrite the oldest sample. 00012 * Producer functions write samples to nextIndex position, allowing the most 00013 * recent complete sample to continue to be read by consumers. 00014 * 00015 * A ticker runs at the sample rate interval. This signals two sampling threads. 00016 * Two threads were used to prevent against delay from I2C interface. 00017 * These threads take the samples then write them to the nextIndex, then signal done. 00018 * On seeing that both threads are complete, the main thread then increments the 00019 * indexes and passes the latest variables to the LCD. 00020 */ 00021 #include "mbed.h" 00022 #include "BMP280.h" 00023 #include "rtos.h" 00024 00025 #define BUFFERSIZE 120 00026 extern unsigned short SAMPLERATE; 00027 00028 //Thread Sync Tools 00029 extern Mutex tempReadingsLock; 00030 extern Mutex presReadingsLock; 00031 extern Mutex LDRReadingsLock; 00032 extern Mutex timeReadingsLock; 00033 00034 //Buffers 00035 extern float tempReadings[BUFFERSIZE]; 00036 extern float presReadings[BUFFERSIZE]; 00037 extern float LDRReadings[BUFFERSIZE]; 00038 extern time_t timeReadings[BUFFERSIZE]; 00039 00040 extern volatile unsigned short currentIndex; 00041 //Position in the buffer of the newest sample 00042 extern volatile unsigned short nextIndex; 00043 //Position in the buffer where the next sample needs to be writen to 00044 extern volatile unsigned short oldestIndex; 00045 //Position in the buffer of the oldest sample 00046 00047 extern bool firstSample; 00048 //Used to indicate this is the first sample to be taken. used in IncrementIndex func 00049 00050 extern Thread t1; //Sample Enviromental Sensor 00051 extern Thread t2; //Sample LDR Sensor 00052 00053 extern Ticker sampleRate; 00054 extern Timeout SampleLEDTimeout; 00055 //Hardware 00056 extern BMP280 sensor; 00057 extern AnalogIn LDRSensor; //Input pin for LDR 00058 extern DigitalOut SamplingLED; //Onboard LED showing when a sample happens 00059 00060 extern bool NewEnvSample; //Is there new data from the envirom sensor to output? 00061 extern bool NewLDRSample; //Is there new data from the LDR to output? 00062 00063 void SampleTimerISR(void); 00064 //Called by ticker. Calls the sample funcs of each device by flagging the threads 00065 00066 void ConfigThreadsAndIR(void); 00067 //Setup Interrupts and Threads 00068 00069 void ThreadSampleEnvSensor(void); 00070 //when flagged by interrupt will capture a sample then calls the addtobufferfuncs 00071 00072 void AddTempSample(float temp); 00073 //Producer function 00074 00075 void AddPresSample(float pres); 00076 //Producer Function 00077 00078 void ThreadSampleLDR(void); 00079 //When flagged by interrupt will read time and LDR value. 00080 00081 void AddLDRSample(float LDR); 00082 //Producer Function 00083 00084 void AddTimeSample(time_t sampledTime); 00085 //Producer Function 00086 00087 void IncrementIndex(void); 00088 //Called after samples are added. Increments the index and moves the oldest 00089 //along if necessary 00090 00091 void FlipSamplingLED(void); 00092 //Called by timeout, turns of LED 00093 00094 unsigned short IndexIncrement(unsigned short thisIndex); 00095 //Incrementing the index with respect for the buffersize. Used to prevent overflow. 00096 00097 unsigned short IndexDecrement(unsigned short thisIndex); 00098 //Decrementing the index with respect for the buffersize. Used to prevent overflow. 00099 00100 void Sampling(bool inputState); 00101 //Start or stop sampling. true = start, false = stop. 00102 00103 void TakeKeys(bool inputState); 00104 //Lock or unlock sampling variables. true = take keys. false = return keys 00105 00106 #endif
Generated on Tue Jul 26 2022 07:29:23 by 1.7.2