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 BDG

Committer:
GeorgeJourneaux
Date:
Thu Dec 14 19:23:45 2017 +0000
Revision:
3:73497379c0cb
Parent:
2:28d12a3db239
Child:
4:93d6d13d4de3
date & time update

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