Sygaldry
Loading...
Searching...
No Matches
sygse-adc: Oneshot ADC Driver

Table of Contents

Copyright 2023 Travis J. West, https://traviswest.ca, Input Devices and Music Interaction Laboratory (IDMIL), Centre for Interdisciplinary Research in Music Media and Technology (CIRMMT), McGill University, Montréal, Canada, and Univ. Lille, Inria, CNRS, Centrale Lille, UMR 9189 CRIStAL, F-59000 Lille, France

SPDX-License-Identifier: MIT

The ADC component provides a very basic wrapper around the one-shot ADC read API provided by the ESP-IDF; it is based on the oneshot_read_main.c example provided with the IDF documentation.

Implementation

Summary

// @#'sygse-adc.hpp'
#pragma once
/*
Copyright 2023 Travis J. West, https://traviswest.ca, Input Devices and Music
Interaction Laboratory (IDMIL), Centre for Interdisciplinary Research in Music
Media and Technology (CIRMMT), McGill University, Montréal, Canada, and Univ.
Lille, Inria, CNRS, Centrale Lille, UMR 9189 CRIStAL, F-59000 Lille, France
SPDX-License-Identifier: MIT
*/
#include "sygah-endpoints.hpp"
#include "sygah-metadata.hpp"
#include "syghe-pins.hpp"
namespace sygaldry { namespace sygse {
struct OneshotAdcOutputs {
slider<"raw", "raw binary representation of the analog voltage measured by the ADC"
, int, 0, 4096, 0 // esp32 technical reference manual V5 section 29.3 says 12 bit max resolution
> raw;
};
namespace detail {
struct OneshotAdcImpl
{
using outputs_t = OneshotAdcOutputs;
void * pimpl;
void init(int gpio_num, outputs_t& outputs);
void main(outputs_t& outputs);
};
}
template<syghe::AdcChannel gpio_num>
struct OneshotAdc
: name_<"ESP32 Oneshot ADC">
, author_<"Travis J. West">
, copyright_<"Copyright 2023 Sygaldry Contributors">
, license_<"SPDX-License-Identifier: MIT">
{
using outputs_t = OneshotAdcOutputs;
outputs_t outputs;
detail::OneshotAdcImpl pimpl;
void init() {pimpl.init(gpio_num, outputs);}
void main() {pimpl.main(outputs);}
};
} }
// @/
void main()
Poll the current value of the given ADC1 channel.
Definition sygse-adc.hpp:64
void init()
Configure the ADC1 channel for the given gpio_num in oneshot read mode.
Definition sygse-adc.hpp:61
// @#'sygse-adc.cpp'
/*
Copyright 2023 Travis J. West, https://traviswest.ca, Input Devices and Music
Interaction Laboratory (IDMIL), Centre for Interdisciplinary Research in Music
Media and Technology (CIRMMT), McGill University, Montréal, Canada, and Univ.
Lille, Inria, CNRS, Centrale Lille, UMR 9189 CRIStAL, F-59000 Lille, France
SPDX-License-Identifier: MIT
*/
#include "sygse-adc.hpp"
#include <esp_err.h>
#include <esp_adc/adc_oneshot.h>
namespace {
struct State_ {
adc_channel_t channel;
adc_oneshot_unit_handle_t adc_handle;
};
}
namespace sygaldry { namespace sygse { namespace detail {
void OneshotAdcImpl::init(int gpio_num, outputs_t& outputs)
{
auto& state = *(new State_{});
adc_oneshot_unit_init_cfg_t unit_config{};
adc_oneshot_chan_cfg_t channel_config = {
.atten = ADC_ATTEN_DB_0,
.bitwidth = ADC_BITWIDTH_12,
};
ESP_ERROR_CHECK(adc_oneshot_io_to_channel(gpio_num, &unit_config.unit_id, &state.channel));
ESP_ERROR_CHECK(adc_oneshot_new_unit(&unit_config, &state.adc_handle));
ESP_ERROR_CHECK(adc_oneshot_config_channel(state.adc_handle, state.channel, &channel_config));
pimpl = static_cast<void*>(&state);
}
void OneshotAdcImpl::main(outputs_t& outputs)
{
auto& state = *static_cast<State_*>(pimpl);
ESP_ERROR_CHECK(adc_oneshot_read(state.adc_handle, state.channel, &outputs.raw.value));
}
} } }
// @/
# @#'CMakeLists.txt'
set(lib sygse-adc)
add_library(${lib} INTERFACE)
target_sources(${lib}
INTERFACE ${lib}.cpp
)
target_include_directories(${lib}
INTERFACE .
)
target_link_libraries(${lib}
INTERFACE sygah-endpoints
INTERFACE sygah-metadata
INTERFACE syghe-pins
INTERFACE idf::esp_adc
INTERFACE idf::esp_common
)
# @/