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