Skip to content

Commit

Permalink
feat(i2c): Add I2cMenu to i2c component
Browse files Browse the repository at this point in the history
* Move I2cMenu from component example into component so that others can use it more effectively
* Update menu to allow easier reuse / subclassing and to be within the espp namespace
* Add protection around menu so that it allows compilation if exceptions are disabled.
  • Loading branch information
finger563 committed Mar 12, 2024
1 parent 23b38ed commit 119b537
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 175 deletions.
2 changes: 1 addition & 1 deletion components/i2c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
idf_component_register(
INCLUDE_DIRS "include"
REQUIRES "driver" "base_component"
REQUIRES "driver" "base_component" "cli"
)
2 changes: 1 addition & 1 deletion components/i2c/example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set(EXTRA_COMPONENT_DIRS

set(
COMPONENTS
"main esptool_py cli i2c logger"
"main esptool_py i2c logger"
CACHE STRING
"List of components to include"
)
Expand Down
21 changes: 17 additions & 4 deletions components/i2c/example/main/i2c_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,31 @@ extern "C" void app_main(void) {
logger.info("Starting");

{
#if CONFIG_COMPILER_CXX_EXCEPTIONS || defined(_DOXYGEN_)
//! [i2c menu example]
espp::I2c i2c({
.port = I2C_NUM_0,
.sda_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SDA_GPIO,
.scl_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SCL_GPIO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.log_level = espp::Logger::Verbosity::INFO,
});
// now make a menu for the auth object
I2cMenu i2c_menu(i2c);
espp::I2cMenu i2c_menu(i2c);
cli::Cli cli(i2c_menu.get());
cli::SetColor();
cli.ExitAction([](auto &out) { out << "Goodbye and thanks for all the fish.\n"; });
espp::Cli input(cli);
input.SetInputHistorySize(10);

input.Start(); // As this is in the primary thread, we hold here until cli is complete.
//! [i2c menu example]
input.Start(); // As this is in the primary thread, we hold here until cli
// is complete. This is a blocking call and will not return until
// the user enters the `exit` command.
//! [i2c menu example]
#else
logger.warn("C++ exceptions are not enabled, skipping I2C menu example");
#endif // CONFIG_COMPILER_CXX_EXCEPTIONS || defined(_DOXYGEN_)
}

{
Expand All @@ -39,10 +47,15 @@ extern "C" void app_main(void) {
.port = I2C_NUM_0,
.sda_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SDA_GPIO,
.scl_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SCL_GPIO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
});

// probe the bus for all addresses and store the ones that were found /
// responded
// responded. NOTE: this will take a while to run, as it will probe all 128
// possible addresses and the hard-coded timeout on the I2C (inside ESP-IDF)
// is 1 second (I2C_CMD_ALIVE_INTERVAL_TICK within
// esp-idf/components/driver/i2c/i2c.c).
std::vector<uint8_t> found_addresses;
for (uint8_t address = 0; address < 128; address++) {
if (i2c.probe_device(address)) {
Expand Down
169 changes: 0 additions & 169 deletions components/i2c/example/main/i2c_menu.hpp

This file was deleted.

Loading

0 comments on commit 119b537

Please sign in to comment.