Module button

Module button 

Source
Expand description

Button event recognition state machine.

Resolves a stream of raw ButtonEdge events plus a monotonic millisecond clock into the high-level ButtonEvents the UX is defined in terms of: single, double, triple, and quadruple clicks, plus long-press and an optional very-long-press.

The machine is pure logic — no embassy, no hardware, no I/O — so it can be exhaustively unit-tested with synthetic time. Callers are expected to:

  1. Call ButtonFsm::on_edge for each debounced press / release.
  2. Call ButtonFsm::poll when the deadline reported by ButtonFsm::next_deadline elapses.

Both methods can produce a ButtonEvent when one becomes resolvable.

§Recognition rules

  • A “click” is a press-then-release where the hold duration is at most max_click_hold.
  • One to four clicks within inter_click_gap of each other produce ButtonEvent::Single, ButtonEvent::Double, ButtonEvent::Triple, or ButtonEvent::Quad respectively. Quad fires immediately on the fourth release without waiting for the gap; single / double / triple fire after the gap elapses with no further press.
  • By default, holding the button continuously for long_press produces ButtonEvent::Long while the button is still pressed and consumes any clicks accumulated before the hold began.
  • When very_long_press is configured, releasing between long_press and very_long_press produces ButtonEvent::Long. Remaining held through very_long_press produces ButtonEvent::VeryLong without first emitting Long. This supports a navigational hold plus a distinct always-available sleep hold.
  • Releases longer than max_click_hold but shorter than long_press are discarded clicks; if there are accumulated prior clicks they are emitted, otherwise nothing fires. This matches the rule: a press that’s “too long to be a click but too short to be a long-press” is a user error and should not silently become either.

Structs§

ButtonFsm
Button event recognition state machine.
ButtonTimings
Tunable timings; tune on real hardware.

Enums§

ButtonEdge
Raw debounced button transition.
ButtonEvent
High-level button event recognized by the FSM.