Addition of FSM to try and set initial starting temperature for use throughout project. Additional menu option. Joystick input 'bouncing' during FSM use - to be debugged

Dependencies:   mbed N5110v02 TMP102 JoystickIoT

Revision:
9:d71b92c916f8
Parent:
8:07323fcab6d1
Child:
10:357b3329620f
diff -r 07323fcab6d1 -r d71b92c916f8 main.cpp
--- a/main.cpp	Sun Jan 16 22:46:29 2022 +0000
+++ b/main.cpp	Wed Jan 19 07:34:20 2022 +0000
@@ -1,12 +1,10 @@
 /* 
 * @ File main.cpp
 * @ Author - David Leaming - 25574043
-* @ Date - December 2021
-*
+* @ Date - January 2022
 * Acknowledgements 
 * Craig A. Evans, University of Leeds, TMP102 Library ,Feb 2016
 * Dr Edmond Nurellari, University of Lincoln, Joystick & N5110 Libraries
-*
 */ 
 
 #include "mbed.h"                                                               // include the library header, ensure the library has been imported into the project
@@ -57,11 +55,11 @@
 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 ButtonA_isr();                                                             // 
+void ButtonB_isr();                                                             // 
+void ButtonBack_isr();                                                          // 
+void sw2_isr();                                                                 // 
+void menu_timer_isr();                                                          // 
 void OnStartup();                                                               // 
 void Run();                                                                     // 
 void ConstantMonitoring();                                                      //   
@@ -84,9 +82,8 @@
     
     lcd.setContrast(0.5);                                                       // change set contrast in range 0.0 to 1.0
     
-    OnStartup();                                                                //    
-    Run();                                                                      //
-     
+    OnStartup();                                                                // Call intro screen    
+    Run();                                                                      // Call main-menu and functions
 }      
 
 void init_serial() {
@@ -101,21 +98,21 @@
     
     sw2.mode(PullNone);                                                         // since the on-board switches have external pull-ups, we should disable the internal pull-down
     sw3.mode(PullNone);                                                         // resistors that are enabled by default using InterruptIn
-
 }
 
 void init_PCB ()
 {
     LED01 = 1;                                                                  // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
-    LED02 = 1;                                     
-    LED03 = 1;
-    LED04 = 1;
-    LED05 = 1;
-    LED06 = 1;
+    LED02 = 1;                                                                  // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
+    LED03 = 1;                                                                  // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
+    LED04 = 1;                                                                  // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly    
+    LED05 = 1;                                                                  // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
+    LED06 = 1;                                                                  // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly    
+    
+    Buzzer = 0;                                                                 // Ensure Piezo Buzzer is off
     
     ButtonA.mode(PullDown);                                                     // Set pin to Pull Down to OV, meaning that a rising edge is looked for when button is pressed
     ButtonB.mode(PullDown);                                                     // Set pin to Pull Down to OV, meaning that a rising edge is looked for when button is pressed
-    
 }
 
 void ButtonA_isr()                                                              // ButtonA event-triggered interrupt
@@ -143,56 +140,67 @@
     g_menu_timer_flag = 1;                                                      // set flag in ISR
 }
 
-void OnStartup()                                                                //Run some start up display 
+void OnStartup()                                                                // Run some start up display 
 {
-    lcd.clear();                                                                // clear buffer at start of every loop
-    lcd.printString("--------------",0,0);                                      // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display)
+    Buzzer.period(1.0/659.0);                                                   // Welcome sounds from Piezo
+    Buzzer = 0.5;                                                               //                   
+    wait(0.5);                                                                  // 
+    Buzzer.period(1.0/494.0);                                                   // 
+    Buzzer = 0.5;                                                               //    
+    wait(0.5);                                                                  // 
+    Buzzer.period(1.0/554.0);                                                   // 
+    Buzzer = 0.5;                                                               // 
+    wait(0.5);                                                                  // 
+    Buzzer = 0;                                                                 // Turn off welcome sounds 
+    lcd.clear();                                                                // Clear buffer at start of every loop
+    lcd.printString("--------------",0,0);                                      // Can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display)
     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("V09 - 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
-    lcd.clear();                                                                // clear buffer at start of every loop
-    lcd.refresh();                                                              // need to refresh display after setting pixels or writing strings 
+    lcd.refresh();                                                              // Need to refresh display after setting pixels or writing strings 
+    wait(5.0);                                                                  // Leave welcome screen on for designated amount of time
+    lcd.clear();                                                                // Clear buffer at start of every loop
+    lcd.refresh();                                                              // Need to refresh display after setting pixels or writing strings 
     lcd.printString("--------------",0,0);                                      //
     lcd.printString(" Use Joystick",0,1);                                       // Instruction for use of menu
     lcd.printString(" To Navigate",0,2);                                        //
     lcd.printString("",0,3);                                                    // Blank Line
     lcd.printString("  A = Select",0,4);                                        //
     lcd.printString("--------------",0,5);                                      //
-    lcd.refresh();                                                              // need to refresh display after setting pixels or writing strings
+    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
 }
 
-enum EMenuState                                                                 //An enum controlling the current state of the display.
+enum EMenuState                                                                 // An enum controlling the current state of the display.
 {
-    MENUSTATE_Main,
-    MENUSTATE_Monitor,
-    MENUSTATE_OneOff,
-    MENUSTATE_About,
-    MENUSTATE_Author,
+    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_About,                                                            // Defining each menu state to be called upon later
+    MENUSTATE_Author,                                                           // Defining each menu state to be called upon later
     
     MENUSTATTE_Num,                                                             // This is a special enum value that just gives is a way to get the number of elements in the enum.
 };
 
 void Run()
 {
-    int MenuState;    
-    int SelectedItem = 0;
-    int NumMenuItems = 1;
+    int MenuState;                                                              // 
+    int SelectedItem = 0;                                                       // 
+    int NumMenuItems = 1;                                                       // 
     
     char buffer[14];                                                            // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
     
     while(1){
-        if (g_menu_timer_flag){         
-            g_menu_timer_flag = 0;
+        if (g_menu_timer_flag){                                                 //        
+            g_menu_timer_flag = 0;                                              // 
             
             bool bAButtonWasPressed = g_ButtonA_flag;                           // Get the value of the input flags and reset them
-            bool bBButtonWasPressed = g_ButtonB_flag;           
-            g_ButtonA_flag = 0;
-            g_ButtonB_flag = 0;
+            bool bBButtonWasPressed = g_ButtonB_flag;                           // Get the value of the input flags and reset them
+            g_ButtonA_flag = 0;                                                 // 
+            g_ButtonB_flag = 0;                                                 // 
             
             lcd.clear();                                                        // clear buffer at start of every loop
                         
@@ -200,103 +208,124 @@
         
             switch(MenuState)                                                   // Update and Draw whichever menu we're on.
             {                   
-                case MENUSTATE_Main:
+                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)
+                    if(SelectedItem >= NumMenuItems)                            // 
                     {
  
                         SelectedItem = 0;                                       // Something has gone wrong, reset selected item.
                     }
-                    lcd.printString("---- MENU ----",0,0);
-                    lcd.printString("M1 - Monitor",0,1);
-                    lcd.printString("M2 - One-off",0,2);
-                    lcd.printString("About",0,3);
-                    lcd.printString("Author",0,4);
+                    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);                              // 
                     
                     if(bAButtonWasPressed)                                      // If A was pressed then we transition to the selected screen.
                     {
                         if(SelectedItem == 0)                                   // If 0 line is selected, move to detailed menu 
                         {                                                       // Actually 0 line + 1, see circle draw and selection below
-                            NewMenuState = MENUSTATE_Monitor;
+                            NewMenuState = MENUSTATE_Monitor;                   // 
                         }
                         else if(SelectedItem == 1)                              // If 1 line is selected, move to detailed menu 
                         {                                                       // Actually 1 line + 1, see circle draw and selection below
-                            NewMenuState = MENUSTATE_OneOff;
+                            NewMenuState = MENUSTATE_OneOff;                    // 
                         }
                         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_About;
+                            NewMenuState = MENUSTATE_About;                     // 
                         }
                         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_Author;
+                            NewMenuState = MENUSTATE_Author;                    // 
                         }
                     }
                 }
-                break;      
-                case MENUSTATE_Monitor:
+                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)
+                    if(SelectedItem >= NumMenuItems)                            // 
                     {
                         NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
                     }
                     
                     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("-- Constant --",0,0);
-                    lcd.printString("- Monitoring -",0,1);
+                    lcd.printString("-- Constant --",0,0);                      // 
+                    lcd.printString("- Monitoring -",0,1);                      // 
                     lcd.printString(buffer,0,3);                                // display on screen
-                    lcd.printString(" 'A' to Menu",0,5);
+                    lcd.printString(" 'A' to Menu",0,5);                        // 
                     lcd.refresh();                                              // need to refresh display after setting pixels or writing strings 
-                    wait(0.5);
+                    wait(0.5);                                                  // 
                     
                     float high_temp = tmp102.get_temperature();                 // read temperature   
-                    if (high_temp >= 22) {                                      // High temp alarm condition - in real world would be lot lower!!
-                        LED01 = 0;                                              // Light LED01 if temperature is over specified
-                        LED02 = 0;                                              // Light LED02 if temperature is over specified
-                        LED03 = 0;                                              // Light LED03 if temperature is over specified
+                    if (high_temp >= 25) {                                      // 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
                     } 
                     else {
-                        LED01 = 1;                                              // LED01 off if temperature is below specified
-                        LED02 = 1;                                              // LED01 off if temperature is below specified
-                        LED03 = 1;                                              // LED01 off if temperature is below specified
+                        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;                                             // 
+                    } 
+                    
+                    float low_temp = tmp102.get_temperature();                  // Read temperature   
+                    if (low_temp <= 22) {                                       // 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
+                    } 
+                    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
                     } 
                                   
-                    if(bAButtonWasPressed)
+                    if(bAButtonWasPressed)                                      // Check if button was pressed
                     {
-                        if(SelectedItem == 0)
+                        if(SelectedItem == 0)                                   // 
                         {
-                            NewMenuState = MENUSTATE_Main;
+                            NewMenuState = MENUSTATE_Main;                      // Transition back to the main menu
+                            LED01 = 1;                                          // Turn off LED upon transition back to main menu
+                            LED02 = 1;                                          // Turn off LED upon transition back to main menu
+                            LED03 = 1;                                          // Turn off LED upon transition back to main menu
+                            LED04 = 1;                                          // Turn off LED upon transition back to main menu
+                            LED05 = 1;                                          // Turn off LED upon transition back to main menu
+                            LED06 = 1;                                          // Turn off LED upon transition back to main menu
                         }
                     }
                 }
                 break;      
-                case MENUSTATE_OneOff:
+                case MENUSTATE_OneOff:                                          // Call a one off measuremnt 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)
+                    if(SelectedItem >= NumMenuItems)                            // 
                     {
                         NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
                     }
                     
                     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("-- One-Off --",0,0);                       // 
+                    lcd.printString("-- Measure --",0,1);                       // 
                     lcd.printString(buffer,0,3);                                // display on screen
-                    lcd.printString(" 'A' to Menu",0,5);
+                    lcd.printString(" 'A' to Menu",0,5);                        // 
                                         
-                    if(bAButtonWasPressed)
+                    if(bAButtonWasPressed)                                      // Check if button was pressed
                     {
-                        if(SelectedItem == 0)
+                        if(SelectedItem == 0)                                   // 
                         {
                             NewMenuState = MENUSTATE_Main;                      // Take us back to top menu
                         }
@@ -304,49 +333,49 @@
                     
                 }
                 break;      
-                case MENUSTATE_About:
+                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)
+                    if(SelectedItem >= NumMenuItems)                            // 
                     {
                         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.refresh();                       
+                    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.refresh();                                              // 
                                         
-                    if(bAButtonWasPressed)
+                    if(bAButtonWasPressed)                                      // Check if button was pressed
                     {
-                        if(SelectedItem == 0)
+                        if(SelectedItem == 0)                                   // 
                         {
-                            NewMenuState = MENUSTATE_Main;
+                            NewMenuState = MENUSTATE_Main;                      // Transition back to Main Menu
                         }
                     }
                 }
                 break;
-                case MENUSTATE_Author:
+                case MENUSTATE_Author:                                          // 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)
+                    if(SelectedItem >= NumMenuItems)                            // 
                     {
                         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);                      // 
+                    lcd.printString("David Leaming ",0,1);                      // 
+                    lcd.printString("   25574043 ",0,2);                        // 
+                    lcd.printString("  VolkerRail",0,3);                        // 
+                    lcd.printString(" 'A' to Menu",0,5);                        // 
                                         
-                    if(bAButtonWasPressed)
+                    if(bAButtonWasPressed)                                      // Check if button was pressed
                     {
-                        if(SelectedItem == 0)
+                        if(SelectedItem == 0)                                   // 
                         {
                             NewMenuState = MENUSTATE_Main;                      // Take us back to top menu 
                         }
@@ -378,11 +407,11 @@
             unsigned int SelectionMarkerRadius = 4       ;                                                  // Draw a marker circle at end of line to show which is the currently selected item.
             unsigned int SelectionMarkerX = WIDTH - (2 * SelectionMarkerRadius);
             unsigned int SelectionMarkerY = (HEIGHT / 5) * (SelectedItem + 1);                              // +1 because of the menu title being on first row
-            lcd.drawCircle(SelectionMarkerX, SelectionMarkerY, SelectionMarkerRadius, FILL_BLACK);                                                             
+            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");                             //
+                        printf("Direction = %i\n");                             // Print direction to serial
                     case N:                                                     //
                     SelectedItem--;                                             //
                     break;                                                      //
@@ -391,13 +420,13 @@
                     break;                                                      //
                 }
                 
-                if(SelectedItem < 0)                                            //Wrap the selection around to the start/end of the menu if it goes below 0 or above NumMenuItems.
+                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;                            // 
                 }
-                else if(SelectedItem >= NumMenuItems)
+                else if(SelectedItem >= NumMenuItems)                           //
                 {
-                    SelectedItem = 0;
+                    SelectedItem = 0;                                           // 
                 }
             }