V4
Dependencies: BMP280
Fork of Thread_Communication_V3 by
main.cpp@13:089f3adb3813, 2018-01-05 (annotated)
- Committer:
- dnonoo
- Date:
- Fri Jan 05 19:47:33 2018 +0000
- Revision:
- 13:089f3adb3813
- Parent:
- 12:a244f6f9d2fe
- Child:
- 14:7c4d87dfc388
okokok
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 | 9:b838c5787ed7 | 20 | void Network1(); |
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; |
dnonoo | 9:b838c5787ed7 | 40 | Thread _Network1; |
GeorgeJourneaux | 3:73497379c0cb | 41 | |
dnonoo | 7:f017a37bcf1b | 42 | /* GLOBAL DATA */ |
dnonoo | 7:f017a37bcf1b | 43 | volatile float LDR = 0; |
dnonoo | 7:f017a37bcf1b | 44 | volatile double PRES = 0; |
dnonoo | 7:f017a37bcf1b | 45 | volatile double TEMP = 0; |
dnonoo | 13:089f3adb3813 | 46 | volatile char TIME[21]; |
benparkes | 1:bca9993a0df3 | 47 | |
dnonoo | 13:089f3adb3813 | 48 | volatile double sampleTime = 15.0; |
dnonoo | 8:ab6322afa341 | 49 | // int to hold current switch state |
dnonoo | 8:ab6322afa341 | 50 | int userButtonState = FallingEdge; |
dnonoo | 8:ab6322afa341 | 51 | |
dnonoo | 8:ab6322afa341 | 52 | /* DEBOUNCER INTERRUPTS */ |
dnonoo | 8:ab6322afa341 | 53 | InterruptIn userButton(USER_BUTTON); |
dnonoo | 8:ab6322afa341 | 54 | |
dnonoo | 8:ab6322afa341 | 55 | void userButtonRise () { |
dnonoo | 8:ab6322afa341 | 56 | userButton.rise(NULL); |
dnonoo | 8:ab6322afa341 | 57 | userButtonState = RisingEdge; |
dnonoo | 8:ab6322afa341 | 58 | userButtonTimeout.attach(&userButtonTimeoutHandler, 0.1); |
dnonoo | 8:ab6322afa341 | 59 | } |
benparkes | 0:cb3a5c15b01e | 60 | |
dnonoo | 8:ab6322afa341 | 61 | void userButtonFall () { |
dnonoo | 8:ab6322afa341 | 62 | userButton.fall(NULL); |
dnonoo | 8:ab6322afa341 | 63 | _writeRemove_SD.signal_set(USER_BUTTON_PRESSED); |
dnonoo | 8:ab6322afa341 | 64 | userButtonState = FallingEdge; |
dnonoo | 8:ab6322afa341 | 65 | userButtonTimeout.attach(&userButtonTimeoutHandler, 0.1); |
dnonoo | 8:ab6322afa341 | 66 | } |
dnonoo | 7:f017a37bcf1b | 67 | |
dnonoo | 8:ab6322afa341 | 68 | void userButtonTimeoutHandler() { |
dnonoo | 8:ab6322afa341 | 69 | userButtonTimeout.detach(); |
dnonoo | 8:ab6322afa341 | 70 | |
dnonoo | 8:ab6322afa341 | 71 | switch (userButtonState) { |
dnonoo | 8:ab6322afa341 | 72 | case RisingEdge: |
dnonoo | 8:ab6322afa341 | 73 | userButton.fall(&userButtonFall); |
dnonoo | 8:ab6322afa341 | 74 | break; |
dnonoo | 8:ab6322afa341 | 75 | case FallingEdge: |
dnonoo | 8:ab6322afa341 | 76 | userButton.rise(userButtonRise); |
dnonoo | 8:ab6322afa341 | 77 | break; |
dnonoo | 8:ab6322afa341 | 78 | }// End Switch |
dnonoo | 8:ab6322afa341 | 79 | } //End ISR |
GeorgeJourneaux | 3:73497379c0cb | 80 | /*--------------------------------MAIN--------------------------------*/ |
GeorgeJourneaux | 3:73497379c0cb | 81 | int main() { |
GeorgeJourneaux | 3:73497379c0cb | 82 | |
dnonoo | 13:089f3adb3813 | 83 | pc.baud(57600); |
GeorgeJourneaux | 3:73497379c0cb | 84 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
dnonoo | 7:f017a37bcf1b | 85 | POST(); |
dnonoo | 7:f017a37bcf1b | 86 | |
dnonoo | 10:c10d1337d754 | 87 | pc.printf("\n\n\nType HELP for list of available Commands\n\n\n\r"); |
dnonoo | 7:f017a37bcf1b | 88 | _serialCMD.start(serialCMD); |
dnonoo | 11:19135c83c208 | 89 | _sensorRead.start(sensorRead); |
dnonoo | 7:f017a37bcf1b | 90 | _PrintLCD.start(PrintLCD); |
dnonoo | 7:f017a37bcf1b | 91 | _circBuff.start(circBuff); |
dnonoo | 8:ab6322afa341 | 92 | _writeRemove_SD.start(writeRemove_SD); |
dnonoo | 9:b838c5787ed7 | 93 | _Network1.start(Network1); |
dnonoo | 7:f017a37bcf1b | 94 | |
dnonoo | 8:ab6322afa341 | 95 | userButton.rise(&userButtonRise); |
dnonoo | 10:c10d1337d754 | 96 | read.attach(&readISR, sampleTime); |
dnonoo | 7:f017a37bcf1b | 97 | |
dnonoo | 8:ab6322afa341 | 98 | while (1) { |
dnonoo | 8:ab6322afa341 | 99 | Yellow_ext = ON; |
dnonoo | 8:ab6322afa341 | 100 | Thread::wait (200); |
dnonoo | 8:ab6322afa341 | 101 | Yellow_ext = OFF; |
dnonoo | 8:ab6322afa341 | 102 | Thread::wait(200); |
dnonoo | 8:ab6322afa341 | 103 | }// End While |
dnonoo | 8:ab6322afa341 | 104 | } // End Main |
dnonoo | 7:f017a37bcf1b | 105 | /*--------------------------------------------------------------------*/ |
dnonoo | 10:c10d1337d754 | 106 | /*-----------------Circular Buffer------------------------------------*/ |
dnonoo | 7:f017a37bcf1b | 107 | void circBuff () { |
dnonoo | 7:f017a37bcf1b | 108 | |
dnonoo | 7:f017a37bcf1b | 109 | while(1) { |
dnonoo | 7:f017a37bcf1b | 110 | Thread::signal_wait(DATA_READY); // wait for signal from sensorRead |
GeorgeJourneaux | 5:ea3ec65cbf5f | 111 | |
dnonoo | 7:f017a37bcf1b | 112 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 113 | DataBuffer.lock(); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 114 | |
dnonoo | 7:f017a37bcf1b | 115 | //Format samples, send to FIFO buffer head |
dnonoo | 13:089f3adb3813 | 116 | memset(data_buffer[sample_h],NULL,64); |
dnonoo | 12:a244f6f9d2fe | 117 | |
dnonoo | 8:ab6322afa341 | 118 | dataLock.lock(); //lock critical section |
dnonoo | 13:089f3adb3813 | 119 | sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", TIME, TEMP, PRES, LDR); |
dnonoo | 8:ab6322afa341 | 120 | dataLock.unlock(); //unlock critical section |
dnonoo | 7:f017a37bcf1b | 121 | |
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 | 10:c10d1337d754 | 142 | }// End While |
dnonoo | 10:c10d1337d754 | 143 | }// End Circular buffer |
dnonoo | 10:c10d1337d754 | 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 | 13:089f3adb3813 | 163 | memset((char *)TIME, NULL, 21); |
dnonoo | 13:089f3adb3813 | 164 | time(&raw_time); |
dnonoo | 13:089f3adb3813 | 165 | sample_epoch = localtime(&raw_time); |
dnonoo | 13:089f3adb3813 | 166 | strftime((char *)TIME, 21,"%d/%m/%Y, %X", sample_epoch); |
dnonoo | 13:089f3adb3813 | 167 | |
dnonoo | 8:ab6322afa341 | 168 | dataLock.unlock(); // Exiting Critical Section |
dnonoo | 7:f017a37bcf1b | 169 | |
dnonoo | 7:f017a37bcf1b | 170 | Green_int = !Green_int; // debugging |
GeorgeJourneaux | 5:ea3ec65cbf5f | 171 | |
dnonoo | 7:f017a37bcf1b | 172 | //Read sensors, send to mail-queue |
dnonoo | 7:f017a37bcf1b | 173 | mail_t *mail = mail_box.alloc(); |
dnonoo | 7:f017a37bcf1b | 174 | mail->LDR_Value = LDR; |
dnonoo | 7:f017a37bcf1b | 175 | mail->temp_Value = TEMP; |
dnonoo | 7:f017a37bcf1b | 176 | mail->press_Value = PRES; |
dnonoo | 7:f017a37bcf1b | 177 | mail_box.put(mail); |
dnonoo | 7:f017a37bcf1b | 178 | |
dnonoo | 11:19135c83c208 | 179 | |
dnonoo | 7:f017a37bcf1b | 180 | _circBuff.signal_set(DATA_READY); // Set signal to buffer to store updated values |
dnonoo | 7:f017a37bcf1b | 181 | Thread::signal_wait(SENSOR_UPDATE); // Wait for the Timer interrupt |
dnonoo | 7:f017a37bcf1b | 182 | } |
GeorgeJourneaux | 3:73497379c0cb | 183 | } |
dnonoo | 10:c10d1337d754 | 184 | /*--------------------------------------------------------------------*/ |
GeorgeJourneaux | 3:73497379c0cb | 185 | |
GeorgeJourneaux | 3:73497379c0cb | 186 | /*--------------------------------LCD---------------------------------*/ |
benparkes | 0:cb3a5c15b01e | 187 | void PrintLCD () { |
benparkes | 0:cb3a5c15b01e | 188 | |
benparkes | 0:cb3a5c15b01e | 189 | int i = 0; |
benparkes | 0:cb3a5c15b01e | 190 | while(1){ |
benparkes | 1:bca9993a0df3 | 191 | char lightString[16]; |
benparkes | 1:bca9993a0df3 | 192 | char tempString[16]; |
benparkes | 1:bca9993a0df3 | 193 | char pressString[16]; |
benparkes | 1:bca9993a0df3 | 194 | |
benparkes | 0:cb3a5c15b01e | 195 | lcd.Clear(); |
benparkes | 0:cb3a5c15b01e | 196 | lcd.RowSelect(0); |
benparkes | 1:bca9993a0df3 | 197 | |
benparkes | 0:cb3a5c15b01e | 198 | switch (i){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 199 | case 0:{ |
GeorgeJourneaux | 2:28d12a3db239 | 200 | osEvent evt = mail_box.get(); |
benparkes | 1:bca9993a0df3 | 201 | |
GeorgeJourneaux | 2:28d12a3db239 | 202 | if (evt.status == osEventMail) { |
GeorgeJourneaux | 2:28d12a3db239 | 203 | mail_t *mail = (mail_t*)evt.value.p; |
benparkes | 0:cb3a5c15b01e | 204 | |
GeorgeJourneaux | 2:28d12a3db239 | 205 | sprintf(lightString,"%.4f", mail->LDR_Value); |
GeorgeJourneaux | 2:28d12a3db239 | 206 | sprintf(tempString,"%2.2f", mail->temp_Value); |
GeorgeJourneaux | 2:28d12a3db239 | 207 | sprintf(pressString,"%4.2f", mail->press_Value); |
benparkes | 1:bca9993a0df3 | 208 | |
GeorgeJourneaux | 2:28d12a3db239 | 209 | mail_box.free(mail); |
GeorgeJourneaux | 2:28d12a3db239 | 210 | } |
benparkes | 1:bca9993a0df3 | 211 | |
dnonoo | 10:c10d1337d754 | 212 | lcd.Write("Light:"); |
GeorgeJourneaux | 2:28d12a3db239 | 213 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 214 | lcd.Write(lightString); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 215 | i++; |
benparkes | 0:cb3a5c15b01e | 216 | break; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 217 | } |
GeorgeJourneaux | 2:28d12a3db239 | 218 | case 1: |
dnonoo | 10:c10d1337d754 | 219 | lcd.Write("Temperature(C):"); |
GeorgeJourneaux | 2:28d12a3db239 | 220 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 221 | lcd.Write(tempString); |
GeorgeJourneaux | 2:28d12a3db239 | 222 | i++; |
benparkes | 0:cb3a5c15b01e | 223 | break; |
benparkes | 0:cb3a5c15b01e | 224 | |
GeorgeJourneaux | 2:28d12a3db239 | 225 | case 2: |
dnonoo | 10:c10d1337d754 | 226 | lcd.Write("Pressure(mBar):"); |
GeorgeJourneaux | 2:28d12a3db239 | 227 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 228 | lcd.Write(pressString); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 229 | i =0; |
benparkes | 0:cb3a5c15b01e | 230 | break; |
GeorgeJourneaux | 2:28d12a3db239 | 231 | |
GeorgeJourneaux | 5:ea3ec65cbf5f | 232 | default: |
GeorgeJourneaux | 5:ea3ec65cbf5f | 233 | i = 0; |
benparkes | 0:cb3a5c15b01e | 234 | break; |
benparkes | 0:cb3a5c15b01e | 235 | } |
benparkes | 0:cb3a5c15b01e | 236 | |
GeorgeJourneaux | 2:28d12a3db239 | 237 | Red_int = !Red_int; |
benparkes | 1:bca9993a0df3 | 238 | |
benparkes | 0:cb3a5c15b01e | 239 | Thread::wait (5000); |
GeorgeJourneaux | 2:28d12a3db239 | 240 | } |
benparkes | 0:cb3a5c15b01e | 241 | } |
GeorgeJourneaux | 3:73497379c0cb | 242 | /*--------------------------------------------------------------------*/ |
benparkes | 1:bca9993a0df3 | 243 | |
GeorgeJourneaux | 3:73497379c0cb | 244 | /*------------------------------SERIAL_CMD----------------------------*/ |
GeorgeJourneaux | 4:93d6d13d4de3 | 245 | //Interrupt when recieving from serial port |
GeorgeJourneaux | 2:28d12a3db239 | 246 | void Rx_interrupt() { |
GeorgeJourneaux | 2:28d12a3db239 | 247 | |
dnonoo | 7:f017a37bcf1b | 248 | //Wait for serial input |
GeorgeJourneaux | 2:28d12a3db239 | 249 | while (pc.readable()) { |
GeorgeJourneaux | 6:64d346936f0e | 250 | |
dnonoo | 7:f017a37bcf1b | 251 | //Return input to serial |
GeorgeJourneaux | 5:ea3ec65cbf5f | 252 | rx_buffer[rx_in] = pc.getc(); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 253 | pc.putc(rx_buffer[rx_in]); |
GeorgeJourneaux | 6:64d346936f0e | 254 | |
dnonoo | 7:f017a37bcf1b | 255 | //If enter key is pressed, set serial thread signal |
GeorgeJourneaux | 5:ea3ec65cbf5f | 256 | if(rx_buffer[rx_in] == 0xD){ |
dnonoo | 7:f017a37bcf1b | 257 | _serialCMD.signal_set(ENTER_KEY); |
GeorgeJourneaux | 2:28d12a3db239 | 258 | } |
GeorgeJourneaux | 6:64d346936f0e | 259 | |
dnonoo | 7:f017a37bcf1b | 260 | //Increment buffer head |
GeorgeJourneaux | 2:28d12a3db239 | 261 | else{ |
dnonoo | 13:089f3adb3813 | 262 | if(rx_in>=32){ |
dnonoo | 13:089f3adb3813 | 263 | puts("\n\rERROR - Stop typing so much!\n\r"); |
dnonoo | 13:089f3adb3813 | 264 | rx_in = 0; |
dnonoo | 13:089f3adb3813 | 265 | memset(rx_buffer, NULL, 32); |
dnonoo | 13:089f3adb3813 | 266 | } |
dnonoo | 13:089f3adb3813 | 267 | else{ |
dnonoo | 13:089f3adb3813 | 268 | rx_in = (rx_in + 1); |
dnonoo | 13:089f3adb3813 | 269 | } |
GeorgeJourneaux | 2:28d12a3db239 | 270 | } |
GeorgeJourneaux | 2:28d12a3db239 | 271 | } |
GeorgeJourneaux | 2:28d12a3db239 | 272 | } |
benparkes | 0:cb3a5c15b01e | 273 | |
GeorgeJourneaux | 4:93d6d13d4de3 | 274 | //Check what command what recieved and execute |
dnonoo | 7:f017a37bcf1b | 275 | void serialCMD(){ |
dnonoo | 11:19135c83c208 | 276 | bool xx = 1; // State for CMD STATE |
GeorgeJourneaux | 2:28d12a3db239 | 277 | while(1){ |
dnonoo | 7:f017a37bcf1b | 278 | //Wait for thread signal |
GeorgeJourneaux | 4:93d6d13d4de3 | 279 | Thread::signal_wait(ENTER_KEY); |
GeorgeJourneaux | 6:64d346936f0e | 280 | |
dnonoo | 7:f017a37bcf1b | 281 | //Detach serial interrupt |
GeorgeJourneaux | 2:28d12a3db239 | 282 | pc.attach(NULL, Serial::RxIrq); |
GeorgeJourneaux | 6:64d346936f0e | 283 | |
GeorgeJourneaux | 3:73497379c0cb | 284 | struct tm * s_time; |
GeorgeJourneaux | 3:73497379c0cb | 285 | char tm_n[4]; |
GeorgeJourneaux | 3:73497379c0cb | 286 | |
GeorgeJourneaux | 6:64d346936f0e | 287 | /*----CARRAGE RETURN-------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 288 | if(rx_buffer[0] == 0xD){ |
GeorgeJourneaux | 6:64d346936f0e | 289 | pc.puts("\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 290 | } |
GeorgeJourneaux | 6:64d346936f0e | 291 | /*----READ ALL----------------------------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 292 | else if(strstr(rx_buffer, "READ ALL")){ |
dnonoo | 13:089f3adb3813 | 293 | pc.puts("\n\r Reading all samples...\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 294 | |
dnonoo | 7:f017a37bcf1b | 295 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 296 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 297 | |
dnonoo | 7:f017a37bcf1b | 298 | //Print all samples to serial |
GeorgeJourneaux | 6:64d346936f0e | 299 | for(int n=data_t; n<=MAX_SAMPLES; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 300 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 301 | } |
GeorgeJourneaux | 6:64d346936f0e | 302 | if(data_t>data_h){ |
GeorgeJourneaux | 6:64d346936f0e | 303 | for(int n=0; n<=(data_t-1); n++){ |
GeorgeJourneaux | 6:64d346936f0e | 304 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 305 | } |
GeorgeJourneaux | 6:64d346936f0e | 306 | } |
GeorgeJourneaux | 5:ea3ec65cbf5f | 307 | |
dnonoo | 7:f017a37bcf1b | 308 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 309 | DataBuffer.unlock(); |
dnonoo | 13:089f3adb3813 | 310 | pc.puts(" All Samples read!\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 311 | } |
GeorgeJourneaux | 6:64d346936f0e | 312 | /*----DELETE ALL----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 313 | else if(strstr(rx_buffer, "DELETE ALL")){ |
dnonoo | 13:089f3adb3813 | 314 | pc.puts("\n\r Deleting all samples...\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 315 | |
GeorgeJourneaux | 6:64d346936f0e | 316 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 317 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 318 | |
GeorgeJourneaux | 6:64d346936f0e | 319 | //Delete all sampled data |
GeorgeJourneaux | 6:64d346936f0e | 320 | for(int n=0; n<=MAX_SAMPLES; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 321 | memset(data_buffer[n], NULL, 64); |
GeorgeJourneaux | 6:64d346936f0e | 322 | } |
GeorgeJourneaux | 6:64d346936f0e | 323 | data_h = data_t; |
GeorgeJourneaux | 6:64d346936f0e | 324 | sample_h = sample_t; |
GeorgeJourneaux | 6:64d346936f0e | 325 | |
GeorgeJourneaux | 6:64d346936f0e | 326 | //Unlock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 327 | DataBuffer.unlock(); |
dnonoo | 13:089f3adb3813 | 328 | pc.puts(" All previous samples deleted!\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 329 | } |
GeorgeJourneaux | 6:64d346936f0e | 330 | /*----READ----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 331 | else if(strstr(rx_buffer, "READ")){ |
dnonoo | 13:089f3adb3813 | 332 | pc.puts("\n\r Reading N samples...\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 333 | int N = atoi(strncpy(tm_n,&rx_buffer[5],4)); |
GeorgeJourneaux | 6:64d346936f0e | 334 | int S = 0; |
GeorgeJourneaux | 6:64d346936f0e | 335 | |
GeorgeJourneaux | 6:64d346936f0e | 336 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 337 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 338 | |
GeorgeJourneaux | 6:64d346936f0e | 339 | //Check if N is greater than buffer size |
GeorgeJourneaux | 6:64d346936f0e | 340 | if(N >= MAX_SAMPLES){ |
GeorgeJourneaux | 6:64d346936f0e | 341 | N = MAX_SAMPLES; |
GeorgeJourneaux | 6:64d346936f0e | 342 | } |
GeorgeJourneaux | 6:64d346936f0e | 343 | |
GeorgeJourneaux | 6:64d346936f0e | 344 | //Read N samples from FIFO buffer |
GeorgeJourneaux | 6:64d346936f0e | 345 | if(N <= 0){ |
dnonoo | 13:089f3adb3813 | 346 | pc.puts("ERROR - N must be greater than 0\n\r"); |
dnonoo | 13:089f3adb3813 | 347 | N = 0; |
GeorgeJourneaux | 6:64d346936f0e | 348 | } |
GeorgeJourneaux | 6:64d346936f0e | 349 | else{ |
dnonoo | 11:19135c83c208 | 350 | for(int n=data_h; n>=0; n--){ |
GeorgeJourneaux | 6:64d346936f0e | 351 | if(S>=N){} |
GeorgeJourneaux | 6:64d346936f0e | 352 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 353 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 354 | S++; |
GeorgeJourneaux | 6:64d346936f0e | 355 | } |
GeorgeJourneaux | 6:64d346936f0e | 356 | } |
dnonoo | 11:19135c83c208 | 357 | for(int n=MAX_SAMPLES-1; n<=data_t; n--){ |
GeorgeJourneaux | 6:64d346936f0e | 358 | if(S>=N){} |
GeorgeJourneaux | 6:64d346936f0e | 359 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 360 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 361 | S++; |
GeorgeJourneaux | 6:64d346936f0e | 362 | } |
GeorgeJourneaux | 6:64d346936f0e | 363 | } |
GeorgeJourneaux | 6:64d346936f0e | 364 | } |
dnonoo | 13:089f3adb3813 | 365 | pc.printf(" Read %d samples\n\r",N); |
GeorgeJourneaux | 6:64d346936f0e | 366 | //Unlock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 367 | DataBuffer.unlock(); |
GeorgeJourneaux | 2:28d12a3db239 | 368 | } |
GeorgeJourneaux | 6:64d346936f0e | 369 | /*----DELETE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 370 | else if(strstr(rx_buffer, "DELETE")){ |
dnonoo | 13:089f3adb3813 | 371 | pc.puts("\n\r Deleting N samples...\n\r"); |
dnonoo | 13:089f3adb3813 | 372 | int N = atoi(strncpy(tm_n,&rx_buffer[6],4)); |
dnonoo | 13:089f3adb3813 | 373 | int S = 0; |
dnonoo | 13:089f3adb3813 | 374 | |
dnonoo | 13:089f3adb3813 | 375 | //Lock data buffer |
dnonoo | 13:089f3adb3813 | 376 | DataBuffer.lock(); |
dnonoo | 13:089f3adb3813 | 377 | |
dnonoo | 13:089f3adb3813 | 378 | //Check if N is greater than buffer size |
dnonoo | 13:089f3adb3813 | 379 | if(N >= MAX_SAMPLES){ |
dnonoo | 13:089f3adb3813 | 380 | N = MAX_SAMPLES; |
dnonoo | 13:089f3adb3813 | 381 | } |
dnonoo | 13:089f3adb3813 | 382 | |
dnonoo | 13:089f3adb3813 | 383 | //Read N samples from FIFO buffer |
dnonoo | 13:089f3adb3813 | 384 | if(N <= 0){ |
dnonoo | 13:089f3adb3813 | 385 | pc.puts("ERROR - N must be greater than 0\n\r"); |
dnonoo | 13:089f3adb3813 | 386 | N = 0; |
dnonoo | 13:089f3adb3813 | 387 | } |
dnonoo | 13:089f3adb3813 | 388 | else{ |
dnonoo | 13:089f3adb3813 | 389 | for(int n=data_t; n<=MAX_SAMPLES; n++){ |
dnonoo | 13:089f3adb3813 | 390 | if(S>=N){} |
dnonoo | 13:089f3adb3813 | 391 | else{ |
dnonoo | 13:089f3adb3813 | 392 | memset(data_buffer[n], NULL, 64); |
dnonoo | 13:089f3adb3813 | 393 | S++; |
dnonoo | 13:089f3adb3813 | 394 | data_t = n; |
dnonoo | 13:089f3adb3813 | 395 | sample_t = n; |
dnonoo | 13:089f3adb3813 | 396 | } |
dnonoo | 13:089f3adb3813 | 397 | } |
dnonoo | 13:089f3adb3813 | 398 | for(int n=MAX_SAMPLES-1; n<=data_t; n--){ |
dnonoo | 13:089f3adb3813 | 399 | if(S>=N){} |
dnonoo | 13:089f3adb3813 | 400 | else{ |
dnonoo | 13:089f3adb3813 | 401 | memset(data_buffer[n], NULL, 64); |
dnonoo | 13:089f3adb3813 | 402 | S++; |
dnonoo | 13:089f3adb3813 | 403 | data_t = n; |
dnonoo | 13:089f3adb3813 | 404 | sample_t = n; |
dnonoo | 13:089f3adb3813 | 405 | } |
dnonoo | 13:089f3adb3813 | 406 | } |
dnonoo | 13:089f3adb3813 | 407 | } |
dnonoo | 13:089f3adb3813 | 408 | pc.printf(" Deleted %d samples\n\r",N); |
dnonoo | 13:089f3adb3813 | 409 | //Unlock data buffer |
dnonoo | 13:089f3adb3813 | 410 | DataBuffer.unlock(); |
GeorgeJourneaux | 3:73497379c0cb | 411 | } |
GeorgeJourneaux | 6:64d346936f0e | 412 | /*----SETDATE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 413 | else if(strstr(rx_buffer, "SETDATE")){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 414 | time(&raw_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 415 | s_time = localtime(&raw_time); |
GeorgeJourneaux | 3:73497379c0cb | 416 | |
GeorgeJourneaux | 6:64d346936f0e | 417 | //Update day in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 418 | int dd = atoi(strncpy(tm_n,&rx_buffer[8],2)); |
GeorgeJourneaux | 3:73497379c0cb | 419 | s_time->tm_mday = dd; |
GeorgeJourneaux | 3:73497379c0cb | 420 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 421 | |
GeorgeJourneaux | 6:64d346936f0e | 422 | //Update month in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 423 | int mm = atoi(strncpy(tm_n,&rx_buffer[11],2)); |
GeorgeJourneaux | 3:73497379c0cb | 424 | s_time->tm_mon = mm-1; |
GeorgeJourneaux | 3:73497379c0cb | 425 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 426 | |
GeorgeJourneaux | 6:64d346936f0e | 427 | //Update year in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 428 | int yyyy = atoi(strncpy(tm_n,&rx_buffer[14],4)); |
GeorgeJourneaux | 3:73497379c0cb | 429 | s_time->tm_year = yyyy-1900; |
GeorgeJourneaux | 3:73497379c0cb | 430 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 431 | |
GeorgeJourneaux | 6:64d346936f0e | 432 | //Set date from updated time structure |
GeorgeJourneaux | 3:73497379c0cb | 433 | set_time(mktime(s_time)); |
dnonoo | 13:089f3adb3813 | 434 | strftime(serial_buffer, 80, "\n\r Date updated to: %d/%m/%Y\n\r", s_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 435 | pc.puts(serial_buffer); |
GeorgeJourneaux | 2:28d12a3db239 | 436 | } |
GeorgeJourneaux | 6:64d346936f0e | 437 | /*----SETTIME---------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 438 | else if(strstr(rx_buffer, "SETTIME")){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 439 | time(&raw_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 440 | s_time = localtime(&raw_time); |
GeorgeJourneaux | 3:73497379c0cb | 441 | |
GeorgeJourneaux | 6:64d346936f0e | 442 | //Update seconds in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 443 | int ss = atoi(strncpy(tm_n,&rx_buffer[14],2)); |
GeorgeJourneaux | 3:73497379c0cb | 444 | s_time->tm_sec = ss; |
GeorgeJourneaux | 3:73497379c0cb | 445 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 6:64d346936f0e | 446 | |
GeorgeJourneaux | 6:64d346936f0e | 447 | //Update minutes in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 448 | int mm = atoi(strncpy(tm_n,&rx_buffer[11],2)); |
GeorgeJourneaux | 3:73497379c0cb | 449 | s_time->tm_min = mm; |
GeorgeJourneaux | 3:73497379c0cb | 450 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 451 | |
GeorgeJourneaux | 6:64d346936f0e | 452 | //Update hour in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 453 | int hh = atoi(strncpy(tm_n,&rx_buffer[8],2)); |
GeorgeJourneaux | 3:73497379c0cb | 454 | s_time->tm_hour = hh; |
GeorgeJourneaux | 3:73497379c0cb | 455 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 6:64d346936f0e | 456 | |
GeorgeJourneaux | 6:64d346936f0e | 457 | //Set time from updated time structure |
GeorgeJourneaux | 3:73497379c0cb | 458 | set_time(mktime(s_time)); |
dnonoo | 13:089f3adb3813 | 459 | strftime(serial_buffer, 80, "\n\r Time updated to: %X\n\r", s_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 460 | pc.puts(serial_buffer); |
GeorgeJourneaux | 2:28d12a3db239 | 461 | } |
GeorgeJourneaux | 6:64d346936f0e | 462 | /*----SETT----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 463 | else if(strstr(rx_buffer, "SETT")){ |
dnonoo | 11:19135c83c208 | 464 | read.detach(); |
dnonoo | 13:089f3adb3813 | 465 | double AA = atof(strncpy(tm_n,&rx_buffer[5],4)); |
dnonoo | 11:19135c83c208 | 466 | |
dnonoo | 13:089f3adb3813 | 467 | if (AA < 0.1 || AA > 60) { |
dnonoo | 11:19135c83c208 | 468 | AA = 15; |
dnonoo | 13:089f3adb3813 | 469 | pc.puts("ERROR - Sample Time out of range (0.1<=T<=60)\n\r"); |
dnonoo | 11:19135c83c208 | 470 | } |
dnonoo | 11:19135c83c208 | 471 | sampleTime = AA; |
dnonoo | 13:089f3adb3813 | 472 | pc.printf("\n\r Sample Time updated to: %2.1f seconds\n\r", sampleTime); |
dnonoo | 11:19135c83c208 | 473 | read.attach(readISR, sampleTime); |
dnonoo | 11:19135c83c208 | 474 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 2:28d12a3db239 | 475 | } |
GeorgeJourneaux | 6:64d346936f0e | 476 | /*----STATE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 477 | else if(strstr(rx_buffer, "STATE")){ |
dnonoo | 11:19135c83c208 | 478 | |
dnonoo | 11:19135c83c208 | 479 | strncpy(tm_n,&rx_buffer[6],3); |
dnonoo | 11:19135c83c208 | 480 | if (strstr(tm_n, "ON")) { |
dnonoo | 11:19135c83c208 | 481 | if (xx == 1) { |
dnonoo | 13:089f3adb3813 | 482 | pc.puts("\n\r Already Sampling\n\r"); |
dnonoo | 11:19135c83c208 | 483 | } |
dnonoo | 11:19135c83c208 | 484 | else { |
dnonoo | 11:19135c83c208 | 485 | read.attach(&readISR, sampleTime); |
dnonoo | 13:089f3adb3813 | 486 | pc.puts("\n\r Sampling ON\n\r"); |
dnonoo | 11:19135c83c208 | 487 | xx = 1; |
dnonoo | 11:19135c83c208 | 488 | } |
dnonoo | 11:19135c83c208 | 489 | } |
dnonoo | 11:19135c83c208 | 490 | else if (strstr(tm_n, "OFF")) { |
dnonoo | 11:19135c83c208 | 491 | if ( xx == 0 ) { |
dnonoo | 13:089f3adb3813 | 492 | pc.puts("\n\r Already not Sampling\n\r"); |
dnonoo | 11:19135c83c208 | 493 | } |
dnonoo | 11:19135c83c208 | 494 | else { |
dnonoo | 11:19135c83c208 | 495 | read.detach(); |
dnonoo | 13:089f3adb3813 | 496 | pc.puts("\n\r Sampling OFF\n\r"); |
dnonoo | 11:19135c83c208 | 497 | xx = 0; |
dnonoo | 11:19135c83c208 | 498 | } |
dnonoo | 11:19135c83c208 | 499 | } |
dnonoo | 11:19135c83c208 | 500 | else { |
dnonoo | 11:19135c83c208 | 501 | pc.puts("Error - STATE can only be ON or OFF\n\r"); |
dnonoo | 11:19135c83c208 | 502 | } |
dnonoo | 11:19135c83c208 | 503 | //pc.puts(" STATE\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 504 | } |
GeorgeJourneaux | 6:64d346936f0e | 505 | /*----LOGGING----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 506 | else if(strstr(rx_buffer, "LOGGING")){ |
GeorgeJourneaux | 6:64d346936f0e | 507 | pc.puts(" LOGGING\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 508 | } |
dnonoo | 10:c10d1337d754 | 509 | /*----HELP--------------------------------------*/ |
dnonoo | 10:c10d1337d754 | 510 | else if (strstr(rx_buffer, "HELP")) { |
dnonoo | 13:089f3adb3813 | 511 | pc.puts("\n\n\r Currently Available Commands:\n\r"); |
dnonoo | 10:c10d1337d754 | 512 | pc.puts("\tREAD n - Read n previous samples\n\r"); |
dnonoo | 10:c10d1337d754 | 513 | pc.puts("\tREAD ALL - Read All previous samples held in memory\n\r"); |
dnonoo | 10:c10d1337d754 | 514 | pc.puts("\tSETTIME hh:mm::ss - Set time in 24hr format\n\r"); |
dnonoo | 10:c10d1337d754 | 515 | pc.puts("\tSETDATE dd/mm/yyyy - Set time in specified format\n\r"); |
dnonoo | 12:a244f6f9d2fe | 516 | pc.puts("\tDELETE ALL - Delete all sampled held in internal memory\n\r"); |
dnonoo | 11:19135c83c208 | 517 | pc.puts("\tSETT - Set sample period. must be an integer in range 0 < T < 61\n\r"); |
dnonoo | 10:c10d1337d754 | 518 | } |
dnonoo | 11:19135c83c208 | 519 | else if (strstr(rx_buffer,"tell me a joke")) { |
dnonoo | 13:089f3adb3813 | 520 | pc.puts("\n\r Why do programmers always get Halloween and Christmas mixed up...?\n\r"); |
dnonoo | 13:089f3adb3813 | 521 | Thread::wait(5000); |
dnonoo | 13:089f3adb3813 | 522 | pc.puts(" Because Oct 31 == Dec 25! LOL"); |
dnonoo | 11:19135c83c208 | 523 | } |
GeorgeJourneaux | 6:64d346936f0e | 524 | /*----ERROR---*/ |
GeorgeJourneaux | 2:28d12a3db239 | 525 | else{ |
dnonoo | 11:19135c83c208 | 526 | pc.puts("Error - Command not recognised. Type HELP for a list of available commands\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 527 | } |
GeorgeJourneaux | 6:64d346936f0e | 528 | /*----------------------------------------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 529 | |
GeorgeJourneaux | 6:64d346936f0e | 530 | //Clear serial buffers |
GeorgeJourneaux | 5:ea3ec65cbf5f | 531 | memset(serial_buffer, NULL, 80); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 532 | memset(rx_buffer, NULL, 32); |
GeorgeJourneaux | 2:28d12a3db239 | 533 | rx_in = 0; |
GeorgeJourneaux | 2:28d12a3db239 | 534 | |
GeorgeJourneaux | 6:64d346936f0e | 535 | //Attach serial interrupt |
GeorgeJourneaux | 2:28d12a3db239 | 536 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
GeorgeJourneaux | 2:28d12a3db239 | 537 | } |
GeorgeJourneaux | 2:28d12a3db239 | 538 | } |
dnonoo | 7:f017a37bcf1b | 539 | /*------------------------------------------------*/ |
dnonoo | 10:c10d1337d754 | 540 | /*---------------SD THread------------------------*/ |
dnonoo | 13:089f3adb3813 | 541 | void writeRemove_SD() |
dnonoo | 13:089f3adb3813 | 542 | { |
dnonoo | 13:089f3adb3813 | 543 | |
dnonoo | 10:c10d1337d754 | 544 | while(1) { |
dnonoo | 10:c10d1337d754 | 545 | Thread::signal_wait(USER_BUTTON_PRESSED); //wait for debounce signal |
dnonoo | 10:c10d1337d754 | 546 | int sd_state = sdIn; |
dnonoo | 13:089f3adb3813 | 547 | |
dnonoo | 10:c10d1337d754 | 548 | switch (sd_state) { |
dnonoo | 10:c10d1337d754 | 549 | case 1: |
dnonoo | 10:c10d1337d754 | 550 | pc.printf("SD Card not inserted!\n\r"); |
dnonoo | 10:c10d1337d754 | 551 | pc.printf("Insert SD Card and press User button again\n\r"); |
dnonoo | 10:c10d1337d754 | 552 | break; |
dnonoo | 13:089f3adb3813 | 553 | default: |
dnonoo | 10:c10d1337d754 | 554 | pc.printf("This should never happen\n\r"); |
dnonoo | 10:c10d1337d754 | 555 | break; |
dnonoo | 10:c10d1337d754 | 556 | case 0: |
dnonoo | 10:c10d1337d754 | 557 | pc.printf("Initalising SD Card\n\r"); |
dnonoo | 7:f017a37bcf1b | 558 | |
dnonoo | 10:c10d1337d754 | 559 | //check init |
dnonoo | 10:c10d1337d754 | 560 | if (sd.init() != 0) { |
dnonoo | 13:089f3adb3813 | 561 | pc.printf(" ERROR - SD card failed to initialise.\n\rRestart board\n\r PROBABLY wires come out\n\r"); |
dnonoo | 13:089f3adb3813 | 562 | } |
dnonoo | 13:089f3adb3813 | 563 | else { |
dnonoo | 13:089f3adb3813 | 564 | // Create Filing system for SD Card |
dnonoo | 13:089f3adb3813 | 565 | FATFileSystem fs("sd", &sd); |
dnonoo | 10:c10d1337d754 | 566 | |
dnonoo | 13:089f3adb3813 | 567 | //OpenFiles to write/append to |
dnonoo | 13:089f3adb3813 | 568 | pc.printf("Writing to SDC\n\r"); |
dnonoo | 10:c10d1337d754 | 569 | |
dnonoo | 13:089f3adb3813 | 570 | FILE* fp = fopen("/sd/TheChamberOfSecrets.txt", "a"); //"w" to overwrite file ftb |
dnonoo | 10:c10d1337d754 | 571 | |
dnonoo | 13:089f3adb3813 | 572 | // Check for error in opening file |
dnonoo | 13:089f3adb3813 | 573 | if (fp == NULL) { |
dnonoo | 13:089f3adb3813 | 574 | pc.printf("*****ERROR - Could not open file for write*****\n\r"); |
dnonoo | 13:089f3adb3813 | 575 | } |
dnonoo | 13:089f3adb3813 | 576 | //HERE IS WHERE TO PRINT DATA TO SD CARD FROM BUFFER (REMEMBER TO EMPTY BUFFER???) |
dnonoo | 13:089f3adb3813 | 577 | //Lock data buffer |
dnonoo | 13:089f3adb3813 | 578 | DataBuffer.lock(); |
dnonoo | 13:089f3adb3813 | 579 | dataLock.lock(); |
dnonoo | 13:089f3adb3813 | 580 | //Print all samples to SD |
dnonoo | 13:089f3adb3813 | 581 | for(int n=data_t; n<=MAX_SAMPLES; n++) { |
dnonoo | 10:c10d1337d754 | 582 | fputs(data_buffer[n], fp); |
dnonoo | 13:089f3adb3813 | 583 | fprintf(fp, "\n\r"); |
dnonoo | 13:089f3adb3813 | 584 | } |
dnonoo | 13:089f3adb3813 | 585 | if(data_t>data_h) { |
dnonoo | 13:089f3adb3813 | 586 | for(int n=0; n<=(data_t-1); n++) { |
dnonoo | 13:089f3adb3813 | 587 | fputs(data_buffer[n], fp); |
dnonoo | 10:c10d1337d754 | 588 | |
dnonoo | 13:089f3adb3813 | 589 | } |
dnonoo | 10:c10d1337d754 | 590 | } |
dnonoo | 10:c10d1337d754 | 591 | |
dnonoo | 13:089f3adb3813 | 592 | //Lock data buffer |
dnonoo | 13:089f3adb3813 | 593 | DataBuffer.unlock(); |
dnonoo | 13:089f3adb3813 | 594 | dataLock.unlock(); |
dnonoo | 13:089f3adb3813 | 595 | //fprintf(fp, "dd/mm/yy hh:mm:ss, TEMPERATURE, PRESSURE, LIGHT\n\r"); |
dnonoo | 10:c10d1337d754 | 596 | |
dnonoo | 13:089f3adb3813 | 597 | fclose(fp); |
dnonoo | 10:c10d1337d754 | 598 | |
dnonoo | 13:089f3adb3813 | 599 | pc.printf("Write Sucessful!\n\r"); |
dnonoo | 10:c10d1337d754 | 600 | |
dnonoo | 13:089f3adb3813 | 601 | sd.deinit(); |
dnonoo | 13:089f3adb3813 | 602 | pc.printf("SD Card Ready to Remove\n\r"); |
dnonoo | 13:089f3adb3813 | 603 | Green_ext = 1; |
dnonoo | 13:089f3adb3813 | 604 | Thread::wait(500); |
dnonoo | 13:089f3adb3813 | 605 | Green_ext = 0; |
dnonoo | 13:089f3adb3813 | 606 | Thread::wait(500); |
dnonoo | 13:089f3adb3813 | 607 | Green_ext = 1; |
dnonoo | 13:089f3adb3813 | 608 | Thread::wait(500); |
dnonoo | 13:089f3adb3813 | 609 | Green_ext = 0; |
dnonoo | 13:089f3adb3813 | 610 | Thread::wait(500); |
dnonoo | 13:089f3adb3813 | 611 | Green_ext = 1; |
dnonoo | 13:089f3adb3813 | 612 | Thread::wait(500); |
dnonoo | 13:089f3adb3813 | 613 | Green_ext = 0; |
dnonoo | 13:089f3adb3813 | 614 | Thread::wait(500); |
dnonoo | 13:089f3adb3813 | 615 | Green_ext = 1; |
dnonoo | 13:089f3adb3813 | 616 | Thread::wait(500); |
dnonoo | 13:089f3adb3813 | 617 | Green_ext = 0; |
dnonoo | 13:089f3adb3813 | 618 | Thread::wait(500); |
dnonoo | 13:089f3adb3813 | 619 | }// end if |
dnonoo | 10:c10d1337d754 | 620 | }//End Switch |
dnonoo | 10:c10d1337d754 | 621 | }// End While |
dnonoo | 10:c10d1337d754 | 622 | }// End Thread |
dnonoo | 10:c10d1337d754 | 623 | /*--------------------------------------------------------------------------*/ |
dnonoo | 10:c10d1337d754 | 624 | |
dnonoo | 10:c10d1337d754 | 625 | /*---------------------------Networking Thread------------------------------*/ |
dnonoo | 12:a244f6f9d2fe | 626 | void Network1 () |
dnonoo | 12:a244f6f9d2fe | 627 | { |
dnonoo | 12:a244f6f9d2fe | 628 | |
dnonoo | 10:c10d1337d754 | 629 | printf("Setting up server\n\r"); |
dnonoo | 12:a244f6f9d2fe | 630 | |
dnonoo | 10:c10d1337d754 | 631 | //Configure an ethernet connection |
dnonoo | 10:c10d1337d754 | 632 | EthernetInterface eth; |
dnonoo | 10:c10d1337d754 | 633 | eth.set_network(IP, NETMASK, GATEWAY); |
dnonoo | 10:c10d1337d754 | 634 | eth.connect(); |
dnonoo | 11:19135c83c208 | 635 | if (eth.get_ip_address() == NULL) { |
dnonoo | 11:19135c83c208 | 636 | pc.printf("Error - Can't get IP. Network not setup\n\r"); |
dnonoo | 11:19135c83c208 | 637 | pc.printf("Reset Required. Make sure network cables are plugged in\n\r"); |
dnonoo | 12:a244f6f9d2fe | 638 | } |
dnonoo | 12:a244f6f9d2fe | 639 | else { |
dnonoo | 12:a244f6f9d2fe | 640 | printf("The target IP address is '%s'\n\r", eth.get_ip_address()); |
dnonoo | 12:a244f6f9d2fe | 641 | |
dnonoo | 12:a244f6f9d2fe | 642 | //Now setup a web server |
dnonoo | 12:a244f6f9d2fe | 643 | TCPServer srv; //TCP/IP Server |
dnonoo | 12:a244f6f9d2fe | 644 | |
dnonoo | 12:a244f6f9d2fe | 645 | SocketAddress clt_addr; //Address of incoming connection |
dnonoo | 12:a244f6f9d2fe | 646 | |
dnonoo | 12:a244f6f9d2fe | 647 | /* Open the server on ethernet stack */ |
dnonoo | 12:a244f6f9d2fe | 648 | srv.open(ð); |
dnonoo | 12:a244f6f9d2fe | 649 | |
dnonoo | 12:a244f6f9d2fe | 650 | /* Bind the HTTP port (TCP 80) to the server */ |
dnonoo | 12:a244f6f9d2fe | 651 | srv.bind(eth.get_ip_address(), 80); |
dnonoo | 12:a244f6f9d2fe | 652 | |
dnonoo | 12:a244f6f9d2fe | 653 | /* Can handle 5 simultaneous connections */ |
dnonoo | 12:a244f6f9d2fe | 654 | srv.listen(5); |
dnonoo | 13:089f3adb3813 | 655 | |
dnonoo | 12:a244f6f9d2fe | 656 | TCPSocket clt_sock; //Socket for communication |
dnonoo | 13:089f3adb3813 | 657 | |
dnonoo | 12:a244f6f9d2fe | 658 | pc.printf("Server Ready\n\r"); |
dnonoo | 11:19135c83c208 | 659 | |
dnonoo | 12:a244f6f9d2fe | 660 | while (true) { |
dnonoo | 12:a244f6f9d2fe | 661 | |
dnonoo | 12:a244f6f9d2fe | 662 | using namespace std; |
dnonoo | 12:a244f6f9d2fe | 663 | //Block and wait on an incoming connection |
dnonoo | 12:a244f6f9d2fe | 664 | srv.accept(&clt_sock, &clt_addr); |
dnonoo | 12:a244f6f9d2fe | 665 | //printf("accept %s:%d\n\r", clt_addr.get_ip_address(), clt_addr.get_port()); |
dnonoo | 12:a244f6f9d2fe | 666 | |
dnonoo | 12:a244f6f9d2fe | 667 | //Uses a C++ string to make it easier to concatinate |
dnonoo | 12:a244f6f9d2fe | 668 | string response; |
dnonoo | 12:a244f6f9d2fe | 669 | string strL = "LDR:"; |
dnonoo | 12:a244f6f9d2fe | 670 | string strP = ", Pressure(mBar): "; |
dnonoo | 12:a244f6f9d2fe | 671 | string strT = ", Temp(C): "; |
dnonoo | 13:089f3adb3813 | 672 | string strDT ="Date/Time: ,"; |
dnonoo | 12:a244f6f9d2fe | 673 | |
dnonoo | 12:a244f6f9d2fe | 674 | //This is a C string |
dnonoo | 13:089f3adb3813 | 675 | |
dnonoo | 13:089f3adb3813 | 676 | char X_str[128]; |
dnonoo | 12:a244f6f9d2fe | 677 | |
dnonoo | 13:089f3adb3813 | 678 | /*//Read the LDR value |
dnonoo | 12:a244f6f9d2fe | 679 | dataLock.lock(); // add watchdog? |
dnonoo | 12:a244f6f9d2fe | 680 | float L = LDR ; |
dnonoo | 12:a244f6f9d2fe | 681 | float T = TEMP; |
dnonoo | 12:a244f6f9d2fe | 682 | float P = PRES; |
dnonoo | 13:089f3adb3813 | 683 | char DT[21]; |
dnonoo | 13:089f3adb3813 | 684 | strcpy((char*)TIME,DT); |
dnonoo | 12:a244f6f9d2fe | 685 | dataLock.unlock(); |
dnonoo | 12:a244f6f9d2fe | 686 | |
dnonoo | 12:a244f6f9d2fe | 687 | //Convert to a C String |
dnonoo | 13:089f3adb3813 | 688 | //sprintf(l_str, "%1.3f", L ); // try \n\r?? |
dnonoo | 13:089f3adb3813 | 689 | //sprintf(t_str, "%2.2f", T); |
dnonoo | 13:089f3adb3813 | 690 | //sprintf(p_str, "%4.2f", P);*/ |
dnonoo | 13:089f3adb3813 | 691 | |
dnonoo | 13:089f3adb3813 | 692 | dataLock.lock(); |
dnonoo | 13:089f3adb3813 | 693 | sprintf(X_str,"%s, \n\rTemperature: %2.2f, \n\rPressure: %4.2f, \n\rLight: %.4f", TIME, TEMP, PRES, LDR); |
dnonoo | 13:089f3adb3813 | 694 | dataLock.unlock(); |
dnonoo | 12:a244f6f9d2fe | 695 | |
dnonoo | 12:a244f6f9d2fe | 696 | |
dnonoo | 12:a244f6f9d2fe | 697 | //Build the C++ string response |
dnonoo | 12:a244f6f9d2fe | 698 | response = HTTP_MESSAGE_BODY1; |
dnonoo | 13:089f3adb3813 | 699 | response += X_str; |
dnonoo | 13:089f3adb3813 | 700 | /* response += strL; |
dnonoo | 13:089f3adb3813 | 701 | response += strDT; |
dnonoo | 13:089f3adb3813 | 702 | response += strL; |
dnonoo | 12:a244f6f9d2fe | 703 | response += l_str; |
dnonoo | 12:a244f6f9d2fe | 704 | response += strT; |
dnonoo | 12:a244f6f9d2fe | 705 | response += t_str; |
dnonoo | 12:a244f6f9d2fe | 706 | response += strP; |
dnonoo | 13:089f3adb3813 | 707 | response += p_str;*/ |
dnonoo | 12:a244f6f9d2fe | 708 | response += HTTP_MESSAGE_BODY2; |
dnonoo | 12:a244f6f9d2fe | 709 | |
dnonoo | 12:a244f6f9d2fe | 710 | //Send static HTML response (as a C string) |
dnonoo | 12:a244f6f9d2fe | 711 | clt_sock.send(response.c_str(), response.size()+6); |
dnonoo | 12:a244f6f9d2fe | 712 | |
dnonoo | 12:a244f6f9d2fe | 713 | }// end While |
dnonoo | 12:a244f6f9d2fe | 714 | }//end if |
dnonoo | 12:a244f6f9d2fe | 715 | }// end thread |
dnonoo | 10:c10d1337d754 | 716 | /*---------------------------POST--------------------------------------------*/ |
dnonoo | 7:f017a37bcf1b | 717 | void POST () { |
dnonoo | 11:19135c83c208 | 718 | pc.printf(" Basic POST\n\r"); |
dnonoo | 7:f017a37bcf1b | 719 | pc.printf(" ALL Leds should be flashing\n\r"); |
dnonoo | 7:f017a37bcf1b | 720 | |
dnonoo | 7:f017a37bcf1b | 721 | for(unsigned int n = 0; n<10; n++) { |
dnonoo | 7:f017a37bcf1b | 722 | Green_int = ON; |
dnonoo | 7:f017a37bcf1b | 723 | Blue_int = ON; |
dnonoo | 7:f017a37bcf1b | 724 | Red_int = ON; |
dnonoo | 7:f017a37bcf1b | 725 | Green_ext = ON; |
dnonoo | 7:f017a37bcf1b | 726 | Yellow_ext = ON; |
dnonoo | 7:f017a37bcf1b | 727 | Red_ext = ON; |
dnonoo | 7:f017a37bcf1b | 728 | |
dnonoo | 7:f017a37bcf1b | 729 | wait (0.2); |
dnonoo | 7:f017a37bcf1b | 730 | Green_int = OFF; |
dnonoo | 7:f017a37bcf1b | 731 | Blue_int = OFF; |
dnonoo | 7:f017a37bcf1b | 732 | Red_int = OFF; |
dnonoo | 7:f017a37bcf1b | 733 | Green_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 734 | Yellow_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 735 | Red_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 736 | wait (0.2); |
dnonoo | 7:f017a37bcf1b | 737 | } |
dnonoo | 7:f017a37bcf1b | 738 | |
dnonoo | 7:f017a37bcf1b | 739 | pc.printf("Switch states:\n\r"); |
dnonoo | 7:f017a37bcf1b | 740 | pc.printf("\tSW_L: %d\n\r\tSW_R %d\n\r", SW_L.read(), SW_R.read()); |
dnonoo | 7:f017a37bcf1b | 741 | |
dnonoo | 7:f017a37bcf1b | 742 | float Temp = Sensor.getTemperature(); |
dnonoo | 7:f017a37bcf1b | 743 | float Pres = Sensor.getPressure(); |
dnonoo | 7:f017a37bcf1b | 744 | float ldrs = LDR_In.read(); |
dnonoo | 7:f017a37bcf1b | 745 | |
dnonoo | 7:f017a37bcf1b | 746 | pc.printf("Sensor test:\n\r"); |
dnonoo | 7:f017a37bcf1b | 747 | pc.printf("T: %f\tP: %f\tL: %f\n\r",Temp,Pres,ldrs); |
dnonoo | 7:f017a37bcf1b | 748 | |
dnonoo | 7:f017a37bcf1b | 749 | pc.printf("LCD Test\n\r"); |
dnonoo | 7:f017a37bcf1b | 750 | |
dnonoo | 7:f017a37bcf1b | 751 | lcd.Clear(); |
dnonoo | 7:f017a37bcf1b | 752 | lcd.RowSelect(1); |
dnonoo | 7:f017a37bcf1b | 753 | lcd.Write("*******LCD******"); |
dnonoo | 7:f017a37bcf1b | 754 | lcd.RowSelect(2); |
dnonoo | 7:f017a37bcf1b | 755 | lcd.Write("******TEST******"); |
dnonoo | 7:f017a37bcf1b | 756 | wait(1); |
dnonoo | 10:c10d1337d754 | 757 | lcd.Clear(); |
dnonoo | 11:19135c83c208 | 758 | pc.printf("Basic POST end\n\r"); |
dnonoo | 7:f017a37bcf1b | 759 | } |