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
knob_gate.hpp
Go to the documentation of this file.
1// sbl/hw/components/input/knob_gate.hpp — Anti-jump knob gate
2//
3// After a mode switch, a knob's position may map to a completely different
4// parameter value. KnobGate suppresses the knob's output until the user
5// physically moves it past a threshold from its entry position. This
6// prevents parameter jumps on mode transitions.
7//
8// Pair one KnobGate with each knob that has multi-mode mappings.
9//
10// Usage:
11// sbl::components::input::KnobGate gate;
12// // On mode switch:
13// gate.arm(knob.value());
14// // In your control loop:
15// gate.update(knob.value());
16// if (gate.live()) {
17// apply_parameter(knob.scaled(...));
18// }
19
20#ifndef SBL_HW_COMPONENTS_INPUT_KNOB_GATE_HPP_
21#define SBL_HW_COMPONENTS_INPUT_KNOB_GATE_HPP_
22
23#include <cstdint>
24
25namespace sbl {
26namespace components {
27namespace input {
28
29class KnobGate {
30public:
31 /// @param threshold Movement required to unlock (0-65535). Default ~3%.
32 explicit KnobGate(uint16_t threshold = 2000)
33 : threshold_(threshold) {}
34
35 /// Lock the gate and record current knob position.
36 void arm(uint16_t entry_value) {
37 entry_ = entry_value;
38 live_ = false;
39 }
40
41 /// Check if knob has moved enough from entry position to unlock.
42 void update(uint16_t current_value) {
43 if (live_) return;
44 int32_t d = static_cast<int32_t>(current_value) - static_cast<int32_t>(entry_);
45 if (d < 0) d = -d;
46 if (d > static_cast<int32_t>(threshold_)) {
47 live_ = true;
48 }
49 }
50
51 /// True when the gate is open (output allowed).
52 bool live() const { return live_; }
53
54 /// Force the gate open (e.g., for the initial mode that needs no gating).
55 void force_live() { live_ = true; }
56
57 /// Set threshold (0-65535).
58 void set_threshold(uint16_t t) { threshold_ = t; }
59
60private:
61 uint16_t threshold_;
62 uint16_t entry_ = 0;
63 bool live_ = true; // Start live (not armed)
64};
65
66} // namespace input
67} // namespace components
68} // namespace sbl
69
70#endif // SBL_HW_COMPONENTS_INPUT_KNOB_GATE_HPP_
void set_threshold(uint16_t t)
Set threshold (0-65535).
Definition knob_gate.hpp:58
void update(uint16_t current_value)
Check if knob has moved enough from entry position to unlock.
Definition knob_gate.hpp:42
bool live() const
True when the gate is open (output allowed).
Definition knob_gate.hpp:52
KnobGate(uint16_t threshold=2000)
Definition knob_gate.hpp:32
void arm(uint16_t entry_value)
Lock the gate and record current knob position.
Definition knob_gate.hpp:36
void force_live()
Force the gate open (e.g., for the initial mode that needs no gating).
Definition knob_gate.hpp:55
Root namespace for all Sound Byte Libs code.
Definition assert.hpp:51