Latest
Dependencies: serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice
main.cpp@19:88d8359306a4, 2018-12-17 (annotated)
- Committer:
- Swabey89
- Date:
- Mon Dec 17 16:11:07 2018 +0000
- Revision:
- 19:88d8359306a4
- Parent:
- 18:3edfb9152b05
- Child:
- 20:cdde0663c481
files now created with date
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Swabey89 | 7:e2bf2d703867 | 1 | #include "mbed.h" |
Swabey89 | 1:31c7b4ab552b | 2 | #include "sample_hardware.hpp" |
Swabey89 | 1:31c7b4ab552b | 3 | #include "Networkbits.hpp" |
Swabey89 | 5:956984cbe447 | 4 | #include "serial_terminal.hpp" |
Swabey89 | 6:a5394c9e5927 | 5 | #include "SDCard.hpp" |
Swabey89 | 5:956984cbe447 | 6 | #include "rtos.h" |
Swabey89 | 11:3ad0327e8c9f | 7 | #include "events/mbed_events.h" |
Swabey89 | 8:c81b0ff8b822 | 8 | #include "LCDdisplay.hpp" |
Swabey89 | 5:956984cbe447 | 9 | |
Swabey89 | 19:88d8359306a4 | 10 | //Defines |
Swabey89 | 19:88d8359306a4 | 11 | #define BUFFERSIZE 120 |
Swabey89 | 19:88d8359306a4 | 12 | |
Swabey89 | 10:0fffc988d325 | 13 | //Signals |
Swabey89 | 8:c81b0ff8b822 | 14 | #define TAKE_SAMPLE 1 |
Swabey89 | 19:88d8359306a4 | 15 | #define STORE_DATA 2 |
Swabey89 | 10:0fffc988d325 | 16 | |
Swabey89 | 17:ead43c1b729d | 17 | //Global variables |
Swabey89 | 19:88d8359306a4 | 18 | |
Swabey89 | 19:88d8359306a4 | 19 | class sensorData |
Swabey89 | 19:88d8359306a4 | 20 | { |
Swabey89 | 19:88d8359306a4 | 21 | private: |
Swabey89 | 19:88d8359306a4 | 22 | double temperature; |
Swabey89 | 19:88d8359306a4 | 23 | double pressure; |
Swabey89 | 19:88d8359306a4 | 24 | float lightlevel; |
Swabey89 | 19:88d8359306a4 | 25 | //time |
Swabey89 | 19:88d8359306a4 | 26 | public: |
Swabey89 | 19:88d8359306a4 | 27 | sensorData(void) |
Swabey89 | 19:88d8359306a4 | 28 | { |
Swabey89 | 19:88d8359306a4 | 29 | this->temperature = 0; |
Swabey89 | 19:88d8359306a4 | 30 | this->pressure = 0; |
Swabey89 | 19:88d8359306a4 | 31 | this->lightlevel = 0; |
Swabey89 | 19:88d8359306a4 | 32 | } |
Swabey89 | 19:88d8359306a4 | 33 | |
Swabey89 | 19:88d8359306a4 | 34 | void updatetemp(double t) |
Swabey89 | 19:88d8359306a4 | 35 | { |
Swabey89 | 19:88d8359306a4 | 36 | this->temperature = t; |
Swabey89 | 19:88d8359306a4 | 37 | }; |
Swabey89 | 19:88d8359306a4 | 38 | |
Swabey89 | 19:88d8359306a4 | 39 | void updatepress(double p) |
Swabey89 | 19:88d8359306a4 | 40 | { |
Swabey89 | 19:88d8359306a4 | 41 | this->pressure = p; |
Swabey89 | 19:88d8359306a4 | 42 | }; |
Swabey89 | 19:88d8359306a4 | 43 | |
Swabey89 | 19:88d8359306a4 | 44 | void updatelight(float l) |
Swabey89 | 19:88d8359306a4 | 45 | { |
Swabey89 | 19:88d8359306a4 | 46 | this->lightlevel = l; |
Swabey89 | 19:88d8359306a4 | 47 | }; |
Swabey89 | 19:88d8359306a4 | 48 | |
Swabey89 | 19:88d8359306a4 | 49 | double gettemp(void) |
Swabey89 | 19:88d8359306a4 | 50 | { |
Swabey89 | 19:88d8359306a4 | 51 | return this->temperature; |
Swabey89 | 19:88d8359306a4 | 52 | }; |
Swabey89 | 19:88d8359306a4 | 53 | |
Swabey89 | 19:88d8359306a4 | 54 | double getpress(void) |
Swabey89 | 19:88d8359306a4 | 55 | { |
Swabey89 | 19:88d8359306a4 | 56 | return this->pressure; |
Swabey89 | 19:88d8359306a4 | 57 | }; |
Swabey89 | 19:88d8359306a4 | 58 | |
Swabey89 | 19:88d8359306a4 | 59 | float getlight(void) |
Swabey89 | 19:88d8359306a4 | 60 | { |
Swabey89 | 19:88d8359306a4 | 61 | return this->lightlevel; |
Swabey89 | 19:88d8359306a4 | 62 | }; |
Swabey89 | 19:88d8359306a4 | 63 | }; |
Swabey89 | 19:88d8359306a4 | 64 | |
Swabey89 | 19:88d8359306a4 | 65 | |
Swabey89 | 19:88d8359306a4 | 66 | unsigned int newestIndex = BUFFERSIZE-1; //First time it is incremented, it will be 0 |
Swabey89 | 19:88d8359306a4 | 67 | unsigned int oldestIndex = BUFFERSIZE-1; |
Swabey89 | 17:ead43c1b729d | 68 | FILE* fp; |
Swabey89 | 17:ead43c1b729d | 69 | FATFileSystem* fs; |
Swabey89 | 10:0fffc988d325 | 70 | |
Swabey89 | 19:88d8359306a4 | 71 | //Shared mutable variables VOLATILE |
Swabey89 | 19:88d8359306a4 | 72 | float sample_rate = 15; |
Swabey89 | 17:ead43c1b729d | 73 | struct tm* timeData; |
Swabey89 | 18:3edfb9152b05 | 74 | char cmdBuffer[256]; |
Swabey89 | 19:88d8359306a4 | 75 | sensorData buffer[BUFFERSIZE]; |
Swabey89 | 17:ead43c1b729d | 76 | RawSerial* pc; |
Swabey89 | 8:c81b0ff8b822 | 77 | |
Swabey89 | 19:88d8359306a4 | 78 | //Thread synchronisation primatives |
Swabey89 | 19:88d8359306a4 | 79 | Semaphore spaceAvailable(BUFFERSIZE); |
Swabey89 | 19:88d8359306a4 | 80 | Semaphore samplesInBuffer(0); |
Swabey89 | 19:88d8359306a4 | 81 | Mutex bufferLock; |
Swabey89 | 19:88d8359306a4 | 82 | |
Swabey89 | 8:c81b0ff8b822 | 83 | //Queues |
Swabey89 | 11:3ad0327e8c9f | 84 | EventQueue SDqueue(32*EVENTS_EVENT_SIZE); |
Swabey89 | 11:3ad0327e8c9f | 85 | EventQueue LCDqueue(32*EVENTS_EVENT_SIZE); |
Swabey89 | 17:ead43c1b729d | 86 | EventQueue serialqueue(32*EVENTS_EVENT_SIZE); |
Swabey89 | 1:31c7b4ab552b | 87 | |
Swabey89 | 1:31c7b4ab552b | 88 | //Threads |
Swabey89 | 19:88d8359306a4 | 89 | Thread producer_thread(osPriorityHigh); |
Swabey89 | 19:88d8359306a4 | 90 | Thread consumer_thread; |
Swabey89 | 19:88d8359306a4 | 91 | Thread serial_thread(osPriorityAboveNormal); |
Swabey89 | 11:3ad0327e8c9f | 92 | Thread SDqueue_thread; |
Swabey89 | 11:3ad0327e8c9f | 93 | Thread LCDqueue_thread; |
Swabey89 | 0:4afd4940a189 | 94 | |
Swabey89 | 17:ead43c1b729d | 95 | //Timers |
Swabey89 | 8:c81b0ff8b822 | 96 | Ticker sample; |
Swabey89 | 10:0fffc988d325 | 97 | |
Swabey89 | 17:ead43c1b729d | 98 | //Function prototypes |
Swabey89 | 8:c81b0ff8b822 | 99 | void sampleISR(void); |
Swabey89 | 19:88d8359306a4 | 100 | void sampleProducer(void); |
Swabey89 | 19:88d8359306a4 | 101 | void sampleConsumer(void); |
Swabey89 | 11:3ad0327e8c9f | 102 | void serialISR(void); |
Swabey89 | 19:88d8359306a4 | 103 | void serialData(void); |
Swabey89 | 11:3ad0327e8c9f | 104 | |
Swabey89 | 15:8af9672a6778 | 105 | int main() { |
Swabey89 | 17:ead43c1b729d | 106 | timeData = new tm; |
Swabey89 | 11:3ad0327e8c9f | 107 | pc = new RawSerial(USBTX, USBRX); |
Swabey89 | 8:c81b0ff8b822 | 108 | |
Swabey89 | 18:3edfb9152b05 | 109 | //Initialisations |
Swabey89 | 17:ead43c1b729d | 110 | SDcard(); |
Swabey89 | 9:fa8a35d9d6c0 | 111 | |
Swabey89 | 1:31c7b4ab552b | 112 | //Power on self test |
Swabey89 | 1:31c7b4ab552b | 113 | post(); |
Swabey89 | 18:3edfb9152b05 | 114 | |
Swabey89 | 18:3edfb9152b05 | 115 | //Start threads |
Swabey89 | 18:3edfb9152b05 | 116 | SDqueue_thread.start(callback(&SDqueue, &EventQueue::dispatch_forever)); |
Swabey89 | 18:3edfb9152b05 | 117 | LCDqueue_thread.start(callback(&LCDqueue, &EventQueue::dispatch_forever)); |
Swabey89 | 19:88d8359306a4 | 118 | serial_thread.start(callback(&serialqueue, &EventQueue::dispatch_forever)); |
Swabey89 | 19:88d8359306a4 | 119 | producer_thread.start(sampleProducer); |
Swabey89 | 19:88d8359306a4 | 120 | consumer_thread.start(sampleConsumer); |
Swabey89 | 3:b1583f309b43 | 121 | |
Swabey89 | 18:3edfb9152b05 | 122 | //Attach ISRs |
Swabey89 | 18:3edfb9152b05 | 123 | sample.attach(&sampleISR, sample_rate); |
Swabey89 | 11:3ad0327e8c9f | 124 | pc->attach(serialISR, Serial::RxIrq); |
Swabey89 | 15:8af9672a6778 | 125 | |
Swabey89 | 1:31c7b4ab552b | 126 | //Flash to indicate goodness |
Swabey89 | 1:31c7b4ab552b | 127 | while(true) { |
Swabey89 | 5:956984cbe447 | 128 | greenLED = !greenLED; |
Swabey89 | 5:956984cbe447 | 129 | Thread::wait(500); |
Swabey89 | 0:4afd4940a189 | 130 | } |
Swabey89 | 0:4afd4940a189 | 131 | } |
Swabey89 | 1:31c7b4ab552b | 132 | |
Swabey89 | 19:88d8359306a4 | 133 | /* |
Swabey89 | 19:88d8359306a4 | 134 | FUNCITONS BELOW NEED MOVING? |
Swabey89 | 19:88d8359306a4 | 135 | */ |
Swabey89 | 19:88d8359306a4 | 136 | |
Swabey89 | 8:c81b0ff8b822 | 137 | void sampleISR() |
Swabey89 | 8:c81b0ff8b822 | 138 | { |
Swabey89 | 19:88d8359306a4 | 139 | producer_thread.signal_set(TAKE_SAMPLE); |
Swabey89 | 8:c81b0ff8b822 | 140 | } |
Swabey89 | 1:31c7b4ab552b | 141 | |
Swabey89 | 11:3ad0327e8c9f | 142 | void serialISR() |
Swabey89 | 11:3ad0327e8c9f | 143 | { |
Swabey89 | 11:3ad0327e8c9f | 144 | pc->attach(NULL, Serial::RxIrq); |
Swabey89 | 19:88d8359306a4 | 145 | serialqueue.call(serialData); |
Swabey89 | 11:3ad0327e8c9f | 146 | } |
Swabey89 | 1:31c7b4ab552b | 147 | |
Swabey89 | 19:88d8359306a4 | 148 | void serialData() |
Swabey89 | 12:3a54cbaa714c | 149 | { |
Swabey89 | 17:ead43c1b729d | 150 | static int i = 0; |
Swabey89 | 11:3ad0327e8c9f | 151 | if (pc->readable()) |
Swabey89 | 11:3ad0327e8c9f | 152 | { |
Swabey89 | 18:3edfb9152b05 | 153 | cmdBuffer[i] = pc->getc(); |
Swabey89 | 18:3edfb9152b05 | 154 | if (cmdBuffer[i] == '\r') |
Swabey89 | 11:3ad0327e8c9f | 155 | { |
Swabey89 | 11:3ad0327e8c9f | 156 | i = 0; |
Swabey89 | 18:3edfb9152b05 | 157 | serialqueue.call(serialterm); |
Swabey89 | 11:3ad0327e8c9f | 158 | } |
Swabey89 | 11:3ad0327e8c9f | 159 | else i++; |
Swabey89 | 11:3ad0327e8c9f | 160 | } |
Swabey89 | 11:3ad0327e8c9f | 161 | pc->attach(serialISR, Serial::RxIrq); |
Swabey89 | 11:3ad0327e8c9f | 162 | } |
Swabey89 | 19:88d8359306a4 | 163 | |
Swabey89 | 19:88d8359306a4 | 164 | |
Swabey89 | 19:88d8359306a4 | 165 | void sampleProducer() |
Swabey89 | 19:88d8359306a4 | 166 | { |
Swabey89 | 19:88d8359306a4 | 167 | while(true) |
Swabey89 | 19:88d8359306a4 | 168 | { |
Swabey89 | 19:88d8359306a4 | 169 | //High priority thread |
Swabey89 | 19:88d8359306a4 | 170 | Thread::signal_wait(TAKE_SAMPLE); |
Swabey89 | 19:88d8359306a4 | 171 | |
Swabey89 | 19:88d8359306a4 | 172 | //int32_t Nspaces = spaceAvailable.wait(); //Blocking if space is not available //Dont check if there is space available becuase we are overwriting |
Swabey89 | 19:88d8359306a4 | 173 | bufferLock.lock(); |
Swabey89 | 19:88d8359306a4 | 174 | //Update buffer |
Swabey89 | 19:88d8359306a4 | 175 | newestIndex = (newestIndex+1) % BUFFERSIZE; //CIRCULAR |
Swabey89 | 19:88d8359306a4 | 176 | buffer[newestIndex].updatetemp(sensor.getTemperature()); |
Swabey89 | 19:88d8359306a4 | 177 | buffer[newestIndex].updatepress(sensor.getPressure()); |
Swabey89 | 19:88d8359306a4 | 178 | buffer[newestIndex].updatelight(adcIn.read()); |
Swabey89 | 19:88d8359306a4 | 179 | //bufferLock.unlock(); //normally here, moved due to updating queues. |
Swabey89 | 19:88d8359306a4 | 180 | samplesInBuffer.release(); |
Swabey89 | 19:88d8359306a4 | 181 | |
Swabey89 | 19:88d8359306a4 | 182 | //Pass onto queues |
Swabey89 | 19:88d8359306a4 | 183 | LCDqueue.call(LCD_display, buffer[newestIndex].gettemp(),buffer[newestIndex].getpress(),buffer[newestIndex].getlight()); |
Swabey89 | 19:88d8359306a4 | 184 | bufferLock.unlock(); |
Swabey89 | 19:88d8359306a4 | 185 | |
Swabey89 | 19:88d8359306a4 | 186 | //Write to the SD card when i have 120 samples in the buffer |
Swabey89 | 19:88d8359306a4 | 187 | if (newestIndex == 119) |
Swabey89 | 19:88d8359306a4 | 188 | { |
Swabey89 | 19:88d8359306a4 | 189 | //save to SD card |
Swabey89 | 19:88d8359306a4 | 190 | consumer_thread.signal_set(STORE_DATA); |
Swabey89 | 19:88d8359306a4 | 191 | |
Swabey89 | 19:88d8359306a4 | 192 | } |
Swabey89 | 19:88d8359306a4 | 193 | |
Swabey89 | 19:88d8359306a4 | 194 | //pc->printf("%d\r\n", newestIndex); |
Swabey89 | 19:88d8359306a4 | 195 | |
Swabey89 | 19:88d8359306a4 | 196 | } |
Swabey89 | 19:88d8359306a4 | 197 | } |
Swabey89 | 19:88d8359306a4 | 198 | |
Swabey89 | 19:88d8359306a4 | 199 | void sampleConsumer() |
Swabey89 | 19:88d8359306a4 | 200 | { |
Swabey89 | 19:88d8359306a4 | 201 | while(true) |
Swabey89 | 19:88d8359306a4 | 202 | { |
Swabey89 | 19:88d8359306a4 | 203 | |
Swabey89 | 19:88d8359306a4 | 204 | static time_t seconds; |
Swabey89 | 19:88d8359306a4 | 205 | |
Swabey89 | 19:88d8359306a4 | 206 | //write to the SD card from 0 up to newestIndex. |
Swabey89 | 19:88d8359306a4 | 207 | Thread::signal_wait(STORE_DATA); |
Swabey89 | 19:88d8359306a4 | 208 | bufferLock.lock(); |
Swabey89 | 19:88d8359306a4 | 209 | redLED = 1; |
Swabey89 | 19:88d8359306a4 | 210 | |
Swabey89 | 19:88d8359306a4 | 211 | char fileDate[30]; |
Swabey89 | 19:88d8359306a4 | 212 | seconds = time(NULL); |
Swabey89 | 19:88d8359306a4 | 213 | timeData = localtime(&seconds); |
Swabey89 | 19:88d8359306a4 | 214 | set_time(mktime(timeData)); |
Swabey89 | 19:88d8359306a4 | 215 | strftime(fileDate, 30, "sd/sampledata/%d_%m_%y.csv", timeData); |
Swabey89 | 19:88d8359306a4 | 216 | |
Swabey89 | 19:88d8359306a4 | 217 | fp = fopen(fileDate,"a"); |
Swabey89 | 19:88d8359306a4 | 218 | |
Swabey89 | 19:88d8359306a4 | 219 | for (int i = 0; i<BUFFERSIZE; i++) |
Swabey89 | 19:88d8359306a4 | 220 | { |
Swabey89 | 19:88d8359306a4 | 221 | fprintf(fp, "%6.1f,%.2f\n\r", buffer[i].gettemp(), buffer[i].getpress()); //just these for now |
Swabey89 | 19:88d8359306a4 | 222 | } |
Swabey89 | 19:88d8359306a4 | 223 | |
Swabey89 | 19:88d8359306a4 | 224 | fclose(fp); |
Swabey89 | 19:88d8359306a4 | 225 | redLED = 0; |
Swabey89 | 19:88d8359306a4 | 226 | bufferLock.unlock(); |
Swabey89 | 19:88d8359306a4 | 227 | printf("SD card updated\r\n"); |
Swabey89 | 19:88d8359306a4 | 228 | } |
Swabey89 | 19:88d8359306a4 | 229 | } |