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_calibration.hpp
Go to the documentation of this file.
1
/**
2
* @file pot_calibration.hpp
3
* @brief Two-point ADC calibration
4
*
5
* Maps raw ADC readings to a normalized integer range using measured endpoint
6
* values. Handles inverted ADC layouts (high raw = CCW) and clamps output.
7
* Uses uint32_t to support any ADC resolution (12-bit, 16-bit, 24-bit).
8
*
9
* Usage:
10
* @code
11
* auto cal = PotCalibration::from_two_point(ccw_reading, cw_reading);
12
* uint32_t t = cal.normalize(raw_adc); // [0, 65535]
13
*
14
* // Convert to float in [0, 1]
15
* float value = t * (1.0f / 65535.0f);
16
* @endcode
17
*/
18
19
#ifndef SBL_CONTROL_INPUT_POT_CALIBRATION_HPP_
20
#define SBL_CONTROL_INPUT_POT_CALIBRATION_HPP_
21
22
#include <cstdint>
23
24
namespace
sbl::control::input
{
25
26
struct
PotCalibration
{
27
static
constexpr
uint32_t OUTPUT_MAX = 65535;
28
29
uint32_t
raw_min
= 0;
/**< ADC reading at logical minimum (CCW after inversion) */
30
uint32_t
raw_max
= 65535;
/**< ADC reading at logical maximum (CW after inversion) */
31
bool
inverted
=
false
;
/**< true if high ADC value = CCW (knob turned left) */
32
33
/** Normalize raw ADC reading to [0, OUTPUT_MAX], clamped. */
34
uint32_t
normalize
(uint32_t raw)
const
{
35
if
(
raw_max
<=
raw_min
) {
36
return
0;
37
}
38
uint32_t clamped = raw;
39
if
(clamped <
raw_min
) {
40
clamped =
raw_min
;
41
}
42
if
(clamped >
raw_max
) {
43
clamped =
raw_max
;
44
}
45
uint32_t val = clamped -
raw_min
;
46
uint32_t range =
raw_max
-
raw_min
;
47
uint32_t result =
static_cast<
uint32_t
>
((
static_cast<
uint64_t
>
(val) * OUTPUT_MAX) / range);
48
return
inverted
? (OUTPUT_MAX - result) : result;
49
}
50
51
/**
52
* Build calibration from measured CCW and CW ADC readings.
53
* Automatically detects inversion (if CCW reading > CW reading).
54
*/
55
static
PotCalibration
from_two_point
(uint32_t ccw_reading, uint32_t cw_reading) {
56
bool
inv = ccw_reading > cw_reading;
57
return
{inv ? cw_reading : ccw_reading, inv ? ccw_reading : cw_reading, inv};
58
}
59
60
/** Preset for an inverted pot spanning raw counts 0 to 33000. */
61
static
constexpr
PotCalibration
default_patch_init
() {
return
{0, 33000,
true
}; }
62
63
/** Default full-range (no calibration needed). */
64
static
constexpr
PotCalibration
default_full_range
() {
return
{0, 65535,
false
}; }
65
};
66
67
}
// namespace sbl::control::input
68
69
#endif
// SBL_CONTROL_INPUT_POT_CALIBRATION_HPP_
sbl::control::input
Inputs: debounce, decoding, smoothing and scaling of raw samples.
Definition
button.hpp:29
sbl::control::input::PotCalibration
Definition
pot_calibration.hpp:26
sbl::control::input::PotCalibration::raw_min
uint32_t raw_min
Definition
pot_calibration.hpp:29
sbl::control::input::PotCalibration::raw_max
uint32_t raw_max
Definition
pot_calibration.hpp:30
sbl::control::input::PotCalibration::default_patch_init
static constexpr PotCalibration default_patch_init()
Definition
pot_calibration.hpp:61
sbl::control::input::PotCalibration::default_full_range
static constexpr PotCalibration default_full_range()
Definition
pot_calibration.hpp:64
sbl::control::input::PotCalibration::from_two_point
static PotCalibration from_two_point(uint32_t ccw_reading, uint32_t cw_reading)
Definition
pot_calibration.hpp:55
sbl::control::input::PotCalibration::inverted
bool inverted
Definition
pot_calibration.hpp:31
sbl::control::input::PotCalibration::normalize
uint32_t normalize(uint32_t raw) const
Definition
pot_calibration.hpp:34
src
sbl
control
input
pot_calibration.hpp
Generated by
1.9.8