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

Dependencies:   mbed

Revision:
18:fc63b51a0302
Parent:
17:793f819e1df6
diff -r 793f819e1df6 -r fc63b51a0302 main.cpp
--- a/main.cpp	Mon Jan 31 20:41:38 2022 +0000
+++ b/main.cpp	Fri Feb 04 09:20:18 2022 +0000
@@ -14,11 +14,11 @@
 */ 
 
 #include "mbed.h"                                                               // Include the library header, ensure the library has been imported into the project
-#include "TMP102.h"                                                             //
-#include "N5110.h"                                                              //
-#include "Joystick.h"                                                           //
-#include "Bitmap.h"                                                             //
-#include "SDFileSystem.h"                                                       //
+#include "TMP102.h"                                                             // Inclusion of library to allow use of code within them as necessary
+#include "N5110.h"                                                              // Inclusion of library to allow use of code within them as necessary
+#include "Joystick.h"                                                           // Inclusion of library to allow use of code within them as necessary
+#include "Bitmap.h"                                                             // Inclusion of library to allow use of code within them as necessary
+#include "SDFileSystem.h"                                                       // Inclusion of library to allow use of code within them as necessary
                                                                                 //
 TMP102 tmp102(I2C_SDA,I2C_SCL);                                                 // Create TMP102 object  
                                                                                 //
@@ -83,27 +83,27 @@
 volatile int g_StartTemp = 0;                                                   // 
 volatile float g_TempReading = 0.0f;                                            //
                                                                                 //
-void error();                                                                   // error function hangs flashing an LED
-void init_serial();                                                             // setup serial port
-void init_K64F();                                                               // set-up the on-board LEDs and switches
-void init_PCB();                                                                // set-up the PCB LEDs and buttons
-void ButtonA_isr();                                                             // 
-void ButtonB_isr();                                                             // 
-void ButtonBack_isr();                                                          // 
-void sw2_isr();                                                                 // 
-void menu_timer_isr();                                                          // 
-void OnStartup();                                                               // 
-void Run();                                                                     // 
-void StartTemp();                                                               //
-void delete_file(char filename[]);                                              //
-void HighTemp();                                                                //
-void SuperHighTemp();                                                           //
-void UltraHighTemp();                                                           //
-void LowTemp();                                                                 //
-void SuperLowTemp();                                                            //
-void UltraLowTemp();                                                            //
-void PeripheralsOffHigh();                                                      //
-void PeripheralsOffLow();                                                       // 
+void error();                                                                   // Error function hangs flashing an LED
+void init_serial();                                                             // Setup serial port
+void init_K64F();                                                               // Set-up the on-board LEDs and switches
+void init_PCB();                                                                // Set-up the PCB LEDs and buttons
+void ButtonA_isr();                                                             // Set-up Button A Interrupt
+void ButtonB_isr();                                                             // Set-up Button B Interrupt
+void ButtonBack_isr();                                                          // Set-up Button Back Interrupt
+void sw2_isr();                                                                 // Set-up Switch 2 Interrupt
+void menu_timer_isr();                                                          // Set-up menu timer
+void OnStartup();                                                               // Set up function to run Welcom Screens
+void Run();                                                                     // Set up function to run main programme
+void StartTemp();                                                               // Set up function to run and control temperature selection
+void delete_file(char filename[]);                                              // Set up function to delete files saved fromovernight temperature readings
+void HighTemp();                                                                // Set up function that gives warning of temperature 2 to 4 degrees over starting temp
+void SuperHighTemp();                                                           // Set up function that gives warning of temperature 4 to 6 degrees over starting temp
+void UltraHighTemp();                                                           // Set up function that gives warning of temperature >= 6 degrees over starting temp
+void LowTemp();                                                                 // Set up function that gives warning of temperature 2 to 4 degrees below starting temp
+void SuperLowTemp();                                                            // Set up function that gives warning of temperature 4 to 6 degrees below starting temp
+void UltraLowTemp();                                                            // Set up function that gives warning of temperature <= 6 degrees below starting temp
+void PeripheralsOffHigh();                                                      // Set up function that turn off all Low Temp warnings
+void PeripheralsOffLow();                                                       // Set up function that turn off all Low Temp warnings
                                                                                 // 
 int main()                                                                      //
 {                                                                               //
@@ -183,13 +183,13 @@
                                                                                 //        
 void OnStartup()                                                                // Run some start up display 
 {                                                                               //
-    Buzzer.period(1.0/659.0);                                                   // Welcome sounds from Piezo
+    Buzzer.period(1.0/659.0);                                                   // Welcome sounds from Piezo buzzer
     Buzzer = 0.5;                                                               //                   
     wait(0.5);                                                                  // 
-    Buzzer.period(1.0/494.0);                                                   // 
+    Buzzer.period(1.0/494.0);                                                   // Welcome sounds from Piezo buzzer
     Buzzer = 0.5;                                                               //    
     wait(0.5);                                                                  // 
-    Buzzer.period(1.0/554.0);                                                   // 
+    Buzzer.period(1.0/554.0);                                                   // Welcome sounds from Piezo buzzer
     Buzzer = 0.5;                                                               // 
     wait(0.5);                                                                  // 
     Buzzer = 0;                                                                 // Turn off welcome sounds 
@@ -198,7 +198,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("V18 - Jan 2022",0,4);                                      //
+    lcd.printString("V19 - 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
@@ -229,7 +229,7 @@
     MENUSTATTE_Num,                                                             // This is a special enum value that allows us to check and see how many menus we have, +1 of menu integer value
 };                                                                              //
                                                                                 //
-void Run()                                                                      //
+void Run()                                                                      // 
 {                                                                               //    
     int MenuState = MENUSTATE_StartTemp;                                        // Ensuring that the first Menu State to be brought up is the Temperature Selection Page 
     int SelectedItem = 0;                                                       // 
@@ -262,9 +262,9 @@
                     {                                                           //
                         SelectedItem = 0;                                       // Something has gone wrong, reset selected item.
                     }                                                           //
-                    Direction d = joystick.get_direction();                     //          
+                    Direction d = joystick.get_direction();                     // Get direction of joystick and set it as 'd'         
                                                                                 //    
-                    StartTemp();                                                //
+                    StartTemp();                                                // Run Start Temp function
                                                                                 //
                     float g_StartTemp = fsm[g_state].output;                    // Read temperature from FSM and print selection to LCD
                     pc.printf("T = %f C\n",g_StartTemp);                        // Print to serial - allows testing without device attached
@@ -295,12 +295,12 @@
                     {                                                           //
                         SelectedItem = 0;                                       // Something has gone wrong, reset selected item.
                     }                                                           // 
-                    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("Results",0,3);                             //
-                    lcd.printString("About",0,4);                               //
-                    lcd.printString("Author",0,5);                              // 
+                    lcd.printString("---- MENU ----",0,0);                      // Menu title and selectable options Printed to the LCD screen    
+                    lcd.printString("M1 - Monitor",0,1);                        // Selectable menu option
+                    lcd.printString("M2 - One-off",0,2);                        // Selectable menu option
+                    lcd.printString("Results",0,3);                             // Selectable menu option
+                    lcd.printString("About",0,4);                               // Selectable menu option
+                    lcd.printString("Author",0,5);                              // Selectable menu option
                                                                                 // 
                     if(bAButtonWasPressed)                                      // If A was pressed then we transition to the selected screen.
                     {                                                           // 
@@ -348,29 +348,41 @@
                     wait(0.5);                                                  // 
                                                                                 //
                     if (T >= g_StartTemp + 2 and T < g_StartTemp + 4) {         // High temp alarm condition - in real world would be lot lower!!
-                        HighTemp();                                             //  
+                        HighTemp();                                             // Run High temp function if temperture is 2 to 4 degrees over selected start temp 
                     }                                                           //
                     else  {                                                     //
-                        PeripheralsOffHigh();                                   //
+                        PeripheralsOffHigh();                                   // Run function to turn off all high temp peripherals
                     }                                                           //
-                    if (T >= g_StartTemp + 4 and T < g_StartTemp + 6) {         // High temp alarm condition - in real world would be lot lower!!
-                        SuperHighTemp();                                        //  
+                    if (T >= g_StartTemp + 4 and T < g_StartTemp + 6) {         // Super High temp alarm condition - in real world would be lot lower!!
+                        SuperHighTemp();                                        // Run Super High temp function if temperture is 4 to 6 degrees over selected start temp  
                     }                                                           //
                     else  {                                                     // 
-                        PeripheralsOffHigh();                                   // 
+                        PeripheralsOffHigh();                                   // Run function to turn off all high temp peripherals
                     }                                                           // 
-                    if (T >= g_StartTemp + 6) {                                 // 
-                        UltraHighTemp();                                        // 
+                    if (T >= g_StartTemp + 6) {                                 // Ultra High temp alarm condition - in real world would be lot lower!!
+                        UltraHighTemp();                                        // Run Ultra High temp function if temperture is 6 degrees or more over selected start temp  
                     }                                                           // 
                     else  {                                                     //
-                        PeripheralsOffHigh();                                   //
+                        PeripheralsOffHigh();                                   // Run function to turn off all high temp peripherals
                     }                                                           //
                     if (T <= g_StartTemp - 2 and T > g_StartTemp - 4) {         // Low temp alarm condition - in real world would be lot lower!!
-                        LowTemp();                                              //
+                        LowTemp();                                              // Run Low Temp function if temperture is 2 to 4 degrees below selected start temp
+                    }                                                           //
+                    else  {                                                     //    
+                        PeripheralsOffLow();                                    // Run function to turn off all low temp peripherals            
+                    }                                                           // 
+                    if (T <= g_StartTemp - 4 and T > g_StartTemp - 6) {         // Super low temp alarm condition - in real world would be lot lower!!
+                        SuperLowTemp();                                         // Run Super Low Temp function if temperture is 2 to 4 degrees below selected start temp
                     }                                                           //
                     else  {                                                     //    
-                        PeripheralsOffLow();                                    //               
+                        PeripheralsOffLow();                                    // Run function to turn off all low temp peripherals            
+                    }                                                           // 
+                    if (T <= g_StartTemp - 6) {                                 // Ultra low temp alarm condition - in real world would be lot lower!!
+                        UltraLowTemp();                                         // Run Ultra Low Temp function if temperture is 2 to 4 degrees below selected start temp
                     }                                                           //
+                    else  {                                                     //    
+                        PeripheralsOffLow();                                    // Run function to turn off all low temp peripherals            
+                    }   
                     if(bAButtonWasPressed)                                      // Check if button was pressed
                     {                                                           //
                         if(SelectedItem == 0)                                   // 
@@ -395,7 +407,7 @@
                         g_TempReading = tmp102.get_temperature();               // Gather the temp readings only on the frame we enter this state and keep printing this same temp, not to refresh and read new temp.
                     }                                                           //
                                                                                 //
-                    pc.printf("T = %f K\n",g_TempReading);                      // Print to serial - allows testing without device attached
+                    pc.printf("T = %f C\n",g_TempReading);                      // Print to serial - allows testing without device attached
                     int length = sprintf(buffer," T = %.2f C",g_TempReading);   // 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);                   // 
@@ -421,7 +433,7 @@
                         NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
                     }                                                           //
                                                                                 //
-                    if(NumFramesInState == 0)                                   //
+                    if(NumFramesInState == 0)                                   // 
                     {                                                           //
                         if(fp != NULL){                                         //        
                             printf("Error! File already open!\n");              //
@@ -438,15 +450,15 @@
                     else                                                        //
                     {                                                           //
                         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
+                        pc.printf("T = %f C\n",T);                              // Print to serial - allows testing without device attached
                         fprintf(fp, "%d,%f\n",NumFramesInState,T);              // Print formatted string to file (CSV)
                                                                                 //
-                        lcd.printString("-- Results --",0,0);                   // 
-                        lcd.printString(" Writing",0,1);                        // 
-                        lcd.printString(" results to ",0,2);                    // 
-                        lcd.printString(" file...",0,3);                        // 
-                        lcd.printString(" ",0,4);                               // 
-                        lcd.printString(" 'A' to Menu",0,5);                    // 
+                        lcd.printString("-- Results --",0,0);                   // Menu Title
+                        lcd.printString(" Writing",0,1);                        // Instructions showing what the function is doing - displayed on screen
+                        lcd.printString(" results to ",0,2);                    // Instructions showing what the function is doing - displayed on screen
+                        lcd.printString(" file...",0,3);                        // Instructions showing what the function is doing - displayed on screen
+                        lcd.printString(" ",0,4);                               // Blank Line
+                        lcd.printString(" 'A' to Menu",0,5);                    // Instructions to return to main
                     }                                                           //
                                                                                 //
                     if(bAButtonWasPressed)                                      // Check if button was pressed
@@ -466,11 +478,11 @@
                         NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
                     }                                                           //  
                                                                                 //    
-                    lcd.printString("--- About ---",0,0);                       // 
-                    lcd.printString("ELE3006M - IoT",0,1);                      // 
-                    lcd.printString("    Project",0,2);                         // 
-                    lcd.printString("Uni of Lincoln",0,3);                      // 
-                    lcd.printString(" 'A' to Menu",0,5);                        // 
+                    lcd.printString("--- About ---",0,0);                       // Menu Title
+                    lcd.printString("ELE3006M - IoT",0,1);                      // Data within menu
+                    lcd.printString("    Project",0,2);                         // Data within menu
+                    lcd.printString("Uni of Lincoln",0,3);                      // Data within menu
+                    lcd.printString(" 'A' to Menu",0,5);                        // Instructions to return to Main menu
                     lcd.refresh();                                              // 
                                                                                 //
                     if(bAButtonWasPressed)                                      // Check if button was pressed
@@ -490,11 +502,11 @@
                         NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
                     }                                                           //
                                                                                 //
-                    lcd.printString("--- Author ---",0,0);                      // 
-                    lcd.printString("David Leaming ",0,1);                      // 
-                    lcd.printString("   25574043 ",0,2);                        // 
-                    lcd.printString("  VolkerRail",0,3);                        // 
-                    lcd.printString(" 'A' to Menu",0,5);                        // 
+                    lcd.printString("--- Author ---",0,0);                      // Menu Title
+                    lcd.printString("David Leaming ",0,1);                      // Data within menu
+                    lcd.printString("   25574043 ",0,2);                        // Data within menu
+                    lcd.printString("  VolkerRail",0,3);                        // Data within menu
+                    lcd.printString(" 'A' to Menu",0,5);                        // Instructions to return to Main menu
                                                                                 //
                     if(bAButtonWasPressed)                                      // Check if button was pressed
                     {                                                           //
@@ -543,23 +555,23 @@
                     case N:                                                     //
                     {                                                           //        
                         SelectedItem--;                                         //  
-                        printf("Selection decremented to %i\n", SelectedItem);  //
+                        printf("Selection decremented to %i\n", SelectedItem);  // Selection pointer move down
                     }                                                           //
                     break;                                                      //
                     case S:                                                     //
                     {                                                           //
                         SelectedItem++;                                         //    
-                        printf("Selection incremented to %i\n", SelectedItem);  //
+                        printf("Selection incremented to %i\n", SelectedItem);  // Selection pointer move up
                     }                                                           //
                     break;                                                      //
                 }                                                               //
                 if(SelectedItem < 0)                                            // Wrap the selection around to the start/end of the menu if it goes below 0 or above NumMenuItems.
                 {                                                               //
-                    SelectedItem = NumMenuItems - 1;                            // 
+                    SelectedItem = NumMenuItems - 1;                            // Move to bottom of menu
                 }                                                               //
-                else if(SelectedItem >= NumMenuItems)                           //
+                else if(SelectedItem >= NumMenuItems)                           // 
                 {                                                               //
-                    SelectedItem = 0;                                           // 
+                    SelectedItem = 0;                                           // Move to top of menu
                 }                                                               //
             }                                                                   //
             lcd.refresh();                                                      // Finally update the display.
@@ -578,7 +590,7 @@
                                                                                 // 
 void HighTemp()                                                                 //
 {                                                                               // 
-    LED01 = !LED01;                                                             // Flash LED01 if temperature is over specified - Simulated starting of cold blowers
+    LED01 = 0;                                                                  // LED01 on if temperature is over specified - Simulated starting of cold blowers
     printf("WARNING - High Temp!! \n");                                         //
     Buzzer.period(1.0/554.0);                                                   // Warning Buzzer to extremely high 
     Buzzer = 0.5;                                                               // 
@@ -588,8 +600,8 @@
                                                                                 // 
 void SuperHighTemp()                                                            //
 {                                                                               // 
-    LED01 = !LED01;                                                             // Flash LED01 if temperature is over specified - Simulated starting of cold blowers
-    LED02 = !LED02;                                                             // Flash LED02 if temperature is over specified - Simulated starting of cold blowers
+    LED01 = 0;                                                                  // LED01 on if temperature is over specified - Simulated starting of cold blowers
+    LED02 = 0;                                                                  // LED02 on if temperature is over specified - Simulated starting of cold blowers
     printf("WARNING - Super High Temp!! \n");                                   //
     Buzzer.period(1.0/554.0);                                                   // Warning Buzzer to extremely high 
     Buzzer = 0.5;                                                               // 
@@ -599,9 +611,9 @@
                                                                                 // 
 void UltraHighTemp()                                                            //
 {                                                                               // 
-    LED01 = !LED01;                                                             // Flash LED01 if temperature is over specified - Simulated starting of cold blowers
-    LED02 = !LED02;                                                             // Flash LED02 if temperature is over specified - Simulated starting of cold blowers
-    LED03 = !LED03;                                                             // Flash LED02 if temperature is over specified - Simulated starting of cold blowers
+    LED01 = 0;                                                                  // LED01 on if temperature is over specified - Simulated starting of cold blowers
+    LED02 = 0;                                                                  // LED02 on if temperature is over specified - Simulated starting of cold blowers
+    LED03 = 0;                                                                  // LED02 on if temperature is over specified - Simulated starting of cold blowers
     printf("WARNING - Ultra High Temp!! \n");                                   //
     Buzzer.period(1.0/554.0);                                                   // Warning Buzzer to extremely high 
     Buzzer = 0.5;                                                               // 
@@ -611,7 +623,7 @@
                                                                                 //
 void LowTemp()                                                                  //
 {                                                                               //
-    LED04 = !LED04;                                                             // Flash LED04 if temperature is over specified - Simulated starting of heaters
+    LED04 = 0;                                                                  // LED04 on if temperature is over specified - Simulated starting of heaters
     printf("WARNING - Low Temp!! \n");                                          //
     Buzzer.period(1.0/554.0);                                                   // Warning Buzzer to extremely low
     wait(0.5);                                                                  // 
@@ -620,8 +632,8 @@
                                                                                 //                                                                                                                                           //
 void SuperLowTemp()                                                             //
 {                                                                               //
-    LED04 = !LED04;                                                             // Flash LED04 if temperature is over specified - Simulated starting of heaters
-    LED05 = !LED05;                                                             // Flash LED05 if temperature is over specified - Simulated starting of heaters
+    LED04 = 0;                                                                  // LED04 on if temperature is over specified - Simulated starting of heaters
+    LED05 = 0;                                                                  // LED05 on if temperature is over specified - Simulated starting of heaters
     printf("WARNING - Super Low Temp!! \n");                                    //
     Buzzer.period(1.0/554.0);                                                   // Warning Buzzer to extremely low
     wait(0.5);                                                                  // 
@@ -630,9 +642,9 @@
                                                                                 //
 void UltraLowTemp()                                                             //
 {                                                                               //
-    LED04 = !LED04;                                                             // Flash LED04 if temperature is over specified - Simulated starting of heaters
-    LED05 = !LED05;                                                             // Flash LED05 if temperature is over specified - Simulated starting of heaters
-    LED06 = !LED06;                                                             // Flash LED06 if temperature is over specified - Simulated starting of heaters
+    LED04 = 0;                                                                  // LED04 on if temperature is over specified - Simulated starting of heaters
+    LED05 = 0;                                                                  // LED05 on if temperature is over specified - Simulated starting of heaters
+    LED06 = 0;                                                                  // LED06 on if temperature is over specified - Simulated starting of heaters
     printf("WARNING - Ultra Low Temp!! \n");                                    //
     Buzzer.period(1.0/554.0);                                                   // Warning Buzzer to extremely low
     wait(0.5);                                                                  // 
@@ -645,6 +657,7 @@
     LED02 = 1;                                                                  // LED02 off if temperature is below specified - Simulated stopping of cold blowers
     LED03 = 1;                                                                  // LED03 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
+    printf("All High Temp Peripherals = OFF \n");                               // 
 }                                                                               // 
                                                                                 // 
 void PeripheralsOffLow()                                                        // 
@@ -653,4 +666,5 @@
     LED05 = 1;                                                                  // LED05 off if temperature is below specified - Simulated stopping of heaters
     LED06 = 1;                                                                  // LED06 off if temperature is below specified - Simulated stopping of heaters
     Buzzer = 0;                                                                 // Buzzer off if temperature is below specified - Simulated stopping of heaters
+    printf("All LOW Temp Peripherals = OFF \n");                                // 
 }                                                                               // 
\ No newline at end of file