fuck this
Dependencies: BMP280
Sampling.h@19:40c721f01ed2, 2018-01-09 (annotated)
- Committer:
- Swaggie
- Date:
- Tue Jan 09 11:39:59 2018 +0000
- Revision:
- 19:40c721f01ed2
- Parent:
- 12:03589f1d5c30
- Child:
- 21:6e733076f49c
Moved SD card HW to main file. Started detail commenting. SDCard commenting needs completing.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Swaggie | 4:2e6d9492b76d | 1 | #ifndef __Sampling__ |
Swaggie | 4:2e6d9492b76d | 2 | #define __Sampling__ |
Swaggie | 19:40c721f01ed2 | 3 | /* |
Swaggie | 19:40c721f01ed2 | 4 | * This module relates to the handling and sampling of enviromental data. |
Swaggie | 19:40c721f01ed2 | 5 | * Sampled data is held in 4 arrays. One for each different reading. Each |
Swaggie | 19:40c721f01ed2 | 6 | * of these is protected by a mutex lock, which is taken when data is being |
Swaggie | 19:40c721f01ed2 | 7 | * written to the registers. |
Swaggie | 19:40c721f01ed2 | 8 | * There are common indexes for the positions in the array. Stored is the position |
Swaggie | 19:40c721f01ed2 | 9 | * position of the oldest sample, the most recent sample, and where the next |
Swaggie | 19:40c721f01ed2 | 10 | * sample is to be written to. These operate in such a way that the array will fill |
Swaggie | 19:40c721f01ed2 | 11 | * and when fill will start to overwrite the oldest sample. |
Swaggie | 19:40c721f01ed2 | 12 | * Producer functions write samples to nextIndex position, allowing the most |
Swaggie | 19:40c721f01ed2 | 13 | * recent complete sample to continue to be read by consumers. |
Swaggie | 19:40c721f01ed2 | 14 | * |
Swaggie | 19:40c721f01ed2 | 15 | * A ticker runs at the sample rate interval. This signals two sampling threads. |
Swaggie | 19:40c721f01ed2 | 16 | * Two threads were used to prevent against delay from I2C interface. |
Swaggie | 19:40c721f01ed2 | 17 | * These threads take the samples then write them to the nextIndex, then signal done. |
Swaggie | 19:40c721f01ed2 | 18 | * On seeing that both threads are complete, the main thread then increments the |
Swaggie | 19:40c721f01ed2 | 19 | * indexes and passes the latest variables to the LCD. |
Swaggie | 19:40c721f01ed2 | 20 | */ |
Swaggie | 6:8e1795a5886b | 21 | #include "mbed.h" |
Swaggie | 5:bea93c8e50b7 | 22 | #include "BMP280.h" |
Swaggie | 8:dbb57b4d5ba4 | 23 | #include "rtos.h" |
Swaggie | 5:bea93c8e50b7 | 24 | |
mwthewsey | 12:03589f1d5c30 | 25 | #define BUFFERSIZE 120 |
mwthewsey | 12:03589f1d5c30 | 26 | #define SAMPLERATE 7 |
Swaggie | 6:8e1795a5886b | 27 | |
Swaggie | 7:bf9f92ff02e8 | 28 | |
Swaggie | 6:8e1795a5886b | 29 | //Thread Sync Tools |
mwthewsey | 9:ac5673cca703 | 30 | extern Mutex tempReadingsLock; |
mwthewsey | 9:ac5673cca703 | 31 | extern Mutex presReadingsLock; |
mwthewsey | 9:ac5673cca703 | 32 | extern Mutex LDRReadingsLock; |
mwthewsey | 9:ac5673cca703 | 33 | extern Mutex timeReadingsLock; |
Swaggie | 6:8e1795a5886b | 34 | |
Swaggie | 6:8e1795a5886b | 35 | //Buffers |
mwthewsey | 9:ac5673cca703 | 36 | extern float tempReadings[BUFFERSIZE]; |
mwthewsey | 9:ac5673cca703 | 37 | extern float presReadings[BUFFERSIZE]; |
mwthewsey | 9:ac5673cca703 | 38 | extern float LDRReadings[BUFFERSIZE]; |
mwthewsey | 10:261f2b69c4c7 | 39 | extern time_t timeReadings[BUFFERSIZE]; |
Swaggie | 6:8e1795a5886b | 40 | |
mwthewsey | 10:261f2b69c4c7 | 41 | extern volatile unsigned short currentIndex; |
Swaggie | 6:8e1795a5886b | 42 | //Position in the buffer of the newest sample |
mwthewsey | 10:261f2b69c4c7 | 43 | extern volatile unsigned short nextIndex; |
mwthewsey | 9:ac5673cca703 | 44 | //Position in the buffer where the next sample needs to be writen to |
mwthewsey | 10:261f2b69c4c7 | 45 | extern volatile unsigned short oldestIndex; |
Swaggie | 6:8e1795a5886b | 46 | //Position in the buffer of the oldest sample |
Swaggie | 6:8e1795a5886b | 47 | |
Swaggie | 8:dbb57b4d5ba4 | 48 | extern bool firstSample; |
Swaggie | 7:bf9f92ff02e8 | 49 | //Used to indicate this is the first sample to be taken. used in IncrementIndex func |
Swaggie | 7:bf9f92ff02e8 | 50 | |
mwthewsey | 9:ac5673cca703 | 51 | extern Thread t1; //Sample Enviromental Sensor |
mwthewsey | 9:ac5673cca703 | 52 | extern Thread t2; //Sample LDR Sensor |
Swaggie | 6:8e1795a5886b | 53 | |
mwthewsey | 9:ac5673cca703 | 54 | extern Ticker sampleRate; |
mwthewsey | 9:ac5673cca703 | 55 | extern Timeout SampleLEDTimeout; |
mwthewsey | 12:03589f1d5c30 | 56 | //Hardware |
Swaggie | 8:dbb57b4d5ba4 | 57 | extern BMP280 sensor; |
Swaggie | 8:dbb57b4d5ba4 | 58 | extern AnalogIn LDRSensor; //Input pin for LDR |
Swaggie | 8:dbb57b4d5ba4 | 59 | extern DigitalOut SamplingLED; //Onboard LED showing when a sample happens |
Swaggie | 6:8e1795a5886b | 60 | |
Swaggie | 6:8e1795a5886b | 61 | /*These can be deleted I think |
Swaggie | 6:8e1795a5886b | 62 | extern float fLatestTemp; |
Swaggie | 6:8e1795a5886b | 63 | extern float fLatestLDR; |
Swaggie | 6:8e1795a5886b | 64 | extern float fLatestPres; |
Swaggie | 6:8e1795a5886b | 65 | */ |
Swaggie | 6:8e1795a5886b | 66 | |
mwthewsey | 9:ac5673cca703 | 67 | extern bool NewEnvSample; //Is there new data from the envirom sensor to output? |
mwthewsey | 9:ac5673cca703 | 68 | extern bool NewLDRSample; //Is there new data from the LDR to output? |
Swaggie | 6:8e1795a5886b | 69 | |
Swaggie | 4:2e6d9492b76d | 70 | void SampleTimerISR(void); |
Swaggie | 4:2e6d9492b76d | 71 | //Called by ticker. Calls the sample funcs of each device by flagging the threads |
Swaggie | 4:2e6d9492b76d | 72 | |
Swaggie | 4:2e6d9492b76d | 73 | void ConfigThreadsAndIR(void); |
Swaggie | 4:2e6d9492b76d | 74 | //Setup Interrupts and Threads |
Swaggie | 4:2e6d9492b76d | 75 | |
Swaggie | 7:bf9f92ff02e8 | 76 | void ThreadSampleEnvSensor(void); |
Swaggie | 6:8e1795a5886b | 77 | //when flagged by interrupt will capture a sample then calls the addtobufferfuncs |
Swaggie | 6:8e1795a5886b | 78 | |
Swaggie | 7:bf9f92ff02e8 | 79 | void AddTempSample(float temp); |
Swaggie | 6:8e1795a5886b | 80 | //Producer function |
Swaggie | 6:8e1795a5886b | 81 | |
Swaggie | 7:bf9f92ff02e8 | 82 | void AddPresSample(float pres); |
Swaggie | 6:8e1795a5886b | 83 | //Producer Function |
Swaggie | 6:8e1795a5886b | 84 | |
Swaggie | 6:8e1795a5886b | 85 | void ThreadSampleLDR(void); |
Swaggie | 7:bf9f92ff02e8 | 86 | //When flagged by interrupt will read time and LDR value. |
Swaggie | 6:8e1795a5886b | 87 | |
Swaggie | 7:bf9f92ff02e8 | 88 | void AddLDRSample(float LDR); |
mwthewsey | 10:261f2b69c4c7 | 89 | //Producer Function |
Swaggie | 4:2e6d9492b76d | 90 | |
mwthewsey | 10:261f2b69c4c7 | 91 | void AddTimeSample(time_t sampledTime); |
Swaggie | 7:bf9f92ff02e8 | 92 | //Producer Function |
Swaggie | 7:bf9f92ff02e8 | 93 | |
Swaggie | 7:bf9f92ff02e8 | 94 | void IncrementIndex(void); |
Swaggie | 7:bf9f92ff02e8 | 95 | //Called after samples are added. Increments the index and moves the oldest |
Swaggie | 7:bf9f92ff02e8 | 96 | //along if necessary |
Swaggie | 7:bf9f92ff02e8 | 97 | |
Swaggie | 8:dbb57b4d5ba4 | 98 | void FlipSamplingLED(void); |
Swaggie | 8:dbb57b4d5ba4 | 99 | //Called by timeout, turns of LED |
Swaggie | 8:dbb57b4d5ba4 | 100 | |
mwthewsey | 9:ac5673cca703 | 101 | unsigned short IndexIncrement(unsigned short thisIndex); |
mwthewsey | 10:261f2b69c4c7 | 102 | //Incrementing the index with respect for the buffersize. Used to prevent overflow. |
mwthewsey | 10:261f2b69c4c7 | 103 | |
mwthewsey | 10:261f2b69c4c7 | 104 | unsigned short IndexDecrement(unsigned short thisIndex); |
mwthewsey | 10:261f2b69c4c7 | 105 | //Decrementing the index with respect for the buffersize. Used to prevent overflow. |
mwthewsey | 10:261f2b69c4c7 | 106 | |
mwthewsey | 10:261f2b69c4c7 | 107 | void Sampling(bool inputState); |
mwthewsey | 10:261f2b69c4c7 | 108 | //Start or stop sampling. true = start, false = stop. |
mwthewsey | 10:261f2b69c4c7 | 109 | |
mwthewsey | 10:261f2b69c4c7 | 110 | void TakeKeys(bool inputState); |
mwthewsey | 10:261f2b69c4c7 | 111 | //Lock or unlock sampling variables. true = take keys. false = return keys |
mwthewsey | 9:ac5673cca703 | 112 | |
Swaggie | 4:2e6d9492b76d | 113 | #endif |