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
pot.hpp
Go to the documentation of this file.
1/**
2 * @file pot.hpp
3 * @brief Potentiometer component - smoothed, calibrated ADC with a [0, 1] output
4 *
5 * Pot is a source adapter: raw ADC counts in, normalized
6 * signal units out. Pipeline: raw -> EWMA (raw domain) -> two-point
7 * calibration -> [0, 1]. Deadband operates on that value.
8 *
9 * Acquisition is owned by the component: feed values with update(), or read
10 * straight from a DMA scan buffer with update_dma() (ISR-safe).
11 *
12 * Usage:
13 * @code
14 * sbl::control::input::Pot knob;
15 * knob.update_dma(adc_buf, 0);
16 * if (knob.changed()) { apply(knob.value()); } // deadbanded
17 * filter.process(in, out, frames, knob.smoothed()); // full resolution, as a scalar port
18 * @endcode
19 */
20
21#ifndef SBL_CONTROL_INPUT_POT_HPP_
22#define SBL_CONTROL_INPUT_POT_HPP_
23
24#include <cstdint>
28
29namespace sbl::control::input {
30
31/** @brief Pot construction settings */
32struct PotConfig {
33 Smoothing smoothing = Smoothing::Medium; /**< EWMA strength */
34 float deadband = 0.001f; /**< in [0, 1]; 0.001 is about 65 counts of a 16-bit span */
35};
36
37/**
38 * @brief Potentiometer with EWMA smoothing, calibration and deadband
39 *
40 * No hardware I/O.
41 *
42 * @context update() and update_dma() may run in an ISR; call them from
43 * exactly one context. changed() reads and clears its flag without masking
44 * interrupts, so a change that update() makes in an ISR during that call
45 * can be lost.
46 */
47class Pot {
48public:
49 /**
50 * @brief Construct with a smoothing strength and a deadband
51 * @param config Settings; the defaults are Smoothing::Medium and a 0.001 deadband
52 */
53 explicit Pot(const PotConfig& config = {})
54 : filter_(static_cast<uint8_t>(config.smoothing)), deadband_(config.deadband) {}
55
56 /**
57 * @brief Feed a new raw ADC sample through EWMA -> calibration -> deadband
58 * @param raw ADC sample, 0 to 65535
59 */
60 void update(uint16_t raw) {
61 raw_ = raw;
62 filter_.update(raw);
63 float s = smoothed();
64
65 float delta = s - stable_value_;
66 if (delta < 0.0f) {
67 delta = -delta;
68 }
69 if (delta >= deadband_) {
70 stable_value_ = s;
71 changed_ = true;
72 }
73 }
74
75 /**
76 * @brief Read one channel from a DMA scan buffer and update
77 *
78 * @param buffer DMA scan buffer
79 * @param channel_index Index of this pot's channel in the buffer
80 * @context ISR-safe, from the one context that calls update().
81 */
82 void update_dma(const uint16_t* buffer, uint8_t channel_index) {
83 update(static_cast<const volatile uint16_t*>(buffer)[channel_index]);
84 }
85
86 /** @brief Stable (deadbanded) value in [0, 1] */
87 float value() const { return stable_value_; }
88
89 /**
90 * @brief Calibrated, smoothed value in [0, 1] - no deadband
91 *
92 * Use for parameters driven unconditionally (no changed() gate):
93 * full resolution, no deadband quantization steps.
94 */
95 float smoothed() const {
96 return static_cast<float>(calibration_.normalize(filter_.value())) *
97 (1.0f / static_cast<float>(PotCalibration::OUTPUT_MAX));
98 }
99
100 /** @brief Last raw (unfiltered) ADC count - for calibration routines */
101 uint16_t raw() const { return raw_; }
102
103 /** @brief Apply two-point calibration */
104 void set_calibration(PotCalibration cal) { calibration_ = cal; }
105
106 /**
107 * @brief True if value moved past deadband since last check
108 *
109 * Resets the flag on read - call once per update cycle.
110 */
111 bool changed() {
112 bool c = changed_;
113 changed_ = false;
114 return c;
115 }
116
117 /** @brief Override the deadband threshold, in the [0, 1] output (default: 0.001) */
118 void set_deadband(float deadband) { deadband_ = deadband; }
119
120 /** @brief Reset all state (filter, stable value, changed flag) */
121 void reset() {
122 filter_.reset();
123 raw_ = 0;
124 stable_value_ = 0.0f;
125 changed_ = false;
126 }
127
128private:
129 Ewma filter_;
130 PotCalibration calibration_;
131 uint16_t raw_ = 0;
132 float deadband_;
133 float stable_value_ = 0.0f;
134 bool changed_ = false;
135};
136
137} // namespace sbl::control::input
138
139#endif // SBL_CONTROL_INPUT_POT_HPP_
Integer EWMA filter, alpha = 1 / 2^shift.
Definition ewma.hpp:38
uint16_t update(uint16_t raw)
Feed a new sample and return the smoothed value.
Definition ewma.hpp:56
void reset()
Reset filter state (next update will re-seed)
Definition ewma.hpp:79
uint16_t value() const
Return current smoothed value without updating.
Definition ewma.hpp:73
Potentiometer with EWMA smoothing, calibration and deadband.
Definition pot.hpp:47
bool changed()
True if value moved past deadband since last check.
Definition pot.hpp:111
void update_dma(const uint16_t *buffer, uint8_t channel_index)
Read one channel from a DMA scan buffer and update.
Definition pot.hpp:82
uint16_t raw() const
Last raw (unfiltered) ADC count - for calibration routines.
Definition pot.hpp:101
float value() const
Stable (deadbanded) value in [0, 1].
Definition pot.hpp:87
float smoothed() const
Calibrated, smoothed value in [0, 1] - no deadband.
Definition pot.hpp:95
void update(uint16_t raw)
Feed a new raw ADC sample through EWMA -> calibration -> deadband.
Definition pot.hpp:60
void reset()
Reset all state (filter, stable value, changed flag)
Definition pot.hpp:121
Pot(const PotConfig &config={})
Construct with a smoothing strength and a deadband.
Definition pot.hpp:53
void set_deadband(float deadband)
Override the deadband threshold, in the [0, 1] output (default: 0.001)
Definition pot.hpp:118
void set_calibration(PotCalibration cal)
Apply two-point calibration.
Definition pot.hpp:104
CV Input component - smoothed ADC reading with a signal output.
Exponentially Weighted Moving Average filter.
Inputs: debounce, decoding, smoothing and scaling of raw samples.
Definition button.hpp:29
Smoothing
Smoothing presets for the raw-domain EWMA filter.
Definition cv_input.hpp:38
Two-point ADC calibration.
uint32_t normalize(uint32_t raw) const
Pot construction settings.
Definition pot.hpp:32