|
idfxx 1.0.0
Modern C++23 components for ESP-IDF
|
A continuous sampler over one or more analog pins. More...
Classes | |
| struct | config |
| Configuration for a continuous sampler. More... | |
| struct | sample |
| A single conversion result. More... | |
Public Member Functions | |
| sampler (config config) | |
| Constructs a continuous sampler. | |
| ~sampler () | |
| Destroys the sampler, stopping conversion and releasing ADC1. | |
| sampler (const sampler &)=delete | |
| sampler & | operator= (const sampler &)=delete |
| sampler (sampler &&other) noexcept | |
| sampler & | operator= (sampler &&other) noexcept |
| std::span< const idfxx::gpio > | pins () const noexcept |
| Returns the configured pins in conversion order. | |
| enum attenuation | attenuation () const noexcept |
| Returns the configured input-range attenuation (uniform across all pins). | |
| freq::hertz | sample_rate () const noexcept |
| Returns the configured total sample rate across all pins. | |
| bool | running () const noexcept |
| Returns true while the sampler is started. | |
| bool | calibrated () const noexcept |
| Returns true when factory calibration is active for every configured pin. | |
| size_t | overruns () const noexcept |
| Returns the number of dropped-data events since the last start. | |
| void | start () |
| Starts continuous conversion. | |
| void | stop () |
| Stops continuous conversion. | |
| result< void > | try_start () |
| Starts continuous conversion. | |
| result< void > | try_stop () |
| Stops continuous conversion. | |
| size_t | read (std::span< sample > out) |
| Reads samples, blocking until at least one is available. | |
| template<typename Rep , typename Period > | |
| size_t | read (std::span< sample > out, const std::chrono::duration< Rep, Period > &timeout) |
| Reads samples, blocking until at least one is available or the timeout expires. | |
| size_t | read (std::span< electro::millivolts > out) |
| Reads samples as calibrated voltages, blocking until at least one is available. | |
| template<typename Rep , typename Period > | |
| size_t | read (std::span< electro::millivolts > out, const std::chrono::duration< Rep, Period > &timeout) |
| Reads samples as calibrated voltages, blocking until at least one is available or the timeout expires. | |
| result< size_t > | try_read (std::span< sample > out) |
| Reads samples, blocking until at least one is available. | |
| template<typename Rep , typename Period > | |
| result< size_t > | try_read (std::span< sample > out, const std::chrono::duration< Rep, Period > &timeout) |
| Reads samples, blocking until at least one is available or the timeout expires. | |
| result< size_t > | try_read (std::span< electro::millivolts > out) |
| Reads samples as calibrated voltages, blocking until at least one is available. | |
| template<typename Rep , typename Period > | |
| result< size_t > | try_read (std::span< electro::millivolts > out, const std::chrono::duration< Rep, Period > &timeout) |
| Reads samples as calibrated voltages, blocking until at least one is available or the timeout expires. | |
| electro::millivolts | to_voltage (const sample &s) const |
| Converts a sample to a voltage using factory calibration. | |
| electro::millivolts | to_voltage (int raw) const |
| Converts a raw conversion value from a single-pin sampler to a voltage. | |
| void | to_voltage (std::span< const sample > in, std::span< electro::millivolts > out) const |
| Converts a batch of samples to voltages using factory calibration. | |
| result< electro::millivolts > | try_to_voltage (const sample &s) const |
| Converts a sample to a voltage using factory calibration. | |
| result< electro::millivolts > | try_to_voltage (int raw) const |
| Converts a raw conversion value from a single-pin sampler to a voltage. | |
| result< void > | try_to_voltage (std::span< const sample > in, std::span< electro::millivolts > out) const |
| Converts a batch of samples to voltages using factory calibration. | |
Static Public Member Functions | |
| static result< sampler > | make (config config) |
| Creates a continuous sampler. | |
A continuous sampler over one or more analog pins.
Converts the configured pins round-robin at a fixed total rate and buffers the results internally; readers drain them with read / try_read. Claims the pins' ADC unit (ADC1) for the lifetime of the object. Move-only.
|
explicit |
Constructs a continuous sampler.
Resolves each pin's ADC channel, claims ADC1, configures the digital controller for round-robin conversion, and sets up factory calibration when the chip provides it. Sampling does not begin until start.
| config | Sampler configuration. |
| std::system_error | on failure (e.g. a pin is not ADC1-capable, the pin list is empty or has duplicates, or the sizes/rate are invalid). |
| idfxx::adc::sampler::~sampler | ( | ) |
Destroys the sampler, stopping conversion and releasing ADC1.
|
delete |
|
noexcept |
|
noexcept |
Returns the configured input-range attenuation (uniform across all pins).
|
noexcept |
Returns true when factory calibration is active for every configured pin.
Creates a continuous sampler.
Resolves each pin's ADC channel, claims ADC1, configures the digital controller for round-robin conversion, and sets up factory calibration when the chip provides it. Sampling does not begin until try_start.
| config | Sampler configuration. |
| idfxx::errc::invalid_arg | The pin list is empty, exceeds the chip's conversion-pattern length, contains duplicates or unconnected pins, or a pin is not ADC1-capable; or frame_samples is zero, buffer_samples is smaller than frame_samples, or sample_rate is not positive. |
|
noexcept |
Returns the number of dropped-data events since the last start.
Increments each time the internal pool overflows because samples were not read fast enough; the samples produced while the pool was full are lost. Reset to zero by start / try_start.
|
noexcept |
Returns the configured pins in conversion order.
|
inline |
Reads samples as calibrated voltages, blocking until at least one is available.
Convenience for the single-pin case: reads and converts in one call, writing calibrated voltages in conversion order. Returns as soon as at least one sample is available; it does not wait to fill the entire span.
For a multi-pin sampler the source pin of each voltage cannot be recovered from out; read with read(std::span<sample>) and convert with to_voltage to keep the association.
| out | Destination for the voltages (must not be empty). |
out (always at least 1). | std::system_error | with idfxx::errc::invalid_state if the sampler is not running, idfxx::errc::invalid_arg if out is empty, or idfxx::errc::not_supported if a sampled pin has no usable calibration data (check calibrated). |
Definition at line 445 of file adc.hpp.
References try_read(), and idfxx::unwrap().
|
inline |
Reads samples as calibrated voltages, blocking until at least one is available or the timeout expires.
Convenience for the single-pin case: reads and converts in one call, writing calibrated voltages in conversion order. Returns as soon as at least one sample is available; it does not wait to fill the entire span.
| Rep | The representation type of the duration. |
| Period | The period type of the duration. |
| out | Destination for the voltages (must not be empty). |
| timeout | Maximum time to wait for a sample. |
out (always at least 1). | std::system_error | with idfxx::errc::timeout if no sample arrived within the timeout, idfxx::errc::invalid_state if the sampler is not running, or idfxx::errc::not_supported if a sampled pin has no usable calibration data. |
Definition at line 466 of file adc.hpp.
References idfxx::timeout, try_read(), and idfxx::unwrap().
|
inline |
Reads samples, blocking until at least one is available.
Fills out with parsed samples in conversion order, tagging each with its source pin. Returns as soon as at least one sample is available; it does not wait to fill the entire span.
| out | Destination for the samples (must not be empty). |
out (always at least 1). | std::system_error | with idfxx::errc::invalid_state if the sampler is not running, or idfxx::errc::invalid_arg if out is empty. |
Definition at line 404 of file adc.hpp.
References try_read(), and idfxx::unwrap().
|
inline |
Reads samples, blocking until at least one is available or the timeout expires.
Fills out with parsed samples in conversion order, tagging each with its source pin. Returns as soon as at least one sample is available; it does not wait to fill the entire span.
| Rep | The representation type of the duration. |
| Period | The period type of the duration. |
| out | Destination for the samples (must not be empty). |
| timeout | Maximum time to wait for a sample. |
out (always at least 1). | std::system_error | with idfxx::errc::timeout if no sample arrived within the timeout, or idfxx::errc::invalid_state if the sampler is not running. |
Definition at line 424 of file adc.hpp.
References idfxx::timeout, try_read(), and idfxx::unwrap().
|
noexcept |
Returns true while the sampler is started.
|
noexcept |
Returns the configured total sample rate across all pins.
|
inline |
Starts continuous conversion.
Resets overruns to zero. Samples accumulate in the internal pool from this point and are drained with read.
| std::system_error | with idfxx::errc::invalid_state if already running, or on driver failure. |
Definition at line 355 of file adc.hpp.
References try_start(), and idfxx::unwrap().
|
inline |
Stops continuous conversion.
Idempotent: stopping a sampler that is not running has no effect.
| std::system_error | on driver failure. |
Definition at line 365 of file adc.hpp.
References try_stop(), and idfxx::unwrap().
|
inline |
Converts a sample to a voltage using factory calibration.
| s | A sample previously produced by this sampler. |
| std::system_error | with idfxx::errc::invalid_arg if the sample's pin is not one of this sampler's configured pins, or idfxx::errc::not_supported if the chip has no usable calibration data (check calibrated). |
Definition at line 565 of file adc.hpp.
References try_to_voltage(), and idfxx::unwrap().
|
inline |
Converts a raw conversion value from a single-pin sampler to a voltage.
Convenience for samplers configured with exactly one pin, where the pin association is unambiguous — converts values computed from raw samples (a minimum, maximum, or mean) without fabricating a sample.
| raw | Raw conversion value (as in sample::raw). |
| std::system_error | with idfxx::errc::invalid_state if the sampler is configured with more than one pin, or idfxx::errc::not_supported if the chip has no usable calibration data (check calibrated). |
Definition at line 582 of file adc.hpp.
References try_to_voltage(), and idfxx::unwrap().
|
inline |
Converts a batch of samples to voltages using factory calibration.
Writes out[i] as the calibrated voltage of in[i], preserving order and pin association. Resolves each pin's calibration once per run of like-pinned samples, so converting a whole read is cheaper than calling to_voltage(const sample&) per element.
| in | Samples previously produced by this sampler. |
| out | Destination for the voltages; must be at least as large as in. |
| std::system_error | with idfxx::errc::invalid_arg if out is smaller than in or a sample's pin is not one of this sampler's configured pins, or idfxx::errc::not_supported if a pin has no usable calibration data (check calibrated). |
Definition at line 599 of file adc.hpp.
References try_to_voltage(), and idfxx::unwrap().
|
inline |
Reads samples as calibrated voltages, blocking until at least one is available.
Convenience for the single-pin case: reads and converts in one call, writing calibrated voltages in conversion order. Returns as soon as at least one sample is available; it does not wait to fill the entire span.
For a multi-pin sampler the source pin of each voltage cannot be recovered from out; read with try_read(std::span<sample>) and convert with try_to_voltage to keep the association.
| out | Destination for the voltages (must not be empty). |
out (always at least 1), or an error. | idfxx::errc::invalid_state | The sampler is not running. |
| idfxx::errc::invalid_arg | out is empty. |
| idfxx::errc::not_supported | A sampled pin has no usable calibration data (check calibrated). |
|
inline |
Reads samples as calibrated voltages, blocking until at least one is available or the timeout expires.
Convenience for the single-pin case: reads and converts in one call, writing calibrated voltages in conversion order. Returns as soon as at least one sample is available; it does not wait to fill the entire span.
| Rep | The representation type of the duration. |
| Period | The period type of the duration. |
| out | Destination for the voltages (must not be empty). |
| timeout | Maximum time to wait for a sample. |
out (always at least 1), or an error. | idfxx::errc::timeout | No sample arrived within the timeout. |
| idfxx::errc::invalid_state | The sampler is not running. |
| idfxx::errc::not_supported | A sampled pin has no usable calibration data. |
Definition at line 549 of file adc.hpp.
References idfxx::timeout.
Reads samples, blocking until at least one is available.
Fills out with parsed samples in conversion order, tagging each with its source pin. Returns as soon as at least one sample is available; it does not wait to fill the entire span.
| out | Destination for the samples (must not be empty). |
out (always at least 1), or an error. | idfxx::errc::invalid_state | The sampler is not running. |
| idfxx::errc::invalid_arg | out is empty. |
|
inline |
Reads samples, blocking until at least one is available or the timeout expires.
Fills out with parsed samples in conversion order, tagging each with its source pin. Returns as soon as at least one sample is available; it does not wait to fill the entire span.
| Rep | The representation type of the duration. |
| Period | The period type of the duration. |
| out | Destination for the samples (must not be empty). |
| timeout | Maximum time to wait for a sample. |
out (always at least 1), or an error. | idfxx::errc::timeout | No sample arrived within the timeout. |
| idfxx::errc::invalid_state | The sampler is not running. |
| idfxx::errc::invalid_arg | out is empty. |
Definition at line 504 of file adc.hpp.
References idfxx::timeout.
| result< void > idfxx::adc::sampler::try_start | ( | ) |
Starts continuous conversion.
Resets overruns to zero. Samples accumulate in the internal pool from this point and are drained with try_read.
| idfxx::errc::invalid_state | The sampler is already running. |
Referenced by start().
| result< void > idfxx::adc::sampler::try_stop | ( | ) |
Stops continuous conversion.
Idempotent: stopping a sampler that is not running succeeds with no effect.
Referenced by stop().
Converts a sample to a voltage using factory calibration.
| s | A sample previously produced by this sampler. |
| idfxx::errc::invalid_arg | The sample's pin is not one of this sampler's configured pins. |
| idfxx::errc::not_supported | The chip has no usable calibration data (check calibrated). |
Referenced by to_voltage(), to_voltage(), and to_voltage().
| result< electro::millivolts > idfxx::adc::sampler::try_to_voltage | ( | int | raw | ) | const |
Converts a raw conversion value from a single-pin sampler to a voltage.
Convenience for samplers configured with exactly one pin, where the pin association is unambiguous.
| raw | Raw conversion value (as in sample::raw). |
| idfxx::errc::invalid_state | The sampler is configured with more than one pin. |
| idfxx::errc::not_supported | The chip has no usable calibration data (check calibrated). |
| result< void > idfxx::adc::sampler::try_to_voltage | ( | std::span< const sample > | in, |
| std::span< electro::millivolts > | out | ||
| ) | const |
Converts a batch of samples to voltages using factory calibration.
Writes out[i] as the calibrated voltage of in[i], preserving order and pin association.
| in | Samples previously produced by this sampler. |
| out | Destination for the voltages; must be at least as large as in. |
| idfxx::errc::invalid_arg | out is smaller than in, or a sample's pin is not one of this sampler's configured pins. |
| idfxx::errc::not_supported | A pin has no usable calibration data (check calibrated). |