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
17 changes: 13 additions & 4 deletions components/ads1x15/example/main/ads1x15_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ extern "C" void app_main(void) {
.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
});
std::error_code ec;
auto ads_device =
i2c.add_device<uint8_t>({.device_address = espp::Ads1x15::DEFAULT_ADDRESS,
.timeout_ms = static_cast<int>(i2c.config().timeout_ms),
.scl_speed_hz = i2c.config().clk_speed,
.log_level = espp::Logger::Verbosity::WARN},
ec);
if (!ads_device) {
logger.error("Failed to initialize ADS1x15 I2C device: {}", ec.message());
return;
}
// make the actual ads class
espp::Ads1x15 ads(espp::Ads1x15::Ads1015Config{
.device_address = espp::Ads1x15::DEFAULT_ADDRESS,
.write = [&i2c](uint8_t addr, const uint8_t *data,
size_t len) { return i2c.write(addr, data, len); },
.read = [&i2c](uint8_t addr, uint8_t *data,
size_t len) { return i2c.read(addr, data, len); },
.write = espp::make_i2c_addressed_write(ads_device),
.read = espp::make_i2c_addressed_read(ads_device),
});
// 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) {
Expand Down
18 changes: 13 additions & 5 deletions components/ads7138/example/main/ads7138_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ extern "C" void app_main(void) {
.sda_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SDA_GPIO,
.scl_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SCL_GPIO,
});
std::error_code ec;
auto ads_device =
i2c.add_device<uint8_t>({.device_address = espp::Ads7138::DEFAULT_ADDRESS,
.timeout_ms = static_cast<int>(i2c.config().timeout_ms),
.scl_speed_hz = i2c.config().clk_speed,
.log_level = espp::Logger::Verbosity::WARN},
ec);
if (!ads_device) {
logger.error("Failed to initialize ADS7138 I2C device: {}", ec.message());
return;
}

// make the actual ads class
static int select_bit_mask = (1 << 5);
Expand All @@ -101,15 +112,12 @@ 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 = 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),
.write = espp::make_i2c_addressed_write(ads_device),
.read = espp::make_i2c_addressed_read(ads_device),
.log_level = espp::Logger::Verbosity::WARN,
});

// calibrate the ADC
std::error_code ec;
ads.calibrate(ec);
if (ec) {
logger.error("error calibrating: {}", ec.message());
Expand Down
16 changes: 12 additions & 4 deletions components/adxl345/example/main/adxl345_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ extern "C" void app_main(void) {
});

std::error_code ec;
auto accel_device =
i2c.add_device<uint8_t>({.device_address = espp::Adxl345::DEFAULT_ADDRESS,
.timeout_ms = static_cast<int>(i2c.config().timeout_ms),
.scl_speed_hz = i2c.config().clk_speed,
.log_level = espp::Logger::Verbosity::WARN},
ec);
if (!accel_device) {
logger.error("ADXL345 I2C device initialization failed: {}", ec.message());
return;
}

//////////////////////
/// ADXL345 Configuration
Expand All @@ -51,10 +61,8 @@ extern "C" void app_main(void) {
.device_address = espp::Adxl345::DEFAULT_ADDRESS,
.range = espp::Adxl345::RANGE_2G,
.data_rate = espp::Adxl345::RATE_100_HZ,
.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),
.write = espp::make_i2c_addressed_write(accel_device),
.read = espp::make_i2c_addressed_read(accel_device),
.log_level = espp::Logger::Verbosity::WARN,
});

Expand Down
22 changes: 15 additions & 7 deletions components/as5600/example/main/as5600_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ extern "C" void app_main(void) {
.sda_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SDA_GPIO,
.scl_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SCL_GPIO,
});
std::error_code ec;
auto as5600_device =
i2c.add_device<uint8_t>({.device_address = espp::As5600::DEFAULT_ADDRESS,
.timeout_ms = static_cast<int>(i2c.config().timeout_ms),
.scl_speed_hz = i2c.config().clk_speed,
.log_level = espp::Logger::Verbosity::WARN},
ec);
if (!as5600_device) {
fmt::print("AS5600 I2C device initialization failed: {}\n", ec.message());
return;
}

// make the velocity filter
static constexpr float filter_cutoff_hz = 4.0f;
Expand All @@ -31,13 +42,10 @@ extern "C" void app_main(void) {
auto filter_fn = [&filter](float raw) -> float { return filter.update(raw); };

// now make the as5600 which decodes the data
espp::As5600 as5600(
{.write_then_read =
std::bind(&espp::I2c::write_read, &i2c, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4, std::placeholders::_5),
.velocity_filter = filter_fn,
.update_period = std::chrono::duration<float>(encoder_update_period),
.log_level = espp::Logger::Verbosity::WARN});
espp::As5600 as5600({.write_then_read = espp::make_i2c_addressed_write_then_read(as5600_device),
.velocity_filter = filter_fn,
.update_period = std::chrono::duration<float>(encoder_update_period),
.log_level = espp::Logger::Verbosity::WARN});

// and finally, make the task to periodically poll the as5600 and print the
// state. NOTE: the As5600 runs its own task to maintain state, so we're
Expand Down
36 changes: 21 additions & 15 deletions components/aw9523/example/main/aw9523_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,28 @@ extern "C" void app_main(void) {
.sda_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SDA_GPIO,
.scl_io_num = (gpio_num_t)CONFIG_EXAMPLE_I2C_SCL_GPIO,
});
// now make the aw9523 which handles GPIO
espp::Aw9523 aw9523(
{// since this uses LEDs, both of the address pins are pulled up in
// hardware to have the LEDs default to off
.device_address = espp::Aw9523::DEFAULT_ADDRESS | 0b11,
// set P0_0 - P0_5 to be inputs
.port_0_direction_mask = 0b00111111,
// set P1_0 - P1_1 to be inputs
.port_1_direction_mask = 0b00000011,
.write = std::bind(&espp::I2c::write, &i2c, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3),
.write_then_read =
std::bind(&espp::I2c::write_read, &i2c, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4, std::placeholders::_5),
.log_level = espp::Logger::Verbosity::WARN});
std::error_code ec;
auto aw9523_device =
i2c.add_device<uint8_t>({.device_address = espp::Aw9523::DEFAULT_ADDRESS | 0b11,
.timeout_ms = static_cast<int>(i2c.config().timeout_ms),
.scl_speed_hz = i2c.config().clk_speed,
.log_level = espp::Logger::Verbosity::WARN},
ec);
if (!aw9523_device) {
fmt::print("aw9523 I2C device initialization failed: {}\n", ec.message());
return;
}
// now make the aw9523 which handles GPIO
espp::Aw9523 aw9523({// since this uses LEDs, both of the address pins are pulled up in
// hardware to have the LEDs default to off
.device_address = espp::Aw9523::DEFAULT_ADDRESS | 0b11,
// set P0_0 - P0_5 to be inputs
.port_0_direction_mask = 0b00111111,
// set P1_0 - P1_1 to be inputs
.port_1_direction_mask = 0b00000011,
.write = espp::make_i2c_addressed_write(aw9523_device),
.write_then_read = espp::make_i2c_addressed_write_then_read(aw9523_device),
.log_level = espp::Logger::Verbosity::WARN});
aw9523.initialize(ec); // Initialized separately from the constructor.
if (ec) {
fmt::print("aw9523 initialization failed: {}\n", ec.message());
Expand Down
17 changes: 13 additions & 4 deletions components/bldc_haptics/example/main/bldc_haptics_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,20 @@ extern "C" void app_main(void) {

// now make the mt6701 which decodes the data
using Encoder = espp::Mt6701<>;
std::error_code ec;
auto encoder_device =
i2c.add_device<uint8_t>({.device_address = Encoder::DEFAULT_ADDRESS,
.timeout_ms = static_cast<int>(i2c.config().timeout_ms),
.scl_speed_hz = i2c.config().clk_speed,
.log_level = espp::Logger::Verbosity::WARN},
ec);
if (!encoder_device) {
logger.error("Failed to initialize MT6701 I2C device: {}", ec.message());
return;
}
std::shared_ptr<Encoder> mt6701 = std::make_shared<Encoder>(
Encoder::Config{.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),
Encoder::Config{.write = espp::make_i2c_addressed_write(encoder_device),
.read = espp::make_i2c_addressed_read(encoder_device),
.velocity_filter = nullptr, // no filtering
.update_period = std::chrono::duration<float>(core_update_period),
.log_level = espp::Logger::Verbosity::WARN});
Expand Down
17 changes: 13 additions & 4 deletions components/bldc_motor/example/main/bldc_motor_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,20 @@ extern "C" void app_main(void) {
//! [bldc_motor example]
// now make the mt6701 which decodes the data
using Encoder = espp::Mt6701<>;
std::error_code ec;
auto encoder_device =
i2c.add_device<uint8_t>({.device_address = Encoder::DEFAULT_ADDRESS,
.timeout_ms = static_cast<int>(i2c.config().timeout_ms),
.scl_speed_hz = i2c.config().clk_speed,
.log_level = espp::Logger::Verbosity::WARN},
ec);
if (!encoder_device) {
logger.error("Failed to initialize MT6701 I2C device: {}", ec.message());
return;
}
std::shared_ptr<Encoder> mt6701 = std::make_shared<Encoder>(
Encoder::Config{.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),
Encoder::Config{.write = espp::make_i2c_addressed_write(encoder_device),
.read = espp::make_i2c_addressed_read(encoder_device),
.velocity_filter = nullptr, // no filtering
.update_period = std::chrono::duration<float>(core_update_period),
.log_level = espp::Logger::Verbosity::WARN});
Expand Down
17 changes: 13 additions & 4 deletions components/bm8563/example/main/bm8563_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,23 @@ extern "C" void app_main(void) {
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
});
std::error_code ec;
auto bm8563_device =
i2c.add_device<uint8_t>({.device_address = espp::Bm8563::DEFAULT_ADDRESS,
.timeout_ms = static_cast<int>(i2c.config().timeout_ms),
.scl_speed_hz = i2c.config().clk_speed,
.log_level = espp::Logger::Verbosity::WARN},
ec);
if (!bm8563_device) {
fmt::print("bm8563 I2C device initialization failed: {}\n", ec.message());
return;
}
// now make the bm8563
auto bm8563 = espp::Bm8563({
.write = std::bind_front(&espp::I2c::write, &i2c),
.write_then_read = std::bind_front(&espp::I2c::write_read, &i2c),
.write = espp::make_i2c_addressed_write(bm8563_device),
.write_then_read = espp::make_i2c_addressed_write_then_read(bm8563_device),
});

std::error_code ec;

// set the time
espp::Bm8563::DateTime date_time = {.date =
{
Expand Down
4 changes: 2 additions & 2 deletions components/bmi270/example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ set(EXTRA_COMPONENT_DIRS

set(
COMPONENTS
"main esptool_py i2c bmi270 filters"
"main esptool_py i2c bmi270 filters task"
CACHE STRING
"List of components to include"
)

project(bmi270_example)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD 20)
20 changes: 14 additions & 6 deletions components/bmi270/example/main/bmi270_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "i2c.hpp"
#include "kalman_filter.hpp"
#include "madgwick_filter.hpp"
#include "task.hpp"

using namespace std::chrono_literals;

Expand Down Expand Up @@ -82,14 +83,23 @@ extern "C" void app_main(void) {
logger.warn("No BMI270 found at address: 0x{:02X}", address);
}
}
std::error_code ec;
auto bmi270_device =
i2c.add_device<uint8_t>({.device_address = bmi270_address,
.timeout_ms = static_cast<int>(i2c.config().timeout_ms),
.scl_speed_hz = i2c.config().clk_speed,
.log_level = espp::Logger::Verbosity::WARN},
ec);
if (!bmi270_device) {
logger.error("Failed to initialize BMI270 I2C device: {}", ec.message());
return;
}

// make the IMU config
Imu::Config config{
.device_address = bmi270_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),
.write = espp::make_i2c_addressed_write(bmi270_device),
.read = espp::make_i2c_addressed_read(bmi270_device),
.imu_config =
{
.accelerometer_range = Imu::AccelerometerRange::RANGE_4G,
Expand Down Expand Up @@ -119,8 +129,6 @@ extern "C" void app_main(void) {
// Note: This assumes the device is flat on a table (Z-axis = 1g)
// For a real application, you might want to trigger this based on a user action
// or store the offsets in NVS.
std::error_code ec;

logger.info("Performing Accelerometer FOC...");
Imu::AccelFocGValue accel_foc_target = {.x = 0, .y = 0, .z = 1, .sign = 0}; // 1g on Z axis
if (imu.perform_accel_foc(accel_foc_target, ec)) {
Expand Down
1 change: 1 addition & 0 deletions components/byte90/include/byte90.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ class Byte90 : public BaseComponent {
button_callback_t button_callback_{nullptr};

// accelerometer
std::shared_ptr<I2c::Device<uint8_t>> accelerometer_i2c_device_{nullptr};
std::shared_ptr<Accelerometer> accelerometer_{nullptr};
accel_callback_t accel_callback_{nullptr};
std::recursive_mutex accel_data_mutex_;
Expand Down
21 changes: 15 additions & 6 deletions components/byte90/src/accelerometer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,29 @@ bool Byte90::initialize_accelerometer(const Byte90::accel_callback_t &callback)
// store the callback
accel_callback_ = callback;

std::error_code ec;
accelerometer_i2c_device_ = internal_i2c_.add_device<uint8_t>(
{
.device_address = Accelerometer::DEFAULT_ADDRESS,
.timeout_ms = static_cast<int>(internal_i2c_.config().timeout_ms),
.scl_speed_hz = internal_i2c_.config().clk_speed,
.log_level = espp::Logger::Verbosity::WARN,
},
ec);
if (!accelerometer_i2c_device_) {
logger_.error("Could not initialize accelerometer I2C device: {}", ec.message());
return false;
}
// create the accelerometer
accelerometer_ = std::make_shared<Accelerometer>(Accelerometer::Config{
.device_address = Accelerometer::DEFAULT_ADDRESS,
.range = Accelerometer::RANGE_2G,
.data_rate = Accelerometer::RATE_100_HZ,
.write = std::bind(&espp::I2c::write, &internal_i2c_, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3),
.read = std::bind(&espp::I2c::read, &internal_i2c_, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3),
.write = espp::make_i2c_addressed_write(accelerometer_i2c_device_),
.read = espp::make_i2c_addressed_read(accelerometer_i2c_device_),
.log_level = espp::Logger::Verbosity::WARN,
});

std::error_code ec;

// Disable measurement mode initially, so we can configure interrupts and such
accelerometer_->set_measurement_mode(false, ec);

Expand Down
17 changes: 13 additions & 4 deletions components/chsc6x/example/main/chsc6x_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@ extern "C" void app_main(void) {

bool has_chsc6x = i2c.probe_device(espp::Chsc6x::DEFAULT_ADDRESS);
fmt::print("Touchpad probe: {}\n", has_chsc6x);
std::error_code ec;
auto chsc6x_device =
i2c.add_device<uint8_t>({.device_address = espp::Chsc6x::DEFAULT_ADDRESS,
.timeout_ms = static_cast<int>(i2c.config().timeout_ms),
.scl_speed_hz = i2c.config().clk_speed,
.log_level = espp::Logger::Verbosity::WARN},
ec);
if (!chsc6x_device) {
fmt::print("CHSC6X I2C device initialization failed: {}\n", ec.message());
return;
}

// now make the chsc6x which decodes the data
espp::Chsc6x chsc6x({.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),
espp::Chsc6x chsc6x({.write = espp::make_i2c_addressed_write(chsc6x_device),
.read = espp::make_i2c_addressed_read(chsc6x_device),
.log_level = espp::Logger::Verbosity::WARN});

// and finally, make the task to periodically poll the chsc6x and print
Expand Down
Loading
Loading