Skip to content

Detect a long press without releasing the button #7

Description

@avsaase

I would like to be able to detect long presses without releasing the button.

I tried this:

loop {
    button.tick();
    if let Some(dur) = button.current_holding_time() {
        if dur >= Duration::from_secs(2) {
            info!("Long press");
        }
    }
    button.reset();
    Timer::after_millis(2).await;
}

This works but after the initial delay it repeatedly prints "Long press" until the button is released. I would like it to ignore the button position until it is released so that the "event" is only fired once.

Looking at the code I think the easiest way to enable this behavior is to add a new State:PendingRelease that can be set by the user and a match pattern in tick() like this:

State::PendingRelease if self.is_pin_released() => self.state = State::Released,

WDTY? Does it make sense to add this to your library? I can make a PR with this change or another solution if you want.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions