V4
Dependencies: BMP280
Fork of Thread_Communication_V4 by
main.cpp@10:c10d1337d754, 2017-12-30 (annotated)
- Committer:
- dnonoo
- Date:
- Sat Dec 30 18:55:22 2017 +0000
- Revision:
- 10:c10d1337d754
- Parent:
- 9:b838c5787ed7
- Child:
- 11:19135c83c208
Added pin CD on SD Card to PE_10 (digitalIn) to check if SD card is inserted and Help command in Serial Thread;
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; |
benparkes | 1:bca9993a0df3 | 46 | |
dnonoo | 10:c10d1337d754 | 47 | volatile int sampleTime = 15; |
dnonoo | 8:ab6322afa341 | 48 | // int to hold current switch state |
dnonoo | 8:ab6322afa341 | 49 | int userButtonState = FallingEdge; |
dnonoo | 8:ab6322afa341 | 50 | |
dnonoo | 8:ab6322afa341 | 51 | /* DEBOUNCER INTERRUPTS */ |
dnonoo | 8:ab6322afa341 | 52 | InterruptIn userButton(USER_BUTTON); |
dnonoo | 8:ab6322afa341 | 53 | |
dnonoo | 8:ab6322afa341 | 54 | void userButtonRise () { |
dnonoo | 8:ab6322afa341 | 55 | userButton.rise(NULL); |
dnonoo | 8:ab6322afa341 | 56 | userButtonState = RisingEdge; |
dnonoo | 8:ab6322afa341 | 57 | userButtonTimeout.attach(&userButtonTimeoutHandler, 0.1); |
dnonoo | 8:ab6322afa341 | 58 | } |
benparkes | 0:cb3a5c15b01e | 59 | |
dnonoo | 8:ab6322afa341 | 60 | void userButtonFall () { |
dnonoo | 8:ab6322afa341 | 61 | userButton.fall(NULL); |
dnonoo | 8:ab6322afa341 | 62 | _writeRemove_SD.signal_set(USER_BUTTON_PRESSED); |
dnonoo | 8:ab6322afa341 | 63 | userButtonState = FallingEdge; |
dnonoo | 8:ab6322afa341 | 64 | userButtonTimeout.attach(&userButtonTimeoutHandler, 0.1); |
dnonoo | 8:ab6322afa341 | 65 | } |
dnonoo | 7:f017a37bcf1b | 66 | |
dnonoo | 8:ab6322afa341 | 67 | void userButtonTimeoutHandler() { |
dnonoo | 8:ab6322afa341 | 68 | userButtonTimeout.detach(); |
dnonoo | 8:ab6322afa341 | 69 | |
dnonoo | 8:ab6322afa341 | 70 | switch (userButtonState) { |
dnonoo | 8:ab6322afa341 | 71 | case RisingEdge: |
dnonoo | 8:ab6322afa341 | 72 | userButton.fall(&userButtonFall); |
dnonoo | 8:ab6322afa341 | 73 | break; |
dnonoo | 8:ab6322afa341 | 74 | case FallingEdge: |
dnonoo | 8:ab6322afa341 | 75 | userButton.rise(userButtonRise); |
dnonoo | 8:ab6322afa341 | 76 | break; |
dnonoo | 8:ab6322afa341 | 77 | }// End Switch |
dnonoo | 8:ab6322afa341 | 78 | } //End ISR |
GeorgeJourneaux | 3:73497379c0cb | 79 | /*--------------------------------MAIN--------------------------------*/ |
GeorgeJourneaux | 3:73497379c0cb | 80 | int main() { |
GeorgeJourneaux | 3:73497379c0cb | 81 | |
GeorgeJourneaux | 3:73497379c0cb | 82 | pc.baud(9600); |
GeorgeJourneaux | 3:73497379c0cb | 83 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
dnonoo | 7:f017a37bcf1b | 84 | POST(); |
dnonoo | 7:f017a37bcf1b | 85 | |
dnonoo | 10:c10d1337d754 | 86 | pc.printf("\n\n\nType HELP for list of available Commands\n\n\n\r"); |
dnonoo | 7:f017a37bcf1b | 87 | _serialCMD.start(serialCMD); |
dnonoo | 7:f017a37bcf1b | 88 | _PrintLCD.start(PrintLCD); |
dnonoo | 7:f017a37bcf1b | 89 | _sensorRead.start(sensorRead); |
dnonoo | 7:f017a37bcf1b | 90 | _circBuff.start(circBuff); |
dnonoo | 8:ab6322afa341 | 91 | _writeRemove_SD.start(writeRemove_SD); |
dnonoo | 9:b838c5787ed7 | 92 | _Network1.start(Network1); |
dnonoo | 7:f017a37bcf1b | 93 | |
dnonoo | 8:ab6322afa341 | 94 | userButton.rise(&userButtonRise); |
dnonoo | 10:c10d1337d754 | 95 | read.attach(&readISR, sampleTime); |
dnonoo | 7:f017a37bcf1b | 96 | |
dnonoo | 8:ab6322afa341 | 97 | while (1) { |
dnonoo | 8:ab6322afa341 | 98 | Yellow_ext = ON; |
dnonoo | 8:ab6322afa341 | 99 | Thread::wait (200); |
dnonoo | 8:ab6322afa341 | 100 | Yellow_ext = OFF; |
dnonoo | 8:ab6322afa341 | 101 | Thread::wait(200); |
dnonoo | 8:ab6322afa341 | 102 | }// End While |
dnonoo | 8:ab6322afa341 | 103 | } // End Main |
dnonoo | 7:f017a37bcf1b | 104 | /*--------------------------------------------------------------------*/ |
dnonoo | 10:c10d1337d754 | 105 | /*-----------------Circular Buffer------------------------------------*/ |
dnonoo | 7:f017a37bcf1b | 106 | void circBuff () { |
dnonoo | 7:f017a37bcf1b | 107 | |
dnonoo | 7:f017a37bcf1b | 108 | while(1) { |
dnonoo | 7:f017a37bcf1b | 109 | Thread::signal_wait(DATA_READY); // wait for signal from sensorRead |
GeorgeJourneaux | 5:ea3ec65cbf5f | 110 | |
dnonoo | 7:f017a37bcf1b | 111 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 112 | DataBuffer.lock(); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 113 | |
dnonoo | 7:f017a37bcf1b | 114 | //Format samples, send to FIFO buffer head |
GeorgeJourneaux | 6:64d346936f0e | 115 | memset(data_buffer[sample_h],NULL,64); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 116 | time( &raw_time ); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 117 | sample_epoch = localtime( &raw_time ); |
GeorgeJourneaux | 6:64d346936f0e | 118 | char sample_time[20]; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 119 | strftime(sample_time,20,"%d/%m/%Y %X",sample_epoch); |
dnonoo | 7:f017a37bcf1b | 120 | |
dnonoo | 8:ab6322afa341 | 121 | dataLock.lock(); //lock critical section |
dnonoo | 7:f017a37bcf1b | 122 | sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, TEMP, PRES, LDR); |
dnonoo | 8:ab6322afa341 | 123 | dataLock.unlock(); //unlock critical section |
dnonoo | 7:f017a37bcf1b | 124 | |
GeorgeJourneaux | 5:ea3ec65cbf5f | 125 | memset(sample_time,NULL,20); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 126 | |
dnonoo | 7:f017a37bcf1b | 127 | //Set seperate FIFO head and tail for printing data |
GeorgeJourneaux | 6:64d346936f0e | 128 | data_h = sample_h; |
GeorgeJourneaux | 6:64d346936f0e | 129 | data_t = sample_t; |
GeorgeJourneaux | 6:64d346936f0e | 130 | |
dnonoo | 7:f017a37bcf1b | 131 | //Move sample FIFO buffer head to next row in buffer |
GeorgeJourneaux | 6:64d346936f0e | 132 | sample_h++; |
dnonoo | 7:f017a37bcf1b | 133 | //Check sample FIFO buffer head |
GeorgeJourneaux | 6:64d346936f0e | 134 | if(sample_h >= MAX_SAMPLES){ |
GeorgeJourneaux | 6:64d346936f0e | 135 | sample_h = 0; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 136 | } |
dnonoo | 7:f017a37bcf1b | 137 | //Check sample FIFO buffer tail |
GeorgeJourneaux | 6:64d346936f0e | 138 | if(sample_t == sample_h){ |
GeorgeJourneaux | 6:64d346936f0e | 139 | sample_t++; |
GeorgeJourneaux | 6:64d346936f0e | 140 | if(sample_t >= (MAX_SAMPLES)){ |
GeorgeJourneaux | 6:64d346936f0e | 141 | sample_t = 0; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 142 | } |
GeorgeJourneaux | 5:ea3ec65cbf5f | 143 | } |
dnonoo | 7:f017a37bcf1b | 144 | //Unlock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 145 | DataBuffer.unlock(); |
dnonoo | 10:c10d1337d754 | 146 | }// End While |
dnonoo | 10:c10d1337d754 | 147 | }// End Circular buffer |
dnonoo | 10:c10d1337d754 | 148 | /*-------------------------------------------------------------*/ |
dnonoo | 7:f017a37bcf1b | 149 | /*---------------------Read Sensors ---------------------------*/ |
dnonoo | 7:f017a37bcf1b | 150 | |
dnonoo | 7:f017a37bcf1b | 151 | void readISR () { // Ticker interrupt defined in main |
dnonoo | 7:f017a37bcf1b | 152 | |
dnonoo | 7:f017a37bcf1b | 153 | _sensorRead.signal_set(SENSOR_UPDATE); |
dnonoo | 7:f017a37bcf1b | 154 | } |
dnonoo | 8:ab6322afa341 | 155 | // Keep short |
dnonoo | 7:f017a37bcf1b | 156 | void sensorRead () { |
dnonoo | 7:f017a37bcf1b | 157 | |
dnonoo | 7:f017a37bcf1b | 158 | while (1) { |
dnonoo | 7:f017a37bcf1b | 159 | |
dnonoo | 8:ab6322afa341 | 160 | dataLock.lock(); // Entering Critial Section |
dnonoo | 7:f017a37bcf1b | 161 | |
dnonoo | 7:f017a37bcf1b | 162 | // Store Data in global Variables |
dnonoo | 7:f017a37bcf1b | 163 | LDR = LDR_In.read(); |
dnonoo | 7:f017a37bcf1b | 164 | TEMP = Sensor.getTemperature(); |
dnonoo | 7:f017a37bcf1b | 165 | PRES = Sensor.getPressure(); |
dnonoo | 7:f017a37bcf1b | 166 | |
dnonoo | 8:ab6322afa341 | 167 | dataLock.unlock(); // Exiting Critical Section |
dnonoo | 7:f017a37bcf1b | 168 | |
dnonoo | 7:f017a37bcf1b | 169 | Green_int = !Green_int; // debugging |
GeorgeJourneaux | 5:ea3ec65cbf5f | 170 | |
dnonoo | 7:f017a37bcf1b | 171 | //Read sensors, send to mail-queue |
dnonoo | 7:f017a37bcf1b | 172 | mail_t *mail = mail_box.alloc(); |
dnonoo | 7:f017a37bcf1b | 173 | mail->LDR_Value = LDR; |
dnonoo | 7:f017a37bcf1b | 174 | mail->temp_Value = TEMP; |
dnonoo | 7:f017a37bcf1b | 175 | mail->press_Value = PRES; |
dnonoo | 7:f017a37bcf1b | 176 | mail_box.put(mail); |
dnonoo | 7:f017a37bcf1b | 177 | |
dnonoo | 7:f017a37bcf1b | 178 | _circBuff.signal_set(DATA_READY); // Set signal to buffer to store updated values |
dnonoo | 7:f017a37bcf1b | 179 | Thread::signal_wait(SENSOR_UPDATE); // Wait for the Timer interrupt |
dnonoo | 7:f017a37bcf1b | 180 | } |
GeorgeJourneaux | 3:73497379c0cb | 181 | } |
dnonoo | 10:c10d1337d754 | 182 | /*--------------------------------------------------------------------*/ |
GeorgeJourneaux | 3:73497379c0cb | 183 | |
GeorgeJourneaux | 3:73497379c0cb | 184 | /*--------------------------------LCD---------------------------------*/ |
benparkes | 0:cb3a5c15b01e | 185 | void PrintLCD () { |
benparkes | 0:cb3a5c15b01e | 186 | |
benparkes | 0:cb3a5c15b01e | 187 | int i = 0; |
benparkes | 0:cb3a5c15b01e | 188 | while(1){ |
benparkes | 1:bca9993a0df3 | 189 | char lightString[16]; |
benparkes | 1:bca9993a0df3 | 190 | char tempString[16]; |
benparkes | 1:bca9993a0df3 | 191 | char pressString[16]; |
benparkes | 1:bca9993a0df3 | 192 | |
benparkes | 0:cb3a5c15b01e | 193 | lcd.Clear(); |
benparkes | 0:cb3a5c15b01e | 194 | lcd.RowSelect(0); |
benparkes | 1:bca9993a0df3 | 195 | |
benparkes | 0:cb3a5c15b01e | 196 | switch (i){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 197 | case 0:{ |
GeorgeJourneaux | 2:28d12a3db239 | 198 | osEvent evt = mail_box.get(); |
benparkes | 1:bca9993a0df3 | 199 | |
GeorgeJourneaux | 2:28d12a3db239 | 200 | if (evt.status == osEventMail) { |
GeorgeJourneaux | 2:28d12a3db239 | 201 | mail_t *mail = (mail_t*)evt.value.p; |
benparkes | 0:cb3a5c15b01e | 202 | |
GeorgeJourneaux | 2:28d12a3db239 | 203 | sprintf(lightString,"%.4f", mail->LDR_Value); |
GeorgeJourneaux | 2:28d12a3db239 | 204 | sprintf(tempString,"%2.2f", mail->temp_Value); |
GeorgeJourneaux | 2:28d12a3db239 | 205 | sprintf(pressString,"%4.2f", mail->press_Value); |
benparkes | 1:bca9993a0df3 | 206 | |
GeorgeJourneaux | 2:28d12a3db239 | 207 | mail_box.free(mail); |
GeorgeJourneaux | 2:28d12a3db239 | 208 | } |
benparkes | 1:bca9993a0df3 | 209 | |
dnonoo | 10:c10d1337d754 | 210 | lcd.Write("Light:"); |
GeorgeJourneaux | 2:28d12a3db239 | 211 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 212 | lcd.Write(lightString); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 213 | i++; |
benparkes | 0:cb3a5c15b01e | 214 | break; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 215 | } |
GeorgeJourneaux | 2:28d12a3db239 | 216 | case 1: |
dnonoo | 10:c10d1337d754 | 217 | lcd.Write("Temperature(C):"); |
GeorgeJourneaux | 2:28d12a3db239 | 218 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 219 | lcd.Write(tempString); |
GeorgeJourneaux | 2:28d12a3db239 | 220 | i++; |
benparkes | 0:cb3a5c15b01e | 221 | break; |
benparkes | 0:cb3a5c15b01e | 222 | |
GeorgeJourneaux | 2:28d12a3db239 | 223 | case 2: |
dnonoo | 10:c10d1337d754 | 224 | lcd.Write("Pressure(mBar):"); |
GeorgeJourneaux | 2:28d12a3db239 | 225 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 226 | lcd.Write(pressString); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 227 | i =0; |
benparkes | 0:cb3a5c15b01e | 228 | break; |
GeorgeJourneaux | 2:28d12a3db239 | 229 | |
GeorgeJourneaux | 5:ea3ec65cbf5f | 230 | default: |
GeorgeJourneaux | 5:ea3ec65cbf5f | 231 | i = 0; |
benparkes | 0:cb3a5c15b01e | 232 | break; |
benparkes | 0:cb3a5c15b01e | 233 | } |
benparkes | 0:cb3a5c15b01e | 234 | |
GeorgeJourneaux | 2:28d12a3db239 | 235 | Red_int = !Red_int; |
benparkes | 1:bca9993a0df3 | 236 | |
benparkes | 0:cb3a5c15b01e | 237 | Thread::wait (5000); |
GeorgeJourneaux | 2:28d12a3db239 | 238 | } |
benparkes | 0:cb3a5c15b01e | 239 | } |
GeorgeJourneaux | 3:73497379c0cb | 240 | /*--------------------------------------------------------------------*/ |
benparkes | 1:bca9993a0df3 | 241 | |
GeorgeJourneaux | 3:73497379c0cb | 242 | /*------------------------------SERIAL_CMD----------------------------*/ |
GeorgeJourneaux | 4:93d6d13d4de3 | 243 | //Interrupt when recieving from serial port |
GeorgeJourneaux | 2:28d12a3db239 | 244 | void Rx_interrupt() { |
GeorgeJourneaux | 2:28d12a3db239 | 245 | |
dnonoo | 7:f017a37bcf1b | 246 | //Wait for serial input |
GeorgeJourneaux | 2:28d12a3db239 | 247 | while (pc.readable()) { |
GeorgeJourneaux | 6:64d346936f0e | 248 | |
dnonoo | 7:f017a37bcf1b | 249 | //Return input to serial |
GeorgeJourneaux | 5:ea3ec65cbf5f | 250 | rx_buffer[rx_in] = pc.getc(); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 251 | pc.putc(rx_buffer[rx_in]); |
GeorgeJourneaux | 6:64d346936f0e | 252 | |
dnonoo | 7:f017a37bcf1b | 253 | //If enter key is pressed, set serial thread signal |
GeorgeJourneaux | 5:ea3ec65cbf5f | 254 | if(rx_buffer[rx_in] == 0xD){ |
dnonoo | 7:f017a37bcf1b | 255 | _serialCMD.signal_set(ENTER_KEY); |
GeorgeJourneaux | 2:28d12a3db239 | 256 | } |
GeorgeJourneaux | 6:64d346936f0e | 257 | |
dnonoo | 7:f017a37bcf1b | 258 | //Increment buffer head |
GeorgeJourneaux | 2:28d12a3db239 | 259 | else{ |
GeorgeJourneaux | 2:28d12a3db239 | 260 | rx_in = (rx_in + 1); |
GeorgeJourneaux | 2:28d12a3db239 | 261 | } |
GeorgeJourneaux | 2:28d12a3db239 | 262 | } |
GeorgeJourneaux | 2:28d12a3db239 | 263 | } |
benparkes | 0:cb3a5c15b01e | 264 | |
GeorgeJourneaux | 4:93d6d13d4de3 | 265 | //Check what command what recieved and execute |
dnonoo | 7:f017a37bcf1b | 266 | void serialCMD(){ |
GeorgeJourneaux | 2:28d12a3db239 | 267 | |
GeorgeJourneaux | 2:28d12a3db239 | 268 | while(1){ |
dnonoo | 7:f017a37bcf1b | 269 | //Wait for thread signal |
GeorgeJourneaux | 4:93d6d13d4de3 | 270 | Thread::signal_wait(ENTER_KEY); |
GeorgeJourneaux | 6:64d346936f0e | 271 | |
dnonoo | 7:f017a37bcf1b | 272 | //Detach serial interrupt |
GeorgeJourneaux | 2:28d12a3db239 | 273 | pc.attach(NULL, Serial::RxIrq); |
GeorgeJourneaux | 6:64d346936f0e | 274 | |
GeorgeJourneaux | 3:73497379c0cb | 275 | struct tm * s_time; |
GeorgeJourneaux | 3:73497379c0cb | 276 | char tm_n[4]; |
GeorgeJourneaux | 3:73497379c0cb | 277 | |
GeorgeJourneaux | 6:64d346936f0e | 278 | /*----CARRAGE RETURN-------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 279 | if(rx_buffer[0] == 0xD){ |
GeorgeJourneaux | 6:64d346936f0e | 280 | pc.puts("\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 281 | } |
GeorgeJourneaux | 6:64d346936f0e | 282 | /*----READ ALL----------------------------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 283 | else if(strstr(rx_buffer, "READ ALL")){ |
GeorgeJourneaux | 6:64d346936f0e | 284 | pc.puts(" READ ALL\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 285 | |
dnonoo | 7:f017a37bcf1b | 286 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 287 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 288 | |
dnonoo | 7:f017a37bcf1b | 289 | //Print all samples to serial |
GeorgeJourneaux | 6:64d346936f0e | 290 | for(int n=data_t; n<=MAX_SAMPLES; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 291 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 292 | } |
GeorgeJourneaux | 6:64d346936f0e | 293 | if(data_t>data_h){ |
GeorgeJourneaux | 6:64d346936f0e | 294 | for(int n=0; n<=(data_t-1); n++){ |
GeorgeJourneaux | 6:64d346936f0e | 295 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 296 | } |
GeorgeJourneaux | 6:64d346936f0e | 297 | } |
GeorgeJourneaux | 5:ea3ec65cbf5f | 298 | |
dnonoo | 7:f017a37bcf1b | 299 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 300 | DataBuffer.unlock(); |
GeorgeJourneaux | 2:28d12a3db239 | 301 | } |
GeorgeJourneaux | 6:64d346936f0e | 302 | /*----DELETE ALL----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 303 | else if(strstr(rx_buffer, "DELETE ALL")){ |
GeorgeJourneaux | 6:64d346936f0e | 304 | pc.puts(" DELETE ALL\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 305 | |
GeorgeJourneaux | 6:64d346936f0e | 306 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 307 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 308 | |
GeorgeJourneaux | 6:64d346936f0e | 309 | //Delete all sampled data |
GeorgeJourneaux | 6:64d346936f0e | 310 | for(int n=0; n<=MAX_SAMPLES; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 311 | memset(data_buffer[n], NULL, 64); |
GeorgeJourneaux | 6:64d346936f0e | 312 | } |
GeorgeJourneaux | 6:64d346936f0e | 313 | data_h = data_t; |
GeorgeJourneaux | 6:64d346936f0e | 314 | sample_h = sample_t; |
GeorgeJourneaux | 6:64d346936f0e | 315 | |
GeorgeJourneaux | 6:64d346936f0e | 316 | //Unlock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 317 | DataBuffer.unlock(); |
GeorgeJourneaux | 2:28d12a3db239 | 318 | } |
GeorgeJourneaux | 6:64d346936f0e | 319 | /*----READ----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 320 | else if(strstr(rx_buffer, "READ")){ |
GeorgeJourneaux | 6:64d346936f0e | 321 | pc.puts(" READ \n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 322 | int N = atoi(strncpy(tm_n,&rx_buffer[5],4)); |
GeorgeJourneaux | 6:64d346936f0e | 323 | int S = 0; |
GeorgeJourneaux | 6:64d346936f0e | 324 | pc.printf("N = %d\n\r",N); |
GeorgeJourneaux | 6:64d346936f0e | 325 | |
GeorgeJourneaux | 6:64d346936f0e | 326 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 327 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 328 | |
GeorgeJourneaux | 6:64d346936f0e | 329 | //Check if N is greater than buffer size |
GeorgeJourneaux | 6:64d346936f0e | 330 | if(N >= MAX_SAMPLES){ |
GeorgeJourneaux | 6:64d346936f0e | 331 | N = MAX_SAMPLES; |
GeorgeJourneaux | 6:64d346936f0e | 332 | } |
GeorgeJourneaux | 6:64d346936f0e | 333 | |
GeorgeJourneaux | 6:64d346936f0e | 334 | //Read N samples from FIFO buffer |
GeorgeJourneaux | 6:64d346936f0e | 335 | if(N <= 0){ |
GeorgeJourneaux | 6:64d346936f0e | 336 | pc.puts("####ERROR####\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 337 | } |
GeorgeJourneaux | 6:64d346936f0e | 338 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 339 | for(int n=data_t; n<=MAX_SAMPLES-1; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 340 | if(S>=N){} |
GeorgeJourneaux | 6:64d346936f0e | 341 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 342 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 343 | S++; |
GeorgeJourneaux | 6:64d346936f0e | 344 | } |
GeorgeJourneaux | 6:64d346936f0e | 345 | } |
GeorgeJourneaux | 6:64d346936f0e | 346 | for(int n=0; n<=data_t; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 347 | if(S>=N){} |
GeorgeJourneaux | 6:64d346936f0e | 348 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 349 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 350 | S++; |
GeorgeJourneaux | 6:64d346936f0e | 351 | } |
GeorgeJourneaux | 6:64d346936f0e | 352 | } |
GeorgeJourneaux | 6:64d346936f0e | 353 | } |
GeorgeJourneaux | 6:64d346936f0e | 354 | |
GeorgeJourneaux | 6:64d346936f0e | 355 | //Unlock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 356 | DataBuffer.unlock(); |
GeorgeJourneaux | 2:28d12a3db239 | 357 | } |
GeorgeJourneaux | 6:64d346936f0e | 358 | /*----DELETE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 359 | else if(strstr(rx_buffer, "DELETE")){ |
GeorgeJourneaux | 6:64d346936f0e | 360 | pc.puts(" DELETE \n\r"); |
GeorgeJourneaux | 3:73497379c0cb | 361 | } |
GeorgeJourneaux | 6:64d346936f0e | 362 | /*----SETDATE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 363 | else if(strstr(rx_buffer, "SETDATE")){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 364 | time(&raw_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 365 | s_time = localtime(&raw_time); |
GeorgeJourneaux | 3:73497379c0cb | 366 | |
GeorgeJourneaux | 6:64d346936f0e | 367 | //Update day in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 368 | int dd = atoi(strncpy(tm_n,&rx_buffer[8],2)); |
GeorgeJourneaux | 3:73497379c0cb | 369 | s_time->tm_mday = dd; |
GeorgeJourneaux | 3:73497379c0cb | 370 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 371 | |
GeorgeJourneaux | 6:64d346936f0e | 372 | //Update month in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 373 | int mm = atoi(strncpy(tm_n,&rx_buffer[11],2)); |
GeorgeJourneaux | 3:73497379c0cb | 374 | s_time->tm_mon = mm-1; |
GeorgeJourneaux | 3:73497379c0cb | 375 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 376 | |
GeorgeJourneaux | 6:64d346936f0e | 377 | //Update year in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 378 | int yyyy = atoi(strncpy(tm_n,&rx_buffer[14],4)); |
GeorgeJourneaux | 3:73497379c0cb | 379 | s_time->tm_year = yyyy-1900; |
GeorgeJourneaux | 3:73497379c0cb | 380 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 381 | |
GeorgeJourneaux | 6:64d346936f0e | 382 | //Set date from updated time structure |
GeorgeJourneaux | 3:73497379c0cb | 383 | set_time(mktime(s_time)); |
GeorgeJourneaux | 6:64d346936f0e | 384 | strftime(serial_buffer, 80, "\n\r Set Date: %d/%m/%Y\n\r", s_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 385 | pc.puts(serial_buffer); |
GeorgeJourneaux | 2:28d12a3db239 | 386 | } |
GeorgeJourneaux | 6:64d346936f0e | 387 | /*----SETTIME---------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 388 | else if(strstr(rx_buffer, "SETTIME")){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 389 | time(&raw_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 390 | s_time = localtime(&raw_time); |
GeorgeJourneaux | 3:73497379c0cb | 391 | |
GeorgeJourneaux | 6:64d346936f0e | 392 | //Update seconds in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 393 | int ss = atoi(strncpy(tm_n,&rx_buffer[14],2)); |
GeorgeJourneaux | 3:73497379c0cb | 394 | s_time->tm_sec = ss; |
GeorgeJourneaux | 3:73497379c0cb | 395 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 6:64d346936f0e | 396 | |
GeorgeJourneaux | 6:64d346936f0e | 397 | //Update minutes in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 398 | int mm = atoi(strncpy(tm_n,&rx_buffer[11],2)); |
GeorgeJourneaux | 3:73497379c0cb | 399 | s_time->tm_min = mm; |
GeorgeJourneaux | 3:73497379c0cb | 400 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 401 | |
GeorgeJourneaux | 6:64d346936f0e | 402 | //Update hour in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 403 | int hh = atoi(strncpy(tm_n,&rx_buffer[8],2)); |
GeorgeJourneaux | 3:73497379c0cb | 404 | s_time->tm_hour = hh; |
GeorgeJourneaux | 3:73497379c0cb | 405 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 6:64d346936f0e | 406 | |
GeorgeJourneaux | 6:64d346936f0e | 407 | //Set time from updated time structure |
GeorgeJourneaux | 3:73497379c0cb | 408 | set_time(mktime(s_time)); |
GeorgeJourneaux | 6:64d346936f0e | 409 | strftime(serial_buffer, 80, "\n\r Set Time: %X\n\r", s_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 410 | pc.puts(serial_buffer); |
GeorgeJourneaux | 2:28d12a3db239 | 411 | } |
GeorgeJourneaux | 6:64d346936f0e | 412 | /*----SETT----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 413 | else if(strstr(rx_buffer, "SETT")){ |
GeorgeJourneaux | 6:64d346936f0e | 414 | pc.puts(" SETT\n\r"); |
dnonoo | 10:c10d1337d754 | 415 | // read.detach(); |
dnonoo | 10:c10d1337d754 | 416 | // sampleTime = atoi(rx_buffer); |
dnonoo | 10:c10d1337d754 | 417 | // pc.printf("Sample Time %d\n\r", sampleTime); |
dnonoo | 10:c10d1337d754 | 418 | // read.attach(&readISR, sampleTime); |
GeorgeJourneaux | 2:28d12a3db239 | 419 | } |
GeorgeJourneaux | 6:64d346936f0e | 420 | /*----STATE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 421 | else if(strstr(rx_buffer, "STATE")){ |
GeorgeJourneaux | 6:64d346936f0e | 422 | pc.puts(" STATE\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 423 | } |
GeorgeJourneaux | 6:64d346936f0e | 424 | /*----LOGGING----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 425 | else if(strstr(rx_buffer, "LOGGING")){ |
GeorgeJourneaux | 6:64d346936f0e | 426 | pc.puts(" LOGGING\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 427 | } |
dnonoo | 10:c10d1337d754 | 428 | /*----HELP--------------------------------------*/ |
dnonoo | 10:c10d1337d754 | 429 | else if (strstr(rx_buffer, "HELP")) { |
dnonoo | 10:c10d1337d754 | 430 | pc.puts("\n\n\nCurrently Available Commands:\n\r"); |
dnonoo | 10:c10d1337d754 | 431 | pc.puts("\tREAD n - Read n previous samples\n\r"); |
dnonoo | 10:c10d1337d754 | 432 | pc.puts("\tREAD ALL - Read All previous samples held in memory\n\r"); |
dnonoo | 10:c10d1337d754 | 433 | pc.puts("\tSETTIME hh:mm::ss - Set time in 24hr format\n\r"); |
dnonoo | 10:c10d1337d754 | 434 | pc.puts("\tSETDATE dd/mm/yyyy - Set time in specified format\n\r"); |
dnonoo | 10:c10d1337d754 | 435 | pc.puts("\tDELETE ALL - Delete all sampled held in internal memory\n\n\n\r"); |
dnonoo | 10:c10d1337d754 | 436 | } |
GeorgeJourneaux | 6:64d346936f0e | 437 | /*----ERROR---*/ |
GeorgeJourneaux | 2:28d12a3db239 | 438 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 439 | pc.puts("####ERROR####\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 440 | } |
GeorgeJourneaux | 6:64d346936f0e | 441 | /*----------------------------------------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 442 | |
GeorgeJourneaux | 6:64d346936f0e | 443 | //Clear serial buffers |
GeorgeJourneaux | 5:ea3ec65cbf5f | 444 | memset(serial_buffer, NULL, 80); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 445 | memset(rx_buffer, NULL, 32); |
GeorgeJourneaux | 2:28d12a3db239 | 446 | rx_in = 0; |
GeorgeJourneaux | 2:28d12a3db239 | 447 | |
GeorgeJourneaux | 6:64d346936f0e | 448 | //Attach serial interrupt |
GeorgeJourneaux | 2:28d12a3db239 | 449 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
GeorgeJourneaux | 2:28d12a3db239 | 450 | } |
GeorgeJourneaux | 2:28d12a3db239 | 451 | } |
dnonoo | 7:f017a37bcf1b | 452 | /*------------------------------------------------*/ |
dnonoo | 10:c10d1337d754 | 453 | /*---------------SD THread------------------------*/ |
dnonoo | 10:c10d1337d754 | 454 | void writeRemove_SD() { |
dnonoo | 10:c10d1337d754 | 455 | |
dnonoo | 10:c10d1337d754 | 456 | while(1) { |
dnonoo | 10:c10d1337d754 | 457 | Thread::signal_wait(USER_BUTTON_PRESSED); //wait for debounce signal |
dnonoo | 10:c10d1337d754 | 458 | int sd_state = sdIn; |
dnonoo | 10:c10d1337d754 | 459 | switch (sd_state) { |
dnonoo | 10:c10d1337d754 | 460 | case 1: |
dnonoo | 10:c10d1337d754 | 461 | pc.printf("SD Card not inserted!\n\r"); |
dnonoo | 10:c10d1337d754 | 462 | pc.printf("Insert SD Card and press User button again\n\r"); |
dnonoo | 10:c10d1337d754 | 463 | break; |
dnonoo | 10:c10d1337d754 | 464 | default: |
dnonoo | 10:c10d1337d754 | 465 | pc.printf("This should never happen\n\r"); |
dnonoo | 10:c10d1337d754 | 466 | break; |
dnonoo | 10:c10d1337d754 | 467 | case 0: |
dnonoo | 10:c10d1337d754 | 468 | pc.printf("Initalising SD Card\n\r"); |
dnonoo | 7:f017a37bcf1b | 469 | |
dnonoo | 10:c10d1337d754 | 470 | //check init |
dnonoo | 10:c10d1337d754 | 471 | if (sd.init() != 0) { |
dnonoo | 10:c10d1337d754 | 472 | pc.printf(" ERROR - SD card failed to initialise.\n\rRestart board\n\r"); |
dnonoo | 10:c10d1337d754 | 473 | } |
dnonoo | 10:c10d1337d754 | 474 | |
dnonoo | 10:c10d1337d754 | 475 | // Create Filing system for SD Card |
dnonoo | 10:c10d1337d754 | 476 | FATFileSystem fs("sd", &sd); |
dnonoo | 10:c10d1337d754 | 477 | |
dnonoo | 10:c10d1337d754 | 478 | //OpenFiles to write/append to |
dnonoo | 10:c10d1337d754 | 479 | pc.printf("Writing to SDC\n\r"); |
dnonoo | 10:c10d1337d754 | 480 | |
dnonoo | 10:c10d1337d754 | 481 | FILE* fp = fopen("/sd/TheChamberOfSecrets.txt", "w"); //"w" to overwrite file ftb |
dnonoo | 10:c10d1337d754 | 482 | |
dnonoo | 10:c10d1337d754 | 483 | // Check for error in opening file |
dnonoo | 10:c10d1337d754 | 484 | if (fp == NULL) { |
dnonoo | 10:c10d1337d754 | 485 | pc.printf("*****ERROR - Could not open file for write*****\n\r"); |
dnonoo | 10:c10d1337d754 | 486 | } |
dnonoo | 10:c10d1337d754 | 487 | //HERE IS WHERE TO PRINT DATA TO SD CARD FROM BUFFER (REMEMBER TO EMPTY BUFFER???) |
dnonoo | 10:c10d1337d754 | 488 | //Lock data buffer |
dnonoo | 10:c10d1337d754 | 489 | DataBuffer.lock(); |
dnonoo | 10:c10d1337d754 | 490 | dataLock.lock(); |
dnonoo | 10:c10d1337d754 | 491 | //Print all samples to SD |
dnonoo | 10:c10d1337d754 | 492 | for(int n=data_t; n<=MAX_SAMPLES; n++) { |
dnonoo | 10:c10d1337d754 | 493 | fputs(data_buffer[n], fp); |
dnonoo | 10:c10d1337d754 | 494 | fprintf(fp, "\n\r"); |
dnonoo | 10:c10d1337d754 | 495 | } |
dnonoo | 10:c10d1337d754 | 496 | if(data_t>data_h) { |
dnonoo | 10:c10d1337d754 | 497 | for(int n=0; n<=(data_t-1); n++) { |
dnonoo | 10:c10d1337d754 | 498 | fputs(data_buffer[n], fp); |
dnonoo | 10:c10d1337d754 | 499 | |
dnonoo | 10:c10d1337d754 | 500 | } |
dnonoo | 10:c10d1337d754 | 501 | } |
dnonoo | 10:c10d1337d754 | 502 | |
dnonoo | 10:c10d1337d754 | 503 | //Lock data buffer |
dnonoo | 10:c10d1337d754 | 504 | DataBuffer.unlock(); |
dnonoo | 10:c10d1337d754 | 505 | dataLock.unlock(); |
dnonoo | 10:c10d1337d754 | 506 | //fprintf(fp, "dd/mm/yy hh:mm:ss, TEMPERATURE, PRESSURE, LIGHT\n\r"); |
dnonoo | 10:c10d1337d754 | 507 | |
dnonoo | 10:c10d1337d754 | 508 | fclose(fp); |
dnonoo | 10:c10d1337d754 | 509 | |
dnonoo | 10:c10d1337d754 | 510 | pc.printf("Write Sucessful!\n\r"); |
dnonoo | 10:c10d1337d754 | 511 | |
dnonoo | 10:c10d1337d754 | 512 | sd.deinit(); |
dnonoo | 10:c10d1337d754 | 513 | pc.printf("SD Card Ready to Remove\n\r"); |
dnonoo | 10:c10d1337d754 | 514 | Green_ext = 1; |
dnonoo | 10:c10d1337d754 | 515 | Thread::wait(500); |
dnonoo | 10:c10d1337d754 | 516 | Green_ext = 0; |
dnonoo | 10:c10d1337d754 | 517 | Thread::wait(500); |
dnonoo | 10:c10d1337d754 | 518 | Green_ext = 1; |
dnonoo | 10:c10d1337d754 | 519 | Thread::wait(500); |
dnonoo | 10:c10d1337d754 | 520 | Green_ext = 0; |
dnonoo | 10:c10d1337d754 | 521 | Thread::wait(500); |
dnonoo | 10:c10d1337d754 | 522 | Green_ext = 1; |
dnonoo | 10:c10d1337d754 | 523 | Thread::wait(500); |
dnonoo | 10:c10d1337d754 | 524 | Green_ext = 0; |
dnonoo | 10:c10d1337d754 | 525 | Thread::wait(500); |
dnonoo | 10:c10d1337d754 | 526 | Green_ext = 1; |
dnonoo | 10:c10d1337d754 | 527 | Thread::wait(500); |
dnonoo | 10:c10d1337d754 | 528 | Green_ext = 0; |
dnonoo | 10:c10d1337d754 | 529 | Thread::wait(500); |
dnonoo | 10:c10d1337d754 | 530 | }//End Switch |
dnonoo | 10:c10d1337d754 | 531 | }// End While |
dnonoo | 10:c10d1337d754 | 532 | }// End Thread |
dnonoo | 10:c10d1337d754 | 533 | /*--------------------------------------------------------------------------*/ |
dnonoo | 10:c10d1337d754 | 534 | |
dnonoo | 10:c10d1337d754 | 535 | /*---------------------------Networking Thread------------------------------*/ |
dnonoo | 10:c10d1337d754 | 536 | void Network1 () { |
dnonoo | 10:c10d1337d754 | 537 | |
dnonoo | 10:c10d1337d754 | 538 | printf("Setting up server\n\r"); |
dnonoo | 10:c10d1337d754 | 539 | |
dnonoo | 10:c10d1337d754 | 540 | //Configure an ethernet connection |
dnonoo | 10:c10d1337d754 | 541 | EthernetInterface eth; |
dnonoo | 10:c10d1337d754 | 542 | eth.set_network(IP, NETMASK, GATEWAY); |
dnonoo | 10:c10d1337d754 | 543 | eth.connect(); |
dnonoo | 10:c10d1337d754 | 544 | printf("The target IP address is '%s'\n\r", eth.get_ip_address()); |
dnonoo | 10:c10d1337d754 | 545 | |
dnonoo | 10:c10d1337d754 | 546 | //Now setup a web server |
dnonoo | 10:c10d1337d754 | 547 | TCPServer srv; //TCP/IP Server |
dnonoo | 10:c10d1337d754 | 548 | |
dnonoo | 10:c10d1337d754 | 549 | SocketAddress clt_addr; //Address of incoming connection |
dnonoo | 10:c10d1337d754 | 550 | |
dnonoo | 10:c10d1337d754 | 551 | /* Open the server on ethernet stack */ |
dnonoo | 10:c10d1337d754 | 552 | srv.open(ð); |
dnonoo | 10:c10d1337d754 | 553 | |
dnonoo | 10:c10d1337d754 | 554 | /* Bind the HTTP port (TCP 80) to the server */ |
dnonoo | 10:c10d1337d754 | 555 | srv.bind(eth.get_ip_address(), 80); |
dnonoo | 10:c10d1337d754 | 556 | |
dnonoo | 10:c10d1337d754 | 557 | /* Can handle 5 simultaneous connections */ |
dnonoo | 10:c10d1337d754 | 558 | srv.listen(5); |
dnonoo | 10:c10d1337d754 | 559 | |
dnonoo | 10:c10d1337d754 | 560 | while (true) { |
dnonoo | 10:c10d1337d754 | 561 | |
dnonoo | 10:c10d1337d754 | 562 | TCPSocket clt_sock; //Socket for communication |
dnonoo | 10:c10d1337d754 | 563 | using namespace std; |
dnonoo | 10:c10d1337d754 | 564 | //Block and wait on an incoming connection |
dnonoo | 10:c10d1337d754 | 565 | srv.accept(&clt_sock, &clt_addr); |
dnonoo | 10:c10d1337d754 | 566 | //printf("accept %s:%d\n\r", clt_addr.get_ip_address(), clt_addr.get_port()); |
dnonoo | 10:c10d1337d754 | 567 | |
dnonoo | 10:c10d1337d754 | 568 | //Uses a C++ string to make it easier to concatinate |
dnonoo | 10:c10d1337d754 | 569 | string response; |
dnonoo | 10:c10d1337d754 | 570 | string strL = "LDR:"; |
dnonoo | 10:c10d1337d754 | 571 | string strP = ", Pressure(mBar): "; |
dnonoo | 10:c10d1337d754 | 572 | string strT = ", Temp(C): "; |
dnonoo | 10:c10d1337d754 | 573 | //This is a C string |
dnonoo | 10:c10d1337d754 | 574 | char l_str[64]; |
dnonoo | 10:c10d1337d754 | 575 | char p_str[64]; |
dnonoo | 10:c10d1337d754 | 576 | char t_str[64]; |
dnonoo | 10:c10d1337d754 | 577 | |
dnonoo | 10:c10d1337d754 | 578 | //Read the LDR value |
dnonoo | 10:c10d1337d754 | 579 | dataLock.lock(); |
dnonoo | 10:c10d1337d754 | 580 | float L = LDR ; |
dnonoo | 10:c10d1337d754 | 581 | float T = TEMP; |
dnonoo | 10:c10d1337d754 | 582 | float P = PRES; |
dnonoo | 10:c10d1337d754 | 583 | dataLock.unlock(); |
dnonoo | 10:c10d1337d754 | 584 | |
dnonoo | 10:c10d1337d754 | 585 | //Convert to a C String |
dnonoo | 10:c10d1337d754 | 586 | sprintf(l_str, "%1.3f", L ); |
dnonoo | 10:c10d1337d754 | 587 | sprintf(t_str, "%2.2f", T); |
dnonoo | 10:c10d1337d754 | 588 | sprintf(p_str, "%4.2f", P); |
dnonoo | 10:c10d1337d754 | 589 | |
dnonoo | 10:c10d1337d754 | 590 | |
dnonoo | 10:c10d1337d754 | 591 | //Build the C++ string response |
dnonoo | 10:c10d1337d754 | 592 | response = HTTP_MESSAGE_BODY1; |
dnonoo | 10:c10d1337d754 | 593 | // response += strL; |
dnonoo | 10:c10d1337d754 | 594 | response += l_str; |
dnonoo | 10:c10d1337d754 | 595 | response += strT; |
dnonoo | 10:c10d1337d754 | 596 | response += t_str; |
dnonoo | 10:c10d1337d754 | 597 | response += strP; |
dnonoo | 10:c10d1337d754 | 598 | response += p_str; |
dnonoo | 10:c10d1337d754 | 599 | response += HTTP_MESSAGE_BODY2; |
dnonoo | 10:c10d1337d754 | 600 | |
dnonoo | 10:c10d1337d754 | 601 | //Send static HTML response (as a C string) |
dnonoo | 10:c10d1337d754 | 602 | clt_sock.send(response.c_str(), response.size()+6); |
dnonoo | 10:c10d1337d754 | 603 | |
dnonoo | 10:c10d1337d754 | 604 | } |
dnonoo | 10:c10d1337d754 | 605 | } |
dnonoo | 10:c10d1337d754 | 606 | /*---------------------------POST--------------------------------------------*/ |
dnonoo | 7:f017a37bcf1b | 607 | void POST () { |
dnonoo | 7:f017a37bcf1b | 608 | |
dnonoo | 7:f017a37bcf1b | 609 | pc.printf(" ALL Leds should be flashing\n\r"); |
dnonoo | 7:f017a37bcf1b | 610 | |
dnonoo | 7:f017a37bcf1b | 611 | for(unsigned int n = 0; n<10; n++) { |
dnonoo | 7:f017a37bcf1b | 612 | Green_int = ON; |
dnonoo | 7:f017a37bcf1b | 613 | Blue_int = ON; |
dnonoo | 7:f017a37bcf1b | 614 | Red_int = ON; |
dnonoo | 7:f017a37bcf1b | 615 | Green_ext = ON; |
dnonoo | 7:f017a37bcf1b | 616 | Yellow_ext = ON; |
dnonoo | 7:f017a37bcf1b | 617 | Red_ext = ON; |
dnonoo | 7:f017a37bcf1b | 618 | |
dnonoo | 7:f017a37bcf1b | 619 | wait (0.2); |
dnonoo | 7:f017a37bcf1b | 620 | Green_int = OFF; |
dnonoo | 7:f017a37bcf1b | 621 | Blue_int = OFF; |
dnonoo | 7:f017a37bcf1b | 622 | Red_int = OFF; |
dnonoo | 7:f017a37bcf1b | 623 | Green_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 624 | Yellow_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 625 | Red_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 626 | wait (0.2); |
dnonoo | 7:f017a37bcf1b | 627 | } |
dnonoo | 7:f017a37bcf1b | 628 | |
dnonoo | 7:f017a37bcf1b | 629 | pc.printf("Switch states:\n\r"); |
dnonoo | 7:f017a37bcf1b | 630 | pc.printf("\tSW_L: %d\n\r\tSW_R %d\n\r", SW_L.read(), SW_R.read()); |
dnonoo | 7:f017a37bcf1b | 631 | |
dnonoo | 7:f017a37bcf1b | 632 | float Temp = Sensor.getTemperature(); |
dnonoo | 7:f017a37bcf1b | 633 | float Pres = Sensor.getPressure(); |
dnonoo | 7:f017a37bcf1b | 634 | float ldrs = LDR_In.read(); |
dnonoo | 7:f017a37bcf1b | 635 | |
dnonoo | 7:f017a37bcf1b | 636 | pc.printf("Sensor test:\n\r"); |
dnonoo | 7:f017a37bcf1b | 637 | pc.printf("T: %f\tP: %f\tL: %f\n\r",Temp,Pres,ldrs); |
dnonoo | 7:f017a37bcf1b | 638 | |
dnonoo | 7:f017a37bcf1b | 639 | pc.printf("LCD Test\n\r"); |
dnonoo | 7:f017a37bcf1b | 640 | |
dnonoo | 7:f017a37bcf1b | 641 | lcd.Clear(); |
dnonoo | 7:f017a37bcf1b | 642 | lcd.RowSelect(1); |
dnonoo | 7:f017a37bcf1b | 643 | lcd.Write("*******LCD******"); |
dnonoo | 7:f017a37bcf1b | 644 | lcd.RowSelect(2); |
dnonoo | 7:f017a37bcf1b | 645 | lcd.Write("******TEST******"); |
dnonoo | 7:f017a37bcf1b | 646 | wait(1); |
dnonoo | 10:c10d1337d754 | 647 | lcd.Clear(); |
dnonoo | 10:c10d1337d754 | 648 | pc.printf("Basic POST Pass\n\r"); |
dnonoo | 7:f017a37bcf1b | 649 | } |