V4

Dependencies:   BMP280

Fork of Thread_Communication_V4 by BDG

Committer:
dnonoo
Date:
Sat Jan 06 12:22:12 2018 +0000
Revision:
14:7c4d87dfc388
Parent:
13:089f3adb3813
Child:
15:864d936b51cf
EVERYTHING WORKING pre object orientated

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