V4
Dependencies: BMP280
Fork of Thread_Communication_V3 by
main.cpp@8:ab6322afa341, 2017-12-29 (annotated)
- Committer:
- dnonoo
- Date:
- Fri Dec 29 17:50:30 2017 +0000
- Revision:
- 8:ab6322afa341
- Parent:
- 7:f017a37bcf1b
- Child:
- 9:b838c5787ed7
Buffer(Mutex) with LCD (Mail Queue), Serial and SD Card (Mutex Locks)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benparkes | 0:cb3a5c15b01e | 1 | #include "mbed.h" |
benparkes | 0:cb3a5c15b01e | 2 | #include "main.h" |
dnonoo | 7:f017a37bcf1b | 3 | #include "stdio.h" |
GeorgeJourneaux | 4:93d6d13d4de3 | 4 | |
dnonoo | 8:ab6322afa341 | 5 | #define FallingEdge 0 |
dnonoo | 8:ab6322afa341 | 6 | #define RisingEdge 1 |
dnonoo | 8:ab6322afa341 | 7 | #define USER_BUTTON_PRESSED 1 |
dnonoo | 8:ab6322afa341 | 8 | |
GeorgeJourneaux | 3:73497379c0cb | 9 | LCD lcd(PD_15, PF_12, PF_13, PE_9, PF_14, PF_15); |
GeorgeJourneaux | 3:73497379c0cb | 10 | BMP280 Sensor(D14, D15); |
benparkes | 0:cb3a5c15b01e | 11 | |
GeorgeJourneaux | 5:ea3ec65cbf5f | 12 | //Define Functions |
benparkes | 0:cb3a5c15b01e | 13 | void PrintLCD (); |
GeorgeJourneaux | 2:28d12a3db239 | 14 | void Rx_interrupt(); |
dnonoo | 7:f017a37bcf1b | 15 | void serialCMD(); |
dnonoo | 7:f017a37bcf1b | 16 | void sensorRead(); |
dnonoo | 7:f017a37bcf1b | 17 | void readISR(); |
dnonoo | 7:f017a37bcf1b | 18 | void circBuff(); |
dnonoo | 8:ab6322afa341 | 19 | void writeRemove_SD(); |
dnonoo | 8:ab6322afa341 | 20 | |
dnonoo | 8:ab6322afa341 | 21 | |
dnonoo | 8:ab6322afa341 | 22 | // USER_BUTTON ISRs (Debounce) |
dnonoo | 8:ab6322afa341 | 23 | |
dnonoo | 8:ab6322afa341 | 24 | void userButtonRise(); |
dnonoo | 8:ab6322afa341 | 25 | void userButtonFall(); |
dnonoo | 8:ab6322afa341 | 26 | void userButtonTimeoutHandler(); |
dnonoo | 8:ab6322afa341 | 27 | |
dnonoo | 8:ab6322afa341 | 28 | // Tickers & Timeouts |
dnonoo | 8:ab6322afa341 | 29 | Timeout userButtonTimeout; // FOR debouncing User Switch |
dnonoo | 8:ab6322afa341 | 30 | Ticker read; // ***Sets sampling period!*** (ISR Signals sampling Thread) |
GeorgeJourneaux | 2:28d12a3db239 | 31 | |
dnonoo | 7:f017a37bcf1b | 32 | /* LOCKS */ |
GeorgeJourneaux | 6:64d346936f0e | 33 | Mutex DataBuffer; |
dnonoo | 8:ab6322afa341 | 34 | Mutex dataLock; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 35 | |
dnonoo | 7:f017a37bcf1b | 36 | /* THREADS */ |
dnonoo | 7:f017a37bcf1b | 37 | Thread _PrintLCD, _serialCMD, _circBuff; |
dnonoo | 8:ab6322afa341 | 38 | Thread _sensorRead (osPriorityRealtime); //dataLock Thread |
dnonoo | 8:ab6322afa341 | 39 | Thread _writeRemove_SD; |
GeorgeJourneaux | 3:73497379c0cb | 40 | |
dnonoo | 7:f017a37bcf1b | 41 | /* GLOBAL DATA */ |
dnonoo | 7:f017a37bcf1b | 42 | volatile float LDR = 0; |
dnonoo | 7:f017a37bcf1b | 43 | volatile double PRES = 0; |
dnonoo | 7:f017a37bcf1b | 44 | volatile double TEMP = 0; |
benparkes | 1:bca9993a0df3 | 45 | |
dnonoo | 8:ab6322afa341 | 46 | // int to hold current switch state |
dnonoo | 8:ab6322afa341 | 47 | int userButtonState = FallingEdge; |
dnonoo | 8:ab6322afa341 | 48 | |
dnonoo | 8:ab6322afa341 | 49 | /* DEBOUNCER INTERRUPTS */ |
dnonoo | 8:ab6322afa341 | 50 | InterruptIn userButton(USER_BUTTON); |
dnonoo | 8:ab6322afa341 | 51 | |
dnonoo | 8:ab6322afa341 | 52 | void userButtonRise () { |
dnonoo | 8:ab6322afa341 | 53 | userButton.rise(NULL); |
dnonoo | 8:ab6322afa341 | 54 | userButtonState = RisingEdge; |
dnonoo | 8:ab6322afa341 | 55 | userButtonTimeout.attach(&userButtonTimeoutHandler, 0.1); |
dnonoo | 8:ab6322afa341 | 56 | } |
benparkes | 0:cb3a5c15b01e | 57 | |
dnonoo | 8:ab6322afa341 | 58 | void userButtonFall () { |
dnonoo | 8:ab6322afa341 | 59 | userButton.fall(NULL); |
dnonoo | 8:ab6322afa341 | 60 | _writeRemove_SD.signal_set(USER_BUTTON_PRESSED); |
dnonoo | 8:ab6322afa341 | 61 | userButtonState = FallingEdge; |
dnonoo | 8:ab6322afa341 | 62 | userButtonTimeout.attach(&userButtonTimeoutHandler, 0.1); |
dnonoo | 8:ab6322afa341 | 63 | } |
dnonoo | 7:f017a37bcf1b | 64 | |
dnonoo | 8:ab6322afa341 | 65 | void userButtonTimeoutHandler() { |
dnonoo | 8:ab6322afa341 | 66 | userButtonTimeout.detach(); |
dnonoo | 8:ab6322afa341 | 67 | |
dnonoo | 8:ab6322afa341 | 68 | switch (userButtonState) { |
dnonoo | 8:ab6322afa341 | 69 | case RisingEdge: |
dnonoo | 8:ab6322afa341 | 70 | userButton.fall(&userButtonFall); |
dnonoo | 8:ab6322afa341 | 71 | break; |
dnonoo | 8:ab6322afa341 | 72 | case FallingEdge: |
dnonoo | 8:ab6322afa341 | 73 | userButton.rise(userButtonRise); |
dnonoo | 8:ab6322afa341 | 74 | break; |
dnonoo | 8:ab6322afa341 | 75 | }// End Switch |
dnonoo | 8:ab6322afa341 | 76 | } //End ISR |
GeorgeJourneaux | 3:73497379c0cb | 77 | /*--------------------------------MAIN--------------------------------*/ |
GeorgeJourneaux | 3:73497379c0cb | 78 | int main() { |
GeorgeJourneaux | 3:73497379c0cb | 79 | |
GeorgeJourneaux | 3:73497379c0cb | 80 | pc.baud(9600); |
GeorgeJourneaux | 3:73497379c0cb | 81 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
dnonoo | 7:f017a37bcf1b | 82 | POST(); |
dnonoo | 7:f017a37bcf1b | 83 | |
dnonoo | 7:f017a37bcf1b | 84 | _serialCMD.start(serialCMD); |
dnonoo | 7:f017a37bcf1b | 85 | _PrintLCD.start(PrintLCD); |
dnonoo | 7:f017a37bcf1b | 86 | _sensorRead.start(sensorRead); |
dnonoo | 7:f017a37bcf1b | 87 | _circBuff.start(circBuff); |
dnonoo | 8:ab6322afa341 | 88 | _writeRemove_SD.start(writeRemove_SD); |
dnonoo | 7:f017a37bcf1b | 89 | |
dnonoo | 8:ab6322afa341 | 90 | userButton.rise(&userButtonRise); |
dnonoo | 7:f017a37bcf1b | 91 | read.attach(readISR, SAMPLING_PERIOD); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 92 | |
dnonoo | 7:f017a37bcf1b | 93 | |
dnonoo | 8:ab6322afa341 | 94 | while (1) { |
dnonoo | 8:ab6322afa341 | 95 | Yellow_ext = ON; |
dnonoo | 8:ab6322afa341 | 96 | Thread::wait (200); |
dnonoo | 8:ab6322afa341 | 97 | Yellow_ext = OFF; |
dnonoo | 8:ab6322afa341 | 98 | Thread::wait(200); |
dnonoo | 8:ab6322afa341 | 99 | }// End While |
dnonoo | 8:ab6322afa341 | 100 | } // End Main |
dnonoo | 7:f017a37bcf1b | 101 | /*--------------------------------------------------------------------*/ |
dnonoo | 7:f017a37bcf1b | 102 | void circBuff () { |
dnonoo | 7:f017a37bcf1b | 103 | |
dnonoo | 7:f017a37bcf1b | 104 | while(1) { |
dnonoo | 7:f017a37bcf1b | 105 | Thread::signal_wait(DATA_READY); // wait for signal from sensorRead |
GeorgeJourneaux | 5:ea3ec65cbf5f | 106 | |
dnonoo | 7:f017a37bcf1b | 107 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 108 | DataBuffer.lock(); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 109 | |
dnonoo | 7:f017a37bcf1b | 110 | //Format samples, send to FIFO buffer head |
GeorgeJourneaux | 6:64d346936f0e | 111 | memset(data_buffer[sample_h],NULL,64); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 112 | time( &raw_time ); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 113 | sample_epoch = localtime( &raw_time ); |
GeorgeJourneaux | 6:64d346936f0e | 114 | char sample_time[20]; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 115 | strftime(sample_time,20,"%d/%m/%Y %X",sample_epoch); |
dnonoo | 7:f017a37bcf1b | 116 | |
dnonoo | 8:ab6322afa341 | 117 | dataLock.lock(); //lock critical section |
dnonoo | 7:f017a37bcf1b | 118 | sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, TEMP, PRES, LDR); |
dnonoo | 8:ab6322afa341 | 119 | dataLock.unlock(); //unlock critical section |
dnonoo | 7:f017a37bcf1b | 120 | |
GeorgeJourneaux | 5:ea3ec65cbf5f | 121 | memset(sample_time,NULL,20); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 122 | |
dnonoo | 7:f017a37bcf1b | 123 | //Set seperate FIFO head and tail for printing data |
GeorgeJourneaux | 6:64d346936f0e | 124 | data_h = sample_h; |
GeorgeJourneaux | 6:64d346936f0e | 125 | data_t = sample_t; |
GeorgeJourneaux | 6:64d346936f0e | 126 | |
dnonoo | 7:f017a37bcf1b | 127 | //Move sample FIFO buffer head to next row in buffer |
GeorgeJourneaux | 6:64d346936f0e | 128 | sample_h++; |
dnonoo | 7:f017a37bcf1b | 129 | //Check sample FIFO buffer head |
GeorgeJourneaux | 6:64d346936f0e | 130 | if(sample_h >= MAX_SAMPLES){ |
GeorgeJourneaux | 6:64d346936f0e | 131 | sample_h = 0; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 132 | } |
dnonoo | 7:f017a37bcf1b | 133 | //Check sample FIFO buffer tail |
GeorgeJourneaux | 6:64d346936f0e | 134 | if(sample_t == sample_h){ |
GeorgeJourneaux | 6:64d346936f0e | 135 | sample_t++; |
GeorgeJourneaux | 6:64d346936f0e | 136 | if(sample_t >= (MAX_SAMPLES)){ |
GeorgeJourneaux | 6:64d346936f0e | 137 | sample_t = 0; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 138 | } |
GeorgeJourneaux | 5:ea3ec65cbf5f | 139 | } |
dnonoo | 7:f017a37bcf1b | 140 | //Unlock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 141 | DataBuffer.unlock(); |
dnonoo | 7:f017a37bcf1b | 142 | } |
dnonoo | 7:f017a37bcf1b | 143 | } |
dnonoo | 7:f017a37bcf1b | 144 | |
dnonoo | 7:f017a37bcf1b | 145 | /*---------------------Read Sensors ---------------------------*/ |
dnonoo | 7:f017a37bcf1b | 146 | |
dnonoo | 7:f017a37bcf1b | 147 | void readISR () { // Ticker interrupt defined in main |
dnonoo | 7:f017a37bcf1b | 148 | |
dnonoo | 7:f017a37bcf1b | 149 | _sensorRead.signal_set(SENSOR_UPDATE); |
dnonoo | 7:f017a37bcf1b | 150 | } |
dnonoo | 8:ab6322afa341 | 151 | // Keep short |
dnonoo | 7:f017a37bcf1b | 152 | void sensorRead () { |
dnonoo | 7:f017a37bcf1b | 153 | |
dnonoo | 7:f017a37bcf1b | 154 | while (1) { |
dnonoo | 7:f017a37bcf1b | 155 | |
dnonoo | 8:ab6322afa341 | 156 | dataLock.lock(); // Entering Critial Section |
dnonoo | 7:f017a37bcf1b | 157 | |
dnonoo | 7:f017a37bcf1b | 158 | // Store Data in global Variables |
dnonoo | 7:f017a37bcf1b | 159 | LDR = LDR_In.read(); |
dnonoo | 7:f017a37bcf1b | 160 | TEMP = Sensor.getTemperature(); |
dnonoo | 7:f017a37bcf1b | 161 | PRES = Sensor.getPressure(); |
dnonoo | 7:f017a37bcf1b | 162 | |
dnonoo | 8:ab6322afa341 | 163 | dataLock.unlock(); // Exiting Critical Section |
dnonoo | 7:f017a37bcf1b | 164 | |
dnonoo | 7:f017a37bcf1b | 165 | Green_int = !Green_int; // debugging |
GeorgeJourneaux | 5:ea3ec65cbf5f | 166 | |
dnonoo | 7:f017a37bcf1b | 167 | //Read sensors, send to mail-queue |
dnonoo | 7:f017a37bcf1b | 168 | mail_t *mail = mail_box.alloc(); |
dnonoo | 7:f017a37bcf1b | 169 | mail->LDR_Value = LDR; |
dnonoo | 7:f017a37bcf1b | 170 | mail->temp_Value = TEMP; |
dnonoo | 7:f017a37bcf1b | 171 | mail->press_Value = PRES; |
dnonoo | 7:f017a37bcf1b | 172 | mail_box.put(mail); |
dnonoo | 7:f017a37bcf1b | 173 | |
dnonoo | 7:f017a37bcf1b | 174 | _circBuff.signal_set(DATA_READY); // Set signal to buffer to store updated values |
dnonoo | 7:f017a37bcf1b | 175 | Thread::signal_wait(SENSOR_UPDATE); // Wait for the Timer interrupt |
dnonoo | 7:f017a37bcf1b | 176 | } |
GeorgeJourneaux | 3:73497379c0cb | 177 | } |
dnonoo | 7:f017a37bcf1b | 178 | |
GeorgeJourneaux | 3:73497379c0cb | 179 | |
GeorgeJourneaux | 3:73497379c0cb | 180 | /*--------------------------------LCD---------------------------------*/ |
benparkes | 0:cb3a5c15b01e | 181 | void PrintLCD () { |
benparkes | 0:cb3a5c15b01e | 182 | |
benparkes | 0:cb3a5c15b01e | 183 | int i = 0; |
benparkes | 0:cb3a5c15b01e | 184 | while(1){ |
benparkes | 1:bca9993a0df3 | 185 | char lightString[16]; |
benparkes | 1:bca9993a0df3 | 186 | char tempString[16]; |
benparkes | 1:bca9993a0df3 | 187 | char pressString[16]; |
benparkes | 1:bca9993a0df3 | 188 | |
benparkes | 0:cb3a5c15b01e | 189 | lcd.Clear(); |
benparkes | 0:cb3a5c15b01e | 190 | lcd.RowSelect(0); |
benparkes | 1:bca9993a0df3 | 191 | |
benparkes | 0:cb3a5c15b01e | 192 | switch (i){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 193 | case 0:{ |
GeorgeJourneaux | 2:28d12a3db239 | 194 | osEvent evt = mail_box.get(); |
benparkes | 1:bca9993a0df3 | 195 | |
GeorgeJourneaux | 2:28d12a3db239 | 196 | if (evt.status == osEventMail) { |
GeorgeJourneaux | 2:28d12a3db239 | 197 | mail_t *mail = (mail_t*)evt.value.p; |
benparkes | 0:cb3a5c15b01e | 198 | |
GeorgeJourneaux | 2:28d12a3db239 | 199 | sprintf(lightString,"%.4f", mail->LDR_Value); |
GeorgeJourneaux | 2:28d12a3db239 | 200 | sprintf(tempString,"%2.2f", mail->temp_Value); |
GeorgeJourneaux | 2:28d12a3db239 | 201 | sprintf(pressString,"%4.2f", mail->press_Value); |
benparkes | 1:bca9993a0df3 | 202 | |
GeorgeJourneaux | 2:28d12a3db239 | 203 | mail_box.free(mail); |
GeorgeJourneaux | 2:28d12a3db239 | 204 | } |
benparkes | 1:bca9993a0df3 | 205 | |
GeorgeJourneaux | 2:28d12a3db239 | 206 | lcd.Write("Light Level:"); |
GeorgeJourneaux | 2:28d12a3db239 | 207 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 208 | lcd.Write(lightString); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 209 | i++; |
benparkes | 0:cb3a5c15b01e | 210 | break; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 211 | } |
GeorgeJourneaux | 2:28d12a3db239 | 212 | case 1: |
GeorgeJourneaux | 2:28d12a3db239 | 213 | lcd.Write("Temperature:"); |
GeorgeJourneaux | 2:28d12a3db239 | 214 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 215 | lcd.Write(tempString); |
GeorgeJourneaux | 2:28d12a3db239 | 216 | i++; |
benparkes | 0:cb3a5c15b01e | 217 | break; |
benparkes | 0:cb3a5c15b01e | 218 | |
GeorgeJourneaux | 2:28d12a3db239 | 219 | case 2: |
GeorgeJourneaux | 2:28d12a3db239 | 220 | lcd.Write("Pressure:"); |
GeorgeJourneaux | 2:28d12a3db239 | 221 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 222 | lcd.Write(pressString); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 223 | i =0; |
benparkes | 0:cb3a5c15b01e | 224 | break; |
GeorgeJourneaux | 2:28d12a3db239 | 225 | |
GeorgeJourneaux | 5:ea3ec65cbf5f | 226 | default: |
GeorgeJourneaux | 5:ea3ec65cbf5f | 227 | i = 0; |
benparkes | 0:cb3a5c15b01e | 228 | break; |
benparkes | 0:cb3a5c15b01e | 229 | } |
benparkes | 0:cb3a5c15b01e | 230 | |
GeorgeJourneaux | 2:28d12a3db239 | 231 | Red_int = !Red_int; |
benparkes | 1:bca9993a0df3 | 232 | |
benparkes | 0:cb3a5c15b01e | 233 | Thread::wait (5000); |
GeorgeJourneaux | 2:28d12a3db239 | 234 | } |
benparkes | 0:cb3a5c15b01e | 235 | } |
GeorgeJourneaux | 3:73497379c0cb | 236 | /*--------------------------------------------------------------------*/ |
benparkes | 1:bca9993a0df3 | 237 | |
GeorgeJourneaux | 3:73497379c0cb | 238 | /*------------------------------SERIAL_CMD----------------------------*/ |
GeorgeJourneaux | 4:93d6d13d4de3 | 239 | //Interrupt when recieving from serial port |
GeorgeJourneaux | 2:28d12a3db239 | 240 | void Rx_interrupt() { |
GeorgeJourneaux | 2:28d12a3db239 | 241 | |
dnonoo | 7:f017a37bcf1b | 242 | //Wait for serial input |
GeorgeJourneaux | 2:28d12a3db239 | 243 | while (pc.readable()) { |
GeorgeJourneaux | 6:64d346936f0e | 244 | |
dnonoo | 7:f017a37bcf1b | 245 | //Return input to serial |
GeorgeJourneaux | 5:ea3ec65cbf5f | 246 | rx_buffer[rx_in] = pc.getc(); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 247 | pc.putc(rx_buffer[rx_in]); |
GeorgeJourneaux | 6:64d346936f0e | 248 | |
dnonoo | 7:f017a37bcf1b | 249 | //If enter key is pressed, set serial thread signal |
GeorgeJourneaux | 5:ea3ec65cbf5f | 250 | if(rx_buffer[rx_in] == 0xD){ |
dnonoo | 7:f017a37bcf1b | 251 | _serialCMD.signal_set(ENTER_KEY); |
GeorgeJourneaux | 2:28d12a3db239 | 252 | } |
GeorgeJourneaux | 6:64d346936f0e | 253 | |
dnonoo | 7:f017a37bcf1b | 254 | //Increment buffer head |
GeorgeJourneaux | 2:28d12a3db239 | 255 | else{ |
GeorgeJourneaux | 2:28d12a3db239 | 256 | rx_in = (rx_in + 1); |
GeorgeJourneaux | 2:28d12a3db239 | 257 | } |
GeorgeJourneaux | 2:28d12a3db239 | 258 | } |
GeorgeJourneaux | 2:28d12a3db239 | 259 | } |
benparkes | 0:cb3a5c15b01e | 260 | |
GeorgeJourneaux | 4:93d6d13d4de3 | 261 | //Check what command what recieved and execute |
dnonoo | 7:f017a37bcf1b | 262 | void serialCMD(){ |
GeorgeJourneaux | 2:28d12a3db239 | 263 | |
GeorgeJourneaux | 2:28d12a3db239 | 264 | while(1){ |
dnonoo | 7:f017a37bcf1b | 265 | //Wait for thread signal |
GeorgeJourneaux | 4:93d6d13d4de3 | 266 | Thread::signal_wait(ENTER_KEY); |
GeorgeJourneaux | 6:64d346936f0e | 267 | |
dnonoo | 7:f017a37bcf1b | 268 | //Detach serial interrupt |
GeorgeJourneaux | 2:28d12a3db239 | 269 | pc.attach(NULL, Serial::RxIrq); |
GeorgeJourneaux | 6:64d346936f0e | 270 | |
GeorgeJourneaux | 3:73497379c0cb | 271 | struct tm * s_time; |
GeorgeJourneaux | 3:73497379c0cb | 272 | char tm_n[4]; |
GeorgeJourneaux | 3:73497379c0cb | 273 | |
GeorgeJourneaux | 6:64d346936f0e | 274 | /*----CARRAGE RETURN-------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 275 | if(rx_buffer[0] == 0xD){ |
GeorgeJourneaux | 6:64d346936f0e | 276 | pc.puts("\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 277 | } |
GeorgeJourneaux | 6:64d346936f0e | 278 | /*----READ ALL----------------------------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 279 | else if(strstr(rx_buffer, "READ ALL")){ |
GeorgeJourneaux | 6:64d346936f0e | 280 | pc.puts(" READ ALL\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 281 | |
dnonoo | 7:f017a37bcf1b | 282 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 283 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 284 | |
dnonoo | 7:f017a37bcf1b | 285 | //Print all samples to serial |
GeorgeJourneaux | 6:64d346936f0e | 286 | for(int n=data_t; n<=MAX_SAMPLES; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 287 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 288 | } |
GeorgeJourneaux | 6:64d346936f0e | 289 | if(data_t>data_h){ |
GeorgeJourneaux | 6:64d346936f0e | 290 | for(int n=0; n<=(data_t-1); n++){ |
GeorgeJourneaux | 6:64d346936f0e | 291 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 292 | } |
GeorgeJourneaux | 6:64d346936f0e | 293 | } |
GeorgeJourneaux | 5:ea3ec65cbf5f | 294 | |
dnonoo | 7:f017a37bcf1b | 295 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 296 | DataBuffer.unlock(); |
GeorgeJourneaux | 2:28d12a3db239 | 297 | } |
GeorgeJourneaux | 6:64d346936f0e | 298 | /*----DELETE ALL----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 299 | else if(strstr(rx_buffer, "DELETE ALL")){ |
GeorgeJourneaux | 6:64d346936f0e | 300 | pc.puts(" DELETE ALL\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 301 | |
GeorgeJourneaux | 6:64d346936f0e | 302 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 303 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 304 | |
GeorgeJourneaux | 6:64d346936f0e | 305 | //Delete all sampled data |
GeorgeJourneaux | 6:64d346936f0e | 306 | for(int n=0; n<=MAX_SAMPLES; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 307 | memset(data_buffer[n], NULL, 64); |
GeorgeJourneaux | 6:64d346936f0e | 308 | } |
GeorgeJourneaux | 6:64d346936f0e | 309 | data_h = data_t; |
GeorgeJourneaux | 6:64d346936f0e | 310 | sample_h = sample_t; |
GeorgeJourneaux | 6:64d346936f0e | 311 | |
GeorgeJourneaux | 6:64d346936f0e | 312 | //Unlock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 313 | DataBuffer.unlock(); |
GeorgeJourneaux | 2:28d12a3db239 | 314 | } |
GeorgeJourneaux | 6:64d346936f0e | 315 | /*----READ----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 316 | else if(strstr(rx_buffer, "READ")){ |
GeorgeJourneaux | 6:64d346936f0e | 317 | pc.puts(" READ \n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 318 | int N = atoi(strncpy(tm_n,&rx_buffer[5],4)); |
GeorgeJourneaux | 6:64d346936f0e | 319 | int S = 0; |
GeorgeJourneaux | 6:64d346936f0e | 320 | pc.printf("N = %d\n\r",N); |
GeorgeJourneaux | 6:64d346936f0e | 321 | |
GeorgeJourneaux | 6:64d346936f0e | 322 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 323 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 324 | |
GeorgeJourneaux | 6:64d346936f0e | 325 | //Check if N is greater than buffer size |
GeorgeJourneaux | 6:64d346936f0e | 326 | if(N >= MAX_SAMPLES){ |
GeorgeJourneaux | 6:64d346936f0e | 327 | N = MAX_SAMPLES; |
GeorgeJourneaux | 6:64d346936f0e | 328 | } |
GeorgeJourneaux | 6:64d346936f0e | 329 | |
GeorgeJourneaux | 6:64d346936f0e | 330 | //Read N samples from FIFO buffer |
GeorgeJourneaux | 6:64d346936f0e | 331 | if(N <= 0){ |
GeorgeJourneaux | 6:64d346936f0e | 332 | pc.puts("####ERROR####\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 333 | } |
GeorgeJourneaux | 6:64d346936f0e | 334 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 335 | for(int n=data_t; n<=MAX_SAMPLES-1; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 336 | if(S>=N){} |
GeorgeJourneaux | 6:64d346936f0e | 337 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 338 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 339 | S++; |
GeorgeJourneaux | 6:64d346936f0e | 340 | } |
GeorgeJourneaux | 6:64d346936f0e | 341 | } |
GeorgeJourneaux | 6:64d346936f0e | 342 | for(int n=0; n<=data_t; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 343 | if(S>=N){} |
GeorgeJourneaux | 6:64d346936f0e | 344 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 345 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 346 | S++; |
GeorgeJourneaux | 6:64d346936f0e | 347 | } |
GeorgeJourneaux | 6:64d346936f0e | 348 | } |
GeorgeJourneaux | 6:64d346936f0e | 349 | } |
GeorgeJourneaux | 6:64d346936f0e | 350 | |
GeorgeJourneaux | 6:64d346936f0e | 351 | //Unlock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 352 | DataBuffer.unlock(); |
GeorgeJourneaux | 2:28d12a3db239 | 353 | } |
GeorgeJourneaux | 6:64d346936f0e | 354 | /*----DELETE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 355 | else if(strstr(rx_buffer, "DELETE")){ |
GeorgeJourneaux | 6:64d346936f0e | 356 | pc.puts(" DELETE \n\r"); |
GeorgeJourneaux | 3:73497379c0cb | 357 | } |
GeorgeJourneaux | 6:64d346936f0e | 358 | /*----SETDATE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 359 | else if(strstr(rx_buffer, "SETDATE")){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 360 | time(&raw_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 361 | s_time = localtime(&raw_time); |
GeorgeJourneaux | 3:73497379c0cb | 362 | |
GeorgeJourneaux | 6:64d346936f0e | 363 | //Update day in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 364 | int dd = atoi(strncpy(tm_n,&rx_buffer[8],2)); |
GeorgeJourneaux | 3:73497379c0cb | 365 | s_time->tm_mday = dd; |
GeorgeJourneaux | 3:73497379c0cb | 366 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 367 | |
GeorgeJourneaux | 6:64d346936f0e | 368 | //Update month in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 369 | int mm = atoi(strncpy(tm_n,&rx_buffer[11],2)); |
GeorgeJourneaux | 3:73497379c0cb | 370 | s_time->tm_mon = mm-1; |
GeorgeJourneaux | 3:73497379c0cb | 371 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 372 | |
GeorgeJourneaux | 6:64d346936f0e | 373 | //Update year in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 374 | int yyyy = atoi(strncpy(tm_n,&rx_buffer[14],4)); |
GeorgeJourneaux | 3:73497379c0cb | 375 | s_time->tm_year = yyyy-1900; |
GeorgeJourneaux | 3:73497379c0cb | 376 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 377 | |
GeorgeJourneaux | 6:64d346936f0e | 378 | //Set date from updated time structure |
GeorgeJourneaux | 3:73497379c0cb | 379 | set_time(mktime(s_time)); |
GeorgeJourneaux | 6:64d346936f0e | 380 | strftime(serial_buffer, 80, "\n\r Set Date: %d/%m/%Y\n\r", s_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 381 | pc.puts(serial_buffer); |
GeorgeJourneaux | 2:28d12a3db239 | 382 | } |
GeorgeJourneaux | 6:64d346936f0e | 383 | /*----SETTIME---------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 384 | else if(strstr(rx_buffer, "SETTIME")){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 385 | time(&raw_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 386 | s_time = localtime(&raw_time); |
GeorgeJourneaux | 3:73497379c0cb | 387 | |
GeorgeJourneaux | 6:64d346936f0e | 388 | //Update seconds in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 389 | int ss = atoi(strncpy(tm_n,&rx_buffer[14],2)); |
GeorgeJourneaux | 3:73497379c0cb | 390 | s_time->tm_sec = ss; |
GeorgeJourneaux | 3:73497379c0cb | 391 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 6:64d346936f0e | 392 | |
GeorgeJourneaux | 6:64d346936f0e | 393 | //Update minutes in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 394 | int mm = atoi(strncpy(tm_n,&rx_buffer[11],2)); |
GeorgeJourneaux | 3:73497379c0cb | 395 | s_time->tm_min = mm; |
GeorgeJourneaux | 3:73497379c0cb | 396 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 397 | |
GeorgeJourneaux | 6:64d346936f0e | 398 | //Update hour in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 399 | int hh = atoi(strncpy(tm_n,&rx_buffer[8],2)); |
GeorgeJourneaux | 3:73497379c0cb | 400 | s_time->tm_hour = hh; |
GeorgeJourneaux | 3:73497379c0cb | 401 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 6:64d346936f0e | 402 | |
GeorgeJourneaux | 6:64d346936f0e | 403 | //Set time from updated time structure |
GeorgeJourneaux | 3:73497379c0cb | 404 | set_time(mktime(s_time)); |
GeorgeJourneaux | 6:64d346936f0e | 405 | strftime(serial_buffer, 80, "\n\r Set Time: %X\n\r", s_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 406 | pc.puts(serial_buffer); |
GeorgeJourneaux | 2:28d12a3db239 | 407 | } |
GeorgeJourneaux | 6:64d346936f0e | 408 | /*----SETT----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 409 | else if(strstr(rx_buffer, "SETT")){ |
GeorgeJourneaux | 6:64d346936f0e | 410 | pc.puts(" SETT\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 411 | } |
GeorgeJourneaux | 6:64d346936f0e | 412 | /*----STATE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 413 | else if(strstr(rx_buffer, "STATE")){ |
GeorgeJourneaux | 6:64d346936f0e | 414 | pc.puts(" STATE\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 415 | } |
GeorgeJourneaux | 6:64d346936f0e | 416 | /*----LOGGING----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 417 | else if(strstr(rx_buffer, "LOGGING")){ |
GeorgeJourneaux | 6:64d346936f0e | 418 | pc.puts(" LOGGING\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 419 | } |
GeorgeJourneaux | 6:64d346936f0e | 420 | /*----ERROR---*/ |
GeorgeJourneaux | 2:28d12a3db239 | 421 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 422 | pc.puts("####ERROR####\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 423 | } |
GeorgeJourneaux | 6:64d346936f0e | 424 | /*----------------------------------------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 425 | |
GeorgeJourneaux | 6:64d346936f0e | 426 | //Clear serial buffers |
GeorgeJourneaux | 5:ea3ec65cbf5f | 427 | memset(serial_buffer, NULL, 80); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 428 | memset(rx_buffer, NULL, 32); |
GeorgeJourneaux | 2:28d12a3db239 | 429 | rx_in = 0; |
GeorgeJourneaux | 2:28d12a3db239 | 430 | |
GeorgeJourneaux | 6:64d346936f0e | 431 | //Attach serial interrupt |
GeorgeJourneaux | 2:28d12a3db239 | 432 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
GeorgeJourneaux | 2:28d12a3db239 | 433 | } |
GeorgeJourneaux | 2:28d12a3db239 | 434 | } |
dnonoo | 7:f017a37bcf1b | 435 | /*------------------------------------------------*/ |
dnonoo | 7:f017a37bcf1b | 436 | |
dnonoo | 7:f017a37bcf1b | 437 | void POST () { |
dnonoo | 7:f017a37bcf1b | 438 | |
dnonoo | 7:f017a37bcf1b | 439 | pc.printf(" ALL Leds should be flashing\n\r"); |
dnonoo | 7:f017a37bcf1b | 440 | |
dnonoo | 7:f017a37bcf1b | 441 | for(unsigned int n = 0; n<10; n++) { |
dnonoo | 7:f017a37bcf1b | 442 | Green_int = ON; |
dnonoo | 7:f017a37bcf1b | 443 | Blue_int = ON; |
dnonoo | 7:f017a37bcf1b | 444 | Red_int = ON; |
dnonoo | 7:f017a37bcf1b | 445 | Green_ext = ON; |
dnonoo | 7:f017a37bcf1b | 446 | Yellow_ext = ON; |
dnonoo | 7:f017a37bcf1b | 447 | Red_ext = ON; |
dnonoo | 7:f017a37bcf1b | 448 | |
dnonoo | 7:f017a37bcf1b | 449 | wait (0.2); |
dnonoo | 7:f017a37bcf1b | 450 | Green_int = OFF; |
dnonoo | 7:f017a37bcf1b | 451 | Blue_int = OFF; |
dnonoo | 7:f017a37bcf1b | 452 | Red_int = OFF; |
dnonoo | 7:f017a37bcf1b | 453 | Green_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 454 | Yellow_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 455 | Red_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 456 | wait (0.2); |
dnonoo | 7:f017a37bcf1b | 457 | } |
dnonoo | 7:f017a37bcf1b | 458 | |
dnonoo | 7:f017a37bcf1b | 459 | pc.printf("Switch states:\n\r"); |
dnonoo | 7:f017a37bcf1b | 460 | pc.printf("\tSW_L: %d\n\r\tSW_R %d\n\r", SW_L.read(), SW_R.read()); |
dnonoo | 7:f017a37bcf1b | 461 | |
dnonoo | 7:f017a37bcf1b | 462 | float Temp = Sensor.getTemperature(); |
dnonoo | 7:f017a37bcf1b | 463 | float Pres = Sensor.getPressure(); |
dnonoo | 7:f017a37bcf1b | 464 | float ldrs = LDR_In.read(); |
dnonoo | 7:f017a37bcf1b | 465 | |
dnonoo | 7:f017a37bcf1b | 466 | pc.printf("Sensor test:\n\r"); |
dnonoo | 7:f017a37bcf1b | 467 | pc.printf("T: %f\tP: %f\tL: %f\n\r",Temp,Pres,ldrs); |
dnonoo | 7:f017a37bcf1b | 468 | |
dnonoo | 7:f017a37bcf1b | 469 | pc.printf("LCD Test\n\r"); |
dnonoo | 7:f017a37bcf1b | 470 | |
dnonoo | 7:f017a37bcf1b | 471 | lcd.Clear(); |
dnonoo | 7:f017a37bcf1b | 472 | lcd.RowSelect(1); |
dnonoo | 7:f017a37bcf1b | 473 | lcd.Write("*******LCD******"); |
dnonoo | 7:f017a37bcf1b | 474 | lcd.RowSelect(2); |
dnonoo | 7:f017a37bcf1b | 475 | lcd.Write("******TEST******"); |
dnonoo | 7:f017a37bcf1b | 476 | wait(1); |
dnonoo | 7:f017a37bcf1b | 477 | lcd.Clear(); |
dnonoo | 7:f017a37bcf1b | 478 | } |
dnonoo | 8:ab6322afa341 | 479 | |
dnonoo | 8:ab6322afa341 | 480 | void writeRemove_SD() { |
dnonoo | 7:f017a37bcf1b | 481 | |
dnonoo | 8:ab6322afa341 | 482 | while(1) { |
dnonoo | 8:ab6322afa341 | 483 | Thread::signal_wait(USER_BUTTON_PRESSED); //wait for debounce signal |
dnonoo | 8:ab6322afa341 | 484 | pc.printf("Initalising SD Card\n\r"); |
dnonoo | 8:ab6322afa341 | 485 | |
dnonoo | 8:ab6322afa341 | 486 | //check init |
dnonoo | 8:ab6322afa341 | 487 | if (sd.init() != 0) { |
dnonoo | 8:ab6322afa341 | 488 | pc.printf("******SD Initialise FAIL*******\n\r"); |
dnonoo | 8:ab6322afa341 | 489 | } |
dnonoo | 8:ab6322afa341 | 490 | |
dnonoo | 8:ab6322afa341 | 491 | // Create Filing system for SD Card |
dnonoo | 8:ab6322afa341 | 492 | FATFileSystem fs("sd", &sd); |
dnonoo | 8:ab6322afa341 | 493 | |
dnonoo | 8:ab6322afa341 | 494 | //OpenFiles to write/append to |
dnonoo | 8:ab6322afa341 | 495 | pc.printf("Writing to SDC\n\r"); |
dnonoo | 8:ab6322afa341 | 496 | |
dnonoo | 8:ab6322afa341 | 497 | FILE* fp = fopen("/sd/TheChamberOfSecrets.txt", "w"); //"w" to overwrite file ftb |
dnonoo | 8:ab6322afa341 | 498 | |
dnonoo | 8:ab6322afa341 | 499 | // Check for error in opening file |
dnonoo | 8:ab6322afa341 | 500 | if (fp == NULL) { |
dnonoo | 8:ab6322afa341 | 501 | pc.printf("*****ERROR - Could not open file for write*****\n\r"); |
dnonoo | 8:ab6322afa341 | 502 | } |
dnonoo | 8:ab6322afa341 | 503 | //HERE IS WHERE TO PRINT DATA TO SD CARD FROM BUFFER (REMEMBER TO EMPTY BUFFER???) |
dnonoo | 8:ab6322afa341 | 504 | //Lock data buffer |
dnonoo | 8:ab6322afa341 | 505 | DataBuffer.lock(); |
dnonoo | 8:ab6322afa341 | 506 | dataLock.lock(); |
dnonoo | 8:ab6322afa341 | 507 | //Print all samples to SD |
dnonoo | 8:ab6322afa341 | 508 | for(int n=data_t; n<=MAX_SAMPLES; n++){ |
dnonoo | 8:ab6322afa341 | 509 | fputs(data_buffer[n], fp); |
dnonoo | 8:ab6322afa341 | 510 | fprintf(fp, "\n\r"); |
dnonoo | 8:ab6322afa341 | 511 | } |
dnonoo | 8:ab6322afa341 | 512 | if(data_t>data_h){ |
dnonoo | 8:ab6322afa341 | 513 | for(int n=0; n<=(data_t-1); n++){ |
dnonoo | 8:ab6322afa341 | 514 | fputs(data_buffer[n], fp); |
dnonoo | 8:ab6322afa341 | 515 | |
dnonoo | 8:ab6322afa341 | 516 | } |
dnonoo | 8:ab6322afa341 | 517 | } |
dnonoo | 8:ab6322afa341 | 518 | |
dnonoo | 8:ab6322afa341 | 519 | //Lock data buffer |
dnonoo | 8:ab6322afa341 | 520 | DataBuffer.unlock(); |
dnonoo | 8:ab6322afa341 | 521 | dataLock.unlock(); |
dnonoo | 8:ab6322afa341 | 522 | //fprintf(fp, "dd/mm/yy hh:mm:ss, TEMPERATURE, PRESSURE, LIGHT\n\r"); |
dnonoo | 8:ab6322afa341 | 523 | |
dnonoo | 8:ab6322afa341 | 524 | fclose(fp); |
dnonoo | 8:ab6322afa341 | 525 | |
dnonoo | 8:ab6322afa341 | 526 | pc.printf("Write Sucessful!\n\r"); |
dnonoo | 8:ab6322afa341 | 527 | |
dnonoo | 8:ab6322afa341 | 528 | sd.deinit(); |
dnonoo | 8:ab6322afa341 | 529 | pc.printf("SD Card Ready to Remove\n\r"); |
dnonoo | 8:ab6322afa341 | 530 | Green_ext = 1; |
dnonoo | 8:ab6322afa341 | 531 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 532 | Green_ext = 0; |
dnonoo | 8:ab6322afa341 | 533 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 534 | Green_ext = 1; |
dnonoo | 8:ab6322afa341 | 535 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 536 | Green_ext = 0; |
dnonoo | 8:ab6322afa341 | 537 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 538 | Green_ext = 1; |
dnonoo | 8:ab6322afa341 | 539 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 540 | Green_ext = 0; |
dnonoo | 8:ab6322afa341 | 541 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 542 | Green_ext = 1; |
dnonoo | 8:ab6322afa341 | 543 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 544 | Green_ext = 0; |
dnonoo | 8:ab6322afa341 | 545 | Thread::wait(500); |
dnonoo | 8:ab6322afa341 | 546 | }// End While |
dnonoo | 8:ab6322afa341 | 547 | }// End Thread |
dnonoo | 8:ab6322afa341 | 548 |