Library for detecting button clicks, doubleclicks and long press pattern on a single button.

Dependents:   OneButton_Example ABBlind

Committer:
jaup
Date:
Wed Aug 31 10:09:43 2016 +0000
Revision:
0:62d614796022
Child:
1:fdf67f893a5c
event-driven button driver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jaup 0:62d614796022 1 // -----
jaup 0:62d614796022 2 // OneButton.h - Library for detecting button clicks, doubleclicks and long press pattern on a single button.
jaup 0:62d614796022 3 // This class is implemented for use with the Arduino environment.
jaup 0:62d614796022 4 // Copyright (c) by Matthias Hertel, http://www.mathertel.de
jaup 0:62d614796022 5 // This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
jaup 0:62d614796022 6 // More information on: http://www.mathertel.de/Arduino
jaup 0:62d614796022 7 // -----
jaup 0:62d614796022 8 // 02.10.2010 created by Matthias Hertel
jaup 0:62d614796022 9 // 21.04.2011 transformed into a library
jaup 0:62d614796022 10 // 01.12.2011 include file changed to work with the Arduino 1.0 environment
jaup 0:62d614796022 11 // 23.03.2014 Enhanced long press functionalities by adding longPressStart and longPressStop callbacks
jaup 0:62d614796022 12 // 21.09.2015 A simple way for debounce detection added.
jaup 0:62d614796022 13 // -----
jaup 0:62d614796022 14
jaup 0:62d614796022 15 #ifndef OneButton_h
jaup 0:62d614796022 16 #define OneButton_h
jaup 0:62d614796022 17
jaup 0:62d614796022 18 #include "mbed.h"
jaup 0:62d614796022 19 #include "millis.h"
jaup 0:62d614796022 20
jaup 0:62d614796022 21 #define HIGH 1
jaup 0:62d614796022 22 #define LOW 0
jaup 0:62d614796022 23
jaup 0:62d614796022 24 // ----- Callback function types -----
jaup 0:62d614796022 25
jaup 0:62d614796022 26 extern "C" {
jaup 0:62d614796022 27 typedef void (*callbackFunction)(void);
jaup 0:62d614796022 28 }
jaup 0:62d614796022 29
jaup 0:62d614796022 30
jaup 0:62d614796022 31 class OneButton
jaup 0:62d614796022 32 {
jaup 0:62d614796022 33 public:
jaup 0:62d614796022 34 // ----- Constructor -----
jaup 0:62d614796022 35 OneButton(PinName pin, int active = 1);
jaup 0:62d614796022 36
jaup 0:62d614796022 37 // ----- Set runtime parameters -----
jaup 0:62d614796022 38
jaup 0:62d614796022 39 // set # millisec after single click is assumed.
jaup 0:62d614796022 40 void setClickTicks(int ticks);
jaup 0:62d614796022 41
jaup 0:62d614796022 42 // set # millisec after press is assumed.
jaup 0:62d614796022 43 void setPressTicks(int ticks);
jaup 0:62d614796022 44
jaup 0:62d614796022 45 // attach functions that will be called when button was pressed in the specified way.
jaup 0:62d614796022 46 void attachClick(callbackFunction newFunction);
jaup 0:62d614796022 47 void attachDoubleClick(callbackFunction newFunction);
jaup 0:62d614796022 48 void attachPress(callbackFunction newFunction); // DEPRECATED, replaced by longPressStart, longPressStop and duringLongPress
jaup 0:62d614796022 49 void attachLongPressStart(callbackFunction newFunction);
jaup 0:62d614796022 50 void attachLongPressStop(callbackFunction newFunction);
jaup 0:62d614796022 51 void attachDuringLongPress(callbackFunction newFunction);
jaup 0:62d614796022 52
jaup 0:62d614796022 53 // ----- State machine functions -----
jaup 0:62d614796022 54
jaup 0:62d614796022 55 // call this function every some milliseconds for handling button events.
jaup 0:62d614796022 56 void tick(void);
jaup 0:62d614796022 57 bool isLongPressed();
jaup 0:62d614796022 58
jaup 0:62d614796022 59 private:
jaup 0:62d614796022 60 int _pin; // hardware pin number.
jaup 0:62d614796022 61 int _clickTicks; // number of ticks that have to pass by before a click is detected
jaup 0:62d614796022 62 int _pressTicks; // number of ticks that have to pass by before a long button press is detected
jaup 0:62d614796022 63 static
jaup 0:62d614796022 64 const int _debounceTicks = 10; // number of ticks for debounce times.
jaup 0:62d614796022 65
jaup 0:62d614796022 66 int _buttonReleased;
jaup 0:62d614796022 67 int _buttonPressed;
jaup 0:62d614796022 68
jaup 0:62d614796022 69 bool _isLongPressed;
jaup 0:62d614796022 70
jaup 0:62d614796022 71 // These variables will hold functions acting as event source.
jaup 0:62d614796022 72 callbackFunction _clickFunc;
jaup 0:62d614796022 73 callbackFunction _doubleClickFunc;
jaup 0:62d614796022 74 callbackFunction _pressFunc;
jaup 0:62d614796022 75 callbackFunction _longPressStartFunc;
jaup 0:62d614796022 76 callbackFunction _longPressStopFunc;
jaup 0:62d614796022 77 callbackFunction _duringLongPressFunc;
jaup 0:62d614796022 78
jaup 0:62d614796022 79 // These variables that hold information across the upcoming tick calls.
jaup 0:62d614796022 80 // They are initialized once on program start and are updated every time the tick function is called.
jaup 0:62d614796022 81 int _state;
jaup 0:62d614796022 82 unsigned long _startTime; // will be set in state 1
jaup 0:62d614796022 83
jaup 0:62d614796022 84 DigitalIn* btn_pin;
jaup 0:62d614796022 85 };
jaup 0:62d614796022 86
jaup 0:62d614796022 87 #endif
jaup 0:62d614796022 88
jaup 0:62d614796022 89
jaup 0:62d614796022 90