Updated following fixing of Joystick 'Bounce'. Also insertion of a Starting Temp to be compared against following user selection.

Dependencies:   mbed N5110v02 TMP102 JoystickIoT

Files at this revision

API Documentation at this revision

Comitter:
legstar85
Date:
Thu Jan 20 22:57:14 2022 +0000
Parent:
11:a6eec09b546e
Commit message:
Updated following fixing of Joystick 'Bounce'. Also insertion of a Starting Temp to be compared against following user selection.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Jan 20 13:42:30 2022 +0000
+++ b/main.cpp	Thu Jan 20 22:57:14 2022 +0000
@@ -30,17 +30,17 @@
 };
 
 State fsm[11] = {
-    {15,0.5,{0,1,0,0,0,10,0,0,0}},                                                       // Start Temp = 15 Degrees
-    {16,0.5,{1,2,1,1,1,0,1,1,1}},                                                       // 1 
-    {17,0.5,{2,3,2,2,2,1,2,2,2}},                                                    // 
-    {18,0.5,{3,4,3,3,3,2,3,3,3}},                                                // 
-    {19,0.5,{4,5,4,4,4,3,4,4,4}},                                                // 
-    {20,0.5,{5,6,5,5,5,4,5,5,5}},                                                // 
-    {21,0.5,{6,7,6,6,6,5,6,6,6}},                                                // 
-    {22,0.5,{7,8,7,7,7,6,7,7,7}},                                                // 
-    {23,0.5,{8,9,8,8,8,7,8,8,8}},                                                // 
-    {24,0.5,{9,10,9,9,9,8,9,9,9}},                                                // 
-    {25,0.5,{10,1,10,10,10,9,10,10,10}}                                                // 
+    {15,0.5,{0,1,0,0,0,10,0,0,0}},                                              // State 0 - 15 Degrees
+    {16,0.5,{1,2,1,1,1,0,1,1,1}},                                               // State 1 - 16 Degrees
+    {17,0.5,{2,3,2,2,2,1,2,2,2}},                                               // State 2 - 17 Degrees
+    {18,0.5,{3,4,3,3,3,2,3,3,3}},                                               // State 3 - 18 Degrees
+    {19,0.5,{4,5,4,4,4,3,4,4,4}},                                               // State 4 - 19 Degrees
+    {20,0.5,{5,6,5,5,5,4,5,5,5}},                                               // State 5 - 20 Degrees
+    {21,0.5,{6,7,6,6,6,5,6,6,6}},                                               // State 6 - 21 Degrees
+    {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
 };
 
 Ticker ticker_menu;                                                             // Create Menu ticker object
@@ -64,11 +64,11 @@
 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 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
 volatile int g_state = 0;                                                       // 
 volatile int g_StartTemp = 0;                                                   // 
@@ -84,7 +84,6 @@
 void menu_timer_isr();                                                          // 
 void OnStartup();                                                               // 
 void Run();                                                                     // 
-void ConstantMonitoring();                                                      //   
 void StartTemp();                                                               //
 
 int main()
@@ -165,22 +164,22 @@
 
 void OnStartup()                                                                // Run some start up 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 
+    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("V11 - Jan 2022",0,4);                                      //
+    lcd.printString("V12 - 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
@@ -199,7 +198,7 @@
 
 enum EMenuState                                                                 // An enum controlling the current state of the display.
 {
-    MENUSTATE_StartTemp,                                                        //
+    MENUSTATE_StartTemp,                                                        // Defining each menu state to be called upon later
     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
@@ -233,7 +232,7 @@
         
             switch(MenuState)                                                   // Update and Draw whichever menu we're on.
             {      
-                case MENUSTATE_StartTemp:                                            //
+                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.
@@ -242,23 +241,22 @@
  
                         SelectedItem = 0;                                       // Something has gone wrong, reset selected item.
                     }
-                              Direction d = joystick.get_direction();                               
+                    Direction d = joystick.get_direction();                               
                     
                     StartTemp();
                     
                     float g_StartTemp = fsm[g_state].output;                    // read temperature and print to lcd
-                    pc.printf("T = %f K\n",g_state);                            // Print to serial - allows testing without device attached
+                    pc.printf("T = %f C\n",g_StartTemp);                        // Print to serial - allows testing without device attached
                     printf ("Joystick Direction Points = %i\n",d);              //
-                    printf ("State selected = %i\n", g_StartTemp);                          //
+                    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.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.
                     {
                         if(SelectedItem == 0)                                   // If 0 line is selected, move to detailed menu 
@@ -326,7 +324,7 @@
                     wait(0.5);                                                  // 
                     
                     float high_temp = tmp102.get_temperature();                 // read temperature   
-                    if (high_temp >= 25) {                                      // High temp alarm condition - in real world would be lot lower!!
+                    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
@@ -340,7 +338,7 @@
                     } 
                     
                     float low_temp = tmp102.get_temperature();                  // Read temperature   
-                    if (low_temp <= 22) {                                       // Low temp alarm condition - in real world would be lot lower!!
+                    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
@@ -500,9 +498,7 @@
 void StartTemp()                                                                //
 {
     Direction d = joystick.get_direction();                                     //
-    
-    joystick.init();
-         
+             
         g_StartTemp = fsm[g_state].output;                                      // set ouput depending on current state
         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