diff --git a/components/adc/example/main/adc_example.cpp b/components/adc/example/main/adc_example.cpp index 74a235d09..b0b2992d4 100644 --- a/components/adc/example/main/adc_example.cpp +++ b/components/adc/example/main/adc_example.cpp @@ -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 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, @@ -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 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, diff --git a/components/adc/include/adc_types.hpp b/components/adc/include/adc_types.hpp index 67afd59dc..a574369b4 100644 --- a/components/adc/include/adc_types.hpp +++ b/components/adc/include/adc_types.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include "format.hpp" namespace espp { @@ -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 : fmt::formatter { + template 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 : fmt::formatter { + template 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 : fmt::formatter { + template 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 { template constexpr auto parse(ParseContext &ctx) { return ctx.begin(); } template 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); } };