Zoltan Hudak / Mbed 2 deprecated Menu_Hello

Dependencies:   Menu mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * An example of using the Menu library - Simple Menu system with push buttons.
00003  *
00004  * The Menu library was created to facilitate designs with a display and push buttons.
00005  * - number of menus and push buttons is practically not limited by the software
00006  * - function of individual push buttons varies depending on the selected menu
00007  *
00008  *    ---------------------------------
00009  *   |                                  |
00010  *   |   ----------------------------   |
00011  *   |  |                            |  |
00012  *   |  |                            |  |
00013  *   |  |                            |  |
00014  *   |  |         Display            |  |
00015  *   |  |                            |  |
00016  *   |  |                            |  |
00017  *   |  |                            |  |
00018  *   |   ----------------------------   |
00019  *   |       O      O     O     O       |
00020  *   |      Btn1   Btn2  Btn3  Btn4     |
00021  *   |                                  |
00022  *    ----------------------------------
00023  *
00024  * Each push button must be connected to an mbed digital input pin and the ground.
00025  *
00026  *
00027  *     -------------------------
00028  *    |
00029  *    |      mbed board
00030  *    |
00031  *     -------------------------
00032  *        | Input pin
00033  *        |
00034  *        o
00035  *         / Button        ...
00036  *        o
00037  *        |
00038  *       ---
00039  *       GND
00040  *
00041  *
00042  * NOTE: When creating a MenuSystem the constructor automatically connects
00043  * an internal pull-up resistor to each PushButton input used by the MenuSytem.
00044  */
00045 
00046 #include "mbed.h"
00047 #include "Menu.h"
00048 
00049 Serial pc(USBTX, USBRX);
00050 
00051 /*
00052  * Variables used in this example
00053  */
00054 int  setpoint;
00055 int* p_setpoint;
00056 int  loSetpoint = 20;
00057 int  hiSetpoint = 50;
00058 
00059 /*
00060  * Define push buttons to be used in menu.
00061  */
00062 #if defined(TARGET_LPC1768)
00063 PushButton btnMode(p21), btnMore(p22), btnLess(p23), btnSave(p24);
00064 #else
00065 PushButton btnMode(D2), btnMore(D3), btnLess(D4), btnSave(D5);
00066 #endif
00067 
00068 
00069 /*
00070  * Define an array of PushButton pointers initialized with push buttons created above.
00071  */
00072 PushButton* buttons[] = {&btnMode, &btnMore, &btnLess, &btnSave};
00073 
00074 
00075 /*
00076  * Create a menu system object associated with the push buttons
00077  */
00078 MenuSystem  menuSystem(buttons, 4 /*, 300 */); // name of the array, number of buttons, (debounce time in ms)
00079 
00080 /*
00081  * Create as many menus as you need.
00082  *
00083  * In this example we create the following three menus:
00084  * 1. Main
00085  * 2. Set high setpoint
00086  * 3. Set low setpoint
00087  *
00088  * NOTE: The function of individual push buttons will vary depending on the actually opened menu.
00089  */
00090 Menu  menuMain(menuSystem);
00091 Menu  menuSetHiSetpoint(menuSystem);
00092 Menu  menuSetLoSetpoint(menuSystem);
00093 
00094 /*
00095  * Declare all functions to be called on 'button pressed' events
00096  */
00097 void openMenu_Main(void);
00098 void openMenu_SetHiSetpoint(void);
00099 void openMenu_SetLoSetpoint(void);
00100 void incrementSetpoint(void);
00101 void decrementSetpoint(void);
00102 void saveSetpoint(void);
00103 
00104 /**
00105  * @brief
00106  * @note
00107  * @param
00108  * @retval
00109  */
00110 int main(void) {
00111 
00112     // Attach 'button pressed' event handler functions to the individual push buttons.
00113     // NOTE: It is not compulsory to attach a function to all push buttons in each menu.
00114     //
00115 
00116     // 'Main' menu:
00117     menuMain.attach(buttons[0], openMenu_SetHiSetpoint);
00118 
00119     //
00120     // 'Set High Setpoint' menu:
00121     menuSetHiSetpoint.attach(buttons[0], openMenu_SetLoSetpoint);
00122     menuSetHiSetpoint.attach(buttons[1], incrementSetpoint);
00123     menuSetHiSetpoint.attach(buttons[2], decrementSetpoint);
00124     menuSetHiSetpoint.attach(buttons[3], saveSetpoint);
00125 
00126     //
00127     // 'Set Low Setpoint' menu:
00128     menuSetLoSetpoint.attach(buttons[0], openMenu_Main);
00129     menuSetLoSetpoint.attach(buttons[1], incrementSetpoint);
00130     menuSetLoSetpoint.attach(buttons[2], decrementSetpoint);
00131     menuSetLoSetpoint.attach(buttons[3], saveSetpoint);
00132 
00133     openMenu_Main();    // open 'Main' menu
00134 
00135     while(1) {
00136         // When a button is pressed a flag is set immediately via an interrupt service routine.
00137         // But the assigned handler is called later on here by the 'handleButtons()' function.
00138         menuSystem.handleButtons();
00139 
00140         //.. Your main code
00141     }
00142 }
00143 
00144 /*
00145  * Lets define te menu functions:
00146 
00147  * NOTE: For the sake of simplicity, in this example we do not connect any display to the mbed.
00148  *       Instead we'll utilize a serial terminal running on the connected PC.
00149  */
00150 
00151 /**
00152  * @brief
00153  * @note    None
00154  * @param   None
00155  * @retval  None
00156  */
00157 void openMenu_Main(void) {
00158     menuSystem.open(menuMain);  // activate the menu
00159     pc.printf("---------------------------------------------------\r\n");
00160     pc.printf("Main menu:\r\n");
00161     pc.printf("Press 'Mode' to open 'High setpoint' menu.\r\n");
00162     pc.printf("hiSetpoint = %i\r\n", hiSetpoint);
00163     pc.printf("loSetpoint = %i\r\n", loSetpoint);
00164     pc.printf("\r\n");
00165 }
00166 
00167 /**
00168  * @brief
00169  * @note
00170  * @param
00171  * @retval
00172  */
00173 void openMenu_SetHiSetpoint(void) {
00174     menuSystem.open(menuSetHiSetpoint); // activate the menu
00175     pc.printf("---------------------------------------------------\r\n");
00176     pc.printf("High setpoint menu:\r\n");
00177     pc.printf("Press 'Mode' to open 'Low setpoint' menu.\r\n");
00178     pc.printf("Press 'More' to increment high setpoint.\r\n");
00179     pc.printf("Press 'Less' to decrement high setpoint.\r\n");
00180     pc.printf("Press 'Save' to save the setoint.\r\n");
00181     pc.printf("\r\n");
00182 
00183     p_setpoint = &hiSetpoint;
00184     setpoint = hiSetpoint;
00185 }
00186 
00187 /**
00188  * @brief
00189  * @note
00190  * @param
00191  * @retval
00192  */
00193 void openMenu_SetLoSetpoint(void) {
00194     menuSystem.open(menuSetLoSetpoint); // activate the menu
00195     pc.printf("---------------------------------------------------\r\n");
00196     pc.printf("Low setpoint menu:\r\n");
00197     pc.printf("Press 'Mode' to open 'Main' menu.\r\n");
00198     pc.printf("Press 'More' to increment low setpoint.\r\n");
00199     pc.printf("Press 'Less' to decrement low setpoint.\r\n");
00200     pc.printf("Press 'Save' to save the setpoint.\r\n");
00201     pc.printf("\r\n");
00202 
00203     p_setpoint = &loSetpoint;
00204     setpoint = loSetpoint;
00205 }
00206 
00207 /**
00208  * @brief
00209  * @note
00210  * @param
00211  * @retval
00212  */
00213 void incrementSetpoint(void) {
00214     setpoint++;
00215     pc.printf("Setpoint incremented\r\n");
00216     pc.printf("setpoint = %i\r\n", setpoint);
00217 }
00218 
00219 /**
00220  * @brief
00221  * @note
00222  * @param
00223  * @retval
00224  */
00225 void decrementSetpoint(void) {
00226     setpoint--;
00227     pc.printf("Setpoint decremented\r\n");
00228     pc.printf("setpoint = %i\r\n", setpoint);
00229 }
00230 
00231 
00232 /**
00233  * @brief
00234  * @note
00235  * @param
00236  * @retval
00237  */
00238 void saveSetpoint(void) {
00239     *p_setpoint = setpoint;
00240     pc.printf("Setpoint saved\r\n");
00241     pc.printf("\r\n");
00242 }
00243 
00244 
00245 
00246