Johannes Stratmann / OneButton

Dependents:   ECH_V004

Fork of OneButton by Zibin Zheng

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OneButton.h Source File

OneButton.h

00001 // Modifyed to support mbed environment, rewrite the state machine.
00002 // by Zibin Zheng <znbin@qq.com>
00003 
00004 // -----
00005 // OneButton.h - Library for detecting button clicks, doubleclicks and long press pattern on a single button.
00006 // This class is implemented for use with the Arduino environment.
00007 // Copyright (c) by Matthias Hertel, http://www.mathertel.de
00008 // This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
00009 // More information on: http://www.mathertel.de/Arduino
00010 // -----
00011 
00012 
00013 #ifndef OneButton_h
00014 #define OneButton_h
00015 
00016 #include "mbed.h"
00017 
00018 /**
00019  * Example:
00020  * @code
00021  
00022 #include "mbed.h"
00023 #include "OneButton.h"
00024 
00025 Serial pc(SERIAL_TX, SERIAL_RX);
00026 
00027 OneButton btn1(PC_13);
00028 
00029 void pressed() {
00030     pc.printf("pressed\r\n");
00031 }
00032 
00033 void click() {
00034     pc.printf("click\r\n");
00035 }
00036 
00037 void double_click() {
00038     pc.printf("double_click\r\n");
00039 }
00040 
00041 void long_start() {
00042     pc.printf("long_start\r\n");
00043 }
00044 
00045 void long_hold() {
00046     pc.printf("long_hold\r\n");
00047 }
00048 
00049 void long_stop() {
00050     pc.printf("long_stop\r\n");
00051 }
00052 
00053 int main() {
00054 
00055     btn1.attachClick(&click);
00056     btn1.attachPress(&pressed);
00057     btn1.attachDoubleClick(&double_click);
00058     btn1.attachLongPressStart(&long_start);
00059     btn1.attachDuringLongPress(&long_hold);
00060     btn1.attachLongPressStop(&long_stop);
00061 
00062     while(1) {
00063 
00064         btn1.tick(); //loop
00065     }
00066 }
00067 
00068 */
00069 
00070 
00071 #if (MBED_LIBRARY_VERSION < 122)
00072 // ----- Callback function types -----
00073 
00074 extern "C" {
00075     typedef void (*callbackFunction)(void);
00076 }
00077 #endif
00078 
00079 class OneButton
00080 {
00081 public:
00082 
00083     // ----- Constructor -----
00084     OneButton(PinName pin, bool active_level = 0);
00085 
00086     // ----- Set runtime parameters -----
00087 
00088     // set # millisec after single click is assumed.
00089     void setClickTicks(int ticks);
00090 
00091     // set # millisec after press is assumed.
00092     void setPressTicks(int ticks);
00093 
00094     // attach functions that will be called when button was pressed in the specified way.
00095     
00096 #if (MBED_LIBRARY_VERSION < 122)
00097     void attachClick(callbackFunction newFunction);
00098     void attachDoubleClick(callbackFunction newFunction);
00099     void attachPress(callbackFunction newFunction); // DEPRECATED, replaced by longPressStart, longPressStop and duringLongPress
00100     void attachLongPressStart(callbackFunction newFunction);
00101     void attachLongPressStop(callbackFunction newFunction);
00102     void attachDuringLongPress(callbackFunction newFunction);
00103 #else
00104     // click function
00105     void attachClick(Callback<void()> func){
00106         clickFn.attach(func);
00107     };
00108 
00109     // doubleClick function
00110     void attachDoubleClick(Callback<void()> func){
00111         doubleClickFn.attach(func);
00112     };
00113 
00114     // press function
00115     void attachPress(Callback<void()> func){
00116         pressFn.attach(func);
00117     };
00118 
00119     // long press start function
00120     void attachLongPressStart(Callback<void()> func){
00121         longPressStartFn.attach(func);
00122     };
00123 
00124     // long press stop function
00125     void attachLongPressStop(Callback<void()> func){
00126         longPressStopFn.attach(func);
00127     };
00128 
00129     // during long press function
00130     void attachDuringLongPress(Callback<void()> func){
00131         duringLongPressFn.attach(func);
00132     };
00133     
00134     Callback<void()> clickFn;
00135     Callback<void()> doubleClickFn;
00136     Callback<void()> pressFn;
00137     Callback<void()> longPressStartFn;
00138     Callback<void()> longPressStopFn;
00139     Callback<void()> duringLongPressFn;
00140 
00141 #endif
00142     // ----- State machine functions -----
00143 
00144     // call this function every some milliseconds for handling button events.
00145     void tick(void);
00146     bool isLongPressed();
00147 
00148 private:
00149     int _clickTicks; // number of ticks that have to pass by before a click is detected
00150     int _pressTicks; // number of ticks that have to pass by before a long button press is detected
00151 
00152     int _buttonReleased;
00153     int _buttonPressed;
00154     bool _isLongPressed;
00155 
00156     // These variables will hold functions acting as event source.
00157     // mbed lib < 122 will use a simple function pointer, 
00158     // for libs >= 122 we can use the new Callback template to avoid compiler warnings
00159 
00160 #if (MBED_LIBRARY_VERSION < 122)
00161     callbackFunction clickFn;
00162     callbackFunction doubleClickFn;
00163     callbackFunction pressFn;
00164     callbackFunction longPressStartFn;
00165     callbackFunction longPressStopFn;
00166     callbackFunction duringLongPressFn;
00167 #endif
00168     // These variables that hold information across the upcoming tick calls.
00169     // They are initialized once on program start and are updated every time the tick function is called.
00170     
00171     int ticks;
00172     uint8_t _state;
00173     uint8_t _debounce_cnt;
00174     uint8_t _debounceTicks; // number of ticks for debounce times.
00175     bool button_level;
00176     bool active;
00177     
00178     DigitalIn* btn_pin;
00179     Timer *timer;
00180 };
00181 
00182 #endif
00183 
00184