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

Dependencies:   mbed

Revision:
15:0cd78b44ea83
Parent:
14:3e9991fe64e5
Child:
16:c63a5d084db0
--- a/main.cpp	Fri Jan 21 22:33:32 2022 +0000
+++ b/main.cpp	Mon Jan 24 20:34:48 2022 +0000
@@ -96,6 +96,7 @@
 void StartTemp();                                                               //
 void delete_file(char filename[]);                                              //
 void WriteToFile();                                                             // Function to attempt to write temperature to file
+void OneOff();                                                                  // 
 
 int main()
 {
@@ -190,7 +191,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("V15 - Jan 2022",0,4);                                      //
+    lcd.printString("V16 - 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
@@ -223,11 +224,11 @@
 
 void Run()
 {
-    int MenuState = MENUSTATE_StartTemp;                                                              // 
+    int MenuState = MENUSTATE_StartTemp;                                        // Ensuring that the first Menu State to be brought up is the Temperature Selection Page 
     int SelectedItem = 0;                                                       // 
     int NumMenuItems = 1;                                                       // 
     
-    char buffer[14];                                                            // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
+    char buffer[14];                                                            // Each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
     
     while(1){
         if (g_menu_timer_flag){                                                 //        
@@ -246,7 +247,7 @@
             {      
                 case MENUSTATE_StartTemp:                                       //
                 {                    
-                    NumMenuItems = 1;                                           // 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 = 1;                                           // Details 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.
@@ -279,7 +280,7 @@
                 break;                                                          //         
                 case MENUSTATE_Main:                                            //
                 {
-                    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.
+                    NumMenuItems = 5;                                           // Details 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.
@@ -305,12 +306,12 @@
                         {                                                       // 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
+                        else if(SelectedItem == 3)                              // If 3 line is selected, move to detailed menu 
+                        {                                                       // Actually 3 line + 1, see circle draw and selection below
                             NewMenuState = MENUSTATE_About;                     // 
                         }
-                        else if(SelectedItem == 4)                              // If 3 line is selected, move to detailed menu 
-                        {                                                       // Actually 3 line + 1, see circle draw and selection below
+                        else if(SelectedItem == 4)                              // If 4 line is selected, move to detailed menu 
+                        {                                                       // Actually 4 line + 1, see circle draw and selection below
                             NewMenuState = MENUSTATE_Author;                    // 
                         }
                     }
@@ -392,15 +393,8 @@
                         NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
                     }
                     
-                    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);                    // 
-                    }
+                    OneOff();                                                   // read temperature and print to lcd
+
                                         
                     if(bAButtonWasPressed)                                      // Check if button was pressed
                     {
@@ -412,7 +406,7 @@
                     
                 }
                 break; 
-                case MENUSTATE_Results:                                         // Call Author menu following top menu selection
+                case MENUSTATE_Results:                                         // Call results menu following top menu selection
                 {
 
                     NumMenuItems = 1;                                           // Detail the number of items in Menu -  need this to wrap the selection around properly etc.
@@ -540,7 +534,7 @@
                 }
             }
             
-            lcd.refresh();                                                      //Finally update the display.
+            lcd.refresh();                                                      // Finally update the display.
         }
     }       
 }  
@@ -562,7 +556,7 @@
     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");
+    fp = fopen("/sd/overnighttemp", "w");                                       // 
     int ONight_Temp = T;                                                        // 
     
     if (fp == NULL) {                                                           // if it can't open the file then print error message
@@ -578,3 +572,20 @@
         fclose(fp);                                                             // ensure you close the file after writing
     }
 }
+
+void OneOff()                                                                   // 
+{
+    char buffer[14];                                                            // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
+    
+    lcd.clear();                                                                // 
+    
+    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
+    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);                                        // 
+    }                                                                           //
+}                                                                               //
\ No newline at end of file