Temp logger for EE Web part 2

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
monpjc
Date:
Tue Nov 13 19:36:07 2012 +0000
Parent:
0:5fefec34f0d3
Commit message:
Added local file sys

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 5fefec34f0d3 -r 6db2c0502c2c main.cpp
--- a/main.cpp	Mon Oct 08 07:38:54 2012 +0000
+++ b/main.cpp	Tue Nov 13 19:36:07 2012 +0000
@@ -16,35 +16,64 @@
 DigitalOut spi_clk(p7);
 DigitalOut spi_cs(p8);
 DigitalIn  spi_data(p6);
+LocalFileSystem local("local");
 
 int enable;
 int button_state;
 int record_state;
 int spi_data_in_temp;
 int spi_data_in;
-float A1, A2;
+float A1, A2, B1, B2;
+time_t seconds = 0;
+int openFile = 0;
+int closeFile = 0;
 
 int main()
 {
+    // Opens the root directory of the local file system
+    pc.printf("Files on mbed\r\n");
+    DIR *d = opendir("/local");
+    struct dirent *p;
+    while((p = readdir(d)) != NULL)
+    {                                             // Print the names of the files in the local file system
+        pc.printf("%s\n\r", p->d_name);            // to stdout (USB Serial).
+    }
+    closedir(d);
+    pc.printf("\n");    
+    // Removes the file "log.csv" from the local file system
+    //remove("/local/log.csv");
+    // Open "log.csv" on the local file system for writing
+    FILE *fp = fopen("/local/log.csv", "a");
+    //Send headers to csv file
+    fprintf(fp, "Time_Seconds,T1_C,T2_C,T3_C\r");
+
     enable = 0;
     button_state = 0;
     record_state = 0;
     init_max31855();
     
-    printf("Data Logger Ready\r\n");
+    pc.printf("Data Logger Ready\r\n");
     
-    while(1)
+    while(closeFile == 0)
     {
         wait(1.0);
         button_FSM();
         max31855();
+              
         if( enable == 1 )
         {
             A1 = Ain1.read_u16();
+            B1 = (0.000000029*(A1*A1))-(0.00341*A1)+104;
             A2 = Ain2.read_u16();
-            printf("Ain1=%f Ain2=%f t'c=%d\r\n", A1, A2, spi_data_in);
+            B2 = (0.000000029*(A2*A2))-(0.00341*A2)+104;
+            pc.printf("Ain1=%.2f Ain2=%.2f t'c=%d\r\n", B1, B2, spi_data_in);
         }
+        //get time in seconds
+        seconds = time(NULL);
+        //and save to log file
+        fprintf(fp, "%d,%.2f,%.2f,%d\n", seconds, B1, B2, spi_data_in );
     }
+    fclose(fp);
 }
 
 void max31855( void )
@@ -118,7 +147,9 @@
             case 4:
                 enable = 1;
                 button_state = 0;
-                printf("Recording...\r\n");
+                pc.printf("Recording...\r\n");
+                //reset time to zero
+                set_time(0);
                 break;
         }
     }
@@ -158,7 +189,8 @@
             case 4:
                 enable = 0;
                 button_state = 0;
-                printf("STOPPED!\r\n");
+                closeFile = 1;
+                pc.printf("STOPPED!\r\n");
                 break;
         }
     }