![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
New version - adding of lights/controls to temperature monitoring
Dependencies: mbed N5110v02 TMP102 JoystickIoT
Diff: main.cpp
- Revision:
- 6:ebf4784ce92b
- Parent:
- 5:eebd36a47049
- Child:
- 7:d77e95505e43
diff -r eebd36a47049 -r ebf4784ce92b main.cpp --- a/main.cpp Fri Jan 07 23:14:23 2022 +0000 +++ b/main.cpp Fri Jan 14 10:41:46 2022 +0000 @@ -57,6 +57,9 @@ void ButtonB_isr(); // void sw2_isr(); // void menu_timer_isr(); // +void OnStartup(); // +void Run(); // +void ConstantMonitoring(); // int main() { @@ -76,27 +79,8 @@ lcd.setContrast(0.5); // change set contrast in range 0.0 to 1.0 - char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) - - 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.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.refresh(); // need to refresh display after setting pixels or writing strings - wait(5.0); - - MainMenu(); // + OnStartup(); // + Run(); // } @@ -146,54 +130,220 @@ void menu_timer_isr() { - g_menu_timer_flag = 1; // set flag in ISR + g_menu_timer_flag = 1; // set flag in ISR +} + +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.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.refresh(); // need to refresh display after setting pixels or writing strings + wait(5.0); } -void MainMenu() +enum EMenuState //An enum controlling the current state of the display. +{ + MENUSTATE_Main, + MENUSTATE_Monitor, + MENUSTATE_OneOff, + MENUSTATE_About, + + 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() { - while(1) { - if (g_menu_timer_flag){ + int MenuState; + int SelectedItem = 0; + int NumMenuItems = 1; + + while(1){ + 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; + lcd.clear(); // clear buffer at start of every loop - joystick.init(); // initialise joystick - - lcd.printString("---- MENU ----",0,0); // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display) - lcd.printString("M1 - Monitor",0,1); // - lcd.printString("M2 - One-off",0,2); // - lcd.printString("About",0,3); // - lcd.refresh(); // + + 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_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. + 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); + + if(bAButtonWasPressed) // If A was pressed then we transition to the selected screen. + { + if(SelectedItem == 0) + { + NewMenuState = MENUSTATE_Monitor; + } + else if(SelectedItem == 1) + { + NewMenuState = MENUSTATE_OneOff; + } + else if(SelectedItem == 2) + { + NewMenuState = MENUSTATE_About; + } + } + } + break; + 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. + + if(SelectedItem >= NumMenuItems) + { + 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... + + if(bAButtonWasPressed) + { + if(SelectedItem == 0) + { + NewMenuState = MENUSTATE_Main; + } + } + } + break; + 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. + 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... + + if(bAButtonWasPressed) + { + if(SelectedItem == 0) + { + NewMenuState = MENUSTATE_Main; + } + } + + } + break; + 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. + if(SelectedItem >= NumMenuItems) + { + NewMenuState = MENUSTATE_Main; // Something has gone wrong, drop back to the main menu. + } + + lcd.printString("-- About --",0,0); + lcd.printString("Back",0,1); + + //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. + } + }; - switch (joystick.get_direction()) { // Call to check direction joystick is pointing - printf("Direction = %i\n"); // - case N: // - option--; // - break; // - case S: // - option++; // - break; // - } - if (option < 0) { // If last/bottom menu item is selcted and user presses joystick down - option = 2; // Selection circle to move to first/top menu option - } - if (option < 2) { // If first/top menu item is selcted and user presses joystick up - option = 0; // Selection circle to move to last/bottom menu option + if(NewMenuState != MENUSTATTE_Num) // If we have requested a new menu state. + { + printf("Transitioning to MenuState: %i\n", NewMenuState); // + + MenuState = NewMenuState; // We want to transition the menu to a new state. + + // Do any bookkeeping needed when moving to new state. + SelectedItem = 0; // Reset the selected item. + + lcd.clear(); // Clear the display for one frame on state transition. + } // Could do something else like invert colour etc here. + 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); + + // Handle Joystick Input + switch (joystick.get_direction()) { // Call to check direction joystick is pointing + printf("Direction = %i\n"); // + case N: // + SelectedItem--; // + break; // + case S: // + SelectedItem++; // + break; // + } + + 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; + } + else if(SelectedItem >= NumMenuItems) + { + SelectedItem = 0; + } } - if (option == 1) { // - lcd.drawCircle (55,20,2,0); // - } - if (option == 2) { // - lcd.drawCircle (35,35,2,0); // - } - if (option == 3) { // - lcd.drawCircle (20,50,2,0); // - } - + lcd.refresh(); //Finally update the display. + } + } } + 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 @@ -225,5 +375,5 @@ wait(5.0); LED02=1; } - } -} \ No newline at end of file +} +} \ No newline at end of file