Oneshot ADC

The OneshotAdc allows the user a simple, low-resource way to sporadically (and with moderate frequency needs) measure an analog voltage for multiple channels on a single ADC UNIT. It does not start or manage any tasks and does not perform any filtering on the data. Each time the user calls read_raw(adc_channel_t) or read_mv(adc_channel_t), it block and trigger an analog read for the associated channel (if it was configured to do so).

API Reference

Header File

Classes

class OneshotAdc : public espp::BaseComponent

OneshotAdc provides a wrapper around the ESP-IDF oneshot adc subsystem, enabling simple, direct (blocking / slow) measurements of analog values. The read_mv() function will always take a new measurement (therefore it is blocking).

Oneshot ADC Example

    std::vector<espp::AdcConfig> channels{
        {.unit = ADC_UNIT_1, .channel = ADC_CHANNEL_6, .attenuation = ADC_ATTEN_DB_12},
        {.unit = ADC_UNIT_1, .channel = ADC_CHANNEL_7, .attenuation = ADC_ATTEN_DB_12}};
    espp::OneshotAdc adc({
        .unit = ADC_UNIT_1,
        .channels = channels,
    });
    auto task_fn = [&adc, &channels](std::mutex &m, std::condition_variable &cv) {
      static bool use_individual_functions = false;
      if (use_individual_functions) {
        // this iteration, we'll use the read_mv function for each channel
        for (auto &conf : channels) {
          auto maybe_mv = adc.read_mv(conf);
          if (maybe_mv.has_value()) {
            fmt::print("{}: {} mV\n", conf, maybe_mv.value());
          } else {
            fmt::print("{}: no value!\n", conf);
          }
        }
      } else {
        // this iteration, we'll use the read_all_mv function to read all
        // configured channels
        auto voltages = adc.read_all_mv();
        for (const auto &mv : voltages) {
          fmt::print("{} mV\n", mv);
        }
      }
      use_individual_functions = !use_individual_functions;
      // NOTE: sleeping in this way allows the sleep to exit early when the
      // task is being stopped / destroyed
      {
        std::unique_lock<std::mutex> lk(m);
        cv.wait_for(lk, 500ms);
      }
      // don't want to stop the task
      return false;
    };
    auto task = espp::Task({.callback = task_fn,
                            .task_config = {.name = "Oneshot ADC"},
                            .log_level = espp::Logger::Verbosity::INFO});
    task.start();

Public Functions

inline explicit OneshotAdc(const Config &config)

Initialize the oneshot adc reader.

Parameters

configConfig used to initialize the reader.

inline ~OneshotAdc()

Delete and destroy the adc reader.

inline std::vector<int> read_all_mv()

Take a new ADC reading for all configured channels and convert it to voltage (mV) if the unit was properly calibrated. If it was not properly calibrated, then it will return the same value as read_raw().

Returns

std::vector<float> Voltage in mV for all configured channels (if they were configured).

inline std::optional<int> read_raw(const AdcConfig &config)

Take a new ADC reading for the provided config.

Parameters

config – The channel configuration to take a reading from.

Returns

std::optional<float> raw value for the provided channel config (if it was configured).

inline std::optional<int> read_mv(const AdcConfig &config)

Take a new ADC reading for the provided config and convert it to voltage (mV) if the unit was properly calibrated. If it was not properly calibrated, then it will return the same value as read_raw().

Parameters

config – The channel configuration to take a reading from.

Returns

std::optional<float> Voltage in mV for the provided channel config (if it was configured).

inline const std::string &get_name() const

Get the name of the component

Note

This is the tag of the logger

Returns

A const reference to the name of the component

inline void set_log_tag(const std::string_view &tag)

Set the tag for the logger

Parameters

tag – The tag to use for the logger

inline espp::Logger::Verbosity get_log_level() const

Get the log level for the logger

Returns

The verbosity level of the logger

inline void set_log_level(espp::Logger::Verbosity level)

Set the log level for the logger

Parameters

level – The verbosity level to use for the logger

inline void set_log_verbosity(espp::Logger::Verbosity level)

Set the log verbosity for the logger

See also

set_log_level

Note

This is a convenience method that calls set_log_level

Parameters

level – The verbosity level to use for the logger

inline espp::Logger::Verbosity get_log_verbosity() const

Get the log verbosity for the logger

See also

get_log_level

Note

This is a convenience method that calls get_log_level

Returns

The verbosity level of the logger

inline void set_log_rate_limit(std::chrono::duration<float> rate_limit)

Set the rate limit for the logger

Note

Only calls to the logger that have _rate_limit suffix will be rate limited

Parameters

rate_limit – The rate limit to use for the logger

struct Config

Configure the unit for which to read adc values from the provided channels.

Note

The unit must be the same as what is provided in each element of channels.

Public Members

adc_unit_t unit

Unit this oneshot reader will be associated with.

Note

all channels must use this unit.

std::vector<AdcConfig> channels

Channels to read from, with associated attenuations.

espp::Logger::Verbosity log_level = {espp::Logger::Verbosity::WARN}

Verbosity for the adc logger.