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

Revision:
2:28d12a3db239
Parent:
1:bca9993a0df3
Child:
3:73497379c0cb
--- a/main.cpp	Wed Dec 13 17:20:18 2017 +0000
+++ b/main.cpp	Thu Dec 14 15:44:49 2017 +0000
@@ -8,6 +8,12 @@
 
 BMP280 Sensor(D14, D15);
 void PrintLCD ();
+void Rx_interrupt();
+void Serial_CMD();
+
+volatile int rx_in=0;
+char rx_line[80];
+int s_cmd = 0;
 
 /* Mail */
 typedef struct {
@@ -16,28 +22,12 @@
     float press_Value;
 } mail_t;
 
-//Class type
-/*class mail_t {
-public:    
-    float LDR_Value;
-    float temp_Value;
-    float press_Value;
-    
-    //Constructor
-    mail_t( float LDR_Value, float temp_Value, int s2) {
-        adcValue = f;
-        sw1State = s1;
-        sw2State = s2;    
-    }
-};*/
-
 Mail<mail_t, 16> mail_box;
 
 
 Thread t1;
 Thread t2;
-
-//Mutex door;
+Thread S_CMD;
 
 void PrintLCD () {
     
@@ -52,57 +42,65 @@
         lcd.RowSelect(0);
          
         switch (i){
-        case 0: 
-            osEvent evt = mail_box.get();
+            case 0: 
+                osEvent evt = mail_box.get();
         
-            if (evt.status == osEventMail) {
-                mail_t *mail = (mail_t*)evt.value.p;
+                if (evt.status == osEventMail) {
+                    mail_t *mail = (mail_t*)evt.value.p;
             
-                sprintf(lightString,"%.4f", mail->LDR_Value);
-                sprintf(tempString,"%2.2f", mail->temp_Value);
-                sprintf(pressString,"%4.2f", mail->press_Value);    
+                    sprintf(lightString,"%.4f", mail->LDR_Value);
+                    sprintf(tempString,"%2.2f", mail->temp_Value);
+                    sprintf(pressString,"%4.2f", mail->press_Value);    
                 
-                mail_box.free(mail);
-            }
+                    mail_box.free(mail);
+                }
                
-            lcd.Write("Light Level:");
-            lcd.RowSelect(1);
-            lcd.Write(lightString);
-            i++;
-            
+                lcd.Write("Light Level:");
+                lcd.RowSelect(1);
+                lcd.Write(lightString);
+                i++;
+                
             break;
-        case 1:
+            
+            case 1:
 
-            lcd.Write("Temperature:");
-            lcd.RowSelect(1);
-            lcd.Write(tempString);
-            i++;
+                lcd.Write("Temperature:");
+                lcd.RowSelect(1);
+                lcd.Write(tempString);
+                i++;
 
             break;
         
-        case 2:
+            case 2:
 
-            lcd.Write("Pressure:");
-            lcd.RowSelect(1);
-            lcd.Write(pressString);
-            i =0;
+                lcd.Write("Pressure:");
+                lcd.RowSelect(1);
+                lcd.Write(pressString);
+                i =0;
+                
             break;
-        default:
-            i = 0;
+            
+            default:
+            
+                i = 0;
+                
             break;
         }
-        Red_int = !Red_int;
         
-        
+        Red_int = !Red_int;        
 
         Thread::wait (5000);
-        }
+    }
 }
     
 
 int main() {
 
     t1.start(PrintLCD);
+    
+    pc.baud(9600);
+    pc.attach(&Rx_interrupt, Serial::RxIrq);
+    S_CMD.start(Serial_CMD);
 
         while(1) {
             Green_int = !Green_int;
@@ -111,33 +109,68 @@
             mail->LDR_Value = LDR_In.read(); 
             mail->temp_Value = Sensor.getTemperature();
             mail->press_Value = Sensor.getPressure();
-            mail_box.put(mail);
-        
-        /*door.lock();
-       
-        LDR_Value = LDR_In.read();
-        temp_Value = Sensor.getTemperature();
-        press_Value = Sensor.getPressure();
-        door.unlock();*/
-        
+            mail_box.put(mail);        
         
             Thread::wait (15000);
         }
 
 }
 
-/*int main(void){
+void Rx_interrupt() {
+    
+    while (pc.readable()) {
+        rx_line[rx_in] = pc.getc();
+        pc.putc(rx_line[rx_in]);
+        
+        if(rx_line[rx_in] == 0xD){
+            s_cmd = 1;
+        }
+        else{
+            rx_in = (rx_in + 1);
+        }
+    }
+}
 
-    float temp = 0;
-    //float pressure = 0;
-    char tempString[16];
-    Sensor->initialize();
-    temp = Sensor->getTemperature();
-    lcd->Clear();
-    lcd->RowSelect(0);
-    lcd->Write("Temperature:");
-    sprintf(tempString,"%f",temp);
-    lcd->RowSelect(1);
-    lcd->Write(tempString);
+void Serial_CMD(){
+    
+    while(1){
+    if(s_cmd == 1){
+        pc.attach(NULL, Serial::RxIrq);
     
-    }*/
\ No newline at end of file
+        if(strstr(rx_line, "READ ALL")){
+            pc.printf("READ ALL\n\r");
+        }
+        else if(strstr(rx_line, "DELETE ALL")){
+            pc.printf("DELETE ALL\n\r");
+        }
+        else if(strstr(rx_line, "READ")){
+            pc.printf("READ\n\r");
+        }
+        else if(strstr(rx_line, "DELETE")){
+            pc.printf("SETDATE\n\r");
+        }
+        else if(strstr(rx_line, "SETTIME")){
+            pc.printf("SETTIME\n\r");
+        }
+        else if(strstr(rx_line, "SETT")){
+            pc.printf("SETT\n\r");
+        }
+        else if(strstr(rx_line, "STATE")){
+            pc.printf("STATE\n\r");
+        }
+        else if(strstr(rx_line, "LOGGING")){
+            pc.printf("LOGGING\n\r");
+        }
+        else{
+            pc.printf("ERROR\n\r");    
+        }
+    
+        memset(rx_line, NULL, 80);
+        rx_in = 0;
+    
+        pc.attach(&Rx_interrupt, Serial::RxIrq);
+        s_cmd = 0;
+    }
+    Thread::wait(5);
+    }
+}