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