''

Dependencies:   BME280 BMP280 TextLCD mbed

Fork of CW_watchdog_08012018a by Calvin Kalintra

Files at this revision

API Documentation at this revision

Comitter:
ckalintra
Date:
Tue Jan 09 15:45:56 2018 +0000
Parent:
4:5dbb5145d0a9
Commit message:
s

Changed in this revision

data.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
putty.h Show annotated file Show diff for this revision Revisions of this file
sd-driver.lib Show annotated file Show diff for this revision Revisions of this file
sd.h Show annotated file Show diff for this revision Revisions of this file
--- a/data.h	Tue Jan 09 12:11:46 2018 +0000
+++ b/data.h	Tue Jan 09 15:45:56 2018 +0000
@@ -4,12 +4,12 @@
 float tempf;
 float pressuref;
 float lvl;
-float data_temp [119], data_press[119], data_light[119];
-int date_a[199], time_a[199], time0_a[199], date0_a[199];
+float data_temp [119], data_press[119], data_light[119],data_tempa[999], data_pressa[999], data_lighta[999];
+int date_a[199], time_a[199], time0_a[199], date0_a[199],date_aa[999], time_aa[999], time0_aa[9999], date0_aa[999];
 BMP280 bmp(D14,D15);
 
 AnalogIn LDR(A0);
-int counterw = 0, full = 0, day, month, year, leap;
+int counterw = 0, full = 0, day, month, year, leap, countera;
 
 void time_rule()
 {
@@ -269,15 +269,23 @@
      data_press[counterw] = pressuref;
      data_light[counterw] = lvl;
      date_a[counterw] = dd*1000000+m*100000+mm*10000+y*1000+yy*100+yyy*10+yyyy;
-     time_a[counterw] = ss*10000+mi*1000+mmi*100+h*10+hh;
+     time_a[counterw] = hh*10000+mi*1000+mmi*100+s*10+ss;
      date0_a[counterw] = d;
-     time0_a[counterw] = s;
+     time0_a[counterw] = h;
+     data_tempa[countera] = tempf;
+     data_pressa[countera] = pressuref;
+     data_lighta[countera] = lvl;
+     date_aa[countera] = dd*1000000+m*100000+mm*10000+y*1000+yy*100+yyy*10+yyyy;
+     time_aa[countera] = hh*10000+mi*1000+mmi*100+s*10+ss;
+     date0_aa[countera] = d;
+     time0_aa[countera] = h;
      if (counterw == 119)
      {
          counterw = 0;
          full = 1;
      }
      counterw++;
+     countera++;
 }
 
 
--- a/main.cpp	Tue Jan 09 12:11:46 2018 +0000
+++ b/main.cpp	Tue Jan 09 15:45:56 2018 +0000
@@ -1,15 +1,16 @@
 #include "mbed.h"
 #include "TextLCD.h"
 #include "BME280.h"
-#include "putty.h"
+#include "sd.h"
 #define watchdog_time 15
 //DigitalOut myled(LED1);
 int dislcd = 0, wdcounter = 0, watchdog = 0;
 float T = 15;
+char ch = 0;
 Ticker display;
 Ticker second_pass;
 InterruptIn button(USER_BUTTON);
-DigitalOut led(PB_11), led2(PB_10);
+
 //NVIC_SetPriority(display, 0);     
 //NVIC_SetPriority(second_pass, 1);
 //NVIC_SetPriority(button, 1);
@@ -31,8 +32,8 @@
         lcd.printf("l:%1.2f", lvl);
         lcd.locate(0,0);
         lcd.printf("p:%5.1f", pressuref);
-        lcd.locate(8,1);
-        lcd.printf("t:%4.1f", tempf);
+        lcd.locate(10,1);
+        lcd.printf("t:%3.1f", tempf);
         display_time(); 
     }
     else if (dislcd == 0)
@@ -42,26 +43,32 @@
     }
    if (wdcounter >= watchdog_time)
    {
+       pc.printf("system stuck, reset\n\r");
        NVIC_SystemReset();
    }
 }
-
+void watchdog()
+{
+    wdcounter = 0;
+    watchdog = 1;
+}
 void lcddisp()
 { 
+    
     if (gather_data == 1)
     {
-        led2 = 1;
+        redLED = 1;
         display_time();
         getdata();
         store();
-        led2 = 0;
+        redLED = 0;
     }
     if (gather_data ==0)
     {
-        led = 1;
+        redLED = 1;
         wait(0.2);
-        led = 0;
     }
+    
 }
 
 void check_display()
@@ -90,7 +97,24 @@
     while(1)
     {
         check_display();
-        putty_write();
+        if (putty_enabled == 1)
+        {
+            //watchdog();
+            putty_write();
+            watchdog = 0;
+        }
+        
+        if (putty_enabled == 0)
+        {
+            while(ch != 'p')
+            {
+                ch = 0;
+                yellowLED = 1;
+                ch = pc.getc();
+            }
+            putty_enabled = 1;
+        }
+        yellowLED = 0;
         check_display();
         if (interrupt == 1)
         {
@@ -98,6 +122,10 @@
             display.attach(&lcddisp, T);
             interrupt = 0;
         }
+        //if(sw1 == 1 && sw2 == 1)
+//        {
+//            sd();
+//        }
     }
 }
     
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Tue Jan 09 15:45:56 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#34b61d1612aaec2e5d61a8a43c9764bc2fe8aac0
--- a/putty.h	Tue Jan 09 12:11:46 2018 +0000
+++ b/putty.h	Tue Jan 09 15:45:56 2018 +0000
@@ -3,7 +3,7 @@
 Serial pc(USBTX, USBRX); 
 char user_command[40] = {0};
 char user_command2[40] = {0};
-int n, datan = 0, datac = 0, counterd = 0, dates, times, interrupt = 0, gather_data = 1;
+int n, datan = 0, datac = 0, counterd = 0, dates, times, interrupt = 0, gather_data = 1, putty_enabled = 1;
 float t;
 
 
@@ -22,7 +22,7 @@
     pc.printf("printting %i datas\n\r", datac);
     while (counterd < datac)  
         { 
-            pc.printf("%i%i:%i%i:%i%i, %i%i/%i%i/%i%i%i%i, light: %2.4f, temp: %3.4f, press: %3.4f\n\r", date0_a[datan-1] ,date_a[datan-1], time0_a[datan-1], time_a[datan-1],data_light[datan-1], data_temp [datan-1], data_press[datan-1]);  
+            pc.printf("%i%i, %i%i, light: %2.4f, temp: %3.4f, press: %3.4f\n\r", date0_a[datan-1] ,date_a[datan-1], time0_a[datan-1], time_a[datan-1],data_light[datan-1], data_temp [datan-1], data_press[datan-1]);  
             if (datan == 0)
                 {
                     datan = 119;
@@ -47,7 +47,7 @@
         {
             while (counterd < n)  
                 { 
-                    pc.printf("%i%i:%i%i:%i%i, %i%i/%i%i/%i%i%i%i,, light: %2.4f, temp: %3.4f, press: %3.4f\n\r", date0_a[datan-1] ,date_a[datan-1], time0_a[datan-1], time_a[datan-1], data_light[datan], data_temp [datan], data_press[datan]);  
+                    pc.printf("%i%i, %i%i, light: %2.4f, temp: %3.4f, press: %3.4f\n\r", date0_a[datan-1] ,date_a[datan-1], time0_a[datan-1], time_a[datan-1], data_light[datan-1], data_temp [datan-1], data_press[datan-1]);  
                     if (datan == 0)
                         {
                             datan = 121;
@@ -239,7 +239,7 @@
             pc.printf("out of range\n\r");
         }
     }
-     else if (user_command[0] == 'S'&& user_command[1] == 'T'&& user_command[2] == 'A'&& user_command[3] == 'T'&& user_command[4] == 'E')
+    else if (user_command[0] == 'S'&& user_command[1] == 'T'&& user_command[2] == 'A'&& user_command[3] == 'T'&& user_command[4] == 'E')
     {
         if (user_command2[0] == 'O'&& user_command2[1] == 'N')
         {
@@ -252,6 +252,19 @@
             pc.printf("data is off\n\r");
         }
     }
+    else if (user_command[0] == 'L'&& user_command[1] == 'O'&& user_command[2] == 'G'&& user_command[3] == 'G'&& user_command[4] == 'I'&& user_command[5] == 'N'&& user_command[6] == 'G')
+    {
+        if (user_command2[0] == 'O'&& user_command2[1] == 'N')
+        {
+            putty_enabled = 1;
+            pc.printf("logging on\n\r");
+        }
+        else if (user_command2[0] == 'O'&& user_command2[1] == 'F'&& user_command2[2] == 'F')
+        {
+            putty_enabled = 0;
+            pc.printf("logging off\n\r");
+        }
+    }
 }
 //int main()
 //{
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sd-driver.lib	Tue Jan 09 15:45:56 2018 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/sd-driver/#ae7e7440054c9447f8255bdccbcc523b3f6dffe4
--- a/sd.h	Tue Jan 09 12:11:46 2018 +0000
+++ b/sd.h	Tue Jan 09 15:45:56 2018 +0000
@@ -1,73 +1,58 @@
  #include "mbed.h"
  #include "SDBlockDevice.h"
  #include "FATFileSystem.h"
- //#include "sample_hardware.hpp"
+ #include "putty.h"
  #include "mbed_events.h"
  
-
+DigitalOut redLED(PB_11), yellowLED(PB_10), greenLED(PE_15);
 SDBlockDevice sd(D11, D12, D13, D10); 
 EventQueue eq;
 FILE* fp;
 bool ejected = false; 
 void takeSample()
 {
-    if (ejected == true) {
-        return;    
-    }
-    float temp     = sensor.getTemperature();
-    float pressure = sensor.getPressure();
-    
-    fprintf(fp, "%f,%f\n", temp, pressure);
+    int counterb = 0;
+    while (counterb <= countera) 
+    {
+        if (ejected == true) 
+        {
+            return;    
+        }   
+        float temp_sd = data_tempa[counterb];
+        float pressure_sd = data_pressa[counterb];
+        float light_sd = data_lighta[counterb];
+        float date_a_sd = date_aa[counterb] ; 
+        float time_a_sd = time_aa [counterb];
+        float date0_a_sd = date0_aa[counterb];
+        float time0_a_sd = time0_aa[counterb];
+        fprintf(fp, "%i%i, %i%i, temp:%f, pressure: %f, light lvl:%f\n\r", time0_a, time_a_sd, date0_a_sd, date_a_sd, temp_sd,pressure_sd,light_sd);
+        redLED = 1;
+        Thread::wait(100);
+        redLED = 0;
     
-    redLED = 1;
-    Thread::wait(100);
-    redLED = 0;
-    
-    if ((SW1 == 1) && (SW2 == 1)) {
-        //Close File
-        fclose(fp);
-        
-        sd.deinit();
-        
-        puts("You can now remove the SD Card\n");
-        yellowLED = 1;
-        
-        ejected = true;
+        if ((sw1 == 1) && (sw2 == 1)) 
+        {
+            fclose(fp);
+            sd.deinit();
+            yellowLED = 1;
+            ejected = true;
+        }
     }
 }
  
- int main()
-{
-    //POWER ON SELF TEST
-    post();
-    
-    printf("Initialise\n");
-    //FileSystemLike(*sd);
-
-    // call the SDBlockDevice instance initialisation method.
-    if ( sd.init() != 0) {
-        printf("Init failed \n");
-        errorCode(FATAL);
-    }    
-    
-    //Create a filing system for SD Card
-    FATFileSystem fs("sd", &sd);
-    
-    // **************
-    // Open to APPEND
-    // **************
-    fp = fopen("/sd/test.txt","a");
-    
-    //Check file handle (stream)
-    if (fp == NULL) {
-        error("Could not open file for write\n");
-        errorCode(FATAL);
-    }
-    
-    //Set up tasks on the main thread
-    queue.call_every(5000, takeSample);
-    
-    //Main queue event loop
-    queue.dispatch();
-
-}
\ No newline at end of file
+//void sd()
+//{
+//    if (sd.init() != 0) {
+//        printf("Init failed \n");
+//        errorCode(FATAL);
+//    }    
+//    FATFileSystem fs("sd", &sd);
+//    fp = fopen("/sd/test.txt","a");
+//    if (fp == NULL) {
+//        error("Could not open file for write\n");
+//        errorCode(FATAL);
+//    }
+//    takeSample();
+//   
+//
+//}
\ No newline at end of file