Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BMP280
Sampling.cpp@15:e61297f9bae9, 2018-01-06 (annotated)
- Committer:
- Swaggie
- Date:
- Sat Jan 06 22:54:42 2018 +0000
- Revision:
- 15:e61297f9bae9
- Parent:
- 9:ac5673cca703
LCD Constructor seems to be working. thread, ticker and button 2 are part of the class.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Swaggie | 5:bea93c8e50b7 | 1 | #include "Sampling.h" |
Swaggie | 5:bea93c8e50b7 | 2 | #include "mbed.h" |
Swaggie | 8:dbb57b4d5ba4 | 3 | #include "rtos.h" |
Swaggie | 6:8e1795a5886b | 4 | |
mwthewsey | 9:ac5673cca703 | 5 | //Thread Sync Tools |
mwthewsey | 9:ac5673cca703 | 6 | Mutex tempReadingsLock; |
mwthewsey | 9:ac5673cca703 | 7 | Mutex presReadingsLock; |
mwthewsey | 9:ac5673cca703 | 8 | Mutex LDRReadingsLock; |
mwthewsey | 9:ac5673cca703 | 9 | Mutex timeReadingsLock; |
mwthewsey | 9:ac5673cca703 | 10 | |
mwthewsey | 9:ac5673cca703 | 11 | //Buffers with zero inital value |
mwthewsey | 9:ac5673cca703 | 12 | float tempReadings[BUFFERSIZE] = {}; |
mwthewsey | 9:ac5673cca703 | 13 | float presReadings[BUFFERSIZE] = {}; |
mwthewsey | 9:ac5673cca703 | 14 | float LDRReadings[BUFFERSIZE] = {}; |
mwthewsey | 9:ac5673cca703 | 15 | float timeReadings[BUFFERSIZE] = {}; |
mwthewsey | 9:ac5673cca703 | 16 | |
mwthewsey | 9:ac5673cca703 | 17 | Thread t1; //Sample Enviromental Sensor |
mwthewsey | 9:ac5673cca703 | 18 | Thread t2; //Sample LDR Sensor |
mwthewsey | 9:ac5673cca703 | 19 | |
mwthewsey | 9:ac5673cca703 | 20 | Ticker sampleRate; |
mwthewsey | 9:ac5673cca703 | 21 | Timeout SampleLEDTimeout; |
mwthewsey | 9:ac5673cca703 | 22 | |
mwthewsey | 9:ac5673cca703 | 23 | bool NewEnvSample; //Is there new data from the envirom sensor to output? |
mwthewsey | 9:ac5673cca703 | 24 | bool NewLDRSample; //Is there new data from the LDR to output? |
mwthewsey | 9:ac5673cca703 | 25 | |
Swaggie | 7:bf9f92ff02e8 | 26 | //Index |
Swaggie | 15:e61297f9bae9 | 27 | volatile unsigned short nextIndex = 0; |
Swaggie | 15:e61297f9bae9 | 28 | volatile unsigned short currentIndex = 0; |
Swaggie | 15:e61297f9bae9 | 29 | volatile unsigned short oldestIndex = 0; |
Swaggie | 7:bf9f92ff02e8 | 30 | |
Swaggie | 7:bf9f92ff02e8 | 31 | bool firstSample = true; |
Swaggie | 6:8e1795a5886b | 32 | |
Swaggie | 7:bf9f92ff02e8 | 33 | //Hardware |
Swaggie | 7:bf9f92ff02e8 | 34 | BMP280 sensor(D14, D15); |
mwthewsey | 9:ac5673cca703 | 35 | AnalogIn LDRSensor(A0); |
mwthewsey | 9:ac5673cca703 | 36 | DigitalOut SamplingLED(PB_10); |
Swaggie | 6:8e1795a5886b | 37 | |
Swaggie | 5:bea93c8e50b7 | 38 | void SampleTimerISR(void) |
Swaggie | 5:bea93c8e50b7 | 39 | { |
Swaggie | 5:bea93c8e50b7 | 40 | //Flag Threads |
Swaggie | 7:bf9f92ff02e8 | 41 | t1.signal_set(1); |
Swaggie | 7:bf9f92ff02e8 | 42 | t2.signal_set(1); |
Swaggie | 8:dbb57b4d5ba4 | 43 | SamplingLED = 1; |
mwthewsey | 9:ac5673cca703 | 44 | SampleLEDTimeout.attach(&FlipSamplingLED,0.1); //To turn LED off |
Swaggie | 5:bea93c8e50b7 | 45 | } |
Swaggie | 7:bf9f92ff02e8 | 46 | |
Swaggie | 5:bea93c8e50b7 | 47 | void ConfigThreadsAndIR(void) |
Swaggie | 5:bea93c8e50b7 | 48 | { |
Swaggie | 7:bf9f92ff02e8 | 49 | NewEnvSample = false; //Reset |
Swaggie | 7:bf9f92ff02e8 | 50 | NewLDRSample = false; //Reset |
Swaggie | 7:bf9f92ff02e8 | 51 | |
Swaggie | 8:dbb57b4d5ba4 | 52 | t1.start(&ThreadSampleEnvSensor); |
Swaggie | 8:dbb57b4d5ba4 | 53 | t2.start(&ThreadSampleLDR); |
Swaggie | 7:bf9f92ff02e8 | 54 | |
mwthewsey | 9:ac5673cca703 | 55 | sampleRate.attach(&SampleTimerISR, 1); //15 second interval |
Swaggie | 7:bf9f92ff02e8 | 56 | } |
Swaggie | 7:bf9f92ff02e8 | 57 | |
Swaggie | 7:bf9f92ff02e8 | 58 | void AddTempSample(float temp) |
Swaggie | 7:bf9f92ff02e8 | 59 | { |
Swaggie | 7:bf9f92ff02e8 | 60 | tempReadingsLock.lock(); //Take the key |
mwthewsey | 9:ac5673cca703 | 61 | tempReadings[nextIndex] = temp; //Add the sample after the most recent |
Swaggie | 7:bf9f92ff02e8 | 62 | tempReadingsLock.unlock(); // Release the key |
Swaggie | 7:bf9f92ff02e8 | 63 | } |
Swaggie | 7:bf9f92ff02e8 | 64 | |
Swaggie | 7:bf9f92ff02e8 | 65 | void AddPresSample(float pres) |
Swaggie | 7:bf9f92ff02e8 | 66 | { |
Swaggie | 7:bf9f92ff02e8 | 67 | presReadingsLock.lock(); //Take the key |
mwthewsey | 9:ac5673cca703 | 68 | presReadings[nextIndex] = pres; //Add to register |
Swaggie | 8:dbb57b4d5ba4 | 69 | presReadingsLock.unlock(); //Release the key |
Swaggie | 6:8e1795a5886b | 70 | } |
Swaggie | 6:8e1795a5886b | 71 | |
Swaggie | 7:bf9f92ff02e8 | 72 | void ThreadSampleEnvSensor(void) |
Swaggie | 6:8e1795a5886b | 73 | { |
Swaggie | 7:bf9f92ff02e8 | 74 | while (true) { |
Swaggie | 7:bf9f92ff02e8 | 75 | Thread::signal_wait(1); //Wait for signal 1 |
Swaggie | 7:bf9f92ff02e8 | 76 | //Get readings |
Swaggie | 7:bf9f92ff02e8 | 77 | float temp = sensor.getTemperature(); |
Swaggie | 7:bf9f92ff02e8 | 78 | float pres = sensor.getPressure(); |
Swaggie | 7:bf9f92ff02e8 | 79 | AddPresSample(pres); //Add value to register |
Swaggie | 7:bf9f92ff02e8 | 80 | AddTempSample(temp); //Add value to register |
Swaggie | 7:bf9f92ff02e8 | 81 | NewEnvSample = true; //Signal to main thread |
Swaggie | 7:bf9f92ff02e8 | 82 | } |
Swaggie | 7:bf9f92ff02e8 | 83 | } |
Swaggie | 7:bf9f92ff02e8 | 84 | |
Swaggie | 7:bf9f92ff02e8 | 85 | void AddLDRSample(float LDRval) |
Swaggie | 7:bf9f92ff02e8 | 86 | { |
Swaggie | 7:bf9f92ff02e8 | 87 | LDRReadingsLock.lock(); //Take the key |
mwthewsey | 9:ac5673cca703 | 88 | LDRReadings[nextIndex] = LDRval; //Add the sample after the most recent |
Swaggie | 7:bf9f92ff02e8 | 89 | LDRReadingsLock.unlock(); // Release the key |
Swaggie | 6:8e1795a5886b | 90 | } |
Swaggie | 6:8e1795a5886b | 91 | |
Swaggie | 7:bf9f92ff02e8 | 92 | void ThreadSampleLDR(void) |
Swaggie | 6:8e1795a5886b | 93 | { |
Swaggie | 7:bf9f92ff02e8 | 94 | while (true) { |
Swaggie | 7:bf9f92ff02e8 | 95 | Thread::signal_wait(1); //Wait for signal 1 |
Swaggie | 7:bf9f92ff02e8 | 96 | //get readings |
Swaggie | 8:dbb57b4d5ba4 | 97 | float LDRval = LDRSensor; //Read the analogue pin value |
Swaggie | 7:bf9f92ff02e8 | 98 | //get time function |
Swaggie | 7:bf9f92ff02e8 | 99 | AddLDRSample(LDRval); |
Swaggie | 7:bf9f92ff02e8 | 100 | //add time sample |
Swaggie | 7:bf9f92ff02e8 | 101 | NewLDRSample = true; //signal to main thread |
Swaggie | 7:bf9f92ff02e8 | 102 | } |
Swaggie | 7:bf9f92ff02e8 | 103 | } |
Swaggie | 7:bf9f92ff02e8 | 104 | |
Swaggie | 7:bf9f92ff02e8 | 105 | void IncrementIndex(void) |
Swaggie | 7:bf9f92ff02e8 | 106 | { |
mwthewsey | 9:ac5673cca703 | 107 | //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]); |
mwthewsey | 9:ac5673cca703 | 108 | |
mwthewsey | 9:ac5673cca703 | 109 | nextIndex = IndexIncrement(nextIndex); //Increment next index |
mwthewsey | 9:ac5673cca703 | 110 | if (firstSample) { |
mwthewsey | 9:ac5673cca703 | 111 | firstSample = false; //During first sample, do not increment current or oldest |
mwthewsey | 9:ac5673cca703 | 112 | } else { |
mwthewsey | 9:ac5673cca703 | 113 | currentIndex = IndexIncrement(currentIndex); |
mwthewsey | 9:ac5673cca703 | 114 | if (currentIndex == oldestIndex) { //When current index overflows, start infrementing oldest |
mwthewsey | 9:ac5673cca703 | 115 | oldestIndex = IndexIncrement(oldestIndex); |
Swaggie | 7:bf9f92ff02e8 | 116 | } |
Swaggie | 7:bf9f92ff02e8 | 117 | } |
Swaggie | 7:bf9f92ff02e8 | 118 | } |
Swaggie | 8:dbb57b4d5ba4 | 119 | |
mwthewsey | 9:ac5673cca703 | 120 | unsigned short IndexIncrement(unsigned short thisIndex) |
mwthewsey | 9:ac5673cca703 | 121 | { |
mwthewsey | 9:ac5673cca703 | 122 | if (thisIndex+1 == BUFFERSIZE) { |
mwthewsey | 9:ac5673cca703 | 123 | thisIndex = 0; //When index reached buffersize, reset to 0 |
mwthewsey | 9:ac5673cca703 | 124 | } else { |
mwthewsey | 9:ac5673cca703 | 125 | thisIndex++; //Else increment |
mwthewsey | 9:ac5673cca703 | 126 | } |
mwthewsey | 9:ac5673cca703 | 127 | return thisIndex; |
mwthewsey | 9:ac5673cca703 | 128 | } |
mwthewsey | 9:ac5673cca703 | 129 | |
mwthewsey | 9:ac5673cca703 | 130 | |
Swaggie | 8:dbb57b4d5ba4 | 131 | void FlipSamplingLED(void) |
Swaggie | 8:dbb57b4d5ba4 | 132 | { |
Swaggie | 8:dbb57b4d5ba4 | 133 | SamplingLED = 0; |
Swaggie | 8:dbb57b4d5ba4 | 134 | } |
mwthewsey | 9:ac5673cca703 | 135 |