V4

Dependencies:   BMP280

Fork of Thread_Communication_V3 by BDG

Committer:
GeorgeJourneaux
Date:
Wed Dec 20 21:28:52 2017 +0000
Revision:
5:ea3ec65cbf5f
Parent:
4:93d6d13d4de3
Child:
6:64d346936f0e
YAY! BUFFER!

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"
benparkes 0:cb3a5c15b01e 3
GeorgeJourneaux 4:93d6d13d4de3 4 #define ENTER_KEY 1
GeorgeJourneaux 5:ea3ec65cbf5f 5 #define MAX_SAMPLES 120
GeorgeJourneaux 4:93d6d13d4de3 6
GeorgeJourneaux 3:73497379c0cb 7 LCD lcd(PD_15, PF_12, PF_13, PE_9, PF_14, PF_15);
GeorgeJourneaux 3:73497379c0cb 8 BMP280 Sensor(D14, D15);
benparkes 0:cb3a5c15b01e 9
GeorgeJourneaux 5:ea3ec65cbf5f 10 //Define Functions
benparkes 0:cb3a5c15b01e 11 void PrintLCD ();
GeorgeJourneaux 2:28d12a3db239 12 void Rx_interrupt();
GeorgeJourneaux 2:28d12a3db239 13 void Serial_CMD();
GeorgeJourneaux 2:28d12a3db239 14
GeorgeJourneaux 5:ea3ec65cbf5f 15 //data FIFO buffer
GeorgeJourneaux 5:ea3ec65cbf5f 16 char data_buffer[MAX_SAMPLES][50];
GeorgeJourneaux 5:ea3ec65cbf5f 17 int data_h = 0;
GeorgeJourneaux 5:ea3ec65cbf5f 18 int data_t = 0;
GeorgeJourneaux 5:ea3ec65cbf5f 19
GeorgeJourneaux 5:ea3ec65cbf5f 20 //Time-Date data
GeorgeJourneaux 5:ea3ec65cbf5f 21 struct tm * sample_epoch;
GeorgeJourneaux 5:ea3ec65cbf5f 22 char sample_time[20];
GeorgeJourneaux 5:ea3ec65cbf5f 23
GeorgeJourneaux 3:73497379c0cb 24 //Serial_CMD
GeorgeJourneaux 2:28d12a3db239 25 volatile int rx_in=0;
GeorgeJourneaux 5:ea3ec65cbf5f 26 char rx_buffer[32];
GeorgeJourneaux 3:73497379c0cb 27 time_t raw_time = time(NULL);
GeorgeJourneaux 5:ea3ec65cbf5f 28 char serial_buffer[80];
GeorgeJourneaux 3:73497379c0cb 29
benparkes 1:bca9993a0df3 30 /* Mail */
benparkes 1:bca9993a0df3 31 typedef struct {
benparkes 1:bca9993a0df3 32 float LDR_Value;
benparkes 1:bca9993a0df3 33 float temp_Value;
benparkes 1:bca9993a0df3 34 float press_Value;
benparkes 1:bca9993a0df3 35 } mail_t;
benparkes 1:bca9993a0df3 36
benparkes 1:bca9993a0df3 37 Mail<mail_t, 16> mail_box;
benparkes 0:cb3a5c15b01e 38
GeorgeJourneaux 3:73497379c0cb 39 //Threads
benparkes 0:cb3a5c15b01e 40 Thread t1;
benparkes 0:cb3a5c15b01e 41 Thread t2;
GeorgeJourneaux 2:28d12a3db239 42 Thread S_CMD;
GeorgeJourneaux 3:73497379c0cb 43
GeorgeJourneaux 3:73497379c0cb 44 /*--------------------------------MAIN--------------------------------*/
GeorgeJourneaux 3:73497379c0cb 45 int main() {
benparkes 0:cb3a5c15b01e 46
GeorgeJourneaux 3:73497379c0cb 47 t1.start(PrintLCD);
GeorgeJourneaux 3:73497379c0cb 48
GeorgeJourneaux 3:73497379c0cb 49 pc.baud(9600);
GeorgeJourneaux 3:73497379c0cb 50 pc.attach(&Rx_interrupt, Serial::RxIrq);
GeorgeJourneaux 3:73497379c0cb 51 S_CMD.start(Serial_CMD);
GeorgeJourneaux 5:ea3ec65cbf5f 52
GeorgeJourneaux 3:73497379c0cb 53 while(1) {
GeorgeJourneaux 3:73497379c0cb 54 Green_int = !Green_int;
GeorgeJourneaux 5:ea3ec65cbf5f 55
GeorgeJourneaux 5:ea3ec65cbf5f 56 //Read sensors, send to mail-queue
GeorgeJourneaux 3:73497379c0cb 57 mail_t *mail = mail_box.alloc();
GeorgeJourneaux 3:73497379c0cb 58 mail->LDR_Value = LDR_In.read();
GeorgeJourneaux 3:73497379c0cb 59 mail->temp_Value = Sensor.getTemperature();
GeorgeJourneaux 3:73497379c0cb 60 mail->press_Value = Sensor.getPressure();
GeorgeJourneaux 5:ea3ec65cbf5f 61 mail_box.put(mail);
GeorgeJourneaux 5:ea3ec65cbf5f 62
GeorgeJourneaux 5:ea3ec65cbf5f 63 //Format samples, send to FIFO buffer
GeorgeJourneaux 5:ea3ec65cbf5f 64 memset(data_buffer[data_h],NULL,50);
GeorgeJourneaux 5:ea3ec65cbf5f 65 time( &raw_time );
GeorgeJourneaux 5:ea3ec65cbf5f 66 sample_epoch = localtime( &raw_time );
GeorgeJourneaux 5:ea3ec65cbf5f 67 strftime(sample_time,20,"%d/%m/%Y %X",sample_epoch);
GeorgeJourneaux 5:ea3ec65cbf5f 68 sprintf(data_buffer[data_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, mail->temp_Value, mail->press_Value, mail->LDR_Value);
GeorgeJourneaux 5:ea3ec65cbf5f 69 memset(sample_time,NULL,20);
GeorgeJourneaux 5:ea3ec65cbf5f 70
GeorgeJourneaux 5:ea3ec65cbf5f 71 //Print all samples to serial
GeorgeJourneaux 5:ea3ec65cbf5f 72 for(int n = data_t; n<= MAX_SAMPLES; n++){
GeorgeJourneaux 5:ea3ec65cbf5f 73 pc.puts(data_buffer[n]);
GeorgeJourneaux 5:ea3ec65cbf5f 74 }
GeorgeJourneaux 5:ea3ec65cbf5f 75
GeorgeJourneaux 5:ea3ec65cbf5f 76 if(data_t > data_h){
GeorgeJourneaux 5:ea3ec65cbf5f 77 for(int n = 0; n<= data_h; n++){
GeorgeJourneaux 5:ea3ec65cbf5f 78 pc.puts(data_buffer[n]);
GeorgeJourneaux 5:ea3ec65cbf5f 79 }
GeorgeJourneaux 5:ea3ec65cbf5f 80 }
GeorgeJourneaux 5:ea3ec65cbf5f 81
GeorgeJourneaux 5:ea3ec65cbf5f 82 //Prepare buffer for next samples
GeorgeJourneaux 5:ea3ec65cbf5f 83 data_h++;
GeorgeJourneaux 5:ea3ec65cbf5f 84 if(data_h >= (MAX_SAMPLES)){
GeorgeJourneaux 5:ea3ec65cbf5f 85 data_h = 0;
GeorgeJourneaux 5:ea3ec65cbf5f 86 }
GeorgeJourneaux 5:ea3ec65cbf5f 87 if(data_h == data_t){
GeorgeJourneaux 5:ea3ec65cbf5f 88 data_t++;
GeorgeJourneaux 5:ea3ec65cbf5f 89 if(data_t >= (MAX_SAMPLES)){
GeorgeJourneaux 5:ea3ec65cbf5f 90 data_t = 0;
GeorgeJourneaux 5:ea3ec65cbf5f 91 }
GeorgeJourneaux 5:ea3ec65cbf5f 92 }
GeorgeJourneaux 5:ea3ec65cbf5f 93
GeorgeJourneaux 3:73497379c0cb 94 Thread::wait (15000);
GeorgeJourneaux 3:73497379c0cb 95 }
GeorgeJourneaux 3:73497379c0cb 96 }
GeorgeJourneaux 3:73497379c0cb 97 /*--------------------------------------------------------------------*/
GeorgeJourneaux 3:73497379c0cb 98
GeorgeJourneaux 3:73497379c0cb 99 /*--------------------------------LCD---------------------------------*/
benparkes 0:cb3a5c15b01e 100 void PrintLCD () {
benparkes 0:cb3a5c15b01e 101
benparkes 0:cb3a5c15b01e 102 int i = 0;
benparkes 0:cb3a5c15b01e 103 while(1){
benparkes 1:bca9993a0df3 104 char lightString[16];
benparkes 1:bca9993a0df3 105 char tempString[16];
benparkes 1:bca9993a0df3 106 char pressString[16];
benparkes 1:bca9993a0df3 107
benparkes 0:cb3a5c15b01e 108 lcd.Clear();
benparkes 0:cb3a5c15b01e 109 lcd.RowSelect(0);
benparkes 1:bca9993a0df3 110
benparkes 0:cb3a5c15b01e 111 switch (i){
GeorgeJourneaux 5:ea3ec65cbf5f 112 case 0:{
GeorgeJourneaux 2:28d12a3db239 113 osEvent evt = mail_box.get();
benparkes 1:bca9993a0df3 114
GeorgeJourneaux 2:28d12a3db239 115 if (evt.status == osEventMail) {
GeorgeJourneaux 2:28d12a3db239 116 mail_t *mail = (mail_t*)evt.value.p;
benparkes 0:cb3a5c15b01e 117
GeorgeJourneaux 2:28d12a3db239 118 sprintf(lightString,"%.4f", mail->LDR_Value);
GeorgeJourneaux 2:28d12a3db239 119 sprintf(tempString,"%2.2f", mail->temp_Value);
GeorgeJourneaux 2:28d12a3db239 120 sprintf(pressString,"%4.2f", mail->press_Value);
benparkes 1:bca9993a0df3 121
GeorgeJourneaux 2:28d12a3db239 122 mail_box.free(mail);
GeorgeJourneaux 2:28d12a3db239 123 }
benparkes 1:bca9993a0df3 124
GeorgeJourneaux 2:28d12a3db239 125 lcd.Write("Light Level:");
GeorgeJourneaux 2:28d12a3db239 126 lcd.RowSelect(1);
GeorgeJourneaux 2:28d12a3db239 127 lcd.Write(lightString);
GeorgeJourneaux 5:ea3ec65cbf5f 128 i++;
benparkes 0:cb3a5c15b01e 129 break;
GeorgeJourneaux 5:ea3ec65cbf5f 130 }
GeorgeJourneaux 2:28d12a3db239 131 case 1:
GeorgeJourneaux 2:28d12a3db239 132 lcd.Write("Temperature:");
GeorgeJourneaux 2:28d12a3db239 133 lcd.RowSelect(1);
GeorgeJourneaux 2:28d12a3db239 134 lcd.Write(tempString);
GeorgeJourneaux 2:28d12a3db239 135 i++;
benparkes 0:cb3a5c15b01e 136 break;
benparkes 0:cb3a5c15b01e 137
GeorgeJourneaux 2:28d12a3db239 138 case 2:
GeorgeJourneaux 2:28d12a3db239 139 lcd.Write("Pressure:");
GeorgeJourneaux 2:28d12a3db239 140 lcd.RowSelect(1);
GeorgeJourneaux 2:28d12a3db239 141 lcd.Write(pressString);
GeorgeJourneaux 5:ea3ec65cbf5f 142 i =0;
benparkes 0:cb3a5c15b01e 143 break;
GeorgeJourneaux 2:28d12a3db239 144
GeorgeJourneaux 5:ea3ec65cbf5f 145 default:
GeorgeJourneaux 5:ea3ec65cbf5f 146 i = 0;
benparkes 0:cb3a5c15b01e 147 break;
benparkes 0:cb3a5c15b01e 148 }
benparkes 0:cb3a5c15b01e 149
GeorgeJourneaux 2:28d12a3db239 150 Red_int = !Red_int;
benparkes 1:bca9993a0df3 151
benparkes 0:cb3a5c15b01e 152 Thread::wait (5000);
GeorgeJourneaux 2:28d12a3db239 153 }
benparkes 0:cb3a5c15b01e 154 }
GeorgeJourneaux 3:73497379c0cb 155 /*--------------------------------------------------------------------*/
benparkes 1:bca9993a0df3 156
GeorgeJourneaux 3:73497379c0cb 157 /*------------------------------SERIAL_CMD----------------------------*/
GeorgeJourneaux 4:93d6d13d4de3 158 //Interrupt when recieving from serial port
GeorgeJourneaux 2:28d12a3db239 159 void Rx_interrupt() {
GeorgeJourneaux 2:28d12a3db239 160
GeorgeJourneaux 2:28d12a3db239 161 while (pc.readable()) {
GeorgeJourneaux 5:ea3ec65cbf5f 162 rx_buffer[rx_in] = pc.getc();
GeorgeJourneaux 5:ea3ec65cbf5f 163 pc.putc(rx_buffer[rx_in]);
GeorgeJourneaux 2:28d12a3db239 164
GeorgeJourneaux 5:ea3ec65cbf5f 165 if(rx_buffer[rx_in] == 0xD){
GeorgeJourneaux 4:93d6d13d4de3 166 S_CMD.signal_set(ENTER_KEY);
GeorgeJourneaux 2:28d12a3db239 167 }
GeorgeJourneaux 2:28d12a3db239 168 else{
GeorgeJourneaux 2:28d12a3db239 169 rx_in = (rx_in + 1);
GeorgeJourneaux 2:28d12a3db239 170 }
GeorgeJourneaux 2:28d12a3db239 171 }
GeorgeJourneaux 2:28d12a3db239 172 }
benparkes 0:cb3a5c15b01e 173
GeorgeJourneaux 4:93d6d13d4de3 174 //Check what command what recieved and execute
GeorgeJourneaux 2:28d12a3db239 175 void Serial_CMD(){
GeorgeJourneaux 2:28d12a3db239 176
GeorgeJourneaux 2:28d12a3db239 177 while(1){
GeorgeJourneaux 4:93d6d13d4de3 178 Thread::signal_wait(ENTER_KEY);
GeorgeJourneaux 2:28d12a3db239 179 pc.attach(NULL, Serial::RxIrq);
GeorgeJourneaux 3:73497379c0cb 180
GeorgeJourneaux 3:73497379c0cb 181 struct tm * s_time;
GeorgeJourneaux 3:73497379c0cb 182 char tm_n[4];
GeorgeJourneaux 3:73497379c0cb 183
GeorgeJourneaux 5:ea3ec65cbf5f 184 if(strstr(rx_buffer, "READ ALL")){
GeorgeJourneaux 5:ea3ec65cbf5f 185
GeorgeJourneaux 5:ea3ec65cbf5f 186 /*time ( &raw_time );
GeorgeJourneaux 3:73497379c0cb 187 s_time = localtime ( &raw_time );
GeorgeJourneaux 5:ea3ec65cbf5f 188 strftime(serial_buffer, 80, "%d/%m/%Y, %X\n\r", time);
GeorgeJourneaux 5:ea3ec65cbf5f 189 pc.puts(serial_buffer);*/
GeorgeJourneaux 2:28d12a3db239 190 }
GeorgeJourneaux 5:ea3ec65cbf5f 191 else if(strstr(rx_buffer, "DELETE ALL")){
GeorgeJourneaux 2:28d12a3db239 192 pc.printf("DELETE ALL\n\r");
GeorgeJourneaux 2:28d12a3db239 193 }
GeorgeJourneaux 5:ea3ec65cbf5f 194 else if(strstr(rx_buffer, "READ")){
GeorgeJourneaux 2:28d12a3db239 195 pc.printf("READ\n\r");
GeorgeJourneaux 2:28d12a3db239 196 }
GeorgeJourneaux 5:ea3ec65cbf5f 197 else if(strstr(rx_buffer, "DELETE")){
GeorgeJourneaux 3:73497379c0cb 198 pc.printf("DELETE\n\r");
GeorgeJourneaux 3:73497379c0cb 199 }
GeorgeJourneaux 5:ea3ec65cbf5f 200 else if(strstr(rx_buffer, "SETDATE")){
GeorgeJourneaux 5:ea3ec65cbf5f 201 time(&raw_time);
GeorgeJourneaux 5:ea3ec65cbf5f 202 s_time = localtime(&raw_time);
GeorgeJourneaux 3:73497379c0cb 203
GeorgeJourneaux 5:ea3ec65cbf5f 204 int dd = atoi(strncpy(tm_n,&rx_buffer[8],2));
GeorgeJourneaux 3:73497379c0cb 205 s_time->tm_mday = dd;
GeorgeJourneaux 3:73497379c0cb 206 memset(tm_n, NULL, 4);
GeorgeJourneaux 3:73497379c0cb 207
GeorgeJourneaux 5:ea3ec65cbf5f 208 int mm = atoi(strncpy(tm_n,&rx_buffer[11],2));
GeorgeJourneaux 3:73497379c0cb 209 s_time->tm_mon = mm-1;
GeorgeJourneaux 3:73497379c0cb 210 memset(tm_n, NULL, 4);
GeorgeJourneaux 3:73497379c0cb 211
GeorgeJourneaux 5:ea3ec65cbf5f 212 int yyyy = atoi(strncpy(tm_n,&rx_buffer[14],4));
GeorgeJourneaux 3:73497379c0cb 213 s_time->tm_year = yyyy-1900;
GeorgeJourneaux 3:73497379c0cb 214 memset(tm_n, NULL, 4);
GeorgeJourneaux 3:73497379c0cb 215
GeorgeJourneaux 3:73497379c0cb 216 set_time(mktime(s_time));
GeorgeJourneaux 5:ea3ec65cbf5f 217 strftime(serial_buffer, 80, "%d/%m/%Y\n\r", s_time);
GeorgeJourneaux 5:ea3ec65cbf5f 218 pc.puts(serial_buffer);
GeorgeJourneaux 2:28d12a3db239 219 }
GeorgeJourneaux 5:ea3ec65cbf5f 220 else if(strstr(rx_buffer, "SETTIME")){
GeorgeJourneaux 5:ea3ec65cbf5f 221 time(&raw_time);
GeorgeJourneaux 5:ea3ec65cbf5f 222 s_time = localtime(&raw_time);
GeorgeJourneaux 3:73497379c0cb 223
GeorgeJourneaux 5:ea3ec65cbf5f 224 int ss = atoi(strncpy(tm_n,&rx_buffer[14],2));
GeorgeJourneaux 3:73497379c0cb 225 s_time->tm_sec = ss;
GeorgeJourneaux 3:73497379c0cb 226 memset(tm_n, NULL, 4);
GeorgeJourneaux 3:73497379c0cb 227
GeorgeJourneaux 5:ea3ec65cbf5f 228 int mm = atoi(strncpy(tm_n,&rx_buffer[11],2));
GeorgeJourneaux 3:73497379c0cb 229 s_time->tm_min = mm;
GeorgeJourneaux 3:73497379c0cb 230 memset(tm_n, NULL, 4);
GeorgeJourneaux 3:73497379c0cb 231
GeorgeJourneaux 5:ea3ec65cbf5f 232 int hh = atoi(strncpy(tm_n,&rx_buffer[8],2));
GeorgeJourneaux 3:73497379c0cb 233 s_time->tm_hour = hh;
GeorgeJourneaux 3:73497379c0cb 234 memset(tm_n, NULL, 4);
GeorgeJourneaux 3:73497379c0cb 235
GeorgeJourneaux 3:73497379c0cb 236 set_time(mktime(s_time));
GeorgeJourneaux 5:ea3ec65cbf5f 237 strftime(serial_buffer, 80, "%X\n\r", s_time);
GeorgeJourneaux 5:ea3ec65cbf5f 238 pc.puts(serial_buffer);
GeorgeJourneaux 2:28d12a3db239 239 }
GeorgeJourneaux 5:ea3ec65cbf5f 240 else if(strstr(rx_buffer, "SETT")){
GeorgeJourneaux 2:28d12a3db239 241 pc.printf("SETT\n\r");
GeorgeJourneaux 2:28d12a3db239 242 }
GeorgeJourneaux 5:ea3ec65cbf5f 243 else if(strstr(rx_buffer, "STATE")){
GeorgeJourneaux 2:28d12a3db239 244 pc.printf("STATE\n\r");
GeorgeJourneaux 2:28d12a3db239 245 }
GeorgeJourneaux 5:ea3ec65cbf5f 246 else if(strstr(rx_buffer, "LOGGING")){
GeorgeJourneaux 2:28d12a3db239 247 pc.printf("LOGGING\n\r");
GeorgeJourneaux 2:28d12a3db239 248 }
GeorgeJourneaux 2:28d12a3db239 249 else{
GeorgeJourneaux 2:28d12a3db239 250 pc.printf("ERROR\n\r");
GeorgeJourneaux 2:28d12a3db239 251 }
GeorgeJourneaux 3:73497379c0cb 252
GeorgeJourneaux 5:ea3ec65cbf5f 253 memset(serial_buffer, NULL, 80);
GeorgeJourneaux 5:ea3ec65cbf5f 254 memset(rx_buffer, NULL, 32);
GeorgeJourneaux 2:28d12a3db239 255 rx_in = 0;
GeorgeJourneaux 2:28d12a3db239 256
GeorgeJourneaux 2:28d12a3db239 257 pc.attach(&Rx_interrupt, Serial::RxIrq);
GeorgeJourneaux 2:28d12a3db239 258 }
GeorgeJourneaux 2:28d12a3db239 259 }
GeorgeJourneaux 3:73497379c0cb 260 /*--------------------------------------------------------------------*/