adc_convert.h
Generated from the header
This page is rodeo-sdk/common/adc_convert.h — its comments and declarations.
To change it, change the header.
Split out of RodeoAdc’s accessors so the value pipeline (clamp, volts, norm) can be unit-tested on the HOST with no libDaisy/SPI in the build. RodeoAdc’s GetCode/GetVolts/GetNorm delegate here, so these functions ARE the production path — one source of truth, no drift between the test and the firmware.
kAdcCodeMax
static constexpr int32_t kAdcCodeMax = 32767;ADS114S08 unipolar single-ended full-scale code. The chip returns a SIGNED 16-bit result; a pot at fully-off can read a few codes negative (offset + noise + leakage), so 32767 (not 65535) is the usable top of a 0..Vref sweep.
ClampAdcCode
inline uint16_t ClampAdcCode(int16_t code);Clamp a signed conversion code to the unipolar [0, 32767] range. THE GLITCH GUARD: a negative tail must clamp to 0, never wrap to ~full-scale when cast to unsigned — otherwise a fully-off knob reads as fully-on. See RODEO_ADC_LIB_DESIGN.md.
AdcCodeToVolts
inline float AdcCodeToVolts(uint16_t code, float vref);Clamped code -> volts against a ratiometric reference (Rodeo: 3.3 V).
AdcCodeToNorm
inline float AdcCodeToNorm(uint16_t code);Clamped code -> normalized 0..1 (high end clamped for the rare over-range).
kPotSeriesOhms
constexpr float kPotSeriesOhms = 1000.0f;R1 on the satellite
kPotNominalOhms
constexpr float kPotNominalOhms = 10000.0f;control-map contract: <= 10 kOhm
kPotTolerance
constexpr float kPotTolerance = 0.20f;Alps RK09K, +/-20 %
PotTopFraction
constexpr float PotTopFraction(float rpot);Fraction of Vref reached at the top of travel for a pot of rpot ohms.
PotTopCode
constexpr uint16_t PotTopCode(float rpot);kPotSeedHiCode
constexpr uint16_t kPotSeedHiCode = PotTopCode(kPotNominalOhms * (1.0f - kPotTolerance));Seed the learned span PESSIMISTICALLY — narrower than any real pot — because the learner only ever widens. Seeding at the worst-case (-20 %) pot means a knob reaches a full 1.0 from the very first boot on any unit; sweeping it then widens the span to that pot’s true endpoint and recovers the lost resolution. Seeding at nominal instead would leave an under-spec pot permanently unable to reach 1.0, which is the failure this whole mechanism exists to prevent.
kPotSeedLoCode
constexpr uint16_t kPotSeedLoCode = static_cast<uint16_t>(kAdcCodeMax / 100);kPotMaxPlausibleHiCode
constexpr uint16_t kPotMaxPlausibleHiCode = static_cast<uint16_t>(static_cast<float>(kAdcCodeMax) * 0.95f);A satellite pot physically cannot read above this: even a +20 % pot only reaches 12/13 of full scale through R1. Anything higher is a glitch, not a knob, so the learned span refuses to follow it — otherwise one spurious full-scale sample would permanently undo the calibration.
kPotEdgeMarginCodes
constexpr uint16_t kPotEdgeMarginCodes = static_cast<uint16_t>(kAdcCodeMax / 200);Pulled in from each end so the last sliver of travel saturates: with an exact span you would reach 0.999 at the stop and never quite 1.0, because the code dithers by a few LSB.
kPotMinSpanCodes
constexpr uint16_t kPotMinSpanCodes = static_cast<uint16_t>(kAdcCodeMax / 10);Below this the span is not a working pot (absent, shorted, never swept), so normalization falls back to the raw full-scale conversion.
PotSpan
struct PotSpanOne channel’s learned travel. Widens only — see kPotSeedHiCode.
Members
uint16_t lo = kPotSeedLoCodeuint16_t hi = kPotSeedHiCode
PotSpan::Observe
bool Observe(uint16_t code);Fold a fresh (already clamped) code into the span. Returns true if the span grew, which callers can use to log a calibration change.
AdcCodeToNormSpan
inline float AdcCodeToNormSpan(uint16_t code, const PotSpan &span);Clamped code -> normalized 0..1 across a channel’s learned travel.