V4

Dependencies:   BMP280

Fork of Thread_Communication_V3 by BDG

Committer:
GeorgeJourneaux
Date:
Thu Dec 14 20:02:48 2017 +0000
Revision:
4:93d6d13d4de3
Parent:
3:73497379c0cb
Child:
5:ea3ec65cbf5f
Thread fix

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