Skip to content

Provide high-level API for buttons #10

Description

@alranel

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

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

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions