Skip to content

Commit

Permalink
Merge pull request #131 from bjrnptrsn/master
Browse files Browse the repository at this point in the history
Add attachIdle and setIdleMs
  • Loading branch information
mathertel authored Dec 2, 2023
2 parents 1832bfb + 60b8dde commit eb36148
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/OneButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions src/OneButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----

/**
Expand Down Expand Up @@ -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 -----

/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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()
Expand Down

0 comments on commit eb36148

Please sign in to comment.