V4

Dependencies:   BMP280

Fork of Thread_Communication_V4_fortest by BDG

Committer:
benparkes
Date:
Sat Jan 06 19:44:43 2018 +0000
Revision:
17:aa585f901750
Parent:
16:f30928e622a2
Child:
18:728fddea6d9c
new

Who changed what in which revision?

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