Skip to content

Commit

Permalink
Enhanced long press functionalities by adding longPressStart and long…
Browse files Browse the repository at this point in the history
…PressStop callbacks
  • Loading branch information
arjenbreur committed Mar 23, 2014
1 parent 7dca076 commit ce8f62c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
38 changes: 36 additions & 2 deletions OneButton.cpp
Expand Up @@ -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"
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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)
{
Expand All @@ -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 {
Expand All @@ -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
Expand Down
15 changes: 12 additions & 3 deletions OneButton.h
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion keywords.txt
Expand Up @@ -17,7 +17,10 @@ setClickTicks KEYWORD2
setPressTicks KEYWORD2
attachClick KEYWORD2
attachDoubleClick KEYWORD2
attachPress KEYWORD2
attachLongPressStart KEYWORD2
attachLongPressStop KEYWORD2
attachDuringLongPress KEYWORD2
isLongPressed KEYWORD2
tick KEYWORD2

#######################################
Expand Down

0 comments on commit ce8f62c

Please sign in to comment.