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.
Revision 3:9b11b0e69ff4, committed 2018-01-05
- Comitter:
- hudakz
- Date:
- Fri Jan 05 14:49:58 2018 +0000
- Parent:
- 2:92f4dad1b937
- Commit message:
- Rather than polling digital inputs, buttons are implemented utilizng interrupts.
Changed in this revision
--- a/Menu.lib Thu Nov 30 18:41:29 2017 +0000 +++ b/Menu.lib Fri Jan 05 14:49:58 2018 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/users/hudakz/code/Menu/#bd104212d2c1 +https://developer.mbed.org/users/hudakz/code/Menu/#3d32db89c8db
--- a/main.cpp Thu Nov 30 18:41:29 2017 +0000
+++ b/main.cpp Fri Jan 05 14:49:58 2018 +0000
@@ -20,7 +20,7 @@
* | Btn1 Btn2 Btn3 Btn4 |
* | |
* ----------------------------------
- *
+ *
* Each push button must be connected to an mbed digital input pin and the ground.
*
*
@@ -39,8 +39,8 @@
* GND
*
*
- * NOTE: When creating a MenuSystem the constructor automatically connects
- * an internal pull-up resistor to each digital input used by the MenuSytem push buttons.
+ * NOTE: When creating a MenuSystem the constructor automatically connects
+ * an internal pull-up resistor to each PushButton input used by the MenuSytem.
*/
#include "mbed.h"
@@ -48,9 +48,8 @@
Serial pc(USBTX, USBRX);
-
/*
- * Some variables needed for this example
+ * Variables used in this example
*/
int setpoint;
int* p_setpoint;
@@ -58,43 +57,35 @@
int hiSetpoint = 50;
/*
- * Create an array of push buttons to be used with the menu system
+ * Define push buttons to be used in menu.
*/
#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) };
+PushButton btnMode(p21), btnMore(p22), btnLess(p23), btnSave(p24);
+#else
+PushButton btnMode(D2), btnMore(D3), btnLess(D4), btnSave(D5);
#endif
+
/*
- * Assign alias names to the buttons (just for the convenience)
+ * Define an array of PushButton pointers initialized with push buttons created above.
*/
-DigitalIn& btnMode = buttons[0]; // Mode
-DigitalIn& btnMore = buttons[1]; // More +
-DigitalIn& btnLess = buttons[2]; // Less -
-DigitalIn& btnSave = buttons[3]; // Save
+PushButton* buttons[] = {&btnMode, &btnMore, &btnLess, &btnSave};
+
/*
* 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
+MenuSystem menuSystem(buttons, 4 /*, 300 */); // name of the 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
+ * 1. Main
+ * 2. Set high setpoint
+ * 3. Set low setpoint
*
- * NOTE: The function of individual push buttons will vary depending on the selected menu.
+ * NOTE: The function of individual push buttons will vary depending on the actually opened menu.
*/
Menu menuMain(menuSystem);
Menu menuSetHiSetpoint(menuSystem);
@@ -123,28 +114,30 @@
//
// 'Main' menu:
- menuMain.attach(&btnMode, &openMenu_SetHiSetpoint);
+ menuMain.attach(buttons[0], openMenu_SetHiSetpoint);
//
// 'Set High Setpoint' menu:
- menuSetHiSetpoint.attach(&btnMode, &openMenu_SetLoSetpoint);
- menuSetHiSetpoint.attach(&btnMore, &incrementSetpoint);
- menuSetHiSetpoint.attach(&btnLess, &decrementSetpoint);
- menuSetHiSetpoint.attach(&btnSave, &saveSetpoint);
+ menuSetHiSetpoint.attach(buttons[0], openMenu_SetLoSetpoint);
+ menuSetHiSetpoint.attach(buttons[1], incrementSetpoint);
+ menuSetHiSetpoint.attach(buttons[2], decrementSetpoint);
+ menuSetHiSetpoint.attach(buttons[3], saveSetpoint);
//
// 'Set Low Setpoint' menu:
- menuSetLoSetpoint.attach(&btnMode, &openMenu_Main);
- menuSetLoSetpoint.attach(&btnMore, &incrementSetpoint);
- menuSetLoSetpoint.attach(&btnLess, &decrementSetpoint);
- menuSetLoSetpoint.attach(&btnSave, &saveSetpoint);
+ menuSetLoSetpoint.attach(buttons[0], openMenu_Main);
+ menuSetLoSetpoint.attach(buttons[1], incrementSetpoint);
+ menuSetLoSetpoint.attach(buttons[2], decrementSetpoint);
+ menuSetLoSetpoint.attach(buttons[3], saveSetpoint);
- openMenu_Main(); // activate main (root) menu
+ openMenu_Main(); // open 'Main' menu
while(1) {
- menuSystem.handleButtons(); // Handle 'button pressed' events
+ // When a button is pressed a flag is set immediately via an interrupt service routine.
+ // But the assigned handler is called later on here by the 'handleButtons()' function.
+ menuSystem.handleButtons();
- //..
+ //.. Your main code
}
}
@@ -164,11 +157,10 @@
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("Main menu:\r\n");
+ pc.printf("Press 'Mode' to open 'High setpoint' menu.\r\n");
pc.printf("hiSetpoint = %i\r\n", hiSetpoint);
- pc.printf("\r\n");
- pc.printf("Press 'Mode' to open 'High setpoint' menu.\r\n");
+ pc.printf("loSetpoint = %i\r\n", loSetpoint);
pc.printf("\r\n");
}
@@ -182,10 +174,10 @@
menuSystem.open(menuSetHiSetpoint); // activate the menu
pc.printf("---------------------------------------------------\r\n");
pc.printf("High setpoint menu:\r\n");
+ pc.printf("Press 'Mode' to open 'Low 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;
@@ -202,10 +194,10 @@
menuSystem.open(menuSetLoSetpoint); // activate the menu
pc.printf("---------------------------------------------------\r\n");
pc.printf("Low setpoint menu:\r\n");
+ pc.printf("Press 'Mode' to open 'Main' 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;
@@ -251,3 +243,4 @@
+
--- a/mbed.bld Thu Nov 30 18:41:29 2017 +0000 +++ b/mbed.bld Fri Jan 05 14:49:58 2018 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/e7ca05fa8600 \ No newline at end of file +https://os.mbed.com/users/mbed_official/code/mbed/builds/7130f322cb7e \ No newline at end of file