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