diff --git a/OneButton.cpp b/OneButton.cpp index 6d3e8db..08646fd 100644 --- a/OneButton.cpp +++ b/OneButton.cpp @@ -10,6 +10,7 @@ // 21.04.2011 support of active LOW and active HIGH button signal input. // 01.12.2011 include file changed to work with the Arduino 1.0 environment // 12.01.2014 some typos fixed. +// 01.03.2014 Enhanced long press functionalities by adding longPressStart and longPressStop callbacks // ----- #include "OneButton.h" @@ -25,6 +26,7 @@ OneButton::OneButton(int pin, int activeLow) _pressTicks = 1000; // number of millisec that have to pass by before a long button press is detected. _state = 0; // starting with state 0: waiting for button to be pressed + _isLongPressed = false; // Keep track of long press state if (activeLow) { // button connects ground to the pin when pressed. @@ -41,13 +43,13 @@ OneButton::OneButton(int pin, int activeLow) } // OneButton -// explicitely set the number of millisec that have to pass by before a click is detected. +// explicitly set the number of millisec that have to pass by before a click is detected. void OneButton::setClickTicks(int ticks) { _clickTicks = ticks; } // setClickTicks -// explicitely set the number of millisec that have to pass by before a lonn button press is detected. +// explicitly set the number of millisec that have to pass by before a long button press is detected. void OneButton::setPressTicks(int ticks) { _pressTicks = ticks; } // setPressTicks @@ -68,11 +70,34 @@ void OneButton::attachDoubleClick(callbackFunction newFunction) // save function for press event +// DEPRECATED, is replaced by attachLongPressStart, attachLongPressStop, attachDuringLongPress, void OneButton::attachPress(callbackFunction newFunction) { _pressFunc = newFunction; } // attachPress +// save function for longPressStart event +void OneButton::attachLongPressStart(callbackFunction newFunction) +{ + _longPressStartFunc = newFunction; +} // attachLongPressStart + +// save function for longPressStop event +void OneButton::attachLongPressStop(callbackFunction newFunction) +{ + _longPressStopFunc = newFunction; +} // attachLongPressStop + +// save function for during longPress event +void OneButton::attachDuringLongPress(callbackFunction newFunction) +{ + _duringLongPressFunc = newFunction; +} // attachDuringLongPress + +// function to get the current long pressed state +bool OneButton::isLongPressed(){ + return _isLongPressed; +} void OneButton::tick(void) { @@ -92,7 +117,10 @@ void OneButton::tick(void) _state = 2; // step to state 2 } else if ((buttonLevel == _buttonPressed) && (now > _startTime + _pressTicks)) { + _isLongPressed = true; // Keep track of long press state if (_pressFunc) _pressFunc(); + if (_longPressStartFunc) _longPressStartFunc(); + if (_duringLongPressFunc) _duringLongPressFunc(); _state = 6; // step to state 6 } else { @@ -118,7 +146,13 @@ void OneButton::tick(void) } else if (_state == 6) { // waiting for menu pin being release after long press. if (buttonLevel == _buttonReleased) { + _isLongPressed = false; // Keep track of long press state + if(_longPressStopFunc) _longPressStopFunc(); _state = 0; // restart. + } else { + // button is being long pressed + _isLongPressed = true; // Keep track of long press state + if (_duringLongPressFunc) _duringLongPressFunc(); } // if } // if diff --git a/OneButton.h b/OneButton.h index 795d860..7e557d8 100644 --- a/OneButton.h +++ b/OneButton.h @@ -8,6 +8,7 @@ // 02.10.2010 created by Matthias Hertel // 21.04.2011 transformed into a library // 01.12.2011 include file changed to work with the Arduino 1.0 environment +// 23.03.2014 Enhanced long press functionalities by adding longPressStart and longPressStop callbacks // ----- #ifndef OneButton_h @@ -36,15 +37,19 @@ class OneButton // set # millisec after press is assumed. void setPressTicks(int ticks); - // attach functions that will be called when button was pressed in th especified way. + // attach functions that will be called when button was pressed in the specified way. void attachClick(callbackFunction newFunction); void attachDoubleClick(callbackFunction newFunction); - void attachPress(callbackFunction newFunction); + void attachPress(callbackFunction newFunction); // DEPRECATED, replaced by longPressStart, longPressStop and duringLongPress + void attachLongPressStart(callbackFunction newFunction); + void attachLongPressStop(callbackFunction newFunction); + void attachDuringLongPress(callbackFunction newFunction); // ----- State machine functions ----- // call this function every some milliseconds for handling button events. void tick(void); + bool isLongPressed(); private: int _pin; // hardware pin number. @@ -54,13 +59,17 @@ class OneButton int _buttonReleased; int _buttonPressed; + bool _isLongPressed; // These variables will hold functions acting as event source. callbackFunction _clickFunc; callbackFunction _doubleClickFunc; callbackFunction _pressFunc; + callbackFunction _longPressStartFunc; + callbackFunction _longPressStopFunc; + callbackFunction _duringLongPressFunc; - // These variables that hold information across the upcomming tick calls. + // These variables that hold information across the upcoming tick calls. // They are initialized once on program start and are updated every time the tick function is called. int _state; unsigned long _startTime; // will be set in state 1 diff --git a/keywords.txt b/keywords.txt index 7506a42..d7a3d33 100644 --- a/keywords.txt +++ b/keywords.txt @@ -17,7 +17,10 @@ setClickTicks KEYWORD2 setPressTicks KEYWORD2 attachClick KEYWORD2 attachDoubleClick KEYWORD2 -attachPress KEYWORD2 +attachLongPressStart KEYWORD2 +attachLongPressStop KEYWORD2 +attachDuringLongPress KEYWORD2 +isLongPressed KEYWORD2 tick KEYWORD2 #######################################