Sound Byte Libs 29c5ff3
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/components/control/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::control::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#pragma once
20
21#include <cstdint>
22
23namespace sbl {
24namespace components {
25namespace control {
26
27class TapHold {
28public:
29 /// @param hold_threshold Ticks before press becomes a hold.
30 explicit TapHold(uint16_t hold_threshold = 60)
31 : threshold_(hold_threshold) {}
32
33 /// Call every tick with the button's current pressed state and release edge.
34 void update(bool pressed, bool just_released) {
35 if (pressed) {
36 ticks_++;
37 if (!hold_fired_ && ticks_ >= threshold_) {
38 hold_edge_ = true;
39 hold_fired_ = true;
40 }
41 }
42
43 if (just_released) {
44 if (!hold_fired_ && ticks_ > 0) {
45 tap_edge_ = true;
46 }
47 if (hold_fired_) {
48 release_from_hold_edge_ = true;
49 }
50 ticks_ = 0;
51 hold_fired_ = false;
52 }
53 }
54
55 /// True once after a short press (released before hold threshold).
56 bool tapped() {
57 bool e = tap_edge_;
58 tap_edge_ = false;
59 return e;
60 }
61
62 /// True once when hold threshold is reached (while still pressed).
63 bool held() {
64 bool e = hold_edge_;
65 hold_edge_ = false;
66 return e;
67 }
68
69 /// True once when released after a hold.
71 bool e = release_from_hold_edge_;
72 release_from_hold_edge_ = false;
73 return e;
74 }
75
76 /// Currently in held state (pressed past threshold, not yet released).
77 bool holding() const { return hold_fired_; }
78
79 /// Current tick count (0 when not pressed).
80 uint16_t hold_ticks() const { return ticks_; }
81
82 void reset() {
83 ticks_ = 0;
84 hold_fired_ = false;
85 tap_edge_ = false;
86 hold_edge_ = false;
87 release_from_hold_edge_ = false;
88 }
89
90private:
91 uint16_t threshold_;
92 uint16_t ticks_ = 0;
93 bool hold_fired_ = false;
94 bool tap_edge_ = false;
95 bool hold_edge_ = false;
96 bool release_from_hold_edge_ = false;
97};
98
99} // namespace control
100} // namespace components
101} // namespace sbl
bool holding() const
Currently in held state (pressed past threshold, not yet released).
Definition tap_hold.hpp:77
bool tapped()
True once after a short press (released before hold threshold).
Definition tap_hold.hpp:56
uint16_t hold_ticks() const
Current tick count (0 when not pressed).
Definition tap_hold.hpp:80
bool released_from_hold()
True once when released after a hold.
Definition tap_hold.hpp:70
bool held()
True once when hold threshold is reached (while still pressed).
Definition tap_hold.hpp:63
TapHold(uint16_t hold_threshold=60)
Definition tap_hold.hpp:30
void update(bool pressed, bool just_released)
Call every tick with the button's current pressed state and release edge.
Definition tap_hold.hpp:34
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24