Provide high-level API for buttons
Closed this issue · 2 comments
alranel commented
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
facchinm commented
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
truefor a double click happened minutes ago? - is the status only consistent between calls to
update()or the first time awas*function was called?
sebromero commented
Implemented via Button2 integration