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
gate_input.hpp
Go to the documentation of this file.
1/**
2 * @file gate_input.hpp
3 * @brief Gate input with Schmitt trigger hysteresis
4 *
5 * GateInput takes raw ADC values (uint16_t) and applies a Schmitt trigger
6 * threshold to produce a clean digital gate signal. Hysteresis prevents
7 * chatter near the threshold. One-shot edge detection for triggers.
8 *
9 * Usage:
10 * @code
11 * sbl::control::input::GateInput gate;
12 * gate.update(adc_value); // Feed raw ADC each tick
13 * if (gate.rising()) { ... } // One-shot rising edge (auto-resets)
14 * if (gate.gate()) { ... } // Current state
15 * @endcode
16 */
17
18#ifndef SBL_CONTROL_INPUT_GATE_INPUT_HPP_
19#define SBL_CONTROL_INPUT_GATE_INPUT_HPP_
20
21#include <cstdint>
22
23namespace sbl::control::input {
24
25/** @brief GateInput construction settings */
26struct GateConfig {
27 /** ADC value at or above which the gate turns on (about 2 V of a 3.3 V, 16-bit span) */
28 uint16_t threshold_high = 40000;
29 /** ADC value below which the gate turns off; keep it below threshold_high */
30 uint16_t threshold_low = 20000;
31};
32
33/**
34 * @brief Gate input with Schmitt trigger hysteresis and edge detection
35 *
36 * @context All public methods are ISR-safe: pure state machine with no hardware I/O.
37 */
38class GateInput {
39public:
40 /**
41 * @brief Construct with the gate off
42 * @param config Thresholds
43 */
44 explicit GateInput(const GateConfig& config = {})
45 : thresh_high_(config.threshold_high),
46 thresh_low_(config.threshold_low),
47 state_(false),
48 rising_(false),
49 falling_(false) {}
50
51 /**
52 * @brief Feed a new raw ADC sample
53 *
54 * Schmitt trigger: gate turns ON when value >= threshold_high,
55 * turns OFF when value < threshold_low. The hysteresis band
56 * between the two thresholds prevents chatter.
57 *
58 * @param raw ADC sample, 0 to 65535
59 */
60 void update(uint16_t raw) {
61 bool was = state_;
62
63 if (!state_ && raw >= thresh_high_) {
64 state_ = true;
65 } else if (state_ && raw < thresh_low_) {
66 state_ = false;
67 }
68
69 if (state_ && !was) {
70 rising_ = true;
71 } else if (!state_ && was) {
72 falling_ = true;
73 }
74 }
75
76 /** @brief Current gate state (true = high) */
77 bool gate() const { return state_; }
78
79 /**
80 * @brief True if gate just went high (rising edge)
81 *
82 * Consuming read - resets after first read. Call once per update cycle.
83 */
84 bool rising() {
85 bool e = rising_;
86 rising_ = false;
87 return e;
88 }
89
90 /**
91 * @brief True if gate just went low (falling edge)
92 *
93 * Consuming read - resets after first read. Call once per update cycle.
94 */
95 bool falling() {
96 bool e = falling_;
97 falling_ = false;
98 return e;
99 }
100
101 /** @brief Reset all state */
102 void reset() {
103 state_ = false;
104 rising_ = false;
105 falling_ = false;
106 }
107
108private:
109 uint16_t thresh_high_;
110 uint16_t thresh_low_;
111 bool state_;
112 bool rising_;
113 bool falling_;
114};
115
116} // namespace sbl::control::input
117
118#endif // SBL_CONTROL_INPUT_GATE_INPUT_HPP_
Gate input with Schmitt trigger hysteresis and edge detection.
bool gate() const
Current gate state (true = high)
GateInput(const GateConfig &config={})
Construct with the gate off.
bool falling()
True if gate just went low (falling edge)
void reset()
Reset all state.
void update(uint16_t raw)
Feed a new raw ADC sample.
bool rising()
True if gate just went high (rising edge)
Inputs: debounce, decoding, smoothing and scaling of raw samples.
Definition button.hpp:29
GateInput construction settings.