Final tidy of code following installation of new sensor, more comments added prior to submission

Dependencies:   mbed

Revision:
14:3e9991fe64e5
Parent:
13:5ad65a688f3f
Child:
15:0cd78b44ea83
diff -r 5ad65a688f3f -r 3e9991fe64e5 main.cpp
--- a/main.cpp	Fri Jan 21 14:26:56 2022 +0000
+++ b/main.cpp	Fri Jan 21 22:33:32 2022 +0000
@@ -8,8 +8,8 @@
 * @ Date - January 2022
 *
 * Acknowledgements 
-* Craig A. Evans, University of Leeds, TMP102 Library ,Feb 2016
-* Dr Edmond Nurellari, University of Lincoln, Joystick & N5110 Libraries
+* Craig A. Evans, University of Leeds, TMP102 Library, Feb 2016
+* Dr Edmond Nurellari, University of Lincoln, Joystick, N5110 Libraries & SD Card Libraries
 * Paul Staron, Piezo Buzzer utility, April 2019
 */ 
 
@@ -18,6 +18,7 @@
 #include "N5110.h"
 #include "Joystick.h"
 #include "Bitmap.h"
+#include "SDFileSystem.h"
 
 TMP102 tmp102(I2C_SDA,I2C_SCL);                                                 // Create TMP102 object  
 
@@ -27,6 +28,8 @@
 //                  y     x     button
 Joystick joystick(PTB10,PTB11,PTC16);                                           // Define Joystick Object 
 
+SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");                                  // MOSI, MISO, SCK, CS - Connections to SD card holder on K64F (SPI interface)
+
 Serial pc(USBTX,USBRX);                                                         // UART connection for PC
 
 struct State {                                                                  // Struct for state
@@ -91,6 +94,8 @@
 void OnStartup();                                                               // 
 void Run();                                                                     // 
 void StartTemp();                                                               //
+void delete_file(char filename[]);                                              //
+void WriteToFile();                                                             // Function to attempt to write temperature to file
 
 int main()
 {
@@ -185,7 +190,7 @@
     lcd.printString("  Smart Cold",0,1);                                        // Just a welcome message before auto moving to main menu
     lcd.printString("   Storage",0,2);                                          //
     lcd.printString("  Monitoring",0,3);                                        // 
-    lcd.printString("V13 - Jan 2022",0,4);                                      //
+    lcd.printString("V15 - Jan 2022",0,4);                                      //
     lcd.printString("--------------",0,5);                                      //
     lcd.refresh();                                                              // Need to refresh display after setting pixels or writing strings 
     wait(5.0);                                                                  // Leave welcome screen on for designated amount of time
@@ -209,6 +214,7 @@
     MENUSTATE_Main,                                                             // Defining each menu state to be called upon later
     MENUSTATE_Monitor,                                                          // Defining each menu state to be called upon later
     MENUSTATE_OneOff,                                                           // Defining each menu state to be called upon later
+    MENUSTATE_Results,                                                          // Defining each menu state to be called upon later
     MENUSTATE_About,                                                            // Defining each menu state to be called upon later
     MENUSTATE_Author,                                                           // Defining each menu state to be called upon later
     
@@ -273,7 +279,7 @@
                 break;                                                          //         
                 case MENUSTATE_Main:                                            //
                 {
-                    NumMenuItems = 4;                                           // Dont for get to set this when changing number of items in the menu. We need this to wrap the selection around properly etc.
+                    NumMenuItems = 5;                                           // Dont for get to set this when changing number of items in the menu. We need this to wrap the selection around properly etc.
                     if(SelectedItem >= NumMenuItems)                            // 
                     {
                         SelectedItem = 0;                                       // Something has gone wrong, reset selected item.
@@ -281,8 +287,9 @@
                     lcd.printString("---- MENU ----",0,0);                      // Menu title and selectable options    
                     lcd.printString("M1 - Monitor",0,1);                        // Printed to the LCD screen
                     lcd.printString("M2 - One-off",0,2);                        // 
-                    lcd.printString("About",0,3);                               //
-                    lcd.printString("Author",0,4);                              // 
+                    lcd.printString("Results",0,3);                             //
+                    lcd.printString("About",0,4);                               //
+                    lcd.printString("Author",0,5);                              // 
                     
                     if(bAButtonWasPressed)                                      // If A was pressed then we transition to the selected screen.
                     {
@@ -296,9 +303,13 @@
                         }
                         else if(SelectedItem == 2)                              // If 2 line is selected, move to detailed menu 
                         {                                                       // Actually 2 line + 1, see circle draw and selection below
+                            NewMenuState = MENUSTATE_Results;                   // 
+                        }
+                        else if(SelectedItem == 3)                              // If 2 line is selected, move to detailed menu 
+                        {                                                       // Actually 2 line + 1, see circle draw and selection below
                             NewMenuState = MENUSTATE_About;                     // 
                         }
-                        else if(SelectedItem == 3)                              // If 3 line is selected, move to detailed menu 
+                        else if(SelectedItem == 4)                              // If 3 line is selected, move to detailed menu 
                         {                                                       // Actually 3 line + 1, see circle draw and selection below
                             NewMenuState = MENUSTATE_Author;                    // 
                         }
@@ -343,9 +354,9 @@
                     } 
                     
                     if (T <= g_StartTemp - 2) {                                 // High temp alarm condition - in real world would be lot lower!!
-                        LED04 = !LED04;                                         // Flash LED01 if temperature is over specified - Simulated starting of cold blowers
-                        LED05 = !LED05;                                         // Flash LED02 if temperature is over specified - Simulated starting of cold blowers
-                        LED06 = !LED06;                                         // Flash LED03 if temperature is over specified - Simulated starting of cold blowers
+                        LED04 = !LED04;                                         // Flash LED01 if temperature is over specified - Simulated starting of heaters
+                        LED05 = !LED05;                                         // Flash LED02 if temperature is over specified - Simulated starting of heaters
+                        LED06 = !LED06;                                         // Flash LED03 if temperature is over specified - Simulated starting of heaters
                         printf("WARNING - Low Temp!! \n");                      //
                         Buzzer.period(1.0/554.0);                               // Warning Buzzer to extremely high 
                         Buzzer = 0.5;                                           // 
@@ -353,10 +364,10 @@
                         Buzzer = 0;                                             //
                     } 
                     else  {
-                        LED04 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of cold blowers
-                        LED05 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of cold blowers
-                        LED06 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of cold blowers
-                        Buzzer = 0;                                             // Buzzer off if temperature is below specified - Simulated stopping of cold blowers
+                        LED04 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of heaters
+                        LED05 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of heaters
+                        LED06 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of heaters
+                        Buzzer = 0;                                             // Buzzer off if temperature is below specified - Simulated stopping of heaters
                     } 
                     if(bAButtonWasPressed)                                      // Check if button was pressed
                     {
@@ -381,14 +392,14 @@
                         NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
                     }
                     
-                    float T = tmp102.get_temperature();                         // read temperature and print to lcd
+                    T = tmp102.get_temperature();                         // read temperature and print to lcd
                     pc.printf("T = %f K\n",T);                                  // Print to serial - allows testing without device attached
                     int length = sprintf(buffer," T = %.2f C",T);               // print formatted data to buffer - it is important the format specifier ensures the length will fit in the buffer
-                    if (length <= 14){                                           // if string will fit on display (assuming printing at x=0)
-                        lcd.printString("-- One-Off --",0,0);                       // 
-                        lcd.printString("-- Measure --",0,1);                       // 
-                        lcd.printString(buffer,0,3);                                // display on screen
-                        lcd.printString(" 'A' to Menu",0,5);                        // 
+                    if (length <= 14){                                          // if string will fit on display (assuming printing at x=0)
+                        lcd.printString("-- One-Off --",0,0);                   // 
+                        lcd.printString("-- Measure --",0,1);                   // 
+                        lcd.printString(buffer,0,3);                            // display on screen
+                        lcd.printString(" 'A' to Menu",0,5);                    // 
                     }
                                         
                     if(bAButtonWasPressed)                                      // Check if button was pressed
@@ -400,7 +411,34 @@
                     }                   
                     
                 }
-                break;      
+                break; 
+                case MENUSTATE_Results:                                         // Call Author menu following top menu selection
+                {
+
+                    NumMenuItems = 1;                                           // Detail the number of items in Menu -  need this to wrap the selection around properly etc.
+                    if(SelectedItem >= NumMenuItems)                            // 
+                    {
+                        NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
+                    }
+                    
+                    WriteToFile();                                              //
+                    
+                    lcd.printString("--- Results ---",0,0);                     // 
+                    lcd.printString(" ",0,1);                                   // 
+                    lcd.printString(" ",0,2);                                   // 
+                    lcd.printString(" ",0,3);                                   // 
+                    lcd.printString(" ",0,4);                                   // 
+                    lcd.printString(" 'A' to Menu",0,5);                        // 
+                                        
+                    if(bAButtonWasPressed)                                      // Check if button was pressed
+                    {
+                        if(SelectedItem == 0)                                   // 
+                        {
+                            NewMenuState = MENUSTATE_Main;                      // Take us back to top menu 
+                        }
+                    }
+                }
+                break;     
                 case MENUSTATE_About:                                           // Call About menu following top menu selection
                 {
                     NumMenuItems = 1;                                           // Detail the number of items in Menu -  need this to wrap the selection around properly etc.
@@ -516,4 +554,27 @@
         g_state = fsm[g_state].nextState[d];                                    // read input and update curent state 
 }
 
-
+void WriteToFile()                                                              // Function to attempt to write temperature to file
+{
+    printf("#### SD Card Example #####\n");
+    FILE *fp;                                                                   // this is our file pointer
+    
+    float T = tmp102.get_temperature();                                         // read temperature and print to lcd
+    pc.printf("T = %f K\n",T);                                                  // Print to serial - allows testing without device attached
+    
+    fp = fopen("OvernightTemps", "w");
+    int ONight_Temp = T;                                                        // 
+    
+    if (fp == NULL) {                                                           // if it can't open the file then print error message
+    printf("Error! Unable to open file!\n");
+    } 
+    else {                                                                      // opened file so can write
+        printf("Writing to file....");
+        for(int i = T; i <= 50; i++) {
+            float T = 1000.0F/i;                                                // dummy variable
+            fprintf(fp, "%d,%f\n",i,T);                                         // print formatted string to file (CSV)
+        }
+        printf("Done.\n");
+        fclose(fp);                                                             // ensure you close the file after writing
+    }
+}