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:
7:d77e95505e43
Parent:
6:ebf4784ce92b
Child:
8:07323fcab6d1
--- a/main.cpp	Fri Jan 14 10:41:46 2022 +0000
+++ b/main.cpp	Fri Jan 14 15:27:19 2022 +0000
@@ -42,9 +42,11 @@
 InterruptIn sw3(SW3);                                                           // K64F on-board switches
 InterruptIn ButtonA (PTB9);                                                     // PCB Button - A
 InterruptIn ButtonB (PTD0);                                                     // PCB Button - B
+InterruptIn ButtonBack (PTB10);                                                 // PCB Button - Back
 
 volatile int g_ButtonA_flag = 0;                                                // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
 volatile int g_ButtonB_flag = 0;                                                // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
+volatile int g_ButtonBack_flag = 0;                                             // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
 volatile int g_sw2_flag = 0;                                                    // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
 volatile int g_menu_timer_flag = 0;                                             // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
 volatile int option = 0;                                                        // Menu option selection based on joystick direction
@@ -55,6 +57,7 @@
 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();                                                               // 
@@ -123,6 +126,11 @@
     g_ButtonB_flag = 1;                                                         // set flag in ISR
 }
 
+void ButtonBack_isr()                                                           // ButtonB event-triggered interrupt
+{
+    g_ButtonBack_flag = 1;                                                      // set flag in ISR
+}
+
 void sw2_isr()                                                                  // SW2 event-triggered interrupt
 {
     g_sw2_flag = 1;                                                             // set flag in ISR
@@ -136,22 +144,22 @@
 void OnStartup()                                                                //Run some start up display 
 {
     lcd.clear();                                                                // clear buffer at start of every loop
-    lcd.printString("Smart Cold",0,0);                                          // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display)
-    lcd.printString("Storage",0,1);                                             // Just a welcome message before auto moving to main menu
-    lcd.printString("Monitoring",0,2);
-    lcd.printString("",0,2);                                                    // Blank Line
-    lcd.printString("V1.0 - 2021",0,4);
-    lcd.printString("David Leaming",0,5);
+    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("V07 - 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.printString("Use Joystick",0,0);                                        // Instruction for use of menu
-    lcd.printString("To Navigate",0,1);
+    lcd.printString(" Use Joystick",0,0);                                       // Instruction for use of menu
+    lcd.printString(" To Navigate",0,1);                                        //
     lcd.printString("",0,2);                                                    // Blank Line
-    lcd.printString("A = Select",0,3);  
+    lcd.printString("  A = Select",0,3);                                        //
     lcd.refresh();                                                              // need to refresh display after setting pixels or writing strings
-    wait(5.0);  
+    wait(5.0);                                                                  //
 }
 
 enum EMenuState                                                                 //An enum controlling the current state of the display.
@@ -160,6 +168,7 @@
     MENUSTATE_Monitor,
     MENUSTATE_OneOff,
     MENUSTATE_About,
+    MENUSTATE_Author,
     
     MENUSTATTE_Num,                                                             // This is a special enum value that just gives is a way to get the number of elements in the enum.
 };
@@ -170,6 +179,8 @@
     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;
@@ -188,7 +199,7 @@
                 case MENUSTATE_Main:
                 {
 
-                    NumMenuItems = 3;                                           // 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 = 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)
                     {
  
@@ -198,6 +209,7 @@
                     lcd.printString("M1 - Monitor",0,1);
                     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.
                     {
@@ -213,6 +225,10 @@
                         {
                             NewMenuState = MENUSTATE_About;
                         }
+                        else if(SelectedItem == 3)
+                        {
+                            NewMenuState = MENUSTATE_Author;
+                        }
                     }
                 }
                 break;      
@@ -226,11 +242,16 @@
                         NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
                     }
                     
-                    lcd.printString("-- Monitor --",0,0);
-                    lcd.printString("Back",0,1);                    
-                    
-                    //TODO: Whatever the monitor screen should display...
-                                        
+                    float T = tmp102.get_temperature();                         // read temperature and print to lcd
+                    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(buffer,0,3);                                // display on screen
+                    lcd.printString(" 'A' to Menu",0,5);
+                    lcd.refresh();                                              // need to refresh display after setting pixels or writing strings 
+                    wait(0.5);      
+                                  
                     if(bAButtonWasPressed)
                     {
                         if(SelectedItem == 0)
@@ -243,10 +264,10 @@
                 case MENUSTATE_OneOff:
                 {
 
-                    NumMenuItems = 1;                                           //TOVEY: 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;                                           // 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)
                     {
-                        NewMenuState = MENUSTATE_Main;                          //Something has gone wrong, drop back to the main menu.
+                        NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
                     }
                     
                     lcd.printString("-- One-off --",0,0);
@@ -273,8 +294,11 @@
                         NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
                     }
                     
-                    lcd.printString("-- About --",0,0);
-                    lcd.printString("Back",0,1);                    
+                    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);                    
                     
                     //TODO: Whatever the about screen should display...
                                         
@@ -287,6 +311,33 @@
                     }
                 }
                 break;
+                case MENUSTATE_Author:
+                {
+
+                    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)
+                    {
+                        NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
+                    }
+                    
+                    lcd.printString("--- Author ---",0,0);
+                    lcd.printString(" ",0,1);
+                    lcd.printString(" ",0,2);
+                    lcd.printString("",0,3);
+                    lcd.printString(" 'A' to Menu",0,5);                    
+                    
+                    //TODO: Whatever the about screen should display...
+                                        
+                    if(bAButtonWasPressed)
+                    {
+                        if(SelectedItem == 0)
+                        {
+                            NewMenuState = MENUSTATE_Main;
+                        }
+                    }
+                }
+                break;
+                
                 default:
                 {
                     NewMenuState = MENUSTATE_Main;                              // Something has gone wrong, drop back to the main menu.
@@ -307,11 +358,11 @@
             else
             {
                 
-                                                                                                                // If we have not selected to move to a new menu.
-                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.
-                lcd.drawCircle(SelectionMarkerX, SelectionMarkerY, SelectionMarkerRadius, FILL_BLACK);                                                             
+                                                                                                            // If we have not selected to move to a new menu.
+            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.
+            lcd.drawCircle(SelectionMarkerX, SelectionMarkerY, SelectionMarkerRadius, FILL_BLACK);                                                             
                             
                                                                                 // Handle Joystick Input
                 switch (joystick.get_direction())  {                            // Call to check direction joystick is pointing
@@ -342,7 +393,6 @@
 
 void ConstantMonitoring()              
 {        
-
     char buffer[14];                                                            // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
     while (1) {
         
@@ -352,12 +402,15 @@
         lcd.refresh();                                                          // need to refresh display after setting pixels or writing strings 
         
         float T = tmp102.get_temperature();                                     // read temperature and print to lcd
-        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
+        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(buffer,0,2);                                            // display on screen
+        lcd.printString("-- Constant --",0,0);
+        lcd.printString("- Monitoring -",0,1);
+        lcd.printString(buffer,0,3);                                            // display on screen
+        lcd.printString(" 'B' to Menu",0,5);
         lcd.refresh();                                                          // need to refresh display after setting pixels or writing strings 
-        wait(2.0);
-                    
+        wait(5.0);
+        
             if (g_ButtonA_flag) {                                               // check if flag i.e. interrupt has occured
             g_ButtonA_flag = 0;                                                 // if it has, clear the flag
 
@@ -371,6 +424,13 @@
             g_ButtonB_flag = 0;                                                 // if it has, clear the flag
 
             printf("Button B Press \n");                                        // send message over serial port - can observe in CoolTerm etc.
+            Run(); 
+            }
+            
+            if (g_ButtonBack_flag) {                                            // check if flag i.e. interrupt has occured
+            g_ButtonBack_flag = 0;                                              // if it has, clear the flag
+
+            printf("Button Back Press \n");                                     // send message over serial port - can observe in CoolTerm etc.
             LED02=0;
             wait(5.0);
             LED02=1;