Sound Byte Libs 0.5.1-121-g3358a44
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
tap_hold.hpp
Go to the documentation of this file.
1// sbl/hw/components/input/tap_hold.hpp — Tap vs hold detector
2//
3// Wraps a Button's pressed/released state to distinguish short taps
4// from long holds. Feed it pressed() and just_released() each tick.
5//
6// - tapped(): true once after a short press (< threshold)
7// - held(): true once when hold threshold is reached
8// - released_from_hold(): true once when released after a hold
9//
10// All edge reads are consuming — they return true once then reset.
11//
12// Usage:
13// sbl::components::input::TapHold th(60); // 300ms at 200 Hz
14// // In your control loop:
15// th.update(button.pressed(), button.just_released());
16// if (th.tapped()) { /* short press action */ }
17// if (th.held()) { /* long press action */ }
18
19#ifndef SBL_HW_COMPONENTS_INPUT_TAP_HOLD_HPP_
20#define SBL_HW_COMPONENTS_INPUT_TAP_HOLD_HPP_
21
22#include <cstdint>
23
24namespace sbl {
25namespace components {
26namespace input {
27
28class TapHold {
29public:
30 /// @param hold_threshold Ticks before press becomes a hold.
31 explicit TapHold(uint16_t hold_threshold = 60)
32 : threshold_(hold_threshold) {}
33
34 /// Call every tick with the button's current pressed state and release edge.
35 void update(bool pressed, bool just_released) {
36 if (pressed) {
37 ticks_++;
38 if (!hold_fired_ && ticks_ >= threshold_) {
39 hold_edge_ = true;
40 hold_fired_ = true;
41 }
42 }
43
44 if (just_released) {
45 if (!hold_fired_ && ticks_ > 0) {
46 tap_edge_ = true;
47 }
48 if (hold_fired_) {
49 release_from_hold_edge_ = true;
50 }
51 ticks_ = 0;
52 hold_fired_ = false;
53 }
54 }
55
56 /// True once after a short press (released before hold threshold).
57 bool tapped() {
58 bool e = tap_edge_;
59 tap_edge_ = false;
60 return e;
61 }
62
63 /// True once when hold threshold is reached (while still pressed).
64 bool held() {
65 bool e = hold_edge_;
66 hold_edge_ = false;
67 return e;
68 }
69
70 /// True once when released after a hold.
72 bool e = release_from_hold_edge_;
73 release_from_hold_edge_ = false;
74 return e;
75 }
76
77 /// Currently in held state (pressed past threshold, not yet released).
78 bool holding() const { return hold_fired_; }
79
80 /// Current tick count (0 when not pressed).
81 uint16_t hold_ticks() const { return ticks_; }
82
83 void reset() {
84 ticks_ = 0;
85 hold_fired_ = false;
86 tap_edge_ = false;
87 hold_edge_ = false;
88 release_from_hold_edge_ = false;
89 }
90
91private:
92 uint16_t threshold_;
93 uint16_t ticks_ = 0;
94 bool hold_fired_ = false;
95 bool tap_edge_ = false;
96 bool hold_edge_ = false;
97 bool release_from_hold_edge_ = false;
98};
99
100} // namespace input
101} // namespace components
102} // namespace sbl
103
104#endif // SBL_HW_COMPONENTS_INPUT_TAP_HOLD_HPP_
bool held()
True once when hold threshold is reached (while still pressed).
Definition tap_hold.hpp:64
uint16_t hold_ticks() const
Current tick count (0 when not pressed).
Definition tap_hold.hpp:81
bool holding() const
Currently in held state (pressed past threshold, not yet released).
Definition tap_hold.hpp:78
bool released_from_hold()
True once when released after a hold.
Definition tap_hold.hpp:71
bool tapped()
True once after a short press (released before hold threshold).
Definition tap_hold.hpp:57
TapHold(uint16_t hold_threshold=60)
Definition tap_hold.hpp:31
void update(bool pressed, bool just_released)
Call every tick with the button's current pressed state and release edge.
Definition tap_hold.hpp:35
Root namespace for all Sound Byte Libs code.
Definition assert.hpp:51