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.
Fork of OneButton by
OneButton.h@2:26960fb1d783, 2016-09-22 (annotated)
- Committer:
- JojoS
- Date:
- Thu Sep 22 20:36:52 2016 +0000
- Revision:
- 2:26960fb1d783
- Parent:
- 1:fdf67f893a5c
with mbed lib >=122, the Callback class is used. For older libs it is compatible with older functions. With Callback class also member functions can be used.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jaup | 1:fdf67f893a5c | 1 | // Modifyed to support mbed environment, rewrite the state machine. |
jaup | 1:fdf67f893a5c | 2 | // by Zibin Zheng <znbin@qq.com> |
jaup | 1:fdf67f893a5c | 3 | |
jaup | 0:62d614796022 | 4 | // ----- |
jaup | 0:62d614796022 | 5 | // OneButton.h - Library for detecting button clicks, doubleclicks and long press pattern on a single button. |
jaup | 0:62d614796022 | 6 | // This class is implemented for use with the Arduino environment. |
jaup | 0:62d614796022 | 7 | // Copyright (c) by Matthias Hertel, http://www.mathertel.de |
jaup | 0:62d614796022 | 8 | // This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx |
jaup | 0:62d614796022 | 9 | // More information on: http://www.mathertel.de/Arduino |
jaup | 0:62d614796022 | 10 | // ----- |
jaup | 1:fdf67f893a5c | 11 | |
jaup | 0:62d614796022 | 12 | |
jaup | 0:62d614796022 | 13 | #ifndef OneButton_h |
jaup | 0:62d614796022 | 14 | #define OneButton_h |
jaup | 0:62d614796022 | 15 | |
jaup | 0:62d614796022 | 16 | #include "mbed.h" |
jaup | 1:fdf67f893a5c | 17 | |
jaup | 1:fdf67f893a5c | 18 | /** |
jaup | 1:fdf67f893a5c | 19 | * Example: |
jaup | 1:fdf67f893a5c | 20 | * @code |
jaup | 1:fdf67f893a5c | 21 | |
jaup | 1:fdf67f893a5c | 22 | #include "mbed.h" |
jaup | 1:fdf67f893a5c | 23 | #include "OneButton.h" |
jaup | 1:fdf67f893a5c | 24 | |
jaup | 1:fdf67f893a5c | 25 | Serial pc(SERIAL_TX, SERIAL_RX); |
jaup | 1:fdf67f893a5c | 26 | |
jaup | 1:fdf67f893a5c | 27 | OneButton btn1(PC_13); |
jaup | 1:fdf67f893a5c | 28 | |
jaup | 1:fdf67f893a5c | 29 | void pressed() { |
jaup | 1:fdf67f893a5c | 30 | pc.printf("pressed\r\n"); |
jaup | 1:fdf67f893a5c | 31 | } |
jaup | 1:fdf67f893a5c | 32 | |
jaup | 1:fdf67f893a5c | 33 | void click() { |
jaup | 1:fdf67f893a5c | 34 | pc.printf("click\r\n"); |
jaup | 1:fdf67f893a5c | 35 | } |
jaup | 1:fdf67f893a5c | 36 | |
jaup | 1:fdf67f893a5c | 37 | void double_click() { |
jaup | 1:fdf67f893a5c | 38 | pc.printf("double_click\r\n"); |
jaup | 1:fdf67f893a5c | 39 | } |
jaup | 0:62d614796022 | 40 | |
jaup | 1:fdf67f893a5c | 41 | void long_start() { |
jaup | 1:fdf67f893a5c | 42 | pc.printf("long_start\r\n"); |
jaup | 1:fdf67f893a5c | 43 | } |
jaup | 1:fdf67f893a5c | 44 | |
jaup | 1:fdf67f893a5c | 45 | void long_hold() { |
jaup | 1:fdf67f893a5c | 46 | pc.printf("long_hold\r\n"); |
jaup | 1:fdf67f893a5c | 47 | } |
jaup | 1:fdf67f893a5c | 48 | |
jaup | 1:fdf67f893a5c | 49 | void long_stop() { |
jaup | 1:fdf67f893a5c | 50 | pc.printf("long_stop\r\n"); |
jaup | 1:fdf67f893a5c | 51 | } |
jaup | 1:fdf67f893a5c | 52 | |
jaup | 1:fdf67f893a5c | 53 | int main() { |
jaup | 1:fdf67f893a5c | 54 | |
jaup | 1:fdf67f893a5c | 55 | btn1.attachClick(&click); |
jaup | 1:fdf67f893a5c | 56 | btn1.attachPress(&pressed); |
jaup | 1:fdf67f893a5c | 57 | btn1.attachDoubleClick(&double_click); |
jaup | 1:fdf67f893a5c | 58 | btn1.attachLongPressStart(&long_start); |
jaup | 1:fdf67f893a5c | 59 | btn1.attachDuringLongPress(&long_hold); |
jaup | 1:fdf67f893a5c | 60 | btn1.attachLongPressStop(&long_stop); |
jaup | 1:fdf67f893a5c | 61 | |
jaup | 1:fdf67f893a5c | 62 | while(1) { |
jaup | 1:fdf67f893a5c | 63 | |
jaup | 1:fdf67f893a5c | 64 | btn1.tick(); //loop |
jaup | 1:fdf67f893a5c | 65 | } |
jaup | 1:fdf67f893a5c | 66 | } |
jaup | 1:fdf67f893a5c | 67 | |
jaup | 1:fdf67f893a5c | 68 | */ |
jaup | 1:fdf67f893a5c | 69 | |
jaup | 0:62d614796022 | 70 | |
JojoS | 2:26960fb1d783 | 71 | #if (MBED_LIBRARY_VERSION < 122) |
jaup | 0:62d614796022 | 72 | // ----- Callback function types ----- |
jaup | 0:62d614796022 | 73 | |
jaup | 0:62d614796022 | 74 | extern "C" { |
jaup | 0:62d614796022 | 75 | typedef void (*callbackFunction)(void); |
jaup | 0:62d614796022 | 76 | } |
JojoS | 2:26960fb1d783 | 77 | #endif |
jaup | 0:62d614796022 | 78 | |
jaup | 0:62d614796022 | 79 | class OneButton |
jaup | 0:62d614796022 | 80 | { |
jaup | 0:62d614796022 | 81 | public: |
jaup | 1:fdf67f893a5c | 82 | |
jaup | 0:62d614796022 | 83 | // ----- Constructor ----- |
jaup | 1:fdf67f893a5c | 84 | OneButton(PinName pin, bool active_level = 0); |
jaup | 0:62d614796022 | 85 | |
jaup | 0:62d614796022 | 86 | // ----- Set runtime parameters ----- |
jaup | 0:62d614796022 | 87 | |
jaup | 0:62d614796022 | 88 | // set # millisec after single click is assumed. |
jaup | 0:62d614796022 | 89 | void setClickTicks(int ticks); |
jaup | 0:62d614796022 | 90 | |
jaup | 0:62d614796022 | 91 | // set # millisec after press is assumed. |
jaup | 0:62d614796022 | 92 | void setPressTicks(int ticks); |
jaup | 0:62d614796022 | 93 | |
jaup | 0:62d614796022 | 94 | // attach functions that will be called when button was pressed in the specified way. |
JojoS | 2:26960fb1d783 | 95 | |
JojoS | 2:26960fb1d783 | 96 | #if (MBED_LIBRARY_VERSION < 122) |
jaup | 0:62d614796022 | 97 | void attachClick(callbackFunction newFunction); |
jaup | 0:62d614796022 | 98 | void attachDoubleClick(callbackFunction newFunction); |
jaup | 0:62d614796022 | 99 | void attachPress(callbackFunction newFunction); // DEPRECATED, replaced by longPressStart, longPressStop and duringLongPress |
jaup | 0:62d614796022 | 100 | void attachLongPressStart(callbackFunction newFunction); |
jaup | 0:62d614796022 | 101 | void attachLongPressStop(callbackFunction newFunction); |
jaup | 0:62d614796022 | 102 | void attachDuringLongPress(callbackFunction newFunction); |
JojoS | 2:26960fb1d783 | 103 | #else |
JojoS | 2:26960fb1d783 | 104 | // click function |
JojoS | 2:26960fb1d783 | 105 | void attachClick(Callback<void()> func){ |
JojoS | 2:26960fb1d783 | 106 | clickFn.attach(func); |
JojoS | 2:26960fb1d783 | 107 | }; |
jaup | 0:62d614796022 | 108 | |
JojoS | 2:26960fb1d783 | 109 | // doubleClick function |
JojoS | 2:26960fb1d783 | 110 | void attachDoubleClick(Callback<void()> func){ |
JojoS | 2:26960fb1d783 | 111 | doubleClickFn.attach(func); |
JojoS | 2:26960fb1d783 | 112 | }; |
JojoS | 2:26960fb1d783 | 113 | |
JojoS | 2:26960fb1d783 | 114 | // press function |
JojoS | 2:26960fb1d783 | 115 | void attachPress(Callback<void()> func){ |
JojoS | 2:26960fb1d783 | 116 | pressFn.attach(func); |
JojoS | 2:26960fb1d783 | 117 | }; |
JojoS | 2:26960fb1d783 | 118 | |
JojoS | 2:26960fb1d783 | 119 | // long press start function |
JojoS | 2:26960fb1d783 | 120 | void attachLongPressStart(Callback<void()> func){ |
JojoS | 2:26960fb1d783 | 121 | longPressStartFn.attach(func); |
JojoS | 2:26960fb1d783 | 122 | }; |
JojoS | 2:26960fb1d783 | 123 | |
JojoS | 2:26960fb1d783 | 124 | // long press stop function |
JojoS | 2:26960fb1d783 | 125 | void attachLongPressStop(Callback<void()> func){ |
JojoS | 2:26960fb1d783 | 126 | longPressStopFn.attach(func); |
JojoS | 2:26960fb1d783 | 127 | }; |
JojoS | 2:26960fb1d783 | 128 | |
JojoS | 2:26960fb1d783 | 129 | // during long press function |
JojoS | 2:26960fb1d783 | 130 | void attachDuringLongPress(Callback<void()> func){ |
JojoS | 2:26960fb1d783 | 131 | duringLongPressFn.attach(func); |
JojoS | 2:26960fb1d783 | 132 | }; |
JojoS | 2:26960fb1d783 | 133 | |
JojoS | 2:26960fb1d783 | 134 | Callback<void()> clickFn; |
JojoS | 2:26960fb1d783 | 135 | Callback<void()> doubleClickFn; |
JojoS | 2:26960fb1d783 | 136 | Callback<void()> pressFn; |
JojoS | 2:26960fb1d783 | 137 | Callback<void()> longPressStartFn; |
JojoS | 2:26960fb1d783 | 138 | Callback<void()> longPressStopFn; |
JojoS | 2:26960fb1d783 | 139 | Callback<void()> duringLongPressFn; |
JojoS | 2:26960fb1d783 | 140 | |
JojoS | 2:26960fb1d783 | 141 | #endif |
jaup | 0:62d614796022 | 142 | // ----- State machine functions ----- |
jaup | 0:62d614796022 | 143 | |
jaup | 0:62d614796022 | 144 | // call this function every some milliseconds for handling button events. |
jaup | 0:62d614796022 | 145 | void tick(void); |
jaup | 0:62d614796022 | 146 | bool isLongPressed(); |
jaup | 0:62d614796022 | 147 | |
jaup | 0:62d614796022 | 148 | private: |
jaup | 0:62d614796022 | 149 | int _clickTicks; // number of ticks that have to pass by before a click is detected |
jaup | 0:62d614796022 | 150 | int _pressTicks; // number of ticks that have to pass by before a long button press is detected |
jaup | 0:62d614796022 | 151 | |
jaup | 0:62d614796022 | 152 | int _buttonReleased; |
jaup | 0:62d614796022 | 153 | int _buttonPressed; |
jaup | 0:62d614796022 | 154 | bool _isLongPressed; |
jaup | 0:62d614796022 | 155 | |
jaup | 0:62d614796022 | 156 | // These variables will hold functions acting as event source. |
JojoS | 2:26960fb1d783 | 157 | // mbed lib < 122 will use a simple function pointer, |
JojoS | 2:26960fb1d783 | 158 | // for libs >= 122 we can use the new Callback template to avoid compiler warnings |
jaup | 0:62d614796022 | 159 | |
JojoS | 2:26960fb1d783 | 160 | #if (MBED_LIBRARY_VERSION < 122) |
JojoS | 2:26960fb1d783 | 161 | callbackFunction clickFn; |
JojoS | 2:26960fb1d783 | 162 | callbackFunction doubleClickFn; |
JojoS | 2:26960fb1d783 | 163 | callbackFunction pressFn; |
JojoS | 2:26960fb1d783 | 164 | callbackFunction longPressStartFn; |
JojoS | 2:26960fb1d783 | 165 | callbackFunction longPressStopFn; |
JojoS | 2:26960fb1d783 | 166 | callbackFunction duringLongPressFn; |
JojoS | 2:26960fb1d783 | 167 | #endif |
jaup | 0:62d614796022 | 168 | // These variables that hold information across the upcoming tick calls. |
jaup | 0:62d614796022 | 169 | // They are initialized once on program start and are updated every time the tick function is called. |
jaup | 1:fdf67f893a5c | 170 | |
JojoS | 2:26960fb1d783 | 171 | int ticks; |
jaup | 1:fdf67f893a5c | 172 | uint8_t _state; |
jaup | 1:fdf67f893a5c | 173 | uint8_t _debounce_cnt; |
jaup | 1:fdf67f893a5c | 174 | uint8_t _debounceTicks; // number of ticks for debounce times. |
jaup | 1:fdf67f893a5c | 175 | bool button_level; |
jaup | 1:fdf67f893a5c | 176 | bool active; |
jaup | 0:62d614796022 | 177 | |
jaup | 0:62d614796022 | 178 | DigitalIn* btn_pin; |
jaup | 1:fdf67f893a5c | 179 | Timer *timer; |
jaup | 0:62d614796022 | 180 | }; |
jaup | 0:62d614796022 | 181 | |
jaup | 0:62d614796022 | 182 | #endif |
jaup | 0:62d614796022 | 183 | |
jaup | 0:62d614796022 | 184 |