Network, SD card, Serial, LCD and sensors all work! :) ** Don't Press the User Button without an SD Card inserted!! **
Dependencies: BMP280
Fork of Thread_Communication_V2 by
main.cpp@7:f017a37bcf1b, 2017-12-28 (annotated)
- Committer:
- dnonoo
- Date:
- Thu Dec 28 19:32:22 2017 +0000
- Revision:
- 7:f017a37bcf1b
- Parent:
- 6:64d346936f0e
- Child:
- 8:ab6322afa341
Mostly Georges buffer code using one mail queue and global variables with mutex locks.;
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 | |
GeorgeJourneaux | 3:73497379c0cb | 5 | LCD lcd(PD_15, PF_12, PF_13, PE_9, PF_14, PF_15); |
GeorgeJourneaux | 3:73497379c0cb | 6 | BMP280 Sensor(D14, D15); |
benparkes | 0:cb3a5c15b01e | 7 | |
GeorgeJourneaux | 5:ea3ec65cbf5f | 8 | //Define Functions |
benparkes | 0:cb3a5c15b01e | 9 | void PrintLCD (); |
GeorgeJourneaux | 2:28d12a3db239 | 10 | void Rx_interrupt(); |
dnonoo | 7:f017a37bcf1b | 11 | void serialCMD(); |
dnonoo | 7:f017a37bcf1b | 12 | void sensorRead(); |
dnonoo | 7:f017a37bcf1b | 13 | void readISR(); |
dnonoo | 7:f017a37bcf1b | 14 | void circBuff(); |
GeorgeJourneaux | 2:28d12a3db239 | 15 | |
dnonoo | 7:f017a37bcf1b | 16 | /* LOCKS */ |
GeorgeJourneaux | 6:64d346936f0e | 17 | Mutex DataBuffer; |
dnonoo | 7:f017a37bcf1b | 18 | Mutex sensorData; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 19 | |
dnonoo | 7:f017a37bcf1b | 20 | /* THREADS */ |
dnonoo | 7:f017a37bcf1b | 21 | Thread _PrintLCD, _serialCMD, _circBuff; |
dnonoo | 7:f017a37bcf1b | 22 | Thread _sensorRead (osPriorityRealtime); //sensorData Thread |
dnonoo | 7:f017a37bcf1b | 23 | |
GeorgeJourneaux | 3:73497379c0cb | 24 | |
dnonoo | 7:f017a37bcf1b | 25 | /* GLOBAL DATA */ |
dnonoo | 7:f017a37bcf1b | 26 | volatile float LDR = 0; |
dnonoo | 7:f017a37bcf1b | 27 | volatile double PRES = 0; |
dnonoo | 7:f017a37bcf1b | 28 | volatile double TEMP = 0; |
benparkes | 1:bca9993a0df3 | 29 | |
dnonoo | 7:f017a37bcf1b | 30 | Ticker read; |
benparkes | 0:cb3a5c15b01e | 31 | |
dnonoo | 7:f017a37bcf1b | 32 | /* INTERRUPTS */ |
dnonoo | 7:f017a37bcf1b | 33 | |
GeorgeJourneaux | 3:73497379c0cb | 34 | /*--------------------------------MAIN--------------------------------*/ |
GeorgeJourneaux | 3:73497379c0cb | 35 | int main() { |
GeorgeJourneaux | 3:73497379c0cb | 36 | |
GeorgeJourneaux | 3:73497379c0cb | 37 | pc.baud(9600); |
GeorgeJourneaux | 3:73497379c0cb | 38 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
dnonoo | 7:f017a37bcf1b | 39 | POST(); |
dnonoo | 7:f017a37bcf1b | 40 | |
dnonoo | 7:f017a37bcf1b | 41 | _serialCMD.start(serialCMD); |
dnonoo | 7:f017a37bcf1b | 42 | _PrintLCD.start(PrintLCD); |
dnonoo | 7:f017a37bcf1b | 43 | _sensorRead.start(sensorRead); |
dnonoo | 7:f017a37bcf1b | 44 | _circBuff.start(circBuff); |
dnonoo | 7:f017a37bcf1b | 45 | |
dnonoo | 7:f017a37bcf1b | 46 | read.attach(readISR, SAMPLING_PERIOD); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 47 | |
dnonoo | 7:f017a37bcf1b | 48 | |
dnonoo | 7:f017a37bcf1b | 49 | while (1) {} |
dnonoo | 7:f017a37bcf1b | 50 | } |
dnonoo | 7:f017a37bcf1b | 51 | /*--------------------------------------------------------------------*/ |
dnonoo | 7:f017a37bcf1b | 52 | void circBuff () { |
dnonoo | 7:f017a37bcf1b | 53 | |
dnonoo | 7:f017a37bcf1b | 54 | while(1) { |
dnonoo | 7:f017a37bcf1b | 55 | Thread::signal_wait(DATA_READY); // wait for signal from sensorRead |
GeorgeJourneaux | 5:ea3ec65cbf5f | 56 | |
dnonoo | 7:f017a37bcf1b | 57 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 58 | DataBuffer.lock(); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 59 | |
dnonoo | 7:f017a37bcf1b | 60 | //Format samples, send to FIFO buffer head |
GeorgeJourneaux | 6:64d346936f0e | 61 | memset(data_buffer[sample_h],NULL,64); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 62 | time( &raw_time ); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 63 | sample_epoch = localtime( &raw_time ); |
GeorgeJourneaux | 6:64d346936f0e | 64 | char sample_time[20]; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 65 | strftime(sample_time,20,"%d/%m/%Y %X",sample_epoch); |
dnonoo | 7:f017a37bcf1b | 66 | |
dnonoo | 7:f017a37bcf1b | 67 | sensorData.lock(); //lock critical section |
dnonoo | 7:f017a37bcf1b | 68 | sprintf(data_buffer[sample_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, TEMP, PRES, LDR); |
dnonoo | 7:f017a37bcf1b | 69 | sensorData.unlock(); //unlock critical section |
dnonoo | 7:f017a37bcf1b | 70 | |
GeorgeJourneaux | 5:ea3ec65cbf5f | 71 | memset(sample_time,NULL,20); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 72 | |
dnonoo | 7:f017a37bcf1b | 73 | //Set seperate FIFO head and tail for printing data |
GeorgeJourneaux | 6:64d346936f0e | 74 | data_h = sample_h; |
GeorgeJourneaux | 6:64d346936f0e | 75 | data_t = sample_t; |
GeorgeJourneaux | 6:64d346936f0e | 76 | |
dnonoo | 7:f017a37bcf1b | 77 | //Move sample FIFO buffer head to next row in buffer |
GeorgeJourneaux | 6:64d346936f0e | 78 | sample_h++; |
dnonoo | 7:f017a37bcf1b | 79 | //Check sample FIFO buffer head |
GeorgeJourneaux | 6:64d346936f0e | 80 | if(sample_h >= MAX_SAMPLES){ |
GeorgeJourneaux | 6:64d346936f0e | 81 | sample_h = 0; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 82 | } |
dnonoo | 7:f017a37bcf1b | 83 | //Check sample FIFO buffer tail |
GeorgeJourneaux | 6:64d346936f0e | 84 | if(sample_t == sample_h){ |
GeorgeJourneaux | 6:64d346936f0e | 85 | sample_t++; |
GeorgeJourneaux | 6:64d346936f0e | 86 | if(sample_t >= (MAX_SAMPLES)){ |
GeorgeJourneaux | 6:64d346936f0e | 87 | sample_t = 0; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 88 | } |
GeorgeJourneaux | 5:ea3ec65cbf5f | 89 | } |
dnonoo | 7:f017a37bcf1b | 90 | //Unlock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 91 | DataBuffer.unlock(); |
dnonoo | 7:f017a37bcf1b | 92 | } |
dnonoo | 7:f017a37bcf1b | 93 | } |
dnonoo | 7:f017a37bcf1b | 94 | |
dnonoo | 7:f017a37bcf1b | 95 | /*---------------------Read Sensors ---------------------------*/ |
dnonoo | 7:f017a37bcf1b | 96 | |
dnonoo | 7:f017a37bcf1b | 97 | void readISR () { // Ticker interrupt defined in main |
dnonoo | 7:f017a37bcf1b | 98 | |
dnonoo | 7:f017a37bcf1b | 99 | _sensorRead.signal_set(SENSOR_UPDATE); |
dnonoo | 7:f017a37bcf1b | 100 | } |
dnonoo | 7:f017a37bcf1b | 101 | |
dnonoo | 7:f017a37bcf1b | 102 | void sensorRead () { |
dnonoo | 7:f017a37bcf1b | 103 | |
dnonoo | 7:f017a37bcf1b | 104 | while (1) { |
dnonoo | 7:f017a37bcf1b | 105 | |
dnonoo | 7:f017a37bcf1b | 106 | sensorData.lock(); // Entering Critial Section |
dnonoo | 7:f017a37bcf1b | 107 | |
dnonoo | 7:f017a37bcf1b | 108 | // Store Data in global Variables |
dnonoo | 7:f017a37bcf1b | 109 | LDR = LDR_In.read(); |
dnonoo | 7:f017a37bcf1b | 110 | TEMP = Sensor.getTemperature(); |
dnonoo | 7:f017a37bcf1b | 111 | PRES = Sensor.getPressure(); |
dnonoo | 7:f017a37bcf1b | 112 | |
dnonoo | 7:f017a37bcf1b | 113 | sensorData.unlock(); // Exiting Critical Section |
dnonoo | 7:f017a37bcf1b | 114 | |
dnonoo | 7:f017a37bcf1b | 115 | Green_int = !Green_int; // debugging |
GeorgeJourneaux | 5:ea3ec65cbf5f | 116 | |
dnonoo | 7:f017a37bcf1b | 117 | //Read sensors, send to mail-queue |
dnonoo | 7:f017a37bcf1b | 118 | mail_t *mail = mail_box.alloc(); |
dnonoo | 7:f017a37bcf1b | 119 | mail->LDR_Value = LDR; |
dnonoo | 7:f017a37bcf1b | 120 | mail->temp_Value = TEMP; |
dnonoo | 7:f017a37bcf1b | 121 | mail->press_Value = PRES; |
dnonoo | 7:f017a37bcf1b | 122 | mail_box.put(mail); |
dnonoo | 7:f017a37bcf1b | 123 | |
dnonoo | 7:f017a37bcf1b | 124 | _circBuff.signal_set(DATA_READY); // Set signal to buffer to store updated values |
dnonoo | 7:f017a37bcf1b | 125 | Thread::signal_wait(SENSOR_UPDATE); // Wait for the Timer interrupt |
dnonoo | 7:f017a37bcf1b | 126 | } |
GeorgeJourneaux | 3:73497379c0cb | 127 | } |
dnonoo | 7:f017a37bcf1b | 128 | |
GeorgeJourneaux | 3:73497379c0cb | 129 | |
GeorgeJourneaux | 3:73497379c0cb | 130 | /*--------------------------------LCD---------------------------------*/ |
benparkes | 0:cb3a5c15b01e | 131 | void PrintLCD () { |
benparkes | 0:cb3a5c15b01e | 132 | |
benparkes | 0:cb3a5c15b01e | 133 | int i = 0; |
benparkes | 0:cb3a5c15b01e | 134 | while(1){ |
benparkes | 1:bca9993a0df3 | 135 | char lightString[16]; |
benparkes | 1:bca9993a0df3 | 136 | char tempString[16]; |
benparkes | 1:bca9993a0df3 | 137 | char pressString[16]; |
benparkes | 1:bca9993a0df3 | 138 | |
benparkes | 0:cb3a5c15b01e | 139 | lcd.Clear(); |
benparkes | 0:cb3a5c15b01e | 140 | lcd.RowSelect(0); |
benparkes | 1:bca9993a0df3 | 141 | |
benparkes | 0:cb3a5c15b01e | 142 | switch (i){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 143 | case 0:{ |
GeorgeJourneaux | 2:28d12a3db239 | 144 | osEvent evt = mail_box.get(); |
benparkes | 1:bca9993a0df3 | 145 | |
GeorgeJourneaux | 2:28d12a3db239 | 146 | if (evt.status == osEventMail) { |
GeorgeJourneaux | 2:28d12a3db239 | 147 | mail_t *mail = (mail_t*)evt.value.p; |
benparkes | 0:cb3a5c15b01e | 148 | |
GeorgeJourneaux | 2:28d12a3db239 | 149 | sprintf(lightString,"%.4f", mail->LDR_Value); |
GeorgeJourneaux | 2:28d12a3db239 | 150 | sprintf(tempString,"%2.2f", mail->temp_Value); |
GeorgeJourneaux | 2:28d12a3db239 | 151 | sprintf(pressString,"%4.2f", mail->press_Value); |
benparkes | 1:bca9993a0df3 | 152 | |
GeorgeJourneaux | 2:28d12a3db239 | 153 | mail_box.free(mail); |
GeorgeJourneaux | 2:28d12a3db239 | 154 | } |
benparkes | 1:bca9993a0df3 | 155 | |
GeorgeJourneaux | 2:28d12a3db239 | 156 | lcd.Write("Light Level:"); |
GeorgeJourneaux | 2:28d12a3db239 | 157 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 158 | lcd.Write(lightString); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 159 | i++; |
benparkes | 0:cb3a5c15b01e | 160 | break; |
GeorgeJourneaux | 5:ea3ec65cbf5f | 161 | } |
GeorgeJourneaux | 2:28d12a3db239 | 162 | case 1: |
GeorgeJourneaux | 2:28d12a3db239 | 163 | lcd.Write("Temperature:"); |
GeorgeJourneaux | 2:28d12a3db239 | 164 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 165 | lcd.Write(tempString); |
GeorgeJourneaux | 2:28d12a3db239 | 166 | i++; |
benparkes | 0:cb3a5c15b01e | 167 | break; |
benparkes | 0:cb3a5c15b01e | 168 | |
GeorgeJourneaux | 2:28d12a3db239 | 169 | case 2: |
GeorgeJourneaux | 2:28d12a3db239 | 170 | lcd.Write("Pressure:"); |
GeorgeJourneaux | 2:28d12a3db239 | 171 | lcd.RowSelect(1); |
GeorgeJourneaux | 2:28d12a3db239 | 172 | lcd.Write(pressString); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 173 | i =0; |
benparkes | 0:cb3a5c15b01e | 174 | break; |
GeorgeJourneaux | 2:28d12a3db239 | 175 | |
GeorgeJourneaux | 5:ea3ec65cbf5f | 176 | default: |
GeorgeJourneaux | 5:ea3ec65cbf5f | 177 | i = 0; |
benparkes | 0:cb3a5c15b01e | 178 | break; |
benparkes | 0:cb3a5c15b01e | 179 | } |
benparkes | 0:cb3a5c15b01e | 180 | |
GeorgeJourneaux | 2:28d12a3db239 | 181 | Red_int = !Red_int; |
benparkes | 1:bca9993a0df3 | 182 | |
benparkes | 0:cb3a5c15b01e | 183 | Thread::wait (5000); |
GeorgeJourneaux | 2:28d12a3db239 | 184 | } |
benparkes | 0:cb3a5c15b01e | 185 | } |
GeorgeJourneaux | 3:73497379c0cb | 186 | /*--------------------------------------------------------------------*/ |
benparkes | 1:bca9993a0df3 | 187 | |
GeorgeJourneaux | 3:73497379c0cb | 188 | /*------------------------------SERIAL_CMD----------------------------*/ |
GeorgeJourneaux | 4:93d6d13d4de3 | 189 | //Interrupt when recieving from serial port |
GeorgeJourneaux | 2:28d12a3db239 | 190 | void Rx_interrupt() { |
GeorgeJourneaux | 2:28d12a3db239 | 191 | |
dnonoo | 7:f017a37bcf1b | 192 | //Wait for serial input |
GeorgeJourneaux | 2:28d12a3db239 | 193 | while (pc.readable()) { |
GeorgeJourneaux | 6:64d346936f0e | 194 | |
dnonoo | 7:f017a37bcf1b | 195 | //Return input to serial |
GeorgeJourneaux | 5:ea3ec65cbf5f | 196 | rx_buffer[rx_in] = pc.getc(); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 197 | pc.putc(rx_buffer[rx_in]); |
GeorgeJourneaux | 6:64d346936f0e | 198 | |
dnonoo | 7:f017a37bcf1b | 199 | //If enter key is pressed, set serial thread signal |
GeorgeJourneaux | 5:ea3ec65cbf5f | 200 | if(rx_buffer[rx_in] == 0xD){ |
dnonoo | 7:f017a37bcf1b | 201 | _serialCMD.signal_set(ENTER_KEY); |
GeorgeJourneaux | 2:28d12a3db239 | 202 | } |
GeorgeJourneaux | 6:64d346936f0e | 203 | |
dnonoo | 7:f017a37bcf1b | 204 | //Increment buffer head |
GeorgeJourneaux | 2:28d12a3db239 | 205 | else{ |
GeorgeJourneaux | 2:28d12a3db239 | 206 | rx_in = (rx_in + 1); |
GeorgeJourneaux | 2:28d12a3db239 | 207 | } |
GeorgeJourneaux | 2:28d12a3db239 | 208 | } |
GeorgeJourneaux | 2:28d12a3db239 | 209 | } |
benparkes | 0:cb3a5c15b01e | 210 | |
GeorgeJourneaux | 4:93d6d13d4de3 | 211 | //Check what command what recieved and execute |
dnonoo | 7:f017a37bcf1b | 212 | void serialCMD(){ |
GeorgeJourneaux | 2:28d12a3db239 | 213 | |
GeorgeJourneaux | 2:28d12a3db239 | 214 | while(1){ |
dnonoo | 7:f017a37bcf1b | 215 | //Wait for thread signal |
GeorgeJourneaux | 4:93d6d13d4de3 | 216 | Thread::signal_wait(ENTER_KEY); |
GeorgeJourneaux | 6:64d346936f0e | 217 | |
dnonoo | 7:f017a37bcf1b | 218 | //Detach serial interrupt |
GeorgeJourneaux | 2:28d12a3db239 | 219 | pc.attach(NULL, Serial::RxIrq); |
GeorgeJourneaux | 6:64d346936f0e | 220 | |
GeorgeJourneaux | 3:73497379c0cb | 221 | struct tm * s_time; |
GeorgeJourneaux | 3:73497379c0cb | 222 | char tm_n[4]; |
GeorgeJourneaux | 3:73497379c0cb | 223 | |
GeorgeJourneaux | 6:64d346936f0e | 224 | /*----CARRAGE RETURN-------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 225 | if(rx_buffer[0] == 0xD){ |
GeorgeJourneaux | 6:64d346936f0e | 226 | pc.puts("\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 227 | } |
GeorgeJourneaux | 6:64d346936f0e | 228 | /*----READ ALL----------------------------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 229 | else if(strstr(rx_buffer, "READ ALL")){ |
GeorgeJourneaux | 6:64d346936f0e | 230 | pc.puts(" READ ALL\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 231 | |
dnonoo | 7:f017a37bcf1b | 232 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 233 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 234 | |
dnonoo | 7:f017a37bcf1b | 235 | //Print all samples to serial |
GeorgeJourneaux | 6:64d346936f0e | 236 | for(int n=data_t; n<=MAX_SAMPLES; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 237 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 238 | } |
GeorgeJourneaux | 6:64d346936f0e | 239 | if(data_t>data_h){ |
GeorgeJourneaux | 6:64d346936f0e | 240 | for(int n=0; n<=(data_t-1); n++){ |
GeorgeJourneaux | 6:64d346936f0e | 241 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 242 | } |
GeorgeJourneaux | 6:64d346936f0e | 243 | } |
GeorgeJourneaux | 5:ea3ec65cbf5f | 244 | |
dnonoo | 7:f017a37bcf1b | 245 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 246 | DataBuffer.unlock(); |
GeorgeJourneaux | 2:28d12a3db239 | 247 | } |
GeorgeJourneaux | 6:64d346936f0e | 248 | /*----DELETE ALL----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 249 | else if(strstr(rx_buffer, "DELETE ALL")){ |
GeorgeJourneaux | 6:64d346936f0e | 250 | pc.puts(" DELETE ALL\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 251 | |
GeorgeJourneaux | 6:64d346936f0e | 252 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 253 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 254 | |
GeorgeJourneaux | 6:64d346936f0e | 255 | //Delete all sampled data |
GeorgeJourneaux | 6:64d346936f0e | 256 | for(int n=0; n<=MAX_SAMPLES; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 257 | memset(data_buffer[n], NULL, 64); |
GeorgeJourneaux | 6:64d346936f0e | 258 | } |
GeorgeJourneaux | 6:64d346936f0e | 259 | data_h = data_t; |
GeorgeJourneaux | 6:64d346936f0e | 260 | sample_h = sample_t; |
GeorgeJourneaux | 6:64d346936f0e | 261 | |
GeorgeJourneaux | 6:64d346936f0e | 262 | //Unlock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 263 | DataBuffer.unlock(); |
GeorgeJourneaux | 2:28d12a3db239 | 264 | } |
GeorgeJourneaux | 6:64d346936f0e | 265 | /*----READ----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 266 | else if(strstr(rx_buffer, "READ")){ |
GeorgeJourneaux | 6:64d346936f0e | 267 | pc.puts(" READ \n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 268 | int N = atoi(strncpy(tm_n,&rx_buffer[5],4)); |
GeorgeJourneaux | 6:64d346936f0e | 269 | int S = 0; |
GeorgeJourneaux | 6:64d346936f0e | 270 | pc.printf("N = %d\n\r",N); |
GeorgeJourneaux | 6:64d346936f0e | 271 | |
GeorgeJourneaux | 6:64d346936f0e | 272 | //Lock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 273 | DataBuffer.lock(); |
GeorgeJourneaux | 6:64d346936f0e | 274 | |
GeorgeJourneaux | 6:64d346936f0e | 275 | //Check if N is greater than buffer size |
GeorgeJourneaux | 6:64d346936f0e | 276 | if(N >= MAX_SAMPLES){ |
GeorgeJourneaux | 6:64d346936f0e | 277 | N = MAX_SAMPLES; |
GeorgeJourneaux | 6:64d346936f0e | 278 | } |
GeorgeJourneaux | 6:64d346936f0e | 279 | |
GeorgeJourneaux | 6:64d346936f0e | 280 | //Read N samples from FIFO buffer |
GeorgeJourneaux | 6:64d346936f0e | 281 | if(N <= 0){ |
GeorgeJourneaux | 6:64d346936f0e | 282 | pc.puts("####ERROR####\n\r"); |
GeorgeJourneaux | 6:64d346936f0e | 283 | } |
GeorgeJourneaux | 6:64d346936f0e | 284 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 285 | for(int n=data_t; n<=MAX_SAMPLES-1; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 286 | if(S>=N){} |
GeorgeJourneaux | 6:64d346936f0e | 287 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 288 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 289 | S++; |
GeorgeJourneaux | 6:64d346936f0e | 290 | } |
GeorgeJourneaux | 6:64d346936f0e | 291 | } |
GeorgeJourneaux | 6:64d346936f0e | 292 | for(int n=0; n<=data_t; n++){ |
GeorgeJourneaux | 6:64d346936f0e | 293 | if(S>=N){} |
GeorgeJourneaux | 6:64d346936f0e | 294 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 295 | pc.puts(data_buffer[n]); |
GeorgeJourneaux | 6:64d346936f0e | 296 | S++; |
GeorgeJourneaux | 6:64d346936f0e | 297 | } |
GeorgeJourneaux | 6:64d346936f0e | 298 | } |
GeorgeJourneaux | 6:64d346936f0e | 299 | } |
GeorgeJourneaux | 6:64d346936f0e | 300 | |
GeorgeJourneaux | 6:64d346936f0e | 301 | //Unlock data buffer |
GeorgeJourneaux | 6:64d346936f0e | 302 | DataBuffer.unlock(); |
GeorgeJourneaux | 2:28d12a3db239 | 303 | } |
GeorgeJourneaux | 6:64d346936f0e | 304 | /*----DELETE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 305 | else if(strstr(rx_buffer, "DELETE")){ |
GeorgeJourneaux | 6:64d346936f0e | 306 | pc.puts(" DELETE \n\r"); |
GeorgeJourneaux | 3:73497379c0cb | 307 | } |
GeorgeJourneaux | 6:64d346936f0e | 308 | /*----SETDATE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 309 | else if(strstr(rx_buffer, "SETDATE")){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 310 | time(&raw_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 311 | s_time = localtime(&raw_time); |
GeorgeJourneaux | 3:73497379c0cb | 312 | |
GeorgeJourneaux | 6:64d346936f0e | 313 | //Update day in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 314 | int dd = atoi(strncpy(tm_n,&rx_buffer[8],2)); |
GeorgeJourneaux | 3:73497379c0cb | 315 | s_time->tm_mday = dd; |
GeorgeJourneaux | 3:73497379c0cb | 316 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 317 | |
GeorgeJourneaux | 6:64d346936f0e | 318 | //Update month in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 319 | int mm = atoi(strncpy(tm_n,&rx_buffer[11],2)); |
GeorgeJourneaux | 3:73497379c0cb | 320 | s_time->tm_mon = mm-1; |
GeorgeJourneaux | 3:73497379c0cb | 321 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 322 | |
GeorgeJourneaux | 6:64d346936f0e | 323 | //Update year in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 324 | int yyyy = atoi(strncpy(tm_n,&rx_buffer[14],4)); |
GeorgeJourneaux | 3:73497379c0cb | 325 | s_time->tm_year = yyyy-1900; |
GeorgeJourneaux | 3:73497379c0cb | 326 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 327 | |
GeorgeJourneaux | 6:64d346936f0e | 328 | //Set date from updated time structure |
GeorgeJourneaux | 3:73497379c0cb | 329 | set_time(mktime(s_time)); |
GeorgeJourneaux | 6:64d346936f0e | 330 | strftime(serial_buffer, 80, "\n\r Set Date: %d/%m/%Y\n\r", s_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 331 | pc.puts(serial_buffer); |
GeorgeJourneaux | 2:28d12a3db239 | 332 | } |
GeorgeJourneaux | 6:64d346936f0e | 333 | /*----SETTIME---------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 334 | else if(strstr(rx_buffer, "SETTIME")){ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 335 | time(&raw_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 336 | s_time = localtime(&raw_time); |
GeorgeJourneaux | 3:73497379c0cb | 337 | |
GeorgeJourneaux | 6:64d346936f0e | 338 | //Update seconds in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 339 | int ss = atoi(strncpy(tm_n,&rx_buffer[14],2)); |
GeorgeJourneaux | 3:73497379c0cb | 340 | s_time->tm_sec = ss; |
GeorgeJourneaux | 3:73497379c0cb | 341 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 6:64d346936f0e | 342 | |
GeorgeJourneaux | 6:64d346936f0e | 343 | //Update minutes in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 344 | int mm = atoi(strncpy(tm_n,&rx_buffer[11],2)); |
GeorgeJourneaux | 3:73497379c0cb | 345 | s_time->tm_min = mm; |
GeorgeJourneaux | 3:73497379c0cb | 346 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 3:73497379c0cb | 347 | |
GeorgeJourneaux | 6:64d346936f0e | 348 | //Update hour in time structure |
GeorgeJourneaux | 5:ea3ec65cbf5f | 349 | int hh = atoi(strncpy(tm_n,&rx_buffer[8],2)); |
GeorgeJourneaux | 3:73497379c0cb | 350 | s_time->tm_hour = hh; |
GeorgeJourneaux | 3:73497379c0cb | 351 | memset(tm_n, NULL, 4); |
GeorgeJourneaux | 6:64d346936f0e | 352 | |
GeorgeJourneaux | 6:64d346936f0e | 353 | //Set time from updated time structure |
GeorgeJourneaux | 3:73497379c0cb | 354 | set_time(mktime(s_time)); |
GeorgeJourneaux | 6:64d346936f0e | 355 | strftime(serial_buffer, 80, "\n\r Set Time: %X\n\r", s_time); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 356 | pc.puts(serial_buffer); |
GeorgeJourneaux | 2:28d12a3db239 | 357 | } |
GeorgeJourneaux | 6:64d346936f0e | 358 | /*----SETT----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 359 | else if(strstr(rx_buffer, "SETT")){ |
GeorgeJourneaux | 6:64d346936f0e | 360 | pc.puts(" SETT\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 361 | } |
GeorgeJourneaux | 6:64d346936f0e | 362 | /*----STATE----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 363 | else if(strstr(rx_buffer, "STATE")){ |
GeorgeJourneaux | 6:64d346936f0e | 364 | pc.puts(" STATE\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 365 | } |
GeorgeJourneaux | 6:64d346936f0e | 366 | /*----LOGGING----------------------------------*/ |
GeorgeJourneaux | 5:ea3ec65cbf5f | 367 | else if(strstr(rx_buffer, "LOGGING")){ |
GeorgeJourneaux | 6:64d346936f0e | 368 | pc.puts(" LOGGING\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 369 | } |
GeorgeJourneaux | 6:64d346936f0e | 370 | /*----ERROR---*/ |
GeorgeJourneaux | 2:28d12a3db239 | 371 | else{ |
GeorgeJourneaux | 6:64d346936f0e | 372 | pc.puts("####ERROR####\n\r"); |
GeorgeJourneaux | 2:28d12a3db239 | 373 | } |
GeorgeJourneaux | 6:64d346936f0e | 374 | /*----------------------------------------------*/ |
GeorgeJourneaux | 6:64d346936f0e | 375 | |
GeorgeJourneaux | 6:64d346936f0e | 376 | //Clear serial buffers |
GeorgeJourneaux | 5:ea3ec65cbf5f | 377 | memset(serial_buffer, NULL, 80); |
GeorgeJourneaux | 5:ea3ec65cbf5f | 378 | memset(rx_buffer, NULL, 32); |
GeorgeJourneaux | 2:28d12a3db239 | 379 | rx_in = 0; |
GeorgeJourneaux | 2:28d12a3db239 | 380 | |
GeorgeJourneaux | 6:64d346936f0e | 381 | //Attach serial interrupt |
GeorgeJourneaux | 2:28d12a3db239 | 382 | pc.attach(&Rx_interrupt, Serial::RxIrq); |
GeorgeJourneaux | 2:28d12a3db239 | 383 | } |
GeorgeJourneaux | 2:28d12a3db239 | 384 | } |
dnonoo | 7:f017a37bcf1b | 385 | /*------------------------------------------------*/ |
dnonoo | 7:f017a37bcf1b | 386 | |
dnonoo | 7:f017a37bcf1b | 387 | void POST () { |
dnonoo | 7:f017a37bcf1b | 388 | |
dnonoo | 7:f017a37bcf1b | 389 | pc.printf(" ALL Leds should be flashing\n\r"); |
dnonoo | 7:f017a37bcf1b | 390 | |
dnonoo | 7:f017a37bcf1b | 391 | for(unsigned int n = 0; n<10; n++) { |
dnonoo | 7:f017a37bcf1b | 392 | Green_int = ON; |
dnonoo | 7:f017a37bcf1b | 393 | Blue_int = ON; |
dnonoo | 7:f017a37bcf1b | 394 | Red_int = ON; |
dnonoo | 7:f017a37bcf1b | 395 | Green_ext = ON; |
dnonoo | 7:f017a37bcf1b | 396 | Yellow_ext = ON; |
dnonoo | 7:f017a37bcf1b | 397 | Red_ext = ON; |
dnonoo | 7:f017a37bcf1b | 398 | |
dnonoo | 7:f017a37bcf1b | 399 | wait (0.2); |
dnonoo | 7:f017a37bcf1b | 400 | Green_int = OFF; |
dnonoo | 7:f017a37bcf1b | 401 | Blue_int = OFF; |
dnonoo | 7:f017a37bcf1b | 402 | Red_int = OFF; |
dnonoo | 7:f017a37bcf1b | 403 | Green_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 404 | Yellow_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 405 | Red_ext = OFF; |
dnonoo | 7:f017a37bcf1b | 406 | wait (0.2); |
dnonoo | 7:f017a37bcf1b | 407 | } |
dnonoo | 7:f017a37bcf1b | 408 | |
dnonoo | 7:f017a37bcf1b | 409 | pc.printf("Switch states:\n\r"); |
dnonoo | 7:f017a37bcf1b | 410 | pc.printf("\tSW_L: %d\n\r\tSW_R %d\n\r", SW_L.read(), SW_R.read()); |
dnonoo | 7:f017a37bcf1b | 411 | pc.printf("\tSW_B: %d\n\r", SW_B.read()); |
dnonoo | 7:f017a37bcf1b | 412 | |
dnonoo | 7:f017a37bcf1b | 413 | float Temp = Sensor.getTemperature(); |
dnonoo | 7:f017a37bcf1b | 414 | float Pres = Sensor.getPressure(); |
dnonoo | 7:f017a37bcf1b | 415 | float ldrs = LDR_In.read(); |
dnonoo | 7:f017a37bcf1b | 416 | |
dnonoo | 7:f017a37bcf1b | 417 | pc.printf("Sensor test:\n\r"); |
dnonoo | 7:f017a37bcf1b | 418 | pc.printf("T: %f\tP: %f\tL: %f\n\r",Temp,Pres,ldrs); |
dnonoo | 7:f017a37bcf1b | 419 | |
dnonoo | 7:f017a37bcf1b | 420 | pc.printf("LCD Test\n\r"); |
dnonoo | 7:f017a37bcf1b | 421 | |
dnonoo | 7:f017a37bcf1b | 422 | lcd.Clear(); |
dnonoo | 7:f017a37bcf1b | 423 | lcd.RowSelect(1); |
dnonoo | 7:f017a37bcf1b | 424 | lcd.Write("*******LCD******"); |
dnonoo | 7:f017a37bcf1b | 425 | lcd.RowSelect(2); |
dnonoo | 7:f017a37bcf1b | 426 | lcd.Write("******TEST******"); |
dnonoo | 7:f017a37bcf1b | 427 | wait(1); |
dnonoo | 7:f017a37bcf1b | 428 | lcd.Clear(); |
dnonoo | 7:f017a37bcf1b | 429 | } |
dnonoo | 7:f017a37bcf1b | 430 | |
dnonoo | 7:f017a37bcf1b | 431 |