Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions components/ads7138/example/main/ads7138_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ extern "C" void app_main(void) {
.digital_inputs = {espp::Ads7138::Channel::CH5},
.digital_outputs =
{espp::Ads7138::Channel::CH7}, // On the BP-ADS7128, CH7 is the LED (active low)
// unordered map of channel to digital output value
.digital_output_values = {{espp::Ads7138::Channel::CH7, 1}}, // start the LED off
// enable oversampling / averaging
.oversampling_ratio = espp::Ads7138::OversamplingRatio::OSR_32,
.write = ads_write,
Expand Down Expand Up @@ -189,9 +191,6 @@ extern "C" void app_main(void) {
// set the digital output drive mode to open-drain
ads.set_digital_output_mode(espp::Ads7138::Channel::CH7, espp::Ads7138::OutputMode::OPEN_DRAIN);

// set the digital output to 1 (turn off the LED)
ads.set_digital_output_value(espp::Ads7138::Channel::CH7, 1);

// set an alert on the digital input (Sel) so that we can get notified when the button is
// pressed (goes low)
ads.set_digital_alert(espp::Ads7138::Channel::CH5, espp::Ads7138::DigitalEvent::LOW);
Expand All @@ -208,6 +207,11 @@ extern "C" void app_main(void) {
auto x_mv = all_mv[0]; // the first channel is channel 1 (X axis)
auto y_mv = all_mv[1]; // the second channel is channel 3 (Y axis)

// alternatively we could get the analog data in a map
auto mapped_mv = ads.get_all_mv_map();
x_mv = mapped_mv[espp::Ads7138::Channel::CH1];
y_mv = mapped_mv[espp::Ads7138::Channel::CH3];

// NOTE: we could get all digital inputs as a bitmask using
// get_digital_input_values(), but we'll just get the one we want.
// If we wanted to get all of them, we could do:
Expand Down
68 changes: 68 additions & 0 deletions components/ads7138/include/ads7138.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <functional>
#include <mutex>
#include <thread>
#include <unordered_map>

#include "logger.hpp"

Expand Down Expand Up @@ -152,6 +153,15 @@ class Ads7138 {
std::vector<Channel> analog_inputs = {}; ///< List of analog input channels to sample.
std::vector<Channel> digital_inputs = {}; ///< List of digital input channels to sample.
std::vector<Channel> digital_outputs = {}; ///< List of digital output channels to sample.
std::unordered_map<Channel, OutputMode> digital_output_modes =
{}; ///< Optional output mode for digital
///< output channels. If not specified,
///< the default value is open-drain.
std::unordered_map<Channel, bool> digital_output_values =
{}; ///< Optional initial values for digital
///< output channels. If not specified,
///< the default value is false in open-drain
///< mode.
OversamplingRatio oversampling_ratio = OversamplingRatio::NONE; ///< Oversampling ratio to use.
bool statistics_enabled = true; ///< Enable statistics collection (min, max, recent)
write_fn write; ///< Function to write to the ADC
Expand Down Expand Up @@ -229,6 +239,28 @@ class Ads7138 {
return values;
}

/**
* @brief Communicate with the ADC to get the analog value for all channels
* and return them.
* @return An unordered map of the voltages (in mV) read from each channel.
* @note These are the channels that were configured as analog inputs.
* @note If the ADC is in autonomous mode, this function will simply read
* the values from the ADC's buffer. If the ADC is in manual mode, this
* function will trigger a conversion and then read the values from the
* ADC's buffer (blocking until conversion is complete).
*/
std::unordered_map<Channel, float> get_all_mv_map() {
std::lock_guard<std::recursive_mutex> lock(mutex_);
std::unordered_map<Channel, float> values;
// TODO: handle the non-autonomous case
auto raw_values = read_mapped_recent_all();
// convert the raw values (uint16_t) to mv (float)
std::transform(
raw_values.begin(), raw_values.end(), std::inserter(values, values.end()),
[this](const auto &pair) { return std::make_pair(pair.first, raw_to_mv(pair.second)); });
return values;
}

/// @brief Configure the ALERT pin
/// @param output_mode Output mode ALERT pin
/// @param alert_logic Alert logic for ALERT pin
Expand Down Expand Up @@ -710,6 +742,20 @@ class Ads7138 {
// set pin configuration (analog vs digital)
set_pin_configuration();

// set the digital output outputmode (push-pull vs open-drain). NOTE: this
// will work because the digital output channels have already been saved
// (though they have not been configured yet)
for (auto &[channel, output_mode] : config.digital_output_modes) {
set_digital_output_mode(channel, output_mode);
}

// set the digital output values. NOTE: this will work because the digital
// output channels have already been saved (though they have not been
// configured yet)
for (auto &[channel, value] : config.digital_output_values) {
set_digital_output_value(channel, value);
}

// Set the digital input/output channels
set_digital_io_direction();

Expand Down Expand Up @@ -894,6 +940,28 @@ class Ads7138 {
return values;
}

std::unordered_map<Channel, uint16_t> read_mapped_recent_all() {
if (!statistics_enabled_) {
logger_.error("Statistics are not enabled, cannot read recent value");
return {};
}
logger_.info("Reading recent mapped values for all channels");
std::unordered_map<Channel, uint16_t> values;
uint8_t raw_values[16];
read_many_(Register::RECENT_CH0_LSB, raw_values, 16);
// only pull out the ones that were configured as analog inputs
for (int i = 0; i < 8; i++) {
Channel ch = static_cast<Channel>(i);
if (is_analog_input(ch)) {
// read both the LSB AND MSB registers and combine them (lsb is first)
uint8_t lsb = raw_values[i * 2];
uint8_t msb = raw_values[i * 2 + 1];
values[ch] = (msb << 8) | lsb;
}
}
return values;
}

uint16_t read_max(Channel ch) {
if (!is_analog_input(ch)) {
logger_.error("Channel {} is not configured as an analog input", ch);
Expand Down
2 changes: 1 addition & 1 deletion docs/adc/adc_types.html
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>ADC Types</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/9212b8c/docs/en/adc/adc_types.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/c76a4da/docs/en/adc/adc_types.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/ads1x15.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>ADS1x15 I2C ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/9212b8c/docs/en/adc/ads1x15.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/c76a4da/docs/en/adc/ads1x15.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -159,7 +159,7 @@ <h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to
<section id="header-file">
<h3>Header File<a class="headerlink" href="#header-file" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/9212b8c/components/ads1x15/include/ads1x15.hpp">components/ads1x15/include/ads1x15.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/c76a4da/components/ads1x15/include/ads1x15.hpp">components/ads1x15/include/ads1x15.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
Loading