Implement a click detector for multiple physical buttons.
You receive button state updates as a stream. Each update provides:
-
button_id
(integer)
-
state
where
1 = pressed
,
0 = released
-
(initially) updates arrive every
10 ms
per button (you may assume updates are ordered in time).
A “click” is a press followed by a release. Classify each completed click as:
-
Single Click
: press duration
≤ 200 ms
-
Long Click
: press duration
≥ 500 ms
For each detected click, output something like:
-
Button 1: Single Click
-
Button 2: Long Click
Requirements / edge cases:
-
Support
multiple buttons
concurrently.
-
Handle
back-to-back clicks
on the same button (press/release cycles repeated over time).
-
Handle
no click
(e.g., no transitions) without producing output.
-
Clarify/handle what to do for press durations in the
(200 ms, 500 ms)
gap.
Follow-up:
If state updates do not arrive every 10 ms (variable intervals), how would you modify the implementation? Assume you can obtain timestamps (e.g., via a system call like now_ms()) for each update.