Library for detecting button clicks, doubleclicks and long press pattern on a single button.
Dependents: OneButton_Example ABBlind
OneButton.h@1:fdf67f893a5c, 2016-09-01 (annotated)
- Committer:
- jaup
- Date:
- Thu Sep 01 09:21:00 2016 +0000
- Revision:
- 1:fdf67f893a5c
- Parent:
- 0:62d614796022
frist commit
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 | /** |
jaup | 1:fdf67f893a5c | 20 | * Example: |
jaup | 1:fdf67f893a5c | 21 | * @code |
jaup | 1:fdf67f893a5c | 22 | |
jaup | 1:fdf67f893a5c | 23 | #include "mbed.h" |
jaup | 1:fdf67f893a5c | 24 | #include "OneButton.h" |
jaup | 1:fdf67f893a5c | 25 | |
jaup | 1:fdf67f893a5c | 26 | Serial pc(SERIAL_TX, SERIAL_RX); |
jaup | 1:fdf67f893a5c | 27 | |
jaup | 1:fdf67f893a5c | 28 | OneButton btn1(PC_13); |
jaup | 1:fdf67f893a5c | 29 | |
jaup | 1:fdf67f893a5c | 30 | void pressed() { |
jaup | 1:fdf67f893a5c | 31 | pc.printf("pressed\r\n"); |
jaup | 1:fdf67f893a5c | 32 | } |
jaup | 1:fdf67f893a5c | 33 | |
jaup | 1:fdf67f893a5c | 34 | void click() { |
jaup | 1:fdf67f893a5c | 35 | pc.printf("click\r\n"); |
jaup | 1:fdf67f893a5c | 36 | } |
jaup | 1:fdf67f893a5c | 37 | |
jaup | 1:fdf67f893a5c | 38 | void double_click() { |
jaup | 1:fdf67f893a5c | 39 | pc.printf("double_click\r\n"); |
jaup | 1:fdf67f893a5c | 40 | } |
jaup | 0:62d614796022 | 41 | |
jaup | 1:fdf67f893a5c | 42 | void long_start() { |
jaup | 1:fdf67f893a5c | 43 | pc.printf("long_start\r\n"); |
jaup | 1:fdf67f893a5c | 44 | } |
jaup | 1:fdf67f893a5c | 45 | |
jaup | 1:fdf67f893a5c | 46 | void long_hold() { |
jaup | 1:fdf67f893a5c | 47 | pc.printf("long_hold\r\n"); |
jaup | 1:fdf67f893a5c | 48 | } |
jaup | 1:fdf67f893a5c | 49 | |
jaup | 1:fdf67f893a5c | 50 | void long_stop() { |
jaup | 1:fdf67f893a5c | 51 | pc.printf("long_stop\r\n"); |
jaup | 1:fdf67f893a5c | 52 | } |
jaup | 1:fdf67f893a5c | 53 | |
jaup | 1:fdf67f893a5c | 54 | int main() { |
jaup | 1:fdf67f893a5c | 55 | |
jaup | 1:fdf67f893a5c | 56 | btn1.attachClick(&click); |
jaup | 1:fdf67f893a5c | 57 | btn1.attachPress(&pressed); |
jaup | 1:fdf67f893a5c | 58 | btn1.attachDoubleClick(&double_click); |
jaup | 1:fdf67f893a5c | 59 | btn1.attachLongPressStart(&long_start); |
jaup | 1:fdf67f893a5c | 60 | btn1.attachDuringLongPress(&long_hold); |
jaup | 1:fdf67f893a5c | 61 | btn1.attachLongPressStop(&long_stop); |
jaup | 1:fdf67f893a5c | 62 | |
jaup | 1:fdf67f893a5c | 63 | while(1) { |
jaup | 1:fdf67f893a5c | 64 | |
jaup | 1:fdf67f893a5c | 65 | btn1.tick(); //loop |
jaup | 1:fdf67f893a5c | 66 | } |
jaup | 1:fdf67f893a5c | 67 | } |
jaup | 1:fdf67f893a5c | 68 | |
jaup | 1:fdf67f893a5c | 69 | */ |
jaup | 1:fdf67f893a5c | 70 | |
jaup | 0:62d614796022 | 71 | |
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 | } |
jaup | 0:62d614796022 | 77 | |
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. |
jaup | 0:62d614796022 | 95 | void attachClick(callbackFunction newFunction); |
jaup | 0:62d614796022 | 96 | void attachDoubleClick(callbackFunction newFunction); |
jaup | 0:62d614796022 | 97 | void attachPress(callbackFunction newFunction); // DEPRECATED, replaced by longPressStart, longPressStop and duringLongPress |
jaup | 0:62d614796022 | 98 | void attachLongPressStart(callbackFunction newFunction); |
jaup | 0:62d614796022 | 99 | void attachLongPressStop(callbackFunction newFunction); |
jaup | 0:62d614796022 | 100 | void attachDuringLongPress(callbackFunction newFunction); |
jaup | 0:62d614796022 | 101 | |
jaup | 0:62d614796022 | 102 | // ----- State machine functions ----- |
jaup | 0:62d614796022 | 103 | |
jaup | 0:62d614796022 | 104 | // call this function every some milliseconds for handling button events. |
jaup | 0:62d614796022 | 105 | void tick(void); |
jaup | 0:62d614796022 | 106 | bool isLongPressed(); |
jaup | 0:62d614796022 | 107 | |
jaup | 0:62d614796022 | 108 | private: |
jaup | 0:62d614796022 | 109 | int _clickTicks; // number of ticks that have to pass by before a click is detected |
jaup | 0:62d614796022 | 110 | int _pressTicks; // number of ticks that have to pass by before a long button press is detected |
jaup | 0:62d614796022 | 111 | |
jaup | 0:62d614796022 | 112 | int _buttonReleased; |
jaup | 0:62d614796022 | 113 | int _buttonPressed; |
jaup | 0:62d614796022 | 114 | bool _isLongPressed; |
jaup | 0:62d614796022 | 115 | |
jaup | 0:62d614796022 | 116 | // These variables will hold functions acting as event source. |
jaup | 0:62d614796022 | 117 | callbackFunction _clickFunc; |
jaup | 0:62d614796022 | 118 | callbackFunction _doubleClickFunc; |
jaup | 0:62d614796022 | 119 | callbackFunction _pressFunc; |
jaup | 0:62d614796022 | 120 | callbackFunction _longPressStartFunc; |
jaup | 0:62d614796022 | 121 | callbackFunction _longPressStopFunc; |
jaup | 0:62d614796022 | 122 | callbackFunction _duringLongPressFunc; |
jaup | 0:62d614796022 | 123 | |
jaup | 0:62d614796022 | 124 | // These variables that hold information across the upcoming tick calls. |
jaup | 0:62d614796022 | 125 | // They are initialized once on program start and are updated every time the tick function is called. |
jaup | 1:fdf67f893a5c | 126 | |
jaup | 1:fdf67f893a5c | 127 | uint16_t ticks; |
jaup | 1:fdf67f893a5c | 128 | uint8_t _state; |
jaup | 1:fdf67f893a5c | 129 | uint8_t _debounce_cnt; |
jaup | 1:fdf67f893a5c | 130 | uint8_t _debounceTicks; // number of ticks for debounce times. |
jaup | 1:fdf67f893a5c | 131 | bool button_level; |
jaup | 1:fdf67f893a5c | 132 | bool active; |
jaup | 0:62d614796022 | 133 | |
jaup | 0:62d614796022 | 134 | DigitalIn* btn_pin; |
jaup | 1:fdf67f893a5c | 135 | Timer *timer; |
jaup | 0:62d614796022 | 136 | }; |
jaup | 0:62d614796022 | 137 | |
jaup | 0:62d614796022 | 138 | #endif |
jaup | 0:62d614796022 | 139 | |
jaup | 0:62d614796022 | 140 |