Zoltan Hudak / Mbed 2 deprecated Menu_Hello

Dependencies:   Menu mbed

Committer:
hudakz
Date:
Tue Dec 08 16:26:37 2015 +0000
Revision:
0:f7514c3f33b6
Child:
1:cc0fa853f718
Demo of Menu library - Simple menu system with push buttons.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:f7514c3f33b6 1 /*
hudakz 0:f7514c3f33b6 2 * An example of using the Menu library - Simple Menu system with push buttons.
hudakz 0:f7514c3f33b6 3 *
hudakz 0:f7514c3f33b6 4 * The Menu library was created to facilitate designs with a display and push buttons.
hudakz 0:f7514c3f33b6 5 * - number of menus and push buttons is practically not limited by the software
hudakz 0:f7514c3f33b6 6 * - function of individual push buttons varies depending on the selected menu
hudakz 0:f7514c3f33b6 7 *
hudakz 0:f7514c3f33b6 8 * ---------------------------------
hudakz 0:f7514c3f33b6 9 * | |
hudakz 0:f7514c3f33b6 10 * | ---------------------------- |
hudakz 0:f7514c3f33b6 11 * | | | |
hudakz 0:f7514c3f33b6 12 * | | | |
hudakz 0:f7514c3f33b6 13 * | | | |
hudakz 0:f7514c3f33b6 14 * | | Display | |
hudakz 0:f7514c3f33b6 15 * | | | |
hudakz 0:f7514c3f33b6 16 * | | | |
hudakz 0:f7514c3f33b6 17 * | | | |
hudakz 0:f7514c3f33b6 18 * | ---------------------------- |
hudakz 0:f7514c3f33b6 19 * | O O O O |
hudakz 0:f7514c3f33b6 20 * | Btn1 Btn2 Btn3 Btn4 |
hudakz 0:f7514c3f33b6 21 * | |
hudakz 0:f7514c3f33b6 22 * ----------------------------------
hudakz 0:f7514c3f33b6 23 *
hudakz 0:f7514c3f33b6 24 * Each push button must be connected to an mbed digital input pin and the ground.
hudakz 0:f7514c3f33b6 25 *
hudakz 0:f7514c3f33b6 26 *
hudakz 0:f7514c3f33b6 27 * -------------------------
hudakz 0:f7514c3f33b6 28 * |
hudakz 0:f7514c3f33b6 29 * | mbed board
hudakz 0:f7514c3f33b6 30 * |
hudakz 0:f7514c3f33b6 31 * -------------------------
hudakz 0:f7514c3f33b6 32 * | Input pin
hudakz 0:f7514c3f33b6 33 * |
hudakz 0:f7514c3f33b6 34 * o
hudakz 0:f7514c3f33b6 35 * / Button ...
hudakz 0:f7514c3f33b6 36 * o
hudakz 0:f7514c3f33b6 37 * |
hudakz 0:f7514c3f33b6 38 * ---
hudakz 0:f7514c3f33b6 39 * GND
hudakz 0:f7514c3f33b6 40 *
hudakz 0:f7514c3f33b6 41 *
hudakz 0:f7514c3f33b6 42 * NOTE: When creating an object the MenuSystem constructor connects an internal pull-up resistor
hudakz 0:f7514c3f33b6 43 * to each digital input used by the MenuSytem push buttons.
hudakz 0:f7514c3f33b6 44 */
hudakz 0:f7514c3f33b6 45
hudakz 0:f7514c3f33b6 46 #include "mbed.h"
hudakz 0:f7514c3f33b6 47 #include "Menu.h"
hudakz 0:f7514c3f33b6 48
hudakz 0:f7514c3f33b6 49 Serial pc(USBTX, USBRX);
hudakz 0:f7514c3f33b6 50
hudakz 0:f7514c3f33b6 51
hudakz 0:f7514c3f33b6 52 /*
hudakz 0:f7514c3f33b6 53 * Some variables needed for this example
hudakz 0:f7514c3f33b6 54 */
hudakz 0:f7514c3f33b6 55 int setpoint;
hudakz 0:f7514c3f33b6 56 int* p_setpoint;
hudakz 0:f7514c3f33b6 57 int loSetpoint = 20;
hudakz 0:f7514c3f33b6 58 int hiSetpoint = 50;
hudakz 0:f7514c3f33b6 59
hudakz 0:f7514c3f33b6 60 /*
hudakz 0:f7514c3f33b6 61 * Create an array of push buttons to be used with the menu system
hudakz 0:f7514c3f33b6 62 */
hudakz 0:f7514c3f33b6 63 #if defined(TARGET_LPC1768)
hudakz 0:f7514c3f33b6 64 DigitalIn buttons[] = { DigitalIn(p21), DigitalIn(p22), DigitalIn(p23), DigitalIn(p24) };
hudakz 0:f7514c3f33b6 65 #elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8) \
hudakz 0:f7514c3f33b6 66 || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8) \
hudakz 0:f7514c3f33b6 67 || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_F072RB) \
hudakz 0:f7514c3f33b6 68 || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB) \
hudakz 0:f7514c3f33b6 69 || defined(TARGET_KL25Z ) || defined(TARGET_KL46Z) || defined(TARGET_K64F) || defined(TARGET_KL05Z) \
hudakz 0:f7514c3f33b6 70 || defined(TARGET_K20D50M) || defined(TARGET_K22F) \
hudakz 0:f7514c3f33b6 71 || defined(TARGET_NRF51822) \
hudakz 0:f7514c3f33b6 72 || defined(TARGET_RZ_A1H)
hudakz 0:f7514c3f33b6 73 DigitalIn buttons[] = { DigitalIn(D2), DigitalIn(D3), DigitalIn(D4), DigitalIn(D5) };
hudakz 0:f7514c3f33b6 74 #endif
hudakz 0:f7514c3f33b6 75
hudakz 0:f7514c3f33b6 76 /*
hudakz 0:f7514c3f33b6 77 * Assign alias names to the buttons (just for the convenience)
hudakz 0:f7514c3f33b6 78 */
hudakz 0:f7514c3f33b6 79 DigitalIn& btnMode = buttons[0]; // Mode
hudakz 0:f7514c3f33b6 80 DigitalIn& btnMore = buttons[1]; // More +
hudakz 0:f7514c3f33b6 81 DigitalIn& btnLess = buttons[2]; // Less -
hudakz 0:f7514c3f33b6 82 DigitalIn& btnSave = buttons[3]; // Save
hudakz 0:f7514c3f33b6 83
hudakz 0:f7514c3f33b6 84 /*
hudakz 0:f7514c3f33b6 85 * Create a menu system object associated with the push buttons
hudakz 0:f7514c3f33b6 86 */
hudakz 0:f7514c3f33b6 87 MenuSystem menuSystem(buttons, 4 /*, 300*/); // name of button array, number of buttons, debounce time in ms
hudakz 0:f7514c3f33b6 88
hudakz 0:f7514c3f33b6 89 /*
hudakz 0:f7514c3f33b6 90 * Create as many menus as you need.
hudakz 0:f7514c3f33b6 91 *
hudakz 0:f7514c3f33b6 92 * In this example we create the following three menus:
hudakz 0:f7514c3f33b6 93 * 1. 'Main' (root) menu
hudakz 0:f7514c3f33b6 94 * 2. 'Set high setpoint' menu
hudakz 0:f7514c3f33b6 95 * 3. 'Set low setpoint' menu
hudakz 0:f7514c3f33b6 96 *
hudakz 0:f7514c3f33b6 97 * NOTE: The function of individual push buttons will vary depending on the selected menu.
hudakz 0:f7514c3f33b6 98 */
hudakz 0:f7514c3f33b6 99 Menu menuMain(menuSystem);
hudakz 0:f7514c3f33b6 100 Menu menuSetHiSetpoint(menuSystem);
hudakz 0:f7514c3f33b6 101 Menu menuSetLoSetpoint(menuSystem);
hudakz 0:f7514c3f33b6 102
hudakz 0:f7514c3f33b6 103 /*
hudakz 0:f7514c3f33b6 104 * Declare all functions to be called on 'button pressed' events
hudakz 0:f7514c3f33b6 105 */
hudakz 0:f7514c3f33b6 106 void openMenu_Main(void);
hudakz 0:f7514c3f33b6 107 void openMenu_SetHiSetpoint(void);
hudakz 0:f7514c3f33b6 108 void openMenu_SetLoSetpoint(void);
hudakz 0:f7514c3f33b6 109 void incrementSetpoint(void);
hudakz 0:f7514c3f33b6 110 void decrementSetpoint(void);
hudakz 0:f7514c3f33b6 111 void saveSetpoint(void);
hudakz 0:f7514c3f33b6 112
hudakz 0:f7514c3f33b6 113 /**
hudakz 0:f7514c3f33b6 114 * @brief
hudakz 0:f7514c3f33b6 115 * @note
hudakz 0:f7514c3f33b6 116 * @param
hudakz 0:f7514c3f33b6 117 * @retval
hudakz 0:f7514c3f33b6 118 */
hudakz 0:f7514c3f33b6 119 int main(void) {
hudakz 0:f7514c3f33b6 120
hudakz 0:f7514c3f33b6 121 // Attach 'button pressed' event handler functions to the individual push buttons.
hudakz 0:f7514c3f33b6 122 // NOTE: In different menus the same button can be linked with different function.
hudakz 0:f7514c3f33b6 123 // It is not compulsory to attach a function to all push buttons in each menu.
hudakz 0:f7514c3f33b6 124 //
hudakz 0:f7514c3f33b6 125
hudakz 0:f7514c3f33b6 126 // 'Main' menu:
hudakz 0:f7514c3f33b6 127 menuMain.attach(&btnMode, &openMenu_SetHiSetpoint);
hudakz 0:f7514c3f33b6 128
hudakz 0:f7514c3f33b6 129 //
hudakz 0:f7514c3f33b6 130 // 'Set High Setpoint' menu:
hudakz 0:f7514c3f33b6 131 menuSetHiSetpoint.attach(&btnMode, &openMenu_SetLoSetpoint);
hudakz 0:f7514c3f33b6 132 menuSetHiSetpoint.attach(&btnMore, &incrementSetpoint);
hudakz 0:f7514c3f33b6 133 menuSetHiSetpoint.attach(&btnLess, &decrementSetpoint);
hudakz 0:f7514c3f33b6 134 menuSetHiSetpoint.attach(&btnSave, &saveSetpoint);
hudakz 0:f7514c3f33b6 135
hudakz 0:f7514c3f33b6 136 //
hudakz 0:f7514c3f33b6 137 // 'Set Low Setpoint' menu:
hudakz 0:f7514c3f33b6 138 menuSetLoSetpoint.attach(&btnMode, &openMenu_Main);
hudakz 0:f7514c3f33b6 139 menuSetLoSetpoint.attach(&btnMore, &incrementSetpoint);
hudakz 0:f7514c3f33b6 140 menuSetLoSetpoint.attach(&btnLess, &decrementSetpoint);
hudakz 0:f7514c3f33b6 141 menuSetLoSetpoint.attach(&btnSave, &saveSetpoint);
hudakz 0:f7514c3f33b6 142
hudakz 0:f7514c3f33b6 143 openMenu_Main(); // activate main (root) menu
hudakz 0:f7514c3f33b6 144
hudakz 0:f7514c3f33b6 145 while(1) {
hudakz 0:f7514c3f33b6 146 menuSystem.handleButtons(); // Handle 'button pressed' events
hudakz 0:f7514c3f33b6 147
hudakz 0:f7514c3f33b6 148 //..
hudakz 0:f7514c3f33b6 149 }
hudakz 0:f7514c3f33b6 150 }
hudakz 0:f7514c3f33b6 151
hudakz 0:f7514c3f33b6 152 /*
hudakz 0:f7514c3f33b6 153 * Lets define te menu functions:
hudakz 0:f7514c3f33b6 154
hudakz 0:f7514c3f33b6 155 * NOTE: For the sake of simplicity, in this example we do not connect any display to the mbed.
hudakz 0:f7514c3f33b6 156 * Instead we'll utilize a serial terminal running on the connected PC.
hudakz 0:f7514c3f33b6 157 */
hudakz 0:f7514c3f33b6 158
hudakz 0:f7514c3f33b6 159 /**
hudakz 0:f7514c3f33b6 160 * @brief
hudakz 0:f7514c3f33b6 161 * @note None
hudakz 0:f7514c3f33b6 162 * @param None
hudakz 0:f7514c3f33b6 163 * @retval None
hudakz 0:f7514c3f33b6 164 */
hudakz 0:f7514c3f33b6 165 void openMenu_Main(void) {
hudakz 0:f7514c3f33b6 166 menuSystem.open(menuMain); // activate the menu
hudakz 0:f7514c3f33b6 167 pc.printf("---------------------------------------------------\r\n");
hudakz 0:f7514c3f33b6 168 pc.printf("Main menu:\r\n\r\n");
hudakz 0:f7514c3f33b6 169 pc.printf("loSetpoint = %i\r\n", loSetpoint);
hudakz 0:f7514c3f33b6 170 pc.printf("hiSetpoint = %i\r\n", hiSetpoint);
hudakz 0:f7514c3f33b6 171 pc.printf("\r\n");
hudakz 0:f7514c3f33b6 172 pc.printf("Press 'Mode' to open 'High setpoint' menu.\r\n");
hudakz 0:f7514c3f33b6 173 pc.printf("\r\n");
hudakz 0:f7514c3f33b6 174 }
hudakz 0:f7514c3f33b6 175
hudakz 0:f7514c3f33b6 176 /**
hudakz 0:f7514c3f33b6 177 * @brief
hudakz 0:f7514c3f33b6 178 * @note
hudakz 0:f7514c3f33b6 179 * @param
hudakz 0:f7514c3f33b6 180 * @retval
hudakz 0:f7514c3f33b6 181 */
hudakz 0:f7514c3f33b6 182 void openMenu_SetHiSetpoint(void) {
hudakz 0:f7514c3f33b6 183 menuSystem.open(menuSetHiSetpoint); // activate the menu
hudakz 0:f7514c3f33b6 184 pc.printf("---------------------------------------------------\r\n");
hudakz 0:f7514c3f33b6 185 pc.printf("High setpoint menu:\r\n");
hudakz 0:f7514c3f33b6 186 pc.printf("Press 'More' to increment high setpoint.\r\n");
hudakz 0:f7514c3f33b6 187 pc.printf("Press 'Less' to decrement high setpoint.\r\n");
hudakz 0:f7514c3f33b6 188 pc.printf("Press 'Save' to save the setoint.\r\n");
hudakz 0:f7514c3f33b6 189 pc.printf("Press 'Mode' to open 'Low setpoint' menu.\r\n");
hudakz 0:f7514c3f33b6 190 pc.printf("\r\n");
hudakz 0:f7514c3f33b6 191
hudakz 0:f7514c3f33b6 192 p_setpoint = &hiSetpoint;
hudakz 0:f7514c3f33b6 193 setpoint = hiSetpoint;
hudakz 0:f7514c3f33b6 194 }
hudakz 0:f7514c3f33b6 195
hudakz 0:f7514c3f33b6 196 /**
hudakz 0:f7514c3f33b6 197 * @brief
hudakz 0:f7514c3f33b6 198 * @note
hudakz 0:f7514c3f33b6 199 * @param
hudakz 0:f7514c3f33b6 200 * @retval
hudakz 0:f7514c3f33b6 201 */
hudakz 0:f7514c3f33b6 202 void openMenu_SetLoSetpoint(void) {
hudakz 0:f7514c3f33b6 203 menuSystem.open(menuSetLoSetpoint); // activate the menu
hudakz 0:f7514c3f33b6 204 pc.printf("---------------------------------------------------\r\n");
hudakz 0:f7514c3f33b6 205 pc.printf("Low setpoint menu:\r\n");
hudakz 0:f7514c3f33b6 206 pc.printf("Press 'More' to increment low setpoint.\r\n");
hudakz 0:f7514c3f33b6 207 pc.printf("Press 'Less' to decrement low setpoint.\r\n");
hudakz 0:f7514c3f33b6 208 pc.printf("Press 'Save' to save the setpoint.\r\n");
hudakz 0:f7514c3f33b6 209 pc.printf("Press 'Mode' to open Main menu.\r\n");
hudakz 0:f7514c3f33b6 210 pc.printf("\r\n");
hudakz 0:f7514c3f33b6 211
hudakz 0:f7514c3f33b6 212 p_setpoint = &loSetpoint;
hudakz 0:f7514c3f33b6 213 setpoint = loSetpoint;
hudakz 0:f7514c3f33b6 214 }
hudakz 0:f7514c3f33b6 215
hudakz 0:f7514c3f33b6 216 /**
hudakz 0:f7514c3f33b6 217 * @brief
hudakz 0:f7514c3f33b6 218 * @note
hudakz 0:f7514c3f33b6 219 * @param
hudakz 0:f7514c3f33b6 220 * @retval
hudakz 0:f7514c3f33b6 221 */
hudakz 0:f7514c3f33b6 222 void incrementSetpoint(void) {
hudakz 0:f7514c3f33b6 223 setpoint++;
hudakz 0:f7514c3f33b6 224 pc.printf("Setpoint incremented\r\n");
hudakz 0:f7514c3f33b6 225 pc.printf("setpoint = %i\r\n", setpoint);
hudakz 0:f7514c3f33b6 226 }
hudakz 0:f7514c3f33b6 227
hudakz 0:f7514c3f33b6 228 /**
hudakz 0:f7514c3f33b6 229 * @brief
hudakz 0:f7514c3f33b6 230 * @note
hudakz 0:f7514c3f33b6 231 * @param
hudakz 0:f7514c3f33b6 232 * @retval
hudakz 0:f7514c3f33b6 233 */
hudakz 0:f7514c3f33b6 234 void decrementSetpoint(void) {
hudakz 0:f7514c3f33b6 235 setpoint--;
hudakz 0:f7514c3f33b6 236 pc.printf("Setpoint decremented\r\n");
hudakz 0:f7514c3f33b6 237 pc.printf("setpoint = %i\r\n", setpoint);
hudakz 0:f7514c3f33b6 238 }
hudakz 0:f7514c3f33b6 239
hudakz 0:f7514c3f33b6 240
hudakz 0:f7514c3f33b6 241 /**
hudakz 0:f7514c3f33b6 242 * @brief
hudakz 0:f7514c3f33b6 243 * @note
hudakz 0:f7514c3f33b6 244 * @param
hudakz 0:f7514c3f33b6 245 * @retval
hudakz 0:f7514c3f33b6 246 */
hudakz 0:f7514c3f33b6 247 void saveSetpoint(void) {
hudakz 0:f7514c3f33b6 248 *p_setpoint = setpoint;
hudakz 0:f7514c3f33b6 249 pc.printf("Setpoint saved\r\n");
hudakz 0:f7514c3f33b6 250 pc.printf("\r\n");
hudakz 0:f7514c3f33b6 251 }
hudakz 0:f7514c3f33b6 252
hudakz 0:f7514c3f33b6 253
hudakz 0:f7514c3f33b6 254