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
31 changes: 30 additions & 1 deletion components/i2c/include/i2c.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <mutex>

#include <driver/i2c.h>

#include "logger.hpp"
Expand Down Expand Up @@ -47,6 +49,7 @@ class I2c {
return;
}

std::lock_guard<std::mutex> lock(mutex_);
i2c_config_t i2c_cfg;
memset(&i2c_cfg, 0, sizeof(i2c_cfg));
i2c_cfg.sda_io_num = config_.sda_io_num;
Expand Down Expand Up @@ -79,6 +82,7 @@ class I2c {
return;
}

std::lock_guard<std::mutex> lock(mutex_);
auto err = i2c_driver_delete(config_.port);
if (err != ESP_OK) {
logger_.error("delete i2c driver failed");
Expand All @@ -100,6 +104,7 @@ class I2c {
return false;
}

std::lock_guard<std::mutex> lock(mutex_);
auto err = i2c_master_write_to_device(config_.port, dev_addr, data, data_len,
config_.timeout_ms / portTICK_PERIOD_MS);
if (err != ESP_OK) {
Expand All @@ -124,6 +129,7 @@ class I2c {
return false;
}

std::lock_guard<std::mutex> lock(mutex_);
auto err =
i2c_master_write_read_device(config_.port, dev_addr, write_data, write_size, read_data,
read_size, config_.timeout_ms / portTICK_PERIOD_MS);
Expand All @@ -147,6 +153,7 @@ class I2c {
return false;
}

std::lock_guard<std::mutex> lock(mutex_);
auto err = i2c_master_write_read_device(config_.port, dev_addr, &reg_addr, 1, data, data_len,
config_.timeout_ms / portTICK_PERIOD_MS);
if (err != ESP_OK) {
Expand All @@ -169,6 +176,7 @@ class I2c {
return false;
}

std::lock_guard<std::mutex> lock(mutex_);
auto err = i2c_master_read_from_device(config_.port, dev_addr, data, data_len,
config_.timeout_ms / portTICK_PERIOD_MS);
if (err != ESP_OK) {
Expand All @@ -179,10 +187,31 @@ class I2c {
return true;
}

/// Probe I2C device
/// \param dev_addr I2C device address
/// \return True if successful
/// \details
/// This function sends a start condition, writes the device address, and then
/// sends a stop condition. If the device acknowledges the address, then it is
/// present on the bus.
bool probe_device(uint8_t dev_addr) {
bool success = false;
std::lock_guard<std::mutex> lock(mutex_);
auto cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev_addr << 1) | I2C_MASTER_WRITE, true);
i2c_master_stop(cmd);
if (i2c_master_cmd_begin(config_.port, cmd, 1000) == ESP_OK) {
success = true;
}
i2c_cmd_link_delete(cmd);
return success;
}

protected:
Config config_;
bool initialized_ = false;

std::mutex mutex_;
espp::Logger logger_;
};
} // namespace espp
6 changes: 5 additions & 1 deletion components/tt21100/example/main/tt21100_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ extern "C" void app_main(void) {
// the state
auto task_fn = [&tt21100](std::mutex &m, std::condition_variable &cv) {
std::error_code ec;
tt21100.update(ec);
bool new_data = tt21100.update(ec);
if (ec) {
fmt::print("TT21100 update failed: {}\n", ec.message());
return true; // stop the task
}
if (!new_data) {
// no new data, so don't bother printing
return false; // don't stop the task
}
// get the state
uint8_t num_touch_points = 0;
uint16_t x = 0, y = 0;
Expand Down
13 changes: 9 additions & 4 deletions components/tt21100/include/tt21100.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,34 @@ class Tt21100 {

/// @brief Read the touch data
/// @param ec The error code to set if an error occurs
void update(std::error_code &ec) {
/// @return True if there is new touch data
bool update(std::error_code &ec) {
static uint16_t data_len;
static uint8_t data[256];

bool success = read_(DEFAULT_ADDRESS, (uint8_t *)&data_len, sizeof(data_len));
if (!success) {
logger_.error("Failed to read data length");
ec = std::make_error_code(std::errc::io_error);
return;
return false;
}

logger_.debug("Data length: {}", data_len);

if (data_len == 0xff) {
logger_.error("Invalid data length");
ec = std::make_error_code(std::errc::io_error);
return;
return false;
}

success = read_(DEFAULT_ADDRESS, data, data_len);
if (!success) {
logger_.error("Failed to read data");
ec = std::make_error_code(std::errc::io_error);
return;
return false;
}

bool new_data = false;
switch (data_len) {
case 2:
// no available data
Expand All @@ -79,6 +81,7 @@ class Tt21100 {
y_ = touch_data->y;
num_touch_points_ = (data_len - sizeof(TouchReport)) / sizeof(TouchRecord);
logger_.debug("Touch event: #={}, [0]=({}, {})", num_touch_points_, x_, y_);
new_data = true;
break;
}
case 14: {
Expand All @@ -88,11 +91,13 @@ class Tt21100 {
auto btn_signal = button_data->btn_signal[0];
logger_.debug("Button event({}): {}, {}", (int)(button_data->length), home_button_pressed_,
btn_signal);
new_data = true;
break;
}
default:
break;
}
return new_data;
}

/// @brief Get the number of touch points
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/adc_types.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,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/b6ec4c4/docs/en/adc/adc_types.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/cdb8bd4/docs/en/adc/adc_types.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -162,7 +162,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/b6ec4c4/components/adc/include/adc_types.hpp">components/adc/include/adc_types.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/0775afc/components/adc/include/adc_types.hpp">components/adc/include/adc_types.hpp</a></p></li>
</ul>
</section>
</section>
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/ads1x15.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,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/b6ec4c4/docs/en/adc/ads1x15.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/cdb8bd4/docs/en/adc/ads1x15.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -163,7 +163,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/b6ec4c4/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/cdb8bd4/components/ads1x15/include/ads1x15.hpp">components/ads1x15/include/ads1x15.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/ads7138.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>ADS7138 I2C ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/b6ec4c4/docs/en/adc/ads7138.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/cdb8bd4/docs/en/adc/ads7138.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -168,7 +168,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/b6ec4c4/components/ads7138/include/ads7138.hpp">components/ads7138/include/ads7138.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/cdb8bd4/components/ads7138/include/ads7138.hpp">components/ads7138/include/ads7138.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/continuous_adc.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>Continuous ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/b6ec4c4/docs/en/adc/continuous_adc.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/cdb8bd4/docs/en/adc/continuous_adc.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -168,7 +168,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/b6ec4c4/components/adc/include/continuous_adc.hpp">components/adc/include/continuous_adc.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/cdb8bd4/components/adc/include/continuous_adc.hpp">components/adc/include/continuous_adc.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
2 changes: 1 addition & 1 deletion docs/adc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li>ADC APIs</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/b6ec4c4/docs/en/adc/index.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/cdb8bd4/docs/en/adc/index.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/oneshot_adc.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>Oneshot ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/b6ec4c4/docs/en/adc/oneshot_adc.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/cdb8bd4/docs/en/adc/oneshot_adc.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -167,7 +167,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/b6ec4c4/components/adc/include/oneshot_adc.hpp">components/adc/include/oneshot_adc.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/cdb8bd4/components/adc/include/oneshot_adc.hpp">components/adc/include/oneshot_adc.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/adc/tla2528.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
<li><a href="index.html">ADC APIs</a> &raquo;</li>
<li>TLA2528 I2C ADC</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/b6ec4c4/docs/en/adc/tla2528.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/cdb8bd4/docs/en/adc/tla2528.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -168,7 +168,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/b6ec4c4/components/tla2528/include/tla2528.hpp">components/tla2528/include/tla2528.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/cdb8bd4/components/tla2528/include/tla2528.hpp">components/tla2528/include/tla2528.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
4 changes: 2 additions & 2 deletions docs/bldc/bldc_driver.html
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<li><a href="index.html">BLDC APIs</a> &raquo;</li>
<li>BLDC Driver</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/b6ec4c4/docs/en/bldc/bldc_driver.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/cdb8bd4/docs/en/bldc/bldc_driver.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/b6ec4c4/components/bldc_driver/include/bldc_driver.hpp">components/bldc_driver/include/bldc_driver.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/cdb8bd4/components/bldc_driver/include/bldc_driver.hpp">components/bldc_driver/include/bldc_driver.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
8 changes: 4 additions & 4 deletions docs/bldc/bldc_motor.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
<li><a href="index.html">BLDC APIs</a> &raquo;</li>
<li>BLDC Motor</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/b6ec4c4/docs/en/bldc/bldc_motor.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/cdb8bd4/docs/en/bldc/bldc_motor.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down Expand Up @@ -175,7 +175,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/b6ec4c4/components/bldc_motor/include/bldc_motor.hpp">components/bldc_motor/include/bldc_motor.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/cdb8bd4/components/bldc_motor/include/bldc_motor.hpp">components/bldc_motor/include/bldc_motor.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down Expand Up @@ -568,13 +568,13 @@ <h4>Example Usage<a class="headerlink" href="#classespp_1_1_bldc_motor_1bldc_mot
<section id="id1">
<h3>Header File<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/b6ec4c4/components/bldc_motor/include/bldc_types.hpp">components/bldc_motor/include/bldc_types.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/cdb8bd4/components/bldc_motor/include/bldc_types.hpp">components/bldc_motor/include/bldc_types.hpp</a></p></li>
</ul>
</section>
<section id="id2">
<h3>Header File<a class="headerlink" href="#id2" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/b6ec4c4/components/bldc_motor/include/sensor_direction.hpp">components/bldc_motor/include/sensor_direction.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/cdb8bd4/components/bldc_motor/include/sensor_direction.hpp">components/bldc_motor/include/sensor_direction.hpp</a></p></li>
</ul>
</section>
</section>
Expand Down
2 changes: 1 addition & 1 deletion docs/bldc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
<li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
<li>BLDC APIs</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/b6ec4c4/docs/en/bldc/index.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/cdb8bd4/docs/en/bldc/index.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down
4 changes: 2 additions & 2 deletions docs/button.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
<li><a href="index.html" class="icon icon-home"></a> &raquo;</li>
<li>Button APIs</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/b6ec4c4/docs/en/button.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/cdb8bd4/docs/en/button.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand All @@ -163,7 +163,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/b6ec4c4/components/button/include/button.hpp">components/button/include/button.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/cdb8bd4/components/button/include/button.hpp">components/button/include/button.hpp</a></p></li>
</ul>
</section>
<section id="classes">
Expand Down
6 changes: 3 additions & 3 deletions docs/cli.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
<li><a href="index.html" class="icon icon-home"></a> &raquo;</li>
<li>Command Line Interface (CLI) APIs</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/esp-cpp/espp/blob/b6ec4c4/docs/en/cli.rst" class="fa fa-github"> Edit on GitHub</a>
<a href="https://github.com/esp-cpp/espp/blob/cdb8bd4/docs/en/cli.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
Expand Down Expand Up @@ -183,7 +183,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/b6ec4c4/components/cli/include/cli.hpp">components/cli/include/cli.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/cdb8bd4/components/cli/include/cli.hpp">components/cli/include/cli.hpp</a></p></li>
</ul>
</section>
<section id="macros">
Expand Down Expand Up @@ -347,7 +347,7 @@ <h4>Oneshot CLI Example<a class="headerlink" href="#classespp_1_1_cli_1cli_ex1"
<section id="id1">
<h3>Header File<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h3>
<ul class="simple">
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/b6ec4c4/components/cli/include/line_input.hpp">components/cli/include/line_input.hpp</a></p></li>
<li><p><a class="reference external" href="https://github.com/esp-cpp/espp/blob/cdb8bd4/components/cli/include/line_input.hpp">components/cli/include/line_input.hpp</a></p></li>
</ul>
</section>
<section id="id2">
Expand Down
Loading