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