Zoltan Hudak / Menu

Dependents:   Menu_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Menu.h Source File

Menu.h

00001 /*
00002  ******************************************************************************
00003  * @file    Menu.h
00004  * @author  Zoltan Hudak
00005  * @version
00006  * @date    04-July-2014
00007  * @brief   Menu system with push buttons
00008   ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 Zoltan Hudak <hudakz@inbox.com>
00012  *
00013  * All rights reserved.
00014  *
00015  * This program is free software: you can redistribute it and/or modify
00016  * it under the terms of the GNU General Public License as published by
00017  * the Free Software Foundation, either version 3 of the License, or
00018  * (at your option) any later version.
00019  *
00020  * This program is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License
00026  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00027  *
00028  *
00029  * The Menu library was created to facilitate designs with a display and push buttons.
00030  * - number of menus and push buttons is practically not limited by the software
00031  * - function of individual push buttons varies depending on the selected menu
00032  *
00033  *    ---------------------------------
00034  *   |                                  |
00035  *   |   ----------------------------   |
00036  *   |  |                            |  |
00037  *   |  |                            |  |
00038  *   |  |                            |  |
00039  *   |  |         Display            |  |
00040  *   |  |                            |  |
00041  *   |  |                            |  |
00042  *   |  |                            |  |
00043  *   |   ----------------------------   |
00044  *   |       O      O     O     O       |
00045  *   |      Btn1   Btn2  Btn3  Btn4     |
00046  *   |                                  |
00047  *    ----------------------------------
00048  *
00049  * Each push button have to be connected to an mbed digital input pin and the ground.
00050  *
00051  *
00052  *     -------------------------
00053  *    |
00054  *    |      mbed board
00055  *    |
00056  *     -------------------------
00057  *        | Input pin
00058  *        |
00059  *        o
00060  *         / Button        ...
00061  *        o
00062  *        |
00063  *       ---
00064  *       GND
00065  *
00066  *
00067  * NOTE: When creating a MenuSystem the constructor automatically connects
00068  * an internal pull-up resistor to each PushButton input used by the MenuSytem.
00069  */
00070 
00071 
00072 #ifndef MENU_H_
00073 #define MENU_H_
00074 
00075 #include "mbed.h"
00076 
00077 typedef void (*MenuFnc_t) (void);
00078 
00079 #include "platform/platform.h"
00080 
00081 #if defined (DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
00082 
00083 #include "hal/gpio_api.h"
00084 #include "hal/gpio_irq_api.h"
00085 #include "platform/Callback.h"
00086 #include "platform/mbed_critical.h"
00087 #include "platform/mbed_toolchain.h"
00088 #include "platform/NonCopyable.h"
00089 
00090 class MenuSystem;
00091 
00092 class PushButton : private NonCopyable<PushButton> {
00093 
00094 public:
00095     bool  _pressed;
00096 
00097     /** Create an PushButton connected to the specified pin
00098      *
00099      *  @param pin InterruptIn pin to connect to
00100      */
00101     PushButton(PinName pin);
00102     virtual ~PushButton();
00103 
00104     /** Attach a function to call when a falling edge occurs on the input
00105      *
00106      *  @param func A pointer to a void function, or 0 to set as none
00107      */
00108     void fall(Callback<void()> func);
00109 
00110     /** Enable IRQ. This method depends on hw implementation, might enable one
00111      *  port interrupts. For further information, check gpio_irq_enable().
00112      */
00113     void enable_irq();
00114 
00115     /** Disable IRQ. This method depends on hw implementation, might disable one
00116      *  port interrupts. For further information, check gpio_irq_disable().
00117      */
00118     void disable_irq();
00119 
00120     static void _irq_handler(uint32_t id, gpio_irq_event event);
00121 
00122 protected:
00123     gpio_t      gpio;
00124     gpio_irq_t  gpio_irq;
00125 
00126     Callback<void()> _fall;
00127 };
00128 
00129 #endif
00130 
00131 
00132 class   Menu
00133 {
00134     MenuFnc_t*      _onBtnPressed;
00135     MenuSystem*     _menuSystem;
00136     void            onBtnPressed(unsigned int i);
00137 public:
00138     Menu(MenuSystem& menuSystem);
00139     void            attach(PushButton* button, MenuFnc_t onButtonPressed);
00140 
00141     friend          MenuSystem;
00142 };
00143 
00144 class MenuSystem
00145 {
00146     PushButton**    _buttons;
00147     unsigned int    _btnCount;
00148     unsigned int    _btnIdx;
00149     volatile bool   _btnBouncing;
00150     int             _debounceTime;
00151     Timeout         _bouncingTimeout;
00152     Menu*           _activeMenu;
00153     unsigned int    btnCount(void);
00154     void            onBtnPressed();
00155     PushButton*     getButton(size_t idx);
00156     void            debounce(void);
00157 public:
00158     MenuSystem(PushButton* buttons[], size_t buttonCount, int debounceTime = 300 /* milliseconds */);
00159     void            open(Menu& menu);
00160     void            handleButtons(void);
00161     Menu*           activeMenu(void);
00162 
00163     friend          Menu;
00164 };
00165 #endif /* MENU_H_ */