Skip to content

Commit

Permalink
optimised
Browse files Browse the repository at this point in the history
inline keyword for small "one line" methods
  • Loading branch information
ShaggyDog18 committed Jan 21, 2021
1 parent fa277c9 commit 301ee4a
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 177 deletions.
7 changes: 4 additions & 3 deletions examples/SimpleOneButton/SimpleOneButton.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// 23.06.2020 synch up with the original library version 1.5; added attachPressStart() function; test includes almost all functions
//--------------------
#include "OneButton.h"
// 3300 / 299

// Setup a new OneButton on pin A1.
OneButton button(A1, true);
Expand All @@ -31,9 +32,9 @@ void setup() {
Serial.begin(9600);

// set more aggressive timing
button.setDebounceTicks( 20 );// default is 50
button.setClickTicks(300); // defaul is 600
button.setPressTicks(500); // defaulty is 1000
button.setDebounceTicks( 40 );// default is 50
button.setClickTicks(400); // defaul is 400
button.setPressTicks(800); // defaulty is 800

// link the doubleclick function to be called on a doubleclick event.
button.attachClick(click);
Expand Down
163 changes: 34 additions & 129 deletions src/OneButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ OneButton::OneButton() {
// further initialization has moved to OneButton.h
}

OneButton::OneButton(const int pin, const bool activeLow, const bool pullupActive) {
OneButton::OneButton(const int16_t pin, const bool activeLow, const bool pullupActive) {
// OneButton();
_pin = pin;
/*
Expand Down Expand Up @@ -59,139 +59,44 @@ OneButton::OneButton(const int pin, const bool activeLow, const bool pullupActiv

} // OneButton


// explicitly set the number of millisec that have to pass by before a click is
// assumed as safe.
void OneButton::setDebounceTicks(const int ticks) {
_debounceTicks = ticks;
} // setDebounceTicks

// explicitly set the number of millisec that have to pass by before a click is
// detected.
void OneButton::setClickTicks(const int ticks) {
_clickTicks = ticks;
} // setClickTicks


// explicitly set the number of millisec that have to pass by before a long
// button press is detected.
void OneButton::setPressTicks(const int ticks) {
_pressTicks = ticks;
} // setPressTicks


// save function for click event
void OneButton::attachClick(callbackFunction newFunction) {
_clickFunc = newFunction;
} // attachClick

#ifdef PARAM_FUNC
// save function for parameterized click event
void OneButton::attachClick(parameterizedCallbackFunction newFunction, void* parameter) {
_paramClickFunc = newFunction;
_clickFuncParam = parameter;
} // attachClick
#endif


// save function for doubleClick event
void OneButton::attachDoubleClick(callbackFunction newFunction) {
_doubleClickFunc = newFunction;
} // attachDoubleClick

#ifdef PARAM_FUNC
// save function for parameterized doubleClick event
void OneButton::attachDoubleClick(parameterizedCallbackFunction newFunction, void* parameter) {
_paramDoubleClickFunc = newFunction;
_doubleClickFuncParam = parameter;
} // attachDoubleClick
#endif


// ShaggyDog ---- Triple and Multiple Clicks ----
// save function for tripleClick event
void OneButton::attachTripleClick(callbackFunction newFunction) {
_tripleClickFunc = newFunction;
} // attachTripleClick

#ifdef PARAM_FUNC
// save function for parameterized doubleClick event
void OneButton::attachTripleClick(parameterizedCallbackFunction newFunction, void* parameter) {
_paramTripleClickFunc = newFunction;
_tripleClickFuncParam = parameter;
} // attachTripleClick
#endif
// ShaggyDog ---- Triple and Multiple Clicks ----


// save function for press event
// DEPRECATED, is replaced by attachLongPressStart, attachLongPressStop,
// attachDuringLongPress,
void OneButton::attachPress(callbackFunction newFunction) {
_pressFunc = newFunction;
} // attachPress


void OneButton::attachPressStart(callbackFunction newFunction) {
_pressStartFunc = newFunction;
} // attachPressStart


// save function for longPressStart event
void OneButton::attachLongPressStart(callbackFunction newFunction) {
_longPressStartFunc = newFunction;
} // attachLongPressStart

#ifdef PARAM_FUNC
// save function for parameterized longPressStart event
void OneButton::attachLongPressStart(parameterizedCallbackFunction newFunction, void* parameter) {
_paramLongPressStartFunc = newFunction;
_longPressStartFuncParam = parameter;
} // attachLongPressStart
#endif


// save function for longPressStop event
void OneButton::attachLongPressStop(callbackFunction newFunction) {
_longPressStopFunc = newFunction;
} // attachLongPressStop

#ifdef PARAM_FUNC
// save function for parameterized longPressStop event
void OneButton::attachLongPressStop(parameterizedCallbackFunction newFunction, void* parameter) {
_paramLongPressStopFunc = newFunction;
_longPressStopFuncParam = parameter;
} // attachLongPressStop
#endif


// save function for during longPress event
void OneButton::attachDuringLongPress(callbackFunction newFunction) {
_duringLongPressFunc = newFunction;
} // attachDuringLongPress

#ifdef PARAM_FUNC
// save function for parameterized during longPress event
void OneButton::attachDuringLongPress(parameterizedCallbackFunction newFunction, void* parameter) {
_paramDuringLongPressFunc = newFunction;
_duringLongPressFuncParam = parameter;
} // attachDuringLongPress
#endif

// function to get the current long pressed state
bool OneButton::isLongPressed() {
return _isLongPressed;
}

int OneButton::getPressedTicks() {
return _stopTime - _startTime;
}

// ShaggyDog ---- return number of ckicks in any case: single or multiple clicks
uint8_t OneButton::getNumberClicks(void) {
return _nClicks;
}

void OneButton::reset(void) {
_state = WAIT_FOR_INITIAL_PRESS; // restart.
_startTime = 0;
Expand All @@ -205,7 +110,7 @@ void OneButton::reset(void) {
* machine (FSM).
*/
void OneButton::tick(void) {
if (_pin >= 0) {
if(_pin >= 0) {
tick(digitalRead(_pin) == _buttonPressed);
}
}
Expand All @@ -230,22 +135,22 @@ void OneButton::tick(const bool buttonIsPressed) {

case DEBOUNCE_OR_LONG_PRESS: // waiting for button being released.
if (buttonIsPressed) {
if ((unsigned long)(now - _startTime) > _pressTicks) {
if ((uint16_t)(now - _startTime) > _pressTicks) {
_isLongPressed = true; // Keep track of long press state
_nClicks = 1;
if (_longPressStartFunc) _longPressStartFunc();
#ifdef PARAM_FUNC
if (_paramLongPressStartFunc) _paramLongPressStartFunc(_longPressStartFuncParam);
#endif
#ifdef PARAM_FUNC
if (_paramLongPressStartFunc) _paramLongPressStartFunc(_longPressStartFuncParam);
#endif
_state = LONG_PRESS; // step to state 6
_stopTime = now; // remember stopping time
}
} else { // button was released
if ((unsigned long)(now - _startTime) < _debounceTicks) {
if ((uint16_t)(now - _startTime) < _debounceTicks) {
// button was released to quickly, so I assume some debouncing.
// go back to state 0 without calling a function.
_state = WAIT_FOR_INITIAL_PRESS;
} else {
} else {
_state = DETECT_CLICK; // step to state 2
_stopTime = now; // remember stopping time
if (_pressStartFunc) _pressStartFunc();
Expand All @@ -254,41 +159,41 @@ void OneButton::tick(const bool buttonIsPressed) {
break;

case DETECT_CLICK: // waiting for button being pressed or timeout.
if ((unsigned long)(now - _startTime) > _clickTicks) { // a click is detected
_nClicks++;
switch(_nClicks) {
case 1: // one click
if (_pressFunc) _pressFunc();
if (_clickFunc) _clickFunc();
#ifdef PARAM_FUNC
if (_paramClickFunc) _paramClickFunc(_clickFuncParam);
#endif
break;
case 2: // two clicks
if (_doubleClickFunc ) _doubleClickFunc();
#ifdef PARAM_FUNC
if (_paramDoubleClickFunc) _paramDoubleClickFunc(_doubleClickFuncParam);
#endif
break;
default: // number of clicks > 2
if (_tripleClickFunc ) _tripleClickFunc();
#ifdef PARAM_FUNC
if (_paramTripleClickFunc) _paramTripleClickFunc(_tripleClickFuncParam);
#endif
} // switch() number of clicks
if ((uint16_t)(now - _startTime) > _clickTicks) { // a click is detected
_nClicks++;
switch(_nClicks) {
case 1: // one click
if (_pressFunc) _pressFunc();
if (_clickFunc) _clickFunc();
#ifdef PARAM_FUNC
if (_paramClickFunc) _paramClickFunc(_clickFuncParam);
#endif
break;
case 2: // two clicks
if (_doubleClickFunc ) _doubleClickFunc();
#ifdef PARAM_FUNC
if (_paramDoubleClickFunc) _paramDoubleClickFunc(_doubleClickFuncParam);
#endif
break;
default: // number of clicks > 2
if (_tripleClickFunc ) _tripleClickFunc();
#ifdef PARAM_FUNC
if (_paramTripleClickFunc) _paramTripleClickFunc(_tripleClickFuncParam);
#endif
} // switch() number of clicks
_state = WAIT_FOR_INITIAL_PRESS; // restart.
} else {
if (buttonIsPressed && ((unsigned long)(now - _stopTime) > _debounceTicks)) {
if (buttonIsPressed && ((uint16_t)(now - _stopTime) > _debounceTicks)) {
_state = COUNT_CLICKS; // step to state 3
_startTime = now; // remember starting time
} // if
} // if
break;
break;

case COUNT_CLICKS: // waiting for button to be released
// Stay here for at least _debounceTicks because else we might end up in
// state 1 if the button bounces for too long.
if ((!buttonIsPressed) && ((unsigned long)(now - _startTime) > _debounceTicks)) {
if ((!buttonIsPressed) && ((uint16_t)(now - _startTime) > _debounceTicks)) {
// this was a two(2) clicks sequence or an X-number of clicks sequence.
_nClicks++;
_state = DETECT_CLICK; // go to state 2 for more clicks
Expand All @@ -315,4 +220,4 @@ void OneButton::tick(const bool buttonIsPressed) {
break;
} // switch(_state) machine
} // OneButton.tick()
// end.
// end
Loading

0 comments on commit 301ee4a

Please sign in to comment.