Sound Byte Libs 0.5.1-340-gab5b79a
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/**
2 * @file tap_hold.hpp
3 * @brief Tap vs hold detector
4 *
5 * Tells a short tap from a long hold. Feed it a Button's pressed() and
6 * just_released() and the time since the update before.
7 *
8 * - tapped(): true once after a press shorter than the hold time
9 * - held(): true once when the hold time is reached
10 * - released_from_hold(): true once when released after a hold
11 *
12 * Reading an edge clears it. Times are ticks of the clock that drives it; it
13 * holds no rate and counts no calls.
14 *
15 * @code
16 * using Clock = sbl::control::TargetClock;
17 * sbl::control::input::TapHold th(Clock::ticks_from_ms(300));
18 * th.update(button.pressed(), button.just_released(), elapsed_ticks);
19 * if (th.tapped()) { ... }
20 * if (th.held()) { ... }
21 * @endcode
22 */
23
24#ifndef SBL_CONTROL_INPUT_TAP_HOLD_HPP_
25#define SBL_CONTROL_INPUT_TAP_HOLD_HPP_
26
27#include <cstdint>
28
29namespace sbl::control::input {
30
31/** @brief Tells a tap from a hold on a debounced button */
32class TapHold {
33public:
34 /** @param hold_ticks How long a press lasts before it is a hold, in ticks. */
35 explicit TapHold(uint32_t hold_ticks) : hold_ticks_(hold_ticks) {}
36
37 /**
38 * @param pressed the button's debounced state
39 * @param just_released its release edge
40 * @param elapsed_ticks ticks since the previous update
41 */
42 void update(bool pressed, bool just_released, uint32_t elapsed_ticks) {
43 if (pressed) {
44 if (pressing_) {
45 const uint32_t sum = pressed_ticks_ + elapsed_ticks;
46 pressed_ticks_ = sum < pressed_ticks_ ? UINT32_MAX : sum; // stops at the top of its range
47 } else {
48 pressing_ = true;
49 pressed_ticks_ = 0;
50 }
51 if (!hold_fired_ && pressed_ticks_ >= hold_ticks_) {
52 hold_edge_ = true;
53 hold_fired_ = true;
54 }
55 }
56
57 if (just_released) {
58 if (hold_fired_) {
59 release_from_hold_edge_ = true;
60 } else if (pressing_) {
61 tap_edge_ = true;
62 }
63 pressing_ = false;
64 pressed_ticks_ = 0;
65 hold_fired_ = false;
66 }
67 }
68
69 /** True once after a short press (released before the hold time). */
70 bool tapped() {
71 const bool e = tap_edge_;
72 tap_edge_ = false;
73 return e;
74 }
75
76 /** True once when the hold time is reached, while still pressed. */
77 bool held() {
78 const bool e = hold_edge_;
79 hold_edge_ = false;
80 return e;
81 }
82
83 /** True once when released after a hold. */
85 const bool e = release_from_hold_edge_;
86 release_from_hold_edge_ = false;
87 return e;
88 }
89
90 /** Pressed past the hold time and not yet released. */
91 bool holding() const { return hold_fired_; }
92
93 /** Ticks since the press was first seen; 0 when not pressed. */
94 uint32_t pressed_ticks() const { return pressed_ticks_; }
95
96 void reset() {
97 pressed_ticks_ = 0;
98 pressing_ = false;
99 hold_fired_ = false;
100 tap_edge_ = false;
101 hold_edge_ = false;
102 release_from_hold_edge_ = false;
103 }
104
105private:
106 uint32_t hold_ticks_;
107 uint32_t pressed_ticks_ = 0;
108 bool pressing_ = false;
109 bool hold_fired_ = false;
110 bool tap_edge_ = false;
111 bool hold_edge_ = false;
112 bool release_from_hold_edge_ = false;
113};
114
115} // namespace sbl::control::input
116
117#endif // SBL_CONTROL_INPUT_TAP_HOLD_HPP_
Tells a tap from a hold on a debounced button.
Definition tap_hold.hpp:32
void update(bool pressed, bool just_released, uint32_t elapsed_ticks)
Definition tap_hold.hpp:42
TapHold(uint32_t hold_ticks)
Definition tap_hold.hpp:35
uint32_t pressed_ticks() const
Definition tap_hold.hpp:94
Inputs: debounce, decoding, smoothing and scaling of raw samples.
Definition button.hpp:29