Latest

Dependencies:   serial_terminal sample_hardware PLL_Config SDCard BMP280 Networkbits TextLCD SDBlockDevice

Committer:
Swabey89
Date:
Mon Dec 17 17:51:14 2018 +0000
Revision:
20:cdde0663c481
Parent:
19:88d8359306a4
Child:
21:2c438eeaab14
Networking thread added with new webpage. Requires work so that it doesn't lock

Who changed what in which revision?

UserRevisionLine numberNew 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 20:cdde0663c481 94 Thread Network_thread;
Swabey89 0:4afd4940a189 95
Swabey89 17:ead43c1b729d 96 //Timers
Swabey89 8:c81b0ff8b822 97 Ticker sample;
Swabey89 10:0fffc988d325 98
Swabey89 17:ead43c1b729d 99 //Function prototypes
Swabey89 8:c81b0ff8b822 100 void sampleISR(void);
Swabey89 19:88d8359306a4 101 void sampleProducer(void);
Swabey89 19:88d8359306a4 102 void sampleConsumer(void);
Swabey89 11:3ad0327e8c9f 103 void serialISR(void);
Swabey89 19:88d8359306a4 104 void serialData(void);
Swabey89 11:3ad0327e8c9f 105
Swabey89 15:8af9672a6778 106 int main() {
Swabey89 17:ead43c1b729d 107 timeData = new tm;
Swabey89 11:3ad0327e8c9f 108 pc = new RawSerial(USBTX, USBRX);
Swabey89 8:c81b0ff8b822 109
Swabey89 18:3edfb9152b05 110 //Initialisations
Swabey89 17:ead43c1b729d 111 SDcard();
Swabey89 9:fa8a35d9d6c0 112
Swabey89 1:31c7b4ab552b 113 //Power on self test
Swabey89 1:31c7b4ab552b 114 post();
Swabey89 18:3edfb9152b05 115
Swabey89 18:3edfb9152b05 116 //Start threads
Swabey89 18:3edfb9152b05 117 SDqueue_thread.start(callback(&SDqueue, &EventQueue::dispatch_forever));
Swabey89 18:3edfb9152b05 118 LCDqueue_thread.start(callback(&LCDqueue, &EventQueue::dispatch_forever));
Swabey89 19:88d8359306a4 119 serial_thread.start(callback(&serialqueue, &EventQueue::dispatch_forever));
Swabey89 20:cdde0663c481 120 Network_thread.start(networktest);
Swabey89 19:88d8359306a4 121 producer_thread.start(sampleProducer);
Swabey89 19:88d8359306a4 122 consumer_thread.start(sampleConsumer);
Swabey89 3:b1583f309b43 123
Swabey89 20:cdde0663c481 124
Swabey89 18:3edfb9152b05 125 //Attach ISRs
Swabey89 18:3edfb9152b05 126 sample.attach(&sampleISR, sample_rate);
Swabey89 11:3ad0327e8c9f 127 pc->attach(serialISR, Serial::RxIrq);
Swabey89 15:8af9672a6778 128
Swabey89 1:31c7b4ab552b 129 //Flash to indicate goodness
Swabey89 1:31c7b4ab552b 130 while(true) {
Swabey89 5:956984cbe447 131 greenLED = !greenLED;
Swabey89 5:956984cbe447 132 Thread::wait(500);
Swabey89 0:4afd4940a189 133 }
Swabey89 0:4afd4940a189 134 }
Swabey89 1:31c7b4ab552b 135
Swabey89 19:88d8359306a4 136 /*
Swabey89 19:88d8359306a4 137 FUNCITONS BELOW NEED MOVING?
Swabey89 19:88d8359306a4 138 */
Swabey89 19:88d8359306a4 139
Swabey89 8:c81b0ff8b822 140 void sampleISR()
Swabey89 8:c81b0ff8b822 141 {
Swabey89 19:88d8359306a4 142 producer_thread.signal_set(TAKE_SAMPLE);
Swabey89 8:c81b0ff8b822 143 }
Swabey89 1:31c7b4ab552b 144
Swabey89 11:3ad0327e8c9f 145 void serialISR()
Swabey89 11:3ad0327e8c9f 146 {
Swabey89 11:3ad0327e8c9f 147 pc->attach(NULL, Serial::RxIrq);
Swabey89 19:88d8359306a4 148 serialqueue.call(serialData);
Swabey89 11:3ad0327e8c9f 149 }
Swabey89 1:31c7b4ab552b 150
Swabey89 19:88d8359306a4 151 void serialData()
Swabey89 12:3a54cbaa714c 152 {
Swabey89 17:ead43c1b729d 153 static int i = 0;
Swabey89 11:3ad0327e8c9f 154 if (pc->readable())
Swabey89 11:3ad0327e8c9f 155 {
Swabey89 18:3edfb9152b05 156 cmdBuffer[i] = pc->getc();
Swabey89 18:3edfb9152b05 157 if (cmdBuffer[i] == '\r')
Swabey89 11:3ad0327e8c9f 158 {
Swabey89 11:3ad0327e8c9f 159 i = 0;
Swabey89 18:3edfb9152b05 160 serialqueue.call(serialterm);
Swabey89 11:3ad0327e8c9f 161 }
Swabey89 11:3ad0327e8c9f 162 else i++;
Swabey89 11:3ad0327e8c9f 163 }
Swabey89 11:3ad0327e8c9f 164 pc->attach(serialISR, Serial::RxIrq);
Swabey89 11:3ad0327e8c9f 165 }
Swabey89 19:88d8359306a4 166
Swabey89 19:88d8359306a4 167
Swabey89 19:88d8359306a4 168 void sampleProducer()
Swabey89 19:88d8359306a4 169 {
Swabey89 19:88d8359306a4 170 while(true)
Swabey89 19:88d8359306a4 171 {
Swabey89 19:88d8359306a4 172 //High priority thread
Swabey89 19:88d8359306a4 173 Thread::signal_wait(TAKE_SAMPLE);
Swabey89 19:88d8359306a4 174
Swabey89 19:88d8359306a4 175 //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 176 bufferLock.lock();
Swabey89 19:88d8359306a4 177 //Update buffer
Swabey89 19:88d8359306a4 178 newestIndex = (newestIndex+1) % BUFFERSIZE; //CIRCULAR
Swabey89 19:88d8359306a4 179 buffer[newestIndex].updatetemp(sensor.getTemperature());
Swabey89 19:88d8359306a4 180 buffer[newestIndex].updatepress(sensor.getPressure());
Swabey89 19:88d8359306a4 181 buffer[newestIndex].updatelight(adcIn.read());
Swabey89 19:88d8359306a4 182 //bufferLock.unlock(); //normally here, moved due to updating queues.
Swabey89 19:88d8359306a4 183 samplesInBuffer.release();
Swabey89 19:88d8359306a4 184
Swabey89 19:88d8359306a4 185 //Pass onto queues
Swabey89 19:88d8359306a4 186 LCDqueue.call(LCD_display, buffer[newestIndex].gettemp(),buffer[newestIndex].getpress(),buffer[newestIndex].getlight());
Swabey89 19:88d8359306a4 187 bufferLock.unlock();
Swabey89 19:88d8359306a4 188
Swabey89 19:88d8359306a4 189 //Write to the SD card when i have 120 samples in the buffer
Swabey89 19:88d8359306a4 190 if (newestIndex == 119)
Swabey89 19:88d8359306a4 191 {
Swabey89 19:88d8359306a4 192 //save to SD card
Swabey89 19:88d8359306a4 193 consumer_thread.signal_set(STORE_DATA);
Swabey89 19:88d8359306a4 194
Swabey89 19:88d8359306a4 195 }
Swabey89 19:88d8359306a4 196
Swabey89 19:88d8359306a4 197 //pc->printf("%d\r\n", newestIndex);
Swabey89 19:88d8359306a4 198
Swabey89 19:88d8359306a4 199 }
Swabey89 19:88d8359306a4 200 }
Swabey89 19:88d8359306a4 201
Swabey89 19:88d8359306a4 202 void sampleConsumer()
Swabey89 19:88d8359306a4 203 {
Swabey89 19:88d8359306a4 204 while(true)
Swabey89 19:88d8359306a4 205 {
Swabey89 19:88d8359306a4 206
Swabey89 19:88d8359306a4 207 static time_t seconds;
Swabey89 19:88d8359306a4 208
Swabey89 19:88d8359306a4 209 //write to the SD card from 0 up to newestIndex.
Swabey89 19:88d8359306a4 210 Thread::signal_wait(STORE_DATA);
Swabey89 19:88d8359306a4 211 bufferLock.lock();
Swabey89 19:88d8359306a4 212 redLED = 1;
Swabey89 19:88d8359306a4 213
Swabey89 19:88d8359306a4 214 char fileDate[30];
Swabey89 19:88d8359306a4 215 seconds = time(NULL);
Swabey89 19:88d8359306a4 216 timeData = localtime(&seconds);
Swabey89 19:88d8359306a4 217 set_time(mktime(timeData));
Swabey89 19:88d8359306a4 218 strftime(fileDate, 30, "sd/sampledata/%d_%m_%y.csv", timeData);
Swabey89 19:88d8359306a4 219
Swabey89 19:88d8359306a4 220 fp = fopen(fileDate,"a");
Swabey89 19:88d8359306a4 221
Swabey89 19:88d8359306a4 222 for (int i = 0; i<BUFFERSIZE; i++)
Swabey89 19:88d8359306a4 223 {
Swabey89 20:cdde0663c481 224 fprintf(fp, "%6.1f,%.2f\n\r", buffer[i].gettemp(), buffer[i].getpress());
Swabey89 19:88d8359306a4 225 }
Swabey89 19:88d8359306a4 226
Swabey89 19:88d8359306a4 227 fclose(fp);
Swabey89 19:88d8359306a4 228 redLED = 0;
Swabey89 19:88d8359306a4 229 bufferLock.unlock();
Swabey89 19:88d8359306a4 230 printf("SD card updated\r\n");
Swabey89 19:88d8359306a4 231 }
Swabey89 19:88d8359306a4 232 }