idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
idfxx::adc::input Class Reference

A one-shot analog input on a single pin. More...

Classes

struct  config
 Configuration for a one-shot analog input. More...
 

Public Member Functions

 input (config config)
 Constructs a one-shot analog input.
 
 ~input ()
 
 input (const input &)=delete
 
inputoperator= (const input &)=delete
 
 input (input &&other) noexcept
 
inputoperator= (input &&other) noexcept
 
idfxx::gpio pin () const noexcept
 Returns the configured pin.
 
enum attenuation attenuation () const noexcept
 Returns the configured input-range attenuation.
 
divider_ratio divider () const noexcept
 Returns the configured external voltage-divider ratio.
 
bool calibrated () const noexcept
 Returns true when factory calibration is active for this input.
 
int read_raw ()
 Reads the raw ADC conversion value.
 
electro::millivolts read_voltage ()
 Reads the input voltage.
 
result< int > try_read_raw ()
 Reads the raw ADC conversion value.
 
result< electro::millivolts > try_read_voltage ()
 Reads the input voltage.
 

Static Public Member Functions

static result< inputmake (config config)
 Creates a one-shot analog input.
 

Detailed Description

A one-shot analog input on a single pin.

Claims the pin's ADC unit for the lifetime of the object. Move-only.

idfxx::adc::input battery({.pin = idfxx::gpio_3});
electro::millivolts v = battery.read_voltage(); // calibrated voltage at the pin
A one-shot analog input on a single pin.
Definition adc.hpp:93
electro::millivolts read_voltage()
Reads the input voltage.
Definition adc.hpp:181
constexpr gpio gpio_3
Definition gpio.hpp:889

Definition at line 93 of file adc.hpp.

Constructor & Destructor Documentation

◆ input() [1/3]

idfxx::adc::input::input ( config  config)
explicit

Constructs a one-shot analog input.

Resolves the ADC unit/channel from the pin, claims the unit, and sets up factory calibration when the chip provides it.

Parameters
configInput configuration.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure (e.g. the pin is not ADC-capable or the unit is already claimed).

◆ ~input()

idfxx::adc::input::~input ( )

◆ input() [2/3]

idfxx::adc::input::input ( const input )
delete

◆ input() [3/3]

idfxx::adc::input::input ( input &&  other)
noexcept

Member Function Documentation

◆ attenuation()

enum attenuation idfxx::adc::input::attenuation ( ) const
noexcept

Returns the configured input-range attenuation.

◆ calibrated()

bool idfxx::adc::input::calibrated ( ) const
noexcept

Returns true when factory calibration is active for this input.

◆ divider()

divider_ratio idfxx::adc::input::divider ( ) const
noexcept

Returns the configured external voltage-divider ratio.

◆ make()

static result< input > idfxx::adc::input::make ( config  config)
static

Creates a one-shot analog input.

Resolves the ADC unit/channel from the pin, claims the unit, and sets up factory calibration when the chip provides it.

Parameters
configInput configuration.
Returns
The input, or an error.
Return values
idfxx::errc::invalid_argThe pin is not connected or not ADC-capable, or the divider ratio is not positive.

◆ operator=() [1/2]

input & idfxx::adc::input::operator= ( const input )
delete

◆ operator=() [2/2]

input & idfxx::adc::input::operator= ( input &&  other)
noexcept

◆ pin()

idfxx::gpio idfxx::adc::input::pin ( ) const
noexcept

Returns the configured pin.

◆ read_raw()

int idfxx::adc::input::read_raw ( )
inline

Reads the raw ADC conversion value.

Returns
The raw value (range depends on the chip's bit width).
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure.

Definition at line 167 of file adc.hpp.

References try_read_raw(), and idfxx::unwrap().

◆ read_voltage()

electro::millivolts idfxx::adc::input::read_voltage ( )
inline

Reads the input voltage.

Returns the source voltage: the calibrated voltage at the pin, scaled by the configured config::divider (1:1 by default).

Returns
The calibrated, divider-scaled voltage.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure, including idfxx::errc::not_supported when the chip has no usable calibration data.

Definition at line 181 of file adc.hpp.

References try_read_voltage(), and idfxx::unwrap().

◆ try_read_raw()

result< int > idfxx::adc::input::try_read_raw ( )

Reads the raw ADC conversion value.

Returns
The raw value, or an error.

Referenced by read_raw().

◆ try_read_voltage()

result< electro::millivolts > idfxx::adc::input::try_read_voltage ( )

Reads the input voltage.

Returns the source voltage: the calibrated voltage at the pin, scaled by the configured config::divider (1:1 by default).

Returns
The calibrated, divider-scaled voltage, or an error.
Return values
idfxx::errc::not_supportedThe chip has no usable calibration data (check calibrated).

Referenced by read_voltage().


The documentation for this class was generated from the following file: