Skip to content

Add support for debouncing non-GPIO inputs. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions include/PicoDebounceButton/PicoDebounceButton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,34 @@ class PicoDebounceButton
gpio_pull_up(mPin);
}

// The constructor can be used without a pin number;
// if so, you will need to manually pass a value to update()
// The default interval is 10ms.
// The default state is PRESSED.
// The default invert is false.
explicit PicoDebounceButton(uint16_t interval = 10,
bool state = PRESSED,
bool invert = false)
: mInterval(interval)
, mState(state)
, mInvert(invert)
{
}

// The update() method should be called in the loop() function. It updates the
// button state and returns true if the button state has changed.
auto update()
bool update()
{
return update(gpio_get(mPin));
}

// The update() method should be called in the loop() function.
// If no pin is specified, you need to pass the latest state value into
// update() as a parameter.
// It updates the button state and returns true if the button state has changed.
bool update(uint8_t val)
{
bool currentState = mInvert ? !gpio_get(mPin) : gpio_get(mPin);
bool currentState = mInvert ? !val : val;
uint32_t now = to_ms_since_boot(get_absolute_time());
if (currentState != mLastState) {
mLastStateTime = now;
Expand Down