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