Latest
Dependencies: serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice
main.cpp@21:2c438eeaab14, 2018-12-18 (annotated)
- Committer:
- Swabey89
- Date:
- Tue Dec 18 14:27:19 2018 +0000
- Revision:
- 21:2c438eeaab14
- Parent:
- 20:cdde0663c481
- Child:
- 22:d9cbbbf3cf69
Webpage currently working with minor bug fixes required
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 | 21:2c438eeaab14 | 3 | #include "Networking.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 | 21:2c438eeaab14 | 11 | //moved |
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 | unsigned int newestIndex = BUFFERSIZE-1; //First time it is incremented, it will be 0 |
Swabey89 | 19:88d8359306a4 | 20 | unsigned int oldestIndex = BUFFERSIZE-1; |
Swabey89 | 17:ead43c1b729d | 21 | FILE* fp; |
Swabey89 | 17:ead43c1b729d | 22 | FATFileSystem* fs; |
Swabey89 | 10:0fffc988d325 | 23 | |
Swabey89 | 19:88d8359306a4 | 24 | //Shared mutable variables VOLATILE |
Swabey89 | 19:88d8359306a4 | 25 | float sample_rate = 15; |
Swabey89 | 17:ead43c1b729d | 26 | struct tm* timeData; |
Swabey89 | 18:3edfb9152b05 | 27 | char cmdBuffer[256]; |
Swabey89 | 19:88d8359306a4 | 28 | sensorData buffer[BUFFERSIZE]; |
Swabey89 | 17:ead43c1b729d | 29 | RawSerial* pc; |
Swabey89 | 8:c81b0ff8b822 | 30 | |
Swabey89 | 19:88d8359306a4 | 31 | //Thread synchronisation primatives |
Swabey89 | 19:88d8359306a4 | 32 | Semaphore spaceAvailable(BUFFERSIZE); |
Swabey89 | 19:88d8359306a4 | 33 | Semaphore samplesInBuffer(0); |
Swabey89 | 19:88d8359306a4 | 34 | Mutex bufferLock; |
Swabey89 | 19:88d8359306a4 | 35 | |
Swabey89 | 8:c81b0ff8b822 | 36 | //Queues |
Swabey89 | 11:3ad0327e8c9f | 37 | EventQueue SDqueue(32*EVENTS_EVENT_SIZE); |
Swabey89 | 11:3ad0327e8c9f | 38 | EventQueue LCDqueue(32*EVENTS_EVENT_SIZE); |
Swabey89 | 17:ead43c1b729d | 39 | EventQueue serialqueue(32*EVENTS_EVENT_SIZE); |
Swabey89 | 1:31c7b4ab552b | 40 | |
Swabey89 | 1:31c7b4ab552b | 41 | //Threads |
Swabey89 | 19:88d8359306a4 | 42 | Thread producer_thread(osPriorityHigh); |
Swabey89 | 19:88d8359306a4 | 43 | Thread consumer_thread; |
Swabey89 | 19:88d8359306a4 | 44 | Thread serial_thread(osPriorityAboveNormal); |
Swabey89 | 11:3ad0327e8c9f | 45 | Thread SDqueue_thread; |
Swabey89 | 11:3ad0327e8c9f | 46 | Thread LCDqueue_thread; |
Swabey89 | 20:cdde0663c481 | 47 | Thread Network_thread; |
Swabey89 | 0:4afd4940a189 | 48 | |
Swabey89 | 17:ead43c1b729d | 49 | //Timers |
Swabey89 | 8:c81b0ff8b822 | 50 | Ticker sample; |
Swabey89 | 10:0fffc988d325 | 51 | |
Swabey89 | 17:ead43c1b729d | 52 | //Function prototypes |
Swabey89 | 8:c81b0ff8b822 | 53 | void sampleISR(void); |
Swabey89 | 19:88d8359306a4 | 54 | void sampleProducer(void); |
Swabey89 | 19:88d8359306a4 | 55 | void sampleConsumer(void); |
Swabey89 | 11:3ad0327e8c9f | 56 | void serialISR(void); |
Swabey89 | 19:88d8359306a4 | 57 | void serialData(void); |
Swabey89 | 11:3ad0327e8c9f | 58 | |
Swabey89 | 15:8af9672a6778 | 59 | int main() { |
Swabey89 | 17:ead43c1b729d | 60 | timeData = new tm; |
Swabey89 | 11:3ad0327e8c9f | 61 | pc = new RawSerial(USBTX, USBRX); |
Swabey89 | 8:c81b0ff8b822 | 62 | |
Swabey89 | 18:3edfb9152b05 | 63 | //Initialisations |
Swabey89 | 17:ead43c1b729d | 64 | SDcard(); |
Swabey89 | 9:fa8a35d9d6c0 | 65 | |
Swabey89 | 1:31c7b4ab552b | 66 | //Power on self test |
Swabey89 | 1:31c7b4ab552b | 67 | post(); |
Swabey89 | 18:3edfb9152b05 | 68 | |
Swabey89 | 18:3edfb9152b05 | 69 | //Start threads |
Swabey89 | 18:3edfb9152b05 | 70 | SDqueue_thread.start(callback(&SDqueue, &EventQueue::dispatch_forever)); |
Swabey89 | 18:3edfb9152b05 | 71 | LCDqueue_thread.start(callback(&LCDqueue, &EventQueue::dispatch_forever)); |
Swabey89 | 19:88d8359306a4 | 72 | serial_thread.start(callback(&serialqueue, &EventQueue::dispatch_forever)); |
Swabey89 | 21:2c438eeaab14 | 73 | Network_thread.start(network); |
Swabey89 | 19:88d8359306a4 | 74 | producer_thread.start(sampleProducer); |
Swabey89 | 19:88d8359306a4 | 75 | consumer_thread.start(sampleConsumer); |
Swabey89 | 3:b1583f309b43 | 76 | |
Swabey89 | 20:cdde0663c481 | 77 | |
Swabey89 | 18:3edfb9152b05 | 78 | //Attach ISRs |
Swabey89 | 18:3edfb9152b05 | 79 | sample.attach(&sampleISR, sample_rate); |
Swabey89 | 11:3ad0327e8c9f | 80 | pc->attach(serialISR, Serial::RxIrq); |
Swabey89 | 15:8af9672a6778 | 81 | |
Swabey89 | 1:31c7b4ab552b | 82 | //Flash to indicate goodness |
Swabey89 | 1:31c7b4ab552b | 83 | while(true) { |
Swabey89 | 5:956984cbe447 | 84 | greenLED = !greenLED; |
Swabey89 | 5:956984cbe447 | 85 | Thread::wait(500); |
Swabey89 | 0:4afd4940a189 | 86 | } |
Swabey89 | 0:4afd4940a189 | 87 | } |
Swabey89 | 1:31c7b4ab552b | 88 | |
Swabey89 | 19:88d8359306a4 | 89 | /* |
Swabey89 | 19:88d8359306a4 | 90 | FUNCITONS BELOW NEED MOVING? |
Swabey89 | 19:88d8359306a4 | 91 | */ |
Swabey89 | 19:88d8359306a4 | 92 | |
Swabey89 | 8:c81b0ff8b822 | 93 | void sampleISR() |
Swabey89 | 8:c81b0ff8b822 | 94 | { |
Swabey89 | 19:88d8359306a4 | 95 | producer_thread.signal_set(TAKE_SAMPLE); |
Swabey89 | 8:c81b0ff8b822 | 96 | } |
Swabey89 | 1:31c7b4ab552b | 97 | |
Swabey89 | 11:3ad0327e8c9f | 98 | void serialISR() |
Swabey89 | 11:3ad0327e8c9f | 99 | { |
Swabey89 | 11:3ad0327e8c9f | 100 | pc->attach(NULL, Serial::RxIrq); |
Swabey89 | 19:88d8359306a4 | 101 | serialqueue.call(serialData); |
Swabey89 | 11:3ad0327e8c9f | 102 | } |
Swabey89 | 1:31c7b4ab552b | 103 | |
Swabey89 | 19:88d8359306a4 | 104 | void serialData() |
Swabey89 | 12:3a54cbaa714c | 105 | { |
Swabey89 | 17:ead43c1b729d | 106 | static int i = 0; |
Swabey89 | 11:3ad0327e8c9f | 107 | if (pc->readable()) |
Swabey89 | 11:3ad0327e8c9f | 108 | { |
Swabey89 | 18:3edfb9152b05 | 109 | cmdBuffer[i] = pc->getc(); |
Swabey89 | 18:3edfb9152b05 | 110 | if (cmdBuffer[i] == '\r') |
Swabey89 | 11:3ad0327e8c9f | 111 | { |
Swabey89 | 11:3ad0327e8c9f | 112 | i = 0; |
Swabey89 | 18:3edfb9152b05 | 113 | serialqueue.call(serialterm); |
Swabey89 | 11:3ad0327e8c9f | 114 | } |
Swabey89 | 11:3ad0327e8c9f | 115 | else i++; |
Swabey89 | 11:3ad0327e8c9f | 116 | } |
Swabey89 | 11:3ad0327e8c9f | 117 | pc->attach(serialISR, Serial::RxIrq); |
Swabey89 | 11:3ad0327e8c9f | 118 | } |
Swabey89 | 19:88d8359306a4 | 119 | |
Swabey89 | 19:88d8359306a4 | 120 | |
Swabey89 | 19:88d8359306a4 | 121 | void sampleProducer() |
Swabey89 | 19:88d8359306a4 | 122 | { |
Swabey89 | 19:88d8359306a4 | 123 | while(true) |
Swabey89 | 19:88d8359306a4 | 124 | { |
Swabey89 | 19:88d8359306a4 | 125 | //High priority thread |
Swabey89 | 19:88d8359306a4 | 126 | Thread::signal_wait(TAKE_SAMPLE); |
Swabey89 | 19:88d8359306a4 | 127 | |
Swabey89 | 19:88d8359306a4 | 128 | //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 | 129 | bufferLock.lock(); |
Swabey89 | 19:88d8359306a4 | 130 | //Update buffer |
Swabey89 | 19:88d8359306a4 | 131 | newestIndex = (newestIndex+1) % BUFFERSIZE; //CIRCULAR |
Swabey89 | 19:88d8359306a4 | 132 | buffer[newestIndex].updatetemp(sensor.getTemperature()); |
Swabey89 | 19:88d8359306a4 | 133 | buffer[newestIndex].updatepress(sensor.getPressure()); |
Swabey89 | 19:88d8359306a4 | 134 | buffer[newestIndex].updatelight(adcIn.read()); |
Swabey89 | 21:2c438eeaab14 | 135 | buffer[newestIndex].updateTime(); |
Swabey89 | 21:2c438eeaab14 | 136 | |
Swabey89 | 19:88d8359306a4 | 137 | //bufferLock.unlock(); //normally here, moved due to updating queues. |
Swabey89 | 19:88d8359306a4 | 138 | samplesInBuffer.release(); |
Swabey89 | 19:88d8359306a4 | 139 | |
Swabey89 | 19:88d8359306a4 | 140 | //Pass onto queues |
Swabey89 | 19:88d8359306a4 | 141 | LCDqueue.call(LCD_display, buffer[newestIndex].gettemp(),buffer[newestIndex].getpress(),buffer[newestIndex].getlight()); |
Swabey89 | 19:88d8359306a4 | 142 | bufferLock.unlock(); |
Swabey89 | 19:88d8359306a4 | 143 | |
Swabey89 | 19:88d8359306a4 | 144 | //Write to the SD card when i have 120 samples in the buffer |
Swabey89 | 19:88d8359306a4 | 145 | if (newestIndex == 119) |
Swabey89 | 19:88d8359306a4 | 146 | { |
Swabey89 | 19:88d8359306a4 | 147 | //save to SD card |
Swabey89 | 19:88d8359306a4 | 148 | consumer_thread.signal_set(STORE_DATA); |
Swabey89 | 19:88d8359306a4 | 149 | |
Swabey89 | 19:88d8359306a4 | 150 | } |
Swabey89 | 19:88d8359306a4 | 151 | |
Swabey89 | 19:88d8359306a4 | 152 | //pc->printf("%d\r\n", newestIndex); |
Swabey89 | 19:88d8359306a4 | 153 | |
Swabey89 | 19:88d8359306a4 | 154 | } |
Swabey89 | 19:88d8359306a4 | 155 | } |
Swabey89 | 19:88d8359306a4 | 156 | |
Swabey89 | 19:88d8359306a4 | 157 | void sampleConsumer() |
Swabey89 | 19:88d8359306a4 | 158 | { |
Swabey89 | 19:88d8359306a4 | 159 | while(true) |
Swabey89 | 19:88d8359306a4 | 160 | { |
Swabey89 | 19:88d8359306a4 | 161 | |
Swabey89 | 19:88d8359306a4 | 162 | static time_t seconds; |
Swabey89 | 19:88d8359306a4 | 163 | |
Swabey89 | 19:88d8359306a4 | 164 | //write to the SD card from 0 up to newestIndex. |
Swabey89 | 19:88d8359306a4 | 165 | Thread::signal_wait(STORE_DATA); |
Swabey89 | 19:88d8359306a4 | 166 | bufferLock.lock(); |
Swabey89 | 19:88d8359306a4 | 167 | redLED = 1; |
Swabey89 | 19:88d8359306a4 | 168 | |
Swabey89 | 19:88d8359306a4 | 169 | char fileDate[30]; |
Swabey89 | 19:88d8359306a4 | 170 | seconds = time(NULL); |
Swabey89 | 19:88d8359306a4 | 171 | timeData = localtime(&seconds); |
Swabey89 | 19:88d8359306a4 | 172 | set_time(mktime(timeData)); |
Swabey89 | 19:88d8359306a4 | 173 | strftime(fileDate, 30, "sd/sampledata/%d_%m_%y.csv", timeData); |
Swabey89 | 19:88d8359306a4 | 174 | |
Swabey89 | 19:88d8359306a4 | 175 | fp = fopen(fileDate,"a"); |
Swabey89 | 19:88d8359306a4 | 176 | |
Swabey89 | 19:88d8359306a4 | 177 | for (int i = 0; i<BUFFERSIZE; i++) |
Swabey89 | 19:88d8359306a4 | 178 | { |
Swabey89 | 20:cdde0663c481 | 179 | fprintf(fp, "%6.1f,%.2f\n\r", buffer[i].gettemp(), buffer[i].getpress()); |
Swabey89 | 19:88d8359306a4 | 180 | } |
Swabey89 | 19:88d8359306a4 | 181 | |
Swabey89 | 19:88d8359306a4 | 182 | fclose(fp); |
Swabey89 | 19:88d8359306a4 | 183 | redLED = 0; |
Swabey89 | 19:88d8359306a4 | 184 | bufferLock.unlock(); |
Swabey89 | 19:88d8359306a4 | 185 | printf("SD card updated\r\n"); |
Swabey89 | 19:88d8359306a4 | 186 | } |
Swabey89 | 19:88d8359306a4 | 187 | } |