V4

Dependencies:   BMP280

Fork of Thread_Communication_V3 by BDG

Revision:
5:ea3ec65cbf5f
Parent:
4:93d6d13d4de3
Child:
6:64d346936f0e
--- a/main.cpp	Thu Dec 14 20:02:48 2017 +0000
+++ b/main.cpp	Wed Dec 20 21:28:52 2017 +0000
@@ -2,22 +2,30 @@
 #include "main.h"
 
 #define ENTER_KEY 1
+#define MAX_SAMPLES 120
 
 LCD  lcd(PD_15, PF_12, PF_13, PE_9, PF_14, PF_15); 
 BMP280 Sensor(D14, D15);
 
-
+//Define Functions
 void PrintLCD ();
 void Rx_interrupt();
 void Serial_CMD();
 
+//data FIFO buffer
+char data_buffer[MAX_SAMPLES][50];
+int data_h = 0;
+int data_t = 0;
+
+//Time-Date data
+struct tm * sample_epoch;
+char sample_time[20];
+
 //Serial_CMD
 volatile int rx_in=0;
-char rx_line[80];
-
-//Time date
+char rx_buffer[32];
 time_t raw_time = time(NULL);
-char time_buffer[80];
+char serial_buffer[80];
 
 /* Mail */
 typedef struct {
@@ -41,16 +49,48 @@
     pc.baud(9600);
     pc.attach(&Rx_interrupt, Serial::RxIrq);
     S_CMD.start(Serial_CMD);
-
+    
         while(1) {
             Green_int = !Green_int;
-
+            
+            //Read sensors, send to mail-queue
             mail_t *mail = mail_box.alloc();
             mail->LDR_Value = LDR_In.read(); 
             mail->temp_Value = Sensor.getTemperature();
             mail->press_Value = Sensor.getPressure();
-            mail_box.put(mail);        
-        
+            mail_box.put(mail);           
+            
+            //Format samples, send to FIFO buffer
+            memset(data_buffer[data_h],NULL,50);
+            time( &raw_time );
+            sample_epoch = localtime( &raw_time );
+            strftime(sample_time,20,"%d/%m/%Y %X",sample_epoch);
+            sprintf(data_buffer[data_h],"%s, %2.2f, %4.2f, %.4f\n\r", sample_time, mail->temp_Value, mail->press_Value, mail->LDR_Value);
+            memset(sample_time,NULL,20);
+            
+            //Print all samples to serial
+            for(int n = data_t; n<= MAX_SAMPLES; n++){
+                pc.puts(data_buffer[n]);
+            }
+            
+            if(data_t > data_h){
+                for(int n = 0; n<= data_h; n++){
+                    pc.puts(data_buffer[n]);
+                }
+            }
+            
+            //Prepare buffer for next samples
+            data_h++;
+            if(data_h >= (MAX_SAMPLES)){
+                data_h = 0;
+            }
+            if(data_h == data_t){
+                data_t++;
+                if(data_t >= (MAX_SAMPLES)){
+                    data_t = 0;
+                }
+            }
+            
             Thread::wait (15000);
         }
 }
@@ -64,13 +104,12 @@
         char lightString[16];
         char tempString[16];
         char pressString[16];
-        
 
         lcd.Clear();
         lcd.RowSelect(0);
          
         switch (i){
-            case 0: 
+            case 0:{
                 osEvent evt = mail_box.get();
         
                 if (evt.status == osEventMail) {
@@ -86,32 +125,25 @@
                 lcd.Write("Light Level:");
                 lcd.RowSelect(1);
                 lcd.Write(lightString);
-                i++;
-                
+                i++;                
             break;
-            
+            }
             case 1:
-
                 lcd.Write("Temperature:");
                 lcd.RowSelect(1);
                 lcd.Write(tempString);
                 i++;
-
             break;
         
             case 2:
-
                 lcd.Write("Pressure:");
                 lcd.RowSelect(1);
                 lcd.Write(pressString);
-                i =0;
-                
+                i =0;                
             break;
             
-            default:
-            
-                i = 0;
-                
+            default:            
+                i = 0;                
             break;
         }
         
@@ -127,10 +159,10 @@
 void Rx_interrupt() {
     
     while (pc.readable()) {
-        rx_line[rx_in] = pc.getc();
-        pc.putc(rx_line[rx_in]);
+        rx_buffer[rx_in] = pc.getc();
+        pc.putc(rx_buffer[rx_in]);
         
-        if(rx_line[rx_in] == 0xD){
+        if(rx_buffer[rx_in] == 0xD){
             S_CMD.signal_set(ENTER_KEY);
         }
         else{
@@ -149,76 +181,77 @@
         struct tm * s_time;
         char tm_n[4];
         
-        if(strstr(rx_line, "READ ALL")){
-            time ( &raw_time );
+        if(strstr(rx_buffer, "READ ALL")){
+            
+            /*time ( &raw_time );
             s_time = localtime ( &raw_time );
-            strftime(time_buffer, 80, "%d/%m/%Y, %X\n\r", s_time);
-            pc.puts(time_buffer);
+            strftime(serial_buffer, 80, "%d/%m/%Y, %X\n\r", time);
+            pc.puts(serial_buffer);*/
         }
-        else if(strstr(rx_line, "DELETE ALL")){
+        else if(strstr(rx_buffer, "DELETE ALL")){
             pc.printf("DELETE ALL\n\r");
         }
-        else if(strstr(rx_line, "READ")){
+        else if(strstr(rx_buffer, "READ")){
             pc.printf("READ\n\r");
         }
-        else if(strstr(rx_line, "DELETE")){
+        else if(strstr(rx_buffer, "DELETE")){
             pc.printf("DELETE\n\r");
         }
-        else if(strstr(rx_line, "SETDATE")){
-            time ( &raw_time );
-            s_time = localtime ( &raw_time );
+        else if(strstr(rx_buffer, "SETDATE")){
+            time(&raw_time);
+            s_time = localtime(&raw_time);
             
-            int dd = atoi(strncpy(tm_n,&rx_line[8],2));
+            int dd = atoi(strncpy(tm_n,&rx_buffer[8],2));
             s_time->tm_mday = dd;
             memset(tm_n, NULL, 4);
             
-            int mm = atoi(strncpy(tm_n,&rx_line[11],2));
+            int mm = atoi(strncpy(tm_n,&rx_buffer[11],2));
             s_time->tm_mon = mm-1;
             memset(tm_n, NULL, 4);
             
-            int yyyy = atoi(strncpy(tm_n,&rx_line[14],4));
+            int yyyy = atoi(strncpy(tm_n,&rx_buffer[14],4));
             s_time->tm_year = yyyy-1900;
             memset(tm_n, NULL, 4);
             
             set_time(mktime(s_time));
-            strftime(time_buffer, 80, "%d/%m/%Y\n\r", s_time);
-            pc.puts(time_buffer);
+            strftime(serial_buffer, 80, "%d/%m/%Y\n\r", s_time);
+            pc.puts(serial_buffer);
         }
-        else if(strstr(rx_line, "SETTIME")){
-            time ( &raw_time );
-            s_time = localtime ( &raw_time );
+        else if(strstr(rx_buffer, "SETTIME")){
+            time(&raw_time);
+            s_time = localtime(&raw_time);
             
-            int ss = atoi(strncpy(tm_n,&rx_line[14],2));
+            int ss = atoi(strncpy(tm_n,&rx_buffer[14],2));
             s_time->tm_sec = ss;
             memset(tm_n, NULL, 4);
             
-            int mm = atoi(strncpy(tm_n,&rx_line[11],2));
+            int mm = atoi(strncpy(tm_n,&rx_buffer[11],2));
             s_time->tm_min = mm;
             memset(tm_n, NULL, 4);
             
-            int hh = atoi(strncpy(tm_n,&rx_line[8],2));
+            int hh = atoi(strncpy(tm_n,&rx_buffer[8],2));
             s_time->tm_hour = hh;
             memset(tm_n, NULL, 4);
             
             set_time(mktime(s_time));
-            strftime(time_buffer, 80, "%X\n\r", s_time);
-            pc.puts(time_buffer);
+            strftime(serial_buffer, 80, "%X\n\r", s_time);
+            pc.puts(serial_buffer);
         }
-        else if(strstr(rx_line, "SETT")){
+        else if(strstr(rx_buffer, "SETT")){
             pc.printf("SETT\n\r");
         }
-        else if(strstr(rx_line, "STATE")){
+        else if(strstr(rx_buffer, "STATE")){
             pc.printf("STATE\n\r");
         }
-        else if(strstr(rx_line, "LOGGING")){
+        else if(strstr(rx_buffer, "LOGGING")){
             pc.printf("LOGGING\n\r");
         }
         else{
             pc.printf("ERROR\n\r");    
         }
         
-        memset(time_buffer, NULL, 80);
-        memset(rx_line, NULL, 80);
+        memset(serial_buffer, NULL, 80);
+        memset(rx_buffer, NULL, 32);
         rx_in = 0;
     
         pc.attach(&Rx_interrupt, Serial::RxIrq);