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:
8:07323fcab6d1
Parent:
7:d77e95505e43
Child:
9:d71b92c916f8
diff -r d77e95505e43 -r 07323fcab6d1 main.cpp
--- a/main.cpp	Fri Jan 14 15:27:19 2022 +0000
+++ b/main.cpp	Sun Jan 16 22:46:29 2022 +0000
@@ -31,12 +31,14 @@
 DigitalOut g_led(LED_GREEN);                                                    // K64F on-board LEDs 
 DigitalOut b_led(LED_BLUE);                                                     // K64F on-board LEDs 
 
-DigitalOut LED01 (PTA1);                                                        // PCB Surface Mounted LED's - LED1
-DigitalOut LED02 (PTA2);                                                        // PCB Surface Mounted LED's - LED2
-DigitalOut LED03 (PTC2);                                                        // PCB Surface Mounted LED's - LED3
-DigitalOut LED04 (PTC3);                                                        // PCB Surface Mounted LED's - LED4
-DigitalOut LED05 (PTC4);                                                        // PCB Surface Mounted LED's - LED5
-DigitalOut LED06 (PTD3);                                                        // PCB Surface Mounted LED's - LED6
+PwmOut LED01 (PTA1);                                                            // PCB Surface Mounted LED's - LED1
+PwmOut LED02 (PTA2);                                                            // PCB Surface Mounted LED's - LED2
+PwmOut LED03 (PTC2);                                                            // PCB Surface Mounted LED's - LED3
+PwmOut LED04 (PTC3);                                                            // PCB Surface Mounted LED's - LED4
+PwmOut LED05 (PTC4);                                                            // PCB Surface Mounted LED's - LED5
+PwmOut LED06 (PTD3);                                                            // PCB Surface Mounted LED's - LED6
+
+PwmOut Buzzer (PTC10);                                                          // PCB Surface Mounted Piezo Buzzer
 
 InterruptIn sw2(SW2);                                                           // K64F on-board switches
 InterruptIn sw3(SW3);                                                           // K64F on-board switches
@@ -148,16 +150,18 @@
     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("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.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("--------------",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
     wait(5.0);                                                                  //
 }
@@ -213,20 +217,20 @@
                     
                     if(bAButtonWasPressed)                                      // If A was pressed then we transition to the selected screen.
                     {
-                        if(SelectedItem == 0)
-                        {
+                        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;
                         }
-                        else if(SelectedItem == 1)
-                        {
+                        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;
                         }
-                        else if(SelectedItem == 2)
-                        {
+                        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;
                         }
-                        else if(SelectedItem == 3)
-                        {
+                        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;
                         }
                     }
@@ -235,7 +239,7 @@
                 case MENUSTATE_Monitor:
                 {
 
-                    NumMenuItems = 1;                                           // Dont for get to set this when changing number of items in the menu. We need this to wrap the selection around properly etc.
+                    NumMenuItems = 1;                                           // Detail the number of items in Menu -  need this to wrap the selection around properly etc.
                     
                     if(SelectedItem >= NumMenuItems)
                     {
@@ -250,7 +254,19 @@
                     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);      
+                    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
+                    } 
+                    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
+                    } 
                                   
                     if(bAButtonWasPressed)
                     {
@@ -264,22 +280,25 @@
                 case MENUSTATE_OneOff:
                 {
 
-                    NumMenuItems = 1;                                           // Dont for get to set this when changing number of items in the menu. We need this to wrap the selection around properly etc.
+                    NumMenuItems = 1;                                           // Detail the number of items in Menu -  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("-- One-off --",0,0);
-                    lcd.printString("Back",0,1);                    
-                    
-                    //TODO: Whatever the One-off 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("-- 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)
                     {
                         if(SelectedItem == 0)
                         {
-                            NewMenuState = MENUSTATE_Main;
+                            NewMenuState = MENUSTATE_Main;                      // Take us back to top menu
                         }
                     }                   
                     
@@ -288,7 +307,7 @@
                 case MENUSTATE_About:
                 {
 
-                    NumMenuItems = 1;                                           // Dont for get to set this when changing number of items in the menu. We need this to wrap the selection around properly etc.
+                    NumMenuItems = 1;                                           // Detail the number of items in Menu -  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.
@@ -298,9 +317,8 @@
                     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...
+                    lcd.printString(" 'A' to Menu",0,5); 
+                    lcd.refresh();                       
                                         
                     if(bAButtonWasPressed)
                     {
@@ -314,25 +332,23 @@
                 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.
+                    NumMenuItems = 1;                                           // Detail the number of items in Menu -  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("David Leaming ",0,1);
+                    lcd.printString("   25574043 ",0,2);
+                    lcd.printString("  VolkerRail",0,3);
                     lcd.printString(" 'A' to Menu",0,5);                    
-                    
-                    //TODO: Whatever the about screen should display...
                                         
                     if(bAButtonWasPressed)
                     {
                         if(SelectedItem == 0)
                         {
-                            NewMenuState = MENUSTATE_Main;
+                            NewMenuState = MENUSTATE_Main;                      // Take us back to top menu 
                         }
                     }
                 }
@@ -346,7 +362,7 @@
             
             if(NewMenuState != MENUSTATTE_Num)                                  // If we have requested a new menu state.
             {
-                printf("Transitioning to MenuState: %i\n", NewMenuState);       //
+                printf("Transitioning to MenuState: %i\n", NewMenuState);       // Observe on serial port - ensure transition to correct screen
                 
                 MenuState = NewMenuState;                                       // We want to transition the menu to a new state.
                 
@@ -361,7 +377,7 @@
                                                                                                             // 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.
+            unsigned int SelectionMarkerY = (HEIGHT / 5) * (SelectedItem + 1);                              // +1 because of the menu title being on first row
             lcd.drawCircle(SelectionMarkerX, SelectionMarkerY, SelectionMarkerRadius, FILL_BLACK);                                                             
                             
                                                                                 // Handle Joystick Input
@@ -391,49 +407,3 @@
 }  
  
 
-void ConstantMonitoring()              
-{        
-    char buffer[14];                                                            // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
-    while (1) {
-        
-        lcd.normalMode();                                                       // normal colour mode
-        lcd.setBrightness(0.5);                                                 // put LED backlight on 50%
-        lcd.clear();                                                            // clear buffer at start of every loop
-        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
-        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(" 'B' to Menu",0,5);
-        lcd.refresh();                                                          // need to refresh display after setting pixels or writing strings 
-        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
-
-            printf("Button A Press \n");                                        // send message over serial port - can observe in CoolTerm etc.
-            LED01=0;
-            wait(5.0);
-            LED01=1;     
-            }
-      
-            if (g_ButtonB_flag) {                                               // check if flag i.e. interrupt has occured
-            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;  
-            }
-}    
-}  
\ No newline at end of file