Skip to content

Commit

Permalink
feat(examples): improve example configuration (#140)
Browse files Browse the repository at this point in the history
* feat(examples): improve example configuration
* Update all peripheral examples to use espp::I2c class for cleaner / more reusable code
* Update all peripheral examples to have a Kconfig.projbuild file for configuring the i2c pins - with default hardware selecting relevant targets for the example as well as CUSTOM hardware config to configure your own i2c pins on your hardware

* fix(i2c example): reduce scope of read_data variable

* fix(bm8563 example): fix kconfig

* fix(controller example): add missing component

* fix(st25dv example): added back missing config for nfc wifi settings

* doc: rebuild
  • Loading branch information
finger563 authored Jan 9, 2024
1 parent 2fd32ac commit 53fac4c
Show file tree
Hide file tree
Showing 144 changed files with 1,295 additions and 1,082 deletions.
2 changes: 1 addition & 1 deletion components/ads1x15/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 driver ads1x15 adc task controller"
"main esptool_py ads1x15 i2c task"
CACHE STRING
"List of components to include"
)
Expand Down
39 changes: 39 additions & 0 deletions components/ads1x15/example/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
menu "Example Configuration"

choice EXAMPLE_HARDWARE
prompt "Hardware"
default EXAMPLE_HARDWARE_QTPYPICO
help
Select the hardware to run this example on.

config EXAMPLE_HARDWARE_QTPYPICO
depends on IDF_TARGET_ESP32
bool "Qt Py PICO"

config EXAMPLE_HARDWARE_QTPYS3
depends on IDF_TARGET_ESP32S3
bool "Qt Py S3"

config EXAMPLE_HARDWARE_CUSTOM
bool "Custom"
endchoice

config EXAMPLE_I2C_SCL_GPIO
int "SCL GPIO Num"
range 0 50
default 19 if EXAMPLE_HARDWARE_QTPYPICO
default 40 if EXAMPLE_HARDWARE_QTPYS3
default 19 if EXAMPLE_HARDWARE_CUSTOM
help
GPIO number for I2C Master clock line.

config EXAMPLE_I2C_SDA_GPIO
int "SDA GPIO Num"
range 0 50
default 22 if EXAMPLE_HARDWARE_QTPYPICO
default 41 if EXAMPLE_HARDWARE_QTPYS3
default 22 if EXAMPLE_HARDWARE_CUSTOM
help
GPIO number for I2C Master data line.

endmenu
50 changes: 12 additions & 38 deletions components/ads1x15/example/main/ads1x15_example.cpp
Original file line number Diff line number Diff line change
@@ -1,56 +1,33 @@
#include <chrono>
#include <sdkconfig.h>
#include <vector>

#include "driver/i2c.h"

#include "ads1x15.hpp"
#include "i2c.hpp"
#include "logger.hpp"
#include "task.hpp"

using namespace std::chrono_literals;

static constexpr auto I2C_NUM = (I2C_NUM_1);
static constexpr auto I2C_SCL_IO = (GPIO_NUM_19);
static constexpr auto I2C_SDA_IO = (GPIO_NUM_22);
static constexpr auto I2C_FREQ_HZ = (400 * 1000);
static constexpr auto I2C_TIMEOUT_MS = 10;

extern "C" void app_main(void) {
static espp::Logger logger({.tag = "ads1015 example", .level = espp::Logger::Verbosity::INFO});
// This example shows using the i2c adc (ads1x15)
{
logger.info("Running i2c adc example!");
//! [ads1x15 example]
// make the I2C that we'll use to communicate
i2c_config_t i2c_cfg;
logger.info("initializing i2c driver...");
memset(&i2c_cfg, 0, sizeof(i2c_cfg));
i2c_cfg.sda_io_num = I2C_SDA_IO; // pin 3 on the joybonnet
i2c_cfg.scl_io_num = I2C_SCL_IO; // pin 5 on the joybonnet
i2c_cfg.mode = I2C_MODE_MASTER;
i2c_cfg.sda_pullup_en = GPIO_PULLUP_ENABLE;
i2c_cfg.scl_pullup_en = GPIO_PULLUP_ENABLE;
i2c_cfg.master.clk_speed = I2C_FREQ_HZ;
auto err = i2c_param_config(I2C_NUM, &i2c_cfg);
if (err != ESP_OK)
logger.error("config i2c failed");
err = i2c_driver_install(I2C_NUM, I2C_MODE_MASTER, 0, 0, 0);
if (err != ESP_OK)
logger.error("install i2c driver failed");
// make some lambda functions we'll use to read/write to the i2c adc
auto ads_write = [](uint8_t dev_addr, uint8_t *data, size_t data_len) {
auto err = i2c_master_write_to_device(I2C_NUM, dev_addr, data, data_len,
I2C_TIMEOUT_MS / portTICK_PERIOD_MS);
return err == ESP_OK;
};
auto ads_read = [](uint8_t dev_addr, uint8_t *data, size_t data_len) {
auto err = i2c_master_read_from_device(I2C_NUM, dev_addr,
data, data_len, I2C_TIMEOUT_MS / portTICK_PERIOD_MS);
return err == ESP_OK;
};
espp::I2c i2c({
.port = I2C_NUM_1,
.sda_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SDA_GPIO, // pin 3 on the joybonnet
.scl_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SCL_GPIO, // pin 5 on the joybonnet
});
// make the actual ads class
espp::Ads1x15 ads(espp::Ads1x15::Ads1015Config{
.device_address = espp::Ads1x15::DEFAULT_ADDRESS, .write = ads_write, .read = ads_read});
.device_address = espp::Ads1x15::DEFAULT_ADDRESS,
.write = std::bind(&espp::I2c::write, &i2c, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3),
.read = std::bind(&espp::I2c::read, &i2c, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3)});
// make the task which will get the raw data from the I2C ADC
auto ads_read_task_fn = [&ads](std::mutex &m, std::condition_variable &cv) {
static auto start = std::chrono::high_resolution_clock::now();
Expand Down Expand Up @@ -89,9 +66,6 @@ extern "C" void app_main(void) {
std::this_thread::sleep_for(100ms);
}
}
// now clean up the i2c driver (by now the task will have stopped, because we
// left its scope.
i2c_driver_delete(I2C_NUM);

logger.info("ADS1x15 example complete!");

Expand Down
2 changes: 1 addition & 1 deletion components/ads7138/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 driver ads7138 adc task controller"
"main esptool_py driver ads7138 i2c task"
CACHE STRING
"List of components to include"
)
Expand Down
46 changes: 46 additions & 0 deletions components/ads7138/example/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
menu "Example Configuration"

choice EXAMPLE_HARDWARE
prompt "Hardware"
default EXAMPLE_HARDWARE_QTPYS3
help
Select the hardware to run this example on.

config EXAMPLE_HARDWARE_QTPYPICO
depends on IDF_TARGET_ESP32
bool "Qt Py PICO"

config EXAMPLE_HARDWARE_QTPYS3
depends on IDF_TARGET_ESP32S3
bool "Qt Py S3"

config EXAMPLE_HARDWARE_CUSTOM
bool "Custom"
endchoice

config EXAMPLE_ALERT_GPIO
int "Alert GPIO Num"
range 0 50
default 18
help
GPIO number for alert pin.

config EXAMPLE_I2C_SCL_GPIO
int "SCL GPIO Num"
range 0 50
default 19 if EXAMPLE_HARDWARE_QTPYPICO
default 40 if EXAMPLE_HARDWARE_QTPYS3
default 19 if EXAMPLE_HARDWARE_CUSTOM
help
GPIO number for I2C Master clock line.

config EXAMPLE_I2C_SDA_GPIO
int "SDA GPIO Num"
range 0 50
default 22 if EXAMPLE_HARDWARE_QTPYPICO
default 41 if EXAMPLE_HARDWARE_QTPYS3
default 22 if EXAMPLE_HARDWARE_CUSTOM
help
GPIO number for I2C Master data line.

endmenu
52 changes: 12 additions & 40 deletions components/ads7138/example/main/ads7138_example.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <chrono>
#include <sdkconfig.h>
#include <vector>

#include "driver/gpio.h"
#include "driver/i2c.h"

#include "ads7138.hpp"
#include "i2c.hpp"
#include "logger.hpp"
#include "task.hpp"

Expand Down Expand Up @@ -64,13 +65,6 @@ using namespace std::chrono_literals;
// active low, so when we set the digital output to 1, the LED should turn off,
// and when we set the digital output to 0, the LED should turn on. We can
// change the digital output value by pressing the button on the joystick.
static constexpr auto ALERT_PIN = (GPIO_NUM_18);
static constexpr auto I2C_NUM = (I2C_NUM_1);
static constexpr auto I2C_SCL_IO = (GPIO_NUM_40);
static constexpr auto I2C_SDA_IO = (GPIO_NUM_41);
static constexpr auto I2C_FREQ_HZ = (400 * 1000);
static constexpr auto I2C_TIMEOUT_MS = (10);

static QueueHandle_t gpio_evt_queue;

static void gpio_isr_handler(void *arg) {
Expand All @@ -85,34 +79,13 @@ extern "C" void app_main(void) {
logger.info("Starting example!");

//! [ads7138 example]
static constexpr gpio_num_t ALERT_PIN = (gpio_num_t)CONFIG_EXAMPLE_ALERT_GPIO;
// make the I2C that we'll use to communicate
i2c_config_t i2c_cfg;
logger.info("initializing i2c driver...");
memset(&i2c_cfg, 0, sizeof(i2c_cfg));
i2c_cfg.sda_io_num = I2C_SDA_IO;
i2c_cfg.scl_io_num = I2C_SCL_IO;
i2c_cfg.mode = I2C_MODE_MASTER;
i2c_cfg.sda_pullup_en = GPIO_PULLUP_DISABLE;
i2c_cfg.scl_pullup_en = GPIO_PULLUP_DISABLE;
i2c_cfg.master.clk_speed = I2C_FREQ_HZ;
auto err = i2c_param_config(I2C_NUM, &i2c_cfg);
if (err != ESP_OK)
logger.error("config i2c failed");
err = i2c_driver_install(I2C_NUM, I2C_MODE_MASTER, 0, 0, 0);
if (err != ESP_OK)
logger.error("install i2c driver failed");

// make some lambda functions we'll use to read/write to the i2c adc
auto ads_write = [](uint8_t dev_addr, uint8_t *data, size_t data_len) -> bool {
auto err = i2c_master_write_to_device(I2C_NUM, dev_addr, data, data_len,
I2C_TIMEOUT_MS / portTICK_PERIOD_MS);
return err == ESP_OK;
};
auto ads_read = [](uint8_t dev_addr, uint8_t *data, size_t data_len) -> bool {
auto err = i2c_master_read_from_device(I2C_NUM, dev_addr, data, data_len,
I2C_TIMEOUT_MS / portTICK_PERIOD_MS);
return err == ESP_OK;
};
espp::I2c i2c({
.port = I2C_NUM_1,
.sda_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SDA_GPIO,
.scl_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SCL_GPIO,
});

// make the actual ads class
static int select_bit_mask = (1 << 5);
Expand All @@ -127,8 +100,10 @@ extern "C" void app_main(void) {
.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,
.read = ads_read,
.write = std::bind(&espp::I2c::write, &i2c, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3),
.read = std::bind(&espp::I2c::read, &i2c, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3),
.log_level = espp::Logger::Verbosity::WARN,
});

Expand Down Expand Up @@ -277,9 +252,6 @@ extern "C" void app_main(void) {
std::this_thread::sleep_for(100ms);
}
}
// now clean up the i2c driver (by now the task will have stopped, because we
// left its scope.
i2c_driver_delete(I2C_NUM);

logger.info("ADS7138 example complete!");

Expand Down
2 changes: 1 addition & 1 deletion components/as5600/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 filters driver task as5600"
"main esptool_py filters i2c task as5600"
CACHE STRING
"List of components to include"
)
Expand Down
39 changes: 39 additions & 0 deletions components/as5600/example/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
menu "Example Configuration"

choice EXAMPLE_HARDWARE
prompt "Hardware"
default EXAMPLE_HARDWARE_QTPYS3
help
Select the hardware to run this example on.

config EXAMPLE_HARDWARE_QTPYPICO
depends on IDF_TARGET_ESP32
bool "Qt Py PICO"

config EXAMPLE_HARDWARE_QTPYS3
depends on IDF_TARGET_ESP32S3
bool "Qt Py S3"

config EXAMPLE_HARDWARE_CUSTOM
bool "Custom"
endchoice

config EXAMPLE_I2C_SCL_GPIO
int "SCL GPIO Num"
range 0 50
default 19 if EXAMPLE_HARDWARE_QTPYPICO
default 40 if EXAMPLE_HARDWARE_QTPYS3
default 19 if EXAMPLE_HARDWARE_CUSTOM
help
GPIO number for I2C Master clock line.

config EXAMPLE_I2C_SDA_GPIO
int "SDA GPIO Num"
range 0 50
default 22 if EXAMPLE_HARDWARE_QTPYPICO
default 41 if EXAMPLE_HARDWARE_QTPYS3
default 22 if EXAMPLE_HARDWARE_CUSTOM
help
GPIO number for I2C Master data line.

endmenu
Loading

0 comments on commit 53fac4c

Please sign in to comment.