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

Dependencies:   mbed

Revision:
13:5ad65a688f3f
Parent:
12:5046b7515d5c
Child:
14:3e9991fe64e5
diff -r 5046b7515d5c -r 5ad65a688f3f main.cpp
--- a/main.cpp	Thu Jan 20 22:57:14 2022 +0000
+++ b/main.cpp	Fri Jan 21 14:26:56 2022 +0000
@@ -1,10 +1,16 @@
 /* 
+* Prints a string of characters to the screen buffer, string is cut off after the 83rd  pixel.
+* @param x - the column number (0 to 83)
+* @param y - the row number (0-5) - the display is split into 6 banks - each bank can be considered a row
+*
 * @ File main.cpp
 * @ Author - David Leaming - 25574043
 * @ Date - January 2022
+*
 * Acknowledgements 
 * Craig A. Evans, University of Leeds, TMP102 Library ,Feb 2016
 * Dr Edmond Nurellari, University of Lincoln, Joystick & N5110 Libraries
+* Paul Staron, Piezo Buzzer utility, April 2019
 */ 
 
 #include "mbed.h"                                                               // include the library header, ensure the library has been imported into the project
@@ -40,7 +46,7 @@
     {22,0.5,{7,8,7,7,7,6,7,7,7}},                                               // State 7 - 22 Degrees
     {23,0.5,{8,9,8,8,8,7,8,8,8}},                                               // State 8 - 23 Degrees
     {24,0.5,{9,10,9,9,9,8,9,9,9}},                                              // State 9 - 24 Degrees
-    {25,0.5,{10,1,10,10,10,9,10,10,10}}                                         // State 10 - 25 Degrees
+    {25,0.5,{10,1,10,10,10,9,10,10,10}}                                         // State 10 - 25 Degrees   
 };
 
 Ticker ticker_menu;                                                             // Create Menu ticker object
@@ -179,7 +185,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("V12 - Jan 2022",0,4);                                      //
+    lcd.printString("V13 - 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
@@ -194,6 +200,7 @@
     lcd.refresh();                                                              // Need to refresh display after setting pixels or writing strings
     wait(5.0);                                                                  //
     init_PCB();                                                                 // Ran again to ensure all LED's etc are turned off
+    printf("Transition to Temp Selection %i\n",StartTemp);                      // Observe on serial port - ensure transition to correct screen
 }
 
 enum EMenuState                                                                 // An enum controlling the current state of the display.
@@ -210,7 +217,7 @@
 
 void Run()
 {
-    int MenuState;                                                              // 
+    int MenuState = MENUSTATE_StartTemp;                                                              // 
     int SelectedItem = 0;                                                       // 
     int NumMenuItems = 1;                                                       // 
     
@@ -228,17 +235,14 @@
             lcd.clear();                                                        // clear buffer at start of every loop
                         
             int NewMenuState = MENUSTATTE_Num;                                  // The new menu we want to transition to, if any.
-            
         
             switch(MenuState)                                                   // Update and Draw whichever menu we're on.
             {      
                 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.
                     if(SelectedItem >= NumMenuItems)                            // 
                     {
- 
                         SelectedItem = 0;                                       // Something has gone wrong, reset selected item.
                     }
                     Direction d = joystick.get_direction();                               
@@ -250,12 +254,13 @@
                     printf ("Joystick Direction Points = %i\n",d);              //
                     printf ("State selected = %i\n", g_state);                  //
                     int length = sprintf(buffer," T = %.2f C",g_StartTemp);     // 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("- Set Target -",0,0);                      // 
-                    lcd.printString("---- Temp ----",0,1);                      // 
-                    lcd.printString(buffer,0,3);                                // display on screen
-                    lcd.printString("'A' to Select",0,5);                       // 
-                    lcd.refresh();                                              // need to refresh display after setting pixels or writing strings 
+                    if (length <= 14){                                          // if string will fit on display (assuming printing at x=0)
+                        lcd.printString("- Set Target -",0,0);                  // 
+                        lcd.printString("---- Temp ----",0,1);                  // 
+                        lcd.printString(buffer,0,3);                            // display on screen
+                        lcd.printString("'A' to Select",0,5);                   //                      
+                        lcd.refresh();      
+                    }                                                           // need to refresh display after setting pixels or writing strings 
  
                     if(bAButtonWasPressed)                                      // If A was pressed then we transition to the selected screen.
                     {
@@ -268,11 +273,9 @@
                 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.
                     if(SelectedItem >= NumMenuItems)                            // 
                     {
- 
                         SelectedItem = 0;                                       // Something has gone wrong, reset selected item.
                     }
                     lcd.printString("---- MENU ----",0,0);                      // Menu title and selectable options    
@@ -304,7 +307,6 @@
                 break;                                                          // 
                 case MENUSTATE_Monitor:                                         // Call constant measurement 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)                            // 
@@ -323,33 +325,39 @@
                     lcd.refresh();                                              // need to refresh display after setting pixels or writing strings 
                     wait(0.5);                                                  // 
                     
-                    float high_temp = tmp102.get_temperature();                 // read temperature   
-                    if (high_temp >= g_StartTemp + 2) {                         // High temp alarm condition - in real world would be lot lower!!
-                        LED01 = 0;                                              // Light LED01 if temperature is over specified - Simulated starting of cold blowers
-                        LED02 = 0;                                              // Light LED02 if temperature is over specified - Simulated starting of cold blowers
-                        LED03 = 0;                                              // Light LED03 if temperature is over specified - Simulated starting of cold blowers
-                    printf("WARNING - High Temp!! \n");                         //
+                    if (T >= g_StartTemp + 2) {                                 // High temp alarm condition - in real world would be lot lower!!
+                        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 LED03 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;                                           // 
+                        wait(0.5);                                              // 
+                        Buzzer = 0;                                             //
                     } 
-                    else {
+                    else  {
                         LED01 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of cold blowers
                         LED02 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of cold blowers
                         LED03 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of cold blowers
-                        Buzzer = 0;                                             // 
+                        Buzzer = 0;                                             // Buzzer off if temperature is below specified - Simulated stopping of cold blowers
                     } 
                     
-                    float low_temp = tmp102.get_temperature();                  // Read temperature   
-                    if (low_temp <= g_StartTemp - 2) {                          // Low temp alarm condition - in real world would be lot lower!!
-                        LED04 = 0;                                              // Light LED04 if temperature is over specified - Simulated starting of heaters
-                        LED05 = 0;                                              // Light LED05 if temperature is over specified - Simulated starting of heaters
-                        LED06 = 0;                                              // Light LED06 if temperature is over specified - Simulated starting of heaters
-                    printf("WARNING - Low Temp!! \n");                          //
+                    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
+                        printf("WARNING - Low Temp!! \n");                      //
+                        Buzzer.period(1.0/554.0);                               // Warning Buzzer to extremely high 
+                        Buzzer = 0.5;                                           // 
+                        wait(0.5);                                              // 
+                        Buzzer = 0;                                             //
                     } 
-                    else {
-                        LED04 = 1;                                              // LED04 off if temperature is above specified - Simulated stopping of heaters
-                        LED05 = 1;                                              // LED05 off if temperature is above specified - Simulated stopping of heaters
-                        LED06 = 1;                                              // LED06 off if temperature is above specified - Simulated stopping of heaters
+                    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
                     } 
-                                  
                     if(bAButtonWasPressed)                                      // Check if button was pressed
                     {
                         if(SelectedItem == 0)                                   // 
@@ -367,7 +375,6 @@
                 break;      
                 case MENUSTATE_OneOff:                                          // Call a one off measurement 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)                            // 
                     {
@@ -377,11 +384,12 @@
                     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);                        // 
+                    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
                     {
@@ -395,7 +403,6 @@
                 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.
                     if(SelectedItem >= NumMenuItems)                            // 
                     {
@@ -462,21 +469,26 @@
             }                                                                   
             else
             {
-                
-                                                                                                            
-            unsigned int SelectionMarkerRadius = 4;                                                         // If we have not selected to move to a new menu.
-            unsigned int SelectionMarkerX = WIDTH - (2 * SelectionMarkerRadius);                            // Draw a marker circle at end of line to show which is the currently selected item.
-            unsigned int SelectionMarkerY = (HEIGHT / 5) * (SelectedItem + 1);                              // +1 because of the menu title being on first row
-            lcd.drawCircle(SelectionMarkerX, SelectionMarkerY, SelectionMarkerRadius, FILL_BLACK);          // Fill the circle black so it can be seen easily                                                   
+                unsigned int SelectionMarkerRadius = 4;                                                         // If we have not selected to move to a new menu.
+                unsigned int SelectionMarkerX = WIDTH - (2 * SelectionMarkerRadius);                            // Draw a marker circle at end of line to show which is the currently selected item.
+                unsigned int SelectionMarkerY = (HEIGHT / 5) * (SelectedItem + 1);                              // +1 because of the menu title being on first row
+                lcd.drawCircle(SelectionMarkerX, SelectionMarkerY, SelectionMarkerRadius, FILL_BLACK);          // Fill the circle black so it can be seen easily                                                   
                             
                                                                                 // Handle Joystick Input
-                switch (joystick.get_direction())  {                            // Call to check direction joystick is pointing
-                        printf("Direction = %i\n");                             // Print direction to serial
-                    case N:                                                     //
-                    SelectedItem--;                                             //
+                Direction d = joystick.get_direction();                                                                 
+                printf("Direction = %i\n"); 
+                switch (joystick.get_direction())  {                            // Call to check direction joystick is pointing                                     
+                    case N:        
+                    {   
+                        SelectedItem--;                                           
+                        printf("Selection decremented to %i\n", SelectedItem); 
+                    }
                     break;                                                      //
-                    case S:                                                     //
-                    SelectedItem++;                                             //
+                    case S:   
+                    {
+                        SelectedItem++;
+                        printf("Selection incremented to %i\n", SelectedItem); 
+                    }
                     break;                                                      //
                 }
                 
@@ -503,3 +515,5 @@
         wait(fsm[g_state].time);                                                // wait in that state for desired time           
         g_state = fsm[g_state].nextState[d];                                    // read input and update curent state 
 }
+
+