From 60b8dde9820a72db4b68c5d9e5ca3b9c18ecaacf Mon Sep 17 00:00:00 2001 From: bjrnptrsn Date: Sun, 8 Oct 2023 15:19:48 +0200 Subject: [PATCH] Add attachIdle and setIdleMs --- src/OneButton.cpp | 22 +++++++++++++++++++++- src/OneButton.h | 16 ++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/OneButton.cpp b/src/OneButton.cpp index 429da21..8eab8ab 100644 --- a/src/OneButton.cpp +++ b/src/OneButton.cpp @@ -78,6 +78,11 @@ void OneButton::setPressMs(const unsigned int ms) _press_ms = ms; } // setPressMs +// explicitly set the number of millisec that have to pass by before button idle is detected. +void OneButton::setIdleMs(const unsigned int ms) +{ + _idle_ms = ms; +} // setIdleMs // save function for click event void OneButton::attachClick(callbackFunction newFunction) @@ -173,11 +178,19 @@ void OneButton::attachDuringLongPress(parameterizedCallbackFunction newFunction, } // attachDuringLongPress +// save function for idle button event +void OneButton::attachIdle(callbackFunction newFunction) +{ + _idleFunc = newFunction; +} // attachIdle + + void OneButton::reset(void) { _state = OneButton::OCS_INIT; _nClicks = 0; - _startTime = 0; + _startTime = millis(); + _idleState = false; } @@ -242,6 +255,13 @@ void OneButton::_fsm(bool activeLevel) // Implementation of the state machine switch (_state) { case OneButton::OCS_INIT: + // on idle for idle_ms call idle function + if (!_idleState and (waitTime > _idle_ms)) + if (_idleFunc) { + _idleState = true; + _idleFunc(); + } + // waiting for level to become active. if (activeLevel) { _newState(OneButton::OCS_DOWN); diff --git a/src/OneButton.h b/src/OneButton.h index 49f6535..8beb5ff 100644 --- a/src/OneButton.h +++ b/src/OneButton.h @@ -78,6 +78,11 @@ class OneButton */ void setLongPressIntervalMs(const unsigned int ms) { _long_press_interval_ms = ms; }; + /** + * set # millisec after idle is assumed. + */ + void setIdleMs(const unsigned int ms); + // ----- Attach events functions ----- /** @@ -123,6 +128,12 @@ class OneButton void attachDuringLongPress(callbackFunction newFunction); void attachDuringLongPress(parameterizedCallbackFunction newFunction, void *parameter); + /** + * Attach an event when the button is in idle position. + * @param newFunction + */ + void attachIdle(callbackFunction newFunction); + // ----- State machine functions ----- /** @@ -169,6 +180,7 @@ class OneButton unsigned int _debounce_ms = 50; // number of msecs for debounce times. unsigned int _click_ms = 400; // number of msecs before a click is detected. unsigned int _press_ms = 800; // number of msecs before a long button press is detected + unsigned int _idle_ms = 1000; // number of msecs before idle is detected int _buttonPressed = 0; // this is the level of the input pin when the button is pressed. // LOW if the button connects the input pin to GND when pressed. @@ -199,6 +211,8 @@ class OneButton parameterizedCallbackFunction _paramDuringLongPressFunc = NULL; void *_duringLongPressFuncParam = NULL; + callbackFunction _idleFunc = NULL; + // 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. @@ -225,6 +239,8 @@ class OneButton stateMachine_t _state = OCS_INIT; + bool _idleState = false; + int debouncedPinLevel = -1; int _lastDebouncePinLevel = -1; // used for pin debouncing unsigned long _lastDebounceTime = 0; // millis()