Skip to content
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

Provide high-level API for buttons #10

Open
alranel opened this issue Feb 27, 2024 · 1 comment
Open

Provide high-level API for buttons #10

alranel opened this issue Feb 27, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@alranel
Copy link
Member

alranel commented Feb 27, 2024

For ModulinoButtons and ModulinoEncoder (which provides a button), it would be very helpful to provide higher level API to detect clicks instead of having users implement their own state detection and debouncing logic.

Example of the desired API:

void loop() {
   buttons.update();

   if (buttons.wasClicked(0)) {
      // button 0 was pressed and released
   }
}

All the methods would return true only once after the state change, so that we don't get multiple triggers while the loop loops.

Maybe the basic .get(0) method could be renamed .isPressed(0) for clarity.

Bonus points:

  • detection of double click (.wasDoubleClicked(0))
  • detection of simple state change (.wasPressed(0), returning true only once after the state change + .wasReleased(0) as alias to .wasClicked(0))
  • calculation of duration of press, useful to let users detect long presses (duration = buttons.wasPressedFor(0))

See also

There are some very nice libraries for this. One of them is https://github.com/LennartHennigs/Button2

@alranel alranel added the enhancement New feature or request label Feb 27, 2024
@facchinm
Copy link
Member

Questions after testing one possible implementation:

  • how often is the user required to call buttons.update() ? The Buttons2 library "overcomes" this by requiring to update the status as much as possible
  • how long is the "memory" of the system? Should it return true for a double click happened minutes ago?
  • is the status only consistent between calls to update() or the first time a was* function was called?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants