Skip to content

Commit

Permalink
feat(adc): add print helpers for esp-idf adc types (#254)
Browse files Browse the repository at this point in the history
* feat(adc): add print helpers for esp-idf adc types
* Add missing include to adc_types.hpp
* Add format helpers for adc_unit_t, adc_channel_t, and adc_atten_t
* Update format helper for espp::AdcConfig to use other helpers instead of casting to int

* update deprecated atten value
  • Loading branch information
finger563 authored Jun 5, 2024
1 parent 3dee929 commit cb443d7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
8 changes: 4 additions & 4 deletions components/adc/example/main/adc_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ extern "C" void app_main(void) {
logger.info("Reading oneshot adc for {} seconds", num_seconds_to_run);
//! [oneshot adc example]
std::vector<espp::AdcConfig> channels{
{.unit = ADC_UNIT_1, .channel = ADC_CHANNEL_6, .attenuation = ADC_ATTEN_DB_11},
{.unit = ADC_UNIT_1, .channel = ADC_CHANNEL_7, .attenuation = ADC_ATTEN_DB_11}};
{.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,
Expand Down Expand Up @@ -62,8 +62,8 @@ extern "C" void app_main(void) {
logger.info("Reading continuous adc for {} seconds", num_seconds_to_run);
//! [continuous adc example]
std::vector<espp::AdcConfig> channels{
{.unit = ADC_UNIT_1, .channel = ADC_CHANNEL_6, .attenuation = ADC_ATTEN_DB_11},
{.unit = ADC_UNIT_1, .channel = ADC_CHANNEL_7, .attenuation = ADC_ATTEN_DB_11}};
{.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}};
// this initailizes the DMA and filter task for the continuous adc
espp::ContinuousAdc adc(
{.sample_rate_hz = 20 * 1000,
Expand Down
37 changes: 35 additions & 2 deletions components/adc/include/adc_types.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <driver/adc.h>

#include "format.hpp"

namespace espp {
Expand All @@ -20,12 +22,43 @@ static bool operator!=(const AdcConfig &lhs, const AdcConfig &rhs) {
static bool operator==(const AdcConfig &lhs, const AdcConfig &rhs) { return !(lhs != rhs); }
} // namespace espp

// for libfmt printing of adc_unit_t
template <> struct fmt::formatter<adc_unit_t> : fmt::formatter<std::string> {
template <typename FormatContext> auto format(adc_unit_t t, FormatContext &ctx) {
return fmt::format_to(ctx.out(), "ADC_UNIT_{}", (int)t + 1);
}
};

// for libfmt printing of adc_channel_t
template <> struct fmt::formatter<adc_channel_t> : fmt::formatter<std::string> {
template <typename FormatContext> auto format(adc_channel_t t, FormatContext &ctx) {
return fmt::format_to(ctx.out(), "ADC_CHANNEL_{}", (int)t);
}
};

// for libfmt printing of adc_atten_t
template <> struct fmt::formatter<adc_atten_t> : fmt::formatter<std::string> {
template <typename FormatContext> auto format(adc_atten_t t, FormatContext &ctx) {
switch (t) {
case ADC_ATTEN_DB_0:
return fmt::format_to(ctx.out(), "ADC_ATTEN_DB_0");
case ADC_ATTEN_DB_2_5:
return fmt::format_to(ctx.out(), "ADC_ATTEN_DB_2_5");
case ADC_ATTEN_DB_6:
return fmt::format_to(ctx.out(), "ADC_ATTEN_DB_6");
case ADC_ATTEN_DB_12:
return fmt::format_to(ctx.out(), "ADC_ATTEN_DB_12");
}
return fmt::format_to(ctx.out(), "ADC_ATTEN_UNKNOWN");
}
};

// for easy serialization of AdcConfig with libfmt
template <> struct fmt::formatter<espp::AdcConfig> {
template <typename ParseContext> constexpr auto parse(ParseContext &ctx) { return ctx.begin(); }

template <typename FormatContext> auto format(const espp::AdcConfig &c, FormatContext &ctx) {
return fmt::format_to(ctx.out(), "AdcConfig(unit={}, channel={}, attenuation={})", (int)c.unit,
(int)c.channel, (int)c.attenuation);
return fmt::format_to(ctx.out(), "AdcConfig(unit={}, channel={}, attenuation={})", c.unit,
c.channel, c.attenuation);
}
};

0 comments on commit cb443d7

Please sign in to comment.