Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- hudakz
- Date:
- 2015-12-08
- Revision:
- 0:f7514c3f33b6
- Child:
- 1:cc0fa853f718
File content as of revision 0:f7514c3f33b6:
/* * An example of using the Menu library - Simple Menu system with push buttons. * * The Menu library was created to facilitate designs with a display and push buttons. * - number of menus and push buttons is practically not limited by the software * - function of individual push buttons varies depending on the selected menu * * --------------------------------- * | | * | ---------------------------- | * | | | | * | | | | * | | | | * | | Display | | * | | | | * | | | | * | | | | * | ---------------------------- | * | O O O O | * | Btn1 Btn2 Btn3 Btn4 | * | | * ---------------------------------- * * Each push button must be connected to an mbed digital input pin and the ground. * * * ------------------------- * | * | mbed board * | * ------------------------- * | Input pin * | * o * / Button ... * o * | * --- * GND * * * NOTE: When creating an object the MenuSystem constructor connects an internal pull-up resistor * to each digital input used by the MenuSytem push buttons. */ #include "mbed.h" #include "Menu.h" Serial pc(USBTX, USBRX); /* * Some variables needed for this example */ int setpoint; int* p_setpoint; int loSetpoint = 20; int hiSetpoint = 50; /* * Create an array of push buttons to be used with the menu system */ #if defined(TARGET_LPC1768) DigitalIn buttons[] = { DigitalIn(p21), DigitalIn(p22), DigitalIn(p23), DigitalIn(p24) }; #elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8) \ || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8) \ || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_F072RB) \ || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB) \ || defined(TARGET_KL25Z ) || defined(TARGET_KL46Z) || defined(TARGET_K64F) || defined(TARGET_KL05Z) \ || defined(TARGET_K20D50M) || defined(TARGET_K22F) \ || defined(TARGET_NRF51822) \ || defined(TARGET_RZ_A1H) DigitalIn buttons[] = { DigitalIn(D2), DigitalIn(D3), DigitalIn(D4), DigitalIn(D5) }; #endif /* * Assign alias names to the buttons (just for the convenience) */ DigitalIn& btnMode = buttons[0]; // Mode DigitalIn& btnMore = buttons[1]; // More + DigitalIn& btnLess = buttons[2]; // Less - DigitalIn& btnSave = buttons[3]; // Save /* * Create a menu system object associated with the push buttons */ MenuSystem menuSystem(buttons, 4 /*, 300*/); // name of button array, number of buttons, debounce time in ms /* * Create as many menus as you need. * * In this example we create the following three menus: * 1. 'Main' (root) menu * 2. 'Set high setpoint' menu * 3. 'Set low setpoint' menu * * NOTE: The function of individual push buttons will vary depending on the selected menu. */ Menu menuMain(menuSystem); Menu menuSetHiSetpoint(menuSystem); Menu menuSetLoSetpoint(menuSystem); /* * Declare all functions to be called on 'button pressed' events */ void openMenu_Main(void); void openMenu_SetHiSetpoint(void); void openMenu_SetLoSetpoint(void); void incrementSetpoint(void); void decrementSetpoint(void); void saveSetpoint(void); /** * @brief * @note * @param * @retval */ int main(void) { // Attach 'button pressed' event handler functions to the individual push buttons. // NOTE: In different menus the same button can be linked with different function. // It is not compulsory to attach a function to all push buttons in each menu. // // 'Main' menu: menuMain.attach(&btnMode, &openMenu_SetHiSetpoint); // // 'Set High Setpoint' menu: menuSetHiSetpoint.attach(&btnMode, &openMenu_SetLoSetpoint); menuSetHiSetpoint.attach(&btnMore, &incrementSetpoint); menuSetHiSetpoint.attach(&btnLess, &decrementSetpoint); menuSetHiSetpoint.attach(&btnSave, &saveSetpoint); // // 'Set Low Setpoint' menu: menuSetLoSetpoint.attach(&btnMode, &openMenu_Main); menuSetLoSetpoint.attach(&btnMore, &incrementSetpoint); menuSetLoSetpoint.attach(&btnLess, &decrementSetpoint); menuSetLoSetpoint.attach(&btnSave, &saveSetpoint); openMenu_Main(); // activate main (root) menu while(1) { menuSystem.handleButtons(); // Handle 'button pressed' events //.. } } /* * Lets define te menu functions: * NOTE: For the sake of simplicity, in this example we do not connect any display to the mbed. * Instead we'll utilize a serial terminal running on the connected PC. */ /** * @brief * @note None * @param None * @retval None */ void openMenu_Main(void) { menuSystem.open(menuMain); // activate the menu pc.printf("---------------------------------------------------\r\n"); pc.printf("Main menu:\r\n\r\n"); pc.printf("loSetpoint = %i\r\n", loSetpoint); pc.printf("hiSetpoint = %i\r\n", hiSetpoint); pc.printf("\r\n"); pc.printf("Press 'Mode' to open 'High setpoint' menu.\r\n"); pc.printf("\r\n"); } /** * @brief * @note * @param * @retval */ void openMenu_SetHiSetpoint(void) { menuSystem.open(menuSetHiSetpoint); // activate the menu pc.printf("---------------------------------------------------\r\n"); pc.printf("High setpoint menu:\r\n"); pc.printf("Press 'More' to increment high setpoint.\r\n"); pc.printf("Press 'Less' to decrement high setpoint.\r\n"); pc.printf("Press 'Save' to save the setoint.\r\n"); pc.printf("Press 'Mode' to open 'Low setpoint' menu.\r\n"); pc.printf("\r\n"); p_setpoint = &hiSetpoint; setpoint = hiSetpoint; } /** * @brief * @note * @param * @retval */ void openMenu_SetLoSetpoint(void) { menuSystem.open(menuSetLoSetpoint); // activate the menu pc.printf("---------------------------------------------------\r\n"); pc.printf("Low setpoint menu:\r\n"); pc.printf("Press 'More' to increment low setpoint.\r\n"); pc.printf("Press 'Less' to decrement low setpoint.\r\n"); pc.printf("Press 'Save' to save the setpoint.\r\n"); pc.printf("Press 'Mode' to open Main menu.\r\n"); pc.printf("\r\n"); p_setpoint = &loSetpoint; setpoint = loSetpoint; } /** * @brief * @note * @param * @retval */ void incrementSetpoint(void) { setpoint++; pc.printf("Setpoint incremented\r\n"); pc.printf("setpoint = %i\r\n", setpoint); } /** * @brief * @note * @param * @retval */ void decrementSetpoint(void) { setpoint--; pc.printf("Setpoint decremented\r\n"); pc.printf("setpoint = %i\r\n", setpoint); } /** * @brief * @note * @param * @retval */ void saveSetpoint(void) { *p_setpoint = setpoint; pc.printf("Setpoint saved\r\n"); pc.printf("\r\n"); }