From 009eef70a445fb176d5d439cae77395924fb2951 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Thu, 19 Feb 2026 08:52:57 -0600 Subject: [PATCH] fix: run clang-format on all files to ensure consistent style --- .../bmi270/example/main/bmi270_example.cpp | 20 +- components/bmi270/include/bmi270.hpp | 495 ++++++++++-------- components/bmi270/include/bmi270_detail.hpp | 14 +- components/i2c/include/i2c_format_helpers.hpp | 8 +- components/led_strip/include/led_strip.hpp | 25 +- components/logger/include/logger.hpp | 2 +- 6 files changed, 321 insertions(+), 243 deletions(-) mode change 100755 => 100644 components/led_strip/include/led_strip.hpp diff --git a/components/bmi270/example/main/bmi270_example.cpp b/components/bmi270/example/main/bmi270_example.cpp index cda679c65..6a4f545cc 100644 --- a/components/bmi270/example/main/bmi270_example.cpp +++ b/components/bmi270/example/main/bmi270_example.cpp @@ -108,19 +108,19 @@ extern "C" void app_main(void) { // create the IMU Imu imu(config); - // --------------------------------------------------------------------------- - // [Optional] Sensor Calibration (FOC & CRT) - // WARNING: - // 1. The device must be COMPLETELY STATIONARY on a flat surface. - // 2. Enable these lines only when you need calibration (e.g., once after assembly). - // --------------------------------------------------------------------------- - #if CONFIG_EXAMPLE_RUN_CALIBRATION +// --------------------------------------------------------------------------- +// [Optional] Sensor Calibration (FOC & CRT) +// WARNING: +// 1. The device must be COMPLETELY STATIONARY on a flat surface. +// 2. Enable these lines only when you need calibration (e.g., once after assembly). +// --------------------------------------------------------------------------- +#if CONFIG_EXAMPLE_RUN_CALIBRATION // Perform FOC (Fast Offset Compensation) // 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)) { @@ -145,7 +145,7 @@ extern "C" void app_main(void) { } else { logger.error("CRT failed: {}", ec.message()); } - #endif +#endif // make a task to read out the IMU data and print it to console auto task_fn = [&](std::mutex &m, std::condition_variable &cv) -> bool { @@ -198,7 +198,7 @@ extern "C" void app_main(void) { // print madgwick filter outputs text += fmt::format("{:03.3f},{:03.3f},{:03.3f},", roll, pitch, yaw); text += fmt::format("{:03.3f},{:03.3f},{:03.3f}", vx, vy, vz); - + fmt::print("{}\n", text); return false; diff --git a/components/bmi270/include/bmi270.hpp b/components/bmi270/include/bmi270.hpp index 482f1a550..16e76d482 100644 --- a/components/bmi270/include/bmi270.hpp +++ b/components/bmi270/include/bmi270.hpp @@ -1,9 +1,9 @@ #pragma once #include +#include #include #include -#include #include "base_peripheral.hpp" #include "bmi270_detail.hpp" @@ -88,13 +88,14 @@ class Bmi270 : public espp::BasePeripheral::write_fn write = nullptr; ///< Write function BasePeripheral::read_fn read = - nullptr; ///< Read function - ImuConfig imu_config; ///< IMU configuration - filter_fn orientation_filter = nullptr; ///< Filter function for orientation + nullptr; ///< Read function + ImuConfig imu_config; ///< IMU configuration + filter_fn orientation_filter = nullptr; ///< Filter function for orientation /** - * @brief Maximum number of bytes to write in a single burst during config upload. If 0 will use full config file size of 8192 bytes. - * Default is 0 (uses full config file size of 8192 bytes). - * Set this to a small non-zero value (e.g., 128) if you encounter stack overflow or want to decrease memory usage. + * @brief Maximum number of bytes to write in a single burst during config upload. If 0 will use + * full config file size of 8192 bytes. Default is 0 (uses full config file size of 8192 bytes). + * Set this to a small non-zero value (e.g., 128) if you encounter stack overflow or want to + * decrease memory usage. */ uint16_t burst_write_size = 0; bool auto_init{true}; ///< Automatically initialize the BMI270 @@ -107,7 +108,8 @@ class Bmi270 : public espp::BasePeripheral({}, "Bmi270", config.log_level) , orientation_filter_(config.orientation_filter) , imu_config_(config.imu_config) - , burst_write_size_(config.burst_write_size == 0 ? config_file_size : config.burst_write_size) { + , burst_write_size_(config.burst_write_size == 0 ? config_file_size + : config.burst_write_size) { if constexpr (Interface == bmi270::Interface::I2C) { set_address(config.device_address); } @@ -441,13 +443,14 @@ class Bmi270 : public espp::BasePeripheral(config.output_type) << 2) | + uint8_t int_io_ctrl = (1 << 3) | (static_cast(config.output_type) << 2) | (static_cast(config.active_level) << 1); - auto pin_reg = (config.pin == InterruptPin::INT1) ? Register::INT1_IO_CTRL : Register::INT2_IO_CTRL; + auto pin_reg = + (config.pin == InterruptPin::INT1) ? Register::INT1_IO_CTRL : Register::INT2_IO_CTRL; write_u8_to_register(static_cast(pin_reg), int_io_ctrl, ec); - if (ec) return false; + if (ec) + return false; // Map interrupt features to pins uint8_t int_map_data = 0; @@ -458,7 +461,8 @@ class Bmi270 : public espp::BasePeripheral(Register::INT_MAP_DATA), int_map_data, ec); - if (ec) return false; + if (ec) + return false; return true; } @@ -481,16 +485,18 @@ class Bmi270 : public espp::BasePeripheral(Register::PWR_CTRL), ec); - if (ec) return false; + if (ec) + return false; uint8_t saved_pwr_conf = read_u8_from_register(static_cast(Register::PWR_CONF), ec); - if (ec) return false; + if (ec) + return false; // Use ScopeGuard to ensure state is restored on all exit paths auto state_guard = make_scope_guard([&]() { - std::error_code internal_ec; - set_config(saved_config, internal_ec); - write_u8_to_register(static_cast(Register::PWR_CTRL), saved_pwr_ctrl, internal_ec); - write_u8_to_register(static_cast(Register::PWR_CONF), saved_pwr_conf, internal_ec); + std::error_code internal_ec; + set_config(saved_config, internal_ec); + write_u8_to_register(static_cast(Register::PWR_CTRL), saved_pwr_ctrl, internal_ec); + write_u8_to_register(static_cast(Register::PWR_CONF), saved_pwr_conf, internal_ec); }); bool saved_acc_en = (saved_pwr_ctrl & ACC_ENABLE) != 0; @@ -499,46 +505,52 @@ class Bmi270 : public espp::BasePeripheral(Register::PWR_CONF), 0x01, ec); - if (ec) return false; + if (ec) + return false; } if (!saved_acc_en) { set_bits_in_register(static_cast(Register::PWR_CTRL), ACC_ENABLE, ec); - if (ec) return false; + if (ec) + return false; std::this_thread::sleep_for(std::chrono::milliseconds(50)); } - write_u8_to_register(static_cast(Register::ACC_CONF), 0xB7, ec); // 50Hz, CIC_AVG8, Perf Opt - if (ec) return false; + write_u8_to_register(static_cast(Register::ACC_CONF), 0xB7, + ec); // 50Hz, CIC_AVG8, Perf Opt + if (ec) + return false; write_u8_to_register(static_cast(Register::ACC_RANGE), 0x00, ec); // 2G - if (ec) return false; + if (ec) + return false; // Read 128 samples int32_t x_sum = 0, y_sum = 0, z_sum = 0; for (int i = 0; i < 128; ++i) { - // Wait for data ready (20ms for 50Hz) - std::this_thread::sleep_for(std::chrono::milliseconds(20)); - auto raw = get_accelerometer_raw(ec); - if (ec) return false; - x_sum += raw.x; - y_sum += raw.y; - z_sum += raw.z; + // Wait for data ready (20ms for 50Hz) + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + auto raw = get_accelerometer_raw(ec); + if (ec) + return false; + x_sum += raw.x; + y_sum += raw.y; + z_sum += raw.z; } // Average - RawValue avg = { - static_cast(x_sum / 128), - static_cast(y_sum / 128), - static_cast(z_sum / 128) - }; + RawValue avg = {static_cast(x_sum / 128), static_cast(y_sum / 128), + static_cast(z_sum / 128)}; // Calculate target int16_t target_x = 0, target_y = 0, target_z = 0; int16_t ref_1g = 16384; int16_t target_g = (accel_g_value.sign == 1) ? -ref_1g : ref_1g; - if (accel_g_value.x) target_x = target_g; - if (accel_g_value.y) target_y = target_g; - if (accel_g_value.z) target_z = target_g; + if (accel_g_value.x) + target_x = target_g; + if (accel_g_value.y) + target_y = target_g; + if (accel_g_value.z) + target_z = target_g; // Calculate delta (offset) // Offset = Average - Target @@ -557,14 +569,17 @@ class Bmi270 : public espp::BasePeripheral(std::clamp(-(delta_z / 64), -128, 127)); // Write offset registers - uint8_t data[3] = {static_cast(off_x), static_cast(off_y), static_cast(off_z)}; + uint8_t data[3] = {static_cast(off_x), static_cast(off_y), + static_cast(off_z)}; write_many_to_register(static_cast(Register::ACC_OFF_COMP_0), data, 3, ec); - if (ec) return false; + if (ec) + return false; // Enable Accelerometer offset compensation // NV_CONF (0x70) bit 3 (BMI2_NV_ACC_OFFSET_MASK = 0x08) set_bits_in_register(static_cast(Register::NV_CONF), 0x08, ec); - if (ec) return false; + if (ec) + return false; logger_.info("Accel FOC completed (offsets: X={}, Y={}, Z={})", off_x, off_y, off_z); return true; @@ -580,16 +595,18 @@ class Bmi270 : public espp::BasePeripheral(Register::PWR_CTRL), ec); - if (ec) return false; + if (ec) + return false; uint8_t saved_pwr_conf = read_u8_from_register(static_cast(Register::PWR_CONF), ec); - if (ec) return false; + if (ec) + return false; // Use ScopeGuard to ensure state is restored on all exit paths auto state_guard = make_scope_guard([&]() { - std::error_code internal_ec; - set_config(saved_config, internal_ec); - write_u8_to_register(static_cast(Register::PWR_CTRL), saved_pwr_ctrl, internal_ec); - write_u8_to_register(static_cast(Register::PWR_CONF), saved_pwr_conf, internal_ec); + std::error_code internal_ec; + set_config(saved_config, internal_ec); + write_u8_to_register(static_cast(Register::PWR_CTRL), saved_pwr_ctrl, internal_ec); + write_u8_to_register(static_cast(Register::PWR_CONF), saved_pwr_conf, internal_ec); }); bool saved_gyr_en = (saved_pwr_ctrl & GYR_ENABLE) != 0; @@ -598,41 +615,45 @@ class Bmi270 : public espp::BasePeripheral(Register::PWR_CONF), 0x01, ec); - if (ec) return false; + if (ec) + return false; } if (!saved_gyr_en) { set_bits_in_register(static_cast(Register::PWR_CTRL), GYR_ENABLE, ec); - if (ec) return false; + if (ec) + return false; std::this_thread::sleep_for(std::chrono::milliseconds(80)); } // Configure FOC (25Hz, 2000dps) write_u8_to_register(static_cast(Register::GYR_CONF), 0xB6, ec); - if (ec) return false; + if (ec) + return false; write_u8_to_register(static_cast(Register::GYR_RANGE), 0x00, ec); - if (ec) return false; + if (ec) + return false; // Accumulate 128 samples int32_t x_sum = 0, y_sum = 0, z_sum = 0; for (int i = 0; i < 128; ++i) { - std::this_thread::sleep_for(std::chrono::milliseconds(45)); - auto raw = get_gyroscope_raw(ec); - if (ec) return false; - x_sum += raw.x; - y_sum += raw.y; - z_sum += raw.z; + std::this_thread::sleep_for(std::chrono::milliseconds(45)); + auto raw = get_gyroscope_raw(ec); + if (ec) + return false; + x_sum += raw.x; + y_sum += raw.y; + z_sum += raw.z; } // Average and saturate (10-bit) - auto saturate = [](int16_t val) -> int16_t { - return std::clamp(val, -512, 511); - }; + auto saturate = [](int16_t val) -> int16_t { return std::clamp(val, -512, 511); }; int16_t off_x = saturate(-(x_sum / 128)); int16_t off_y = saturate(-(y_sum / 128)); int16_t off_z = saturate(-(z_sum / 128)); uint8_t current_reg = read_u8_from_register(static_cast(Register::GYR_OFF_COMP_6), ec); - if (ec) return false; + if (ec) + return false; bool gain_enabled = (current_reg & 0x80) != 0; // Write offset and enable compensation (Bit 6) @@ -640,14 +661,17 @@ class Bmi270 : public espp::BasePeripheral(off_x & 0xFF); data[1] = static_cast(off_y & 0xFF); data[2] = static_cast(off_z & 0xFF); - data[3] = static_cast(((off_x >> 8) & 0x03) | ((off_y >> 6) & 0x0C) | ((off_z >> 4) & 0x30)) | 0x40; + data[3] = static_cast(((off_x >> 8) & 0x03) | ((off_y >> 6) & 0x0C) | + ((off_z >> 4) & 0x30)) | + 0x40; if (gain_enabled) { - data[3] |= 0x80; + data[3] |= 0x80; } write_many_to_register(static_cast(Register::GYR_OFF_COMP_3), data, 4, ec); - if (ec) return false; + if (ec) + return false; logger_.info("Gyro FOC completed (offsets: X={}, Y={}, Z={})", off_x, off_y, off_z); return true; @@ -662,122 +686,143 @@ class Bmi270 : public espp::BasePeripheral(Register::PWR_CONF), ec); - if (ec) return false; + if (ec) + return false; uint8_t saved_pwr_ctrl = read_u8_from_register(static_cast(Register::PWR_CTRL), ec); - if (ec) return false; - uint8_t saved_fifo_conf_1 = read_u8_from_register(static_cast(Register::FIFO_CONFIG_1), ec); - if (ec) return false; + if (ec) + return false; + uint8_t saved_fifo_conf_1 = + read_u8_from_register(static_cast(Register::FIFO_CONFIG_1), ec); + if (ec) + return false; // Use ScopeGuard to ensure state is restored on all exit paths auto state_guard = make_scope_guard([&]() { - std::error_code internal_ec; - set_config(imu_config_, internal_ec); - write_u8_to_register(static_cast(Register::PWR_CTRL), saved_pwr_ctrl, internal_ec); - write_u8_to_register(static_cast(Register::PWR_CONF), saved_pwr_conf, internal_ec); - write_u8_to_register(static_cast(Register::FIFO_CONFIG_1), saved_fifo_conf_1, internal_ec); + std::error_code internal_ec; + set_config(imu_config_, internal_ec); + write_u8_to_register(static_cast(Register::PWR_CTRL), saved_pwr_ctrl, internal_ec); + write_u8_to_register(static_cast(Register::PWR_CONF), saved_pwr_conf, internal_ec); + write_u8_to_register(static_cast(Register::FIFO_CONFIG_1), saved_fifo_conf_1, + internal_ec); }); bool saved_aps = (saved_pwr_conf & 0x01) != 0; if (saved_aps) { clear_bits_in_register(static_cast(Register::PWR_CONF), 0x01, ec); - if (ec) return false; + if (ec) + return false; } // Setup: Disable Gyro/FIFO, Enable Accel enable_gyroscope(false, ec); - if (ec) return false; + if (ec) + return false; clear_bits_in_register(static_cast(Register::FIFO_CONFIG_1), 0xE0, ec); - if (ec) return false; + if (ec) + return false; enable_accelerometer(true, ec); - if (ec) return false; + if (ec) + return false; // Initialize CRT: Clear running bit first, then set both CRT mode and running write_u8_to_register(static_cast(Register::GYR_CRT_CONF), 0x00, ec); - if (ec) return false; + if (ec) + return false; std::this_thread::sleep_for(std::chrono::milliseconds(5)); // Set CRT mode (bit 0) set_bits_in_register(static_cast(Register::GYR_CRT_CONF), 0x01, ec); - if (ec) return false; + if (ec) + return false; std::this_thread::sleep_for(std::chrono::milliseconds(5)); // Trigger CRT by setting running bit (bit 2) set_bits_in_register(static_cast(Register::GYR_CRT_CONF), 0x04, ec); - if (ec) return false; + if (ec) + return false; std::this_thread::sleep_for(std::chrono::milliseconds(20)); // Read initial state before CMD uint8_t gyr_crt_conf = read_u8_from_register(static_cast(Register::GYR_CRT_CONF), ec); - if (ec) return false; + if (ec) + return false; bool rdy_for_dl_initial = (gyr_crt_conf & 0x08) != 0; logger_.debug("CRT: Setup complete, state: 0x{:02X}", gyr_crt_conf); // Send command to trigger test (bit 1 of CMD register) write_u8_to_register(static_cast(Register::CMD), 0x02, ec); - if (ec) return false; + if (ec) + return false; // Wait for bit 3 to toggle/change bool rdy_toggled = false; for (int i = 0; i < 200; ++i) { - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - uint8_t reg = read_u8_from_register(static_cast(Register::GYR_CRT_CONF), ec); - if (ec) return false; - - bool rdy_for_dl_current = (reg & 0x08) != 0; - - // Check if bit 3 changed from initial state - if (rdy_for_dl_current != rdy_for_dl_initial) { - rdy_toggled = true; - logger_.debug("CRT: Ready bit transitioned at {}ms", i * 10); - break; - } + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + uint8_t reg = read_u8_from_register(static_cast(Register::GYR_CRT_CONF), ec); + if (ec) + return false; + + bool rdy_for_dl_current = (reg & 0x08) != 0; + + // Check if bit 3 changed from initial state + if (rdy_for_dl_current != rdy_for_dl_initial) { + rdy_toggled = true; + logger_.debug("CRT: Ready bit transitioned at {}ms", i * 10); + break; + } } if (!rdy_toggled) { - logger_.debug("CRT: Ready bit transition not detected (device variant)"); + logger_.debug("CRT: Ready bit transition not detected (device variant)"); } // Upload CRT configuration image - uint8_t addr_data[2] = {static_cast((0x1800 / 2) & 0x0F), static_cast((0x1800 / 2) >> 4)}; + uint8_t addr_data[2] = {static_cast((0x1800 / 2) & 0x0F), + static_cast((0x1800 / 2) >> 4)}; write_many_to_register(static_cast(Register::INIT_ADDR_0), addr_data, 2, ec); - if (ec) return false; + if (ec) + return false; if (config_file_size < 0x1800 + 2048) { - logger_.error("Config file too small for CRT"); - return false; + logger_.error("Config file too small for CRT"); + return false; } - const uint8_t* crt_data = config_file + 0x1800; + const uint8_t *crt_data = config_file + 0x1800; for (size_t offset = 0; offset < 2048; offset += burst_write_size_) { - size_t to_write = std::min(static_cast(burst_write_size_), 2048 - offset); - write_many_to_register(static_cast(Register::INIT_DATA), crt_data + offset, to_write, ec); - if (ec) return false; + size_t to_write = std::min(static_cast(burst_write_size_), 2048 - offset); + write_many_to_register(static_cast(Register::INIT_DATA), crt_data + offset, to_write, + ec); + if (ec) + return false; } // Wait for completion: Running flag (bit 2) clears when CRT finishes bool completed = false; for (int i = 0; i < 250; ++i) { - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - uint8_t reg = read_u8_from_register(static_cast(Register::GYR_CRT_CONF), ec); - if (ec) return false; - if ((reg & 0x04) == 0) { - completed = true; - break; - } + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + uint8_t reg = read_u8_from_register(static_cast(Register::GYR_CRT_CONF), ec); + if (ec) + return false; + if ((reg & 0x04) == 0) { + completed = true; + break; + } } if (!completed) { - logger_.error("CRT: Timeout (2.5s)"); - return false; + logger_.error("CRT: Timeout (2.5s)"); + return false; } // Verify result uint8_t status_reg = read_u8_from_register(static_cast(Register::GYR_USR_GAIN_0), ec); - if (ec) return false; + if (ec) + return false; uint8_t status = (status_reg & 0x38) >> 3; if (status != 0x00) { - logger_.error("CRT: Failed with status: {}", static_cast(status)); - return false; + logger_.error("CRT: Failed with status: {}", static_cast(status)); + return false; } logger_.info("CRT: Completed successfully"); @@ -788,9 +833,9 @@ class Bmi270 : public espp::BasePeripheral lock(base_mutex_); - set_bits_in_register(static_cast(Register::PWR_CONF), 0x02, ec); - return !ec; + std::lock_guard lock(base_mutex_); + set_bits_in_register(static_cast(Register::PWR_CONF), 0x02, ec); + return !ec; } /// Perform the gyroscope self-test procedure. @@ -798,71 +843,90 @@ class Bmi270 : public espp::BasePeripheral lock(base_mutex_); - - uint8_t saved_pwr_conf = read_u8_from_register(static_cast(Register::PWR_CONF), ec); - if (ec) return false; - uint8_t saved_pwr_ctrl = read_u8_from_register(static_cast(Register::PWR_CTRL), ec); - if (ec) return false; - uint8_t saved_fifo_conf_1 = read_u8_from_register(static_cast(Register::FIFO_CONFIG_1), ec); - if (ec) return false; - - // Use ScopeGuard to ensure state is restored on all exit paths - auto state_guard = make_scope_guard([&]() { - std::error_code internal_ec; - set_config(imu_config_, internal_ec); - write_u8_to_register(static_cast(Register::PWR_CTRL), saved_pwr_ctrl, internal_ec); - write_u8_to_register(static_cast(Register::PWR_CONF), saved_pwr_conf, internal_ec); - write_u8_to_register(static_cast(Register::FIFO_CONFIG_1), saved_fifo_conf_1, internal_ec); - }); - - bool saved_aps = (saved_pwr_conf & 0x01) != 0; - - if (saved_aps) { - clear_bits_in_register(static_cast(Register::PWR_CONF), 0x01, ec); - if (ec) return false; - } + std::lock_guard lock(base_mutex_); - enable_gyroscope(false, ec); - if (ec) return false; - clear_bits_in_register(static_cast(Register::FIFO_CONFIG_1), 0xE0, ec); - if (ec) return false; - enable_accelerometer(true, ec); - if (ec) return false; - - // Set Running (Bit 2), Clear Selection (Bit 0 for Self Test) - set_bits_in_register(static_cast(Register::GYR_CRT_CONF), 0x04, ec); - if (ec) return false; - clear_bits_in_register(static_cast(Register::GYR_CRT_CONF), 0x01, ec); - if (ec) return false; - - write_u8_to_register(static_cast(Register::CMD), 0x02, ec); - if (ec) return false; - - bool completed = false; - for (int i = 0; i < 200; ++i) { - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - uint8_t reg = read_u8_from_register(static_cast(Register::GYR_CRT_CONF), ec); - if (ec) return false; - if ((reg & 0x04) == 0) { completed = true; break; } - } + uint8_t saved_pwr_conf = read_u8_from_register(static_cast(Register::PWR_CONF), ec); + if (ec) + return false; + uint8_t saved_pwr_ctrl = read_u8_from_register(static_cast(Register::PWR_CTRL), ec); + if (ec) + return false; + uint8_t saved_fifo_conf_1 = + read_u8_from_register(static_cast(Register::FIFO_CONFIG_1), ec); + if (ec) + return false; - if (!completed) { - logger_.error("Gyro Self Test timeout"); - return false; - } + // Use ScopeGuard to ensure state is restored on all exit paths + auto state_guard = make_scope_guard([&]() { + std::error_code internal_ec; + set_config(imu_config_, internal_ec); + write_u8_to_register(static_cast(Register::PWR_CTRL), saved_pwr_ctrl, internal_ec); + write_u8_to_register(static_cast(Register::PWR_CONF), saved_pwr_conf, internal_ec); + write_u8_to_register(static_cast(Register::FIFO_CONFIG_1), saved_fifo_conf_1, + internal_ec); + }); + + bool saved_aps = (saved_pwr_conf & 0x01) != 0; + + if (saved_aps) { + clear_bits_in_register(static_cast(Register::PWR_CONF), 0x01, ec); + if (ec) + return false; + } + + enable_gyroscope(false, ec); + if (ec) + return false; + clear_bits_in_register(static_cast(Register::FIFO_CONFIG_1), 0xE0, ec); + if (ec) + return false; + enable_accelerometer(true, ec); + if (ec) + return false; + + // Set Running (Bit 2), Clear Selection (Bit 0 for Self Test) + set_bits_in_register(static_cast(Register::GYR_CRT_CONF), 0x04, ec); + if (ec) + return false; + clear_bits_in_register(static_cast(Register::GYR_CRT_CONF), 0x01, ec); + if (ec) + return false; - uint8_t st_result = read_u8_from_register(static_cast(Register::GYR_SELF_TEST_AXES), ec); - if (ec) return false; + write_u8_to_register(static_cast(Register::CMD), 0x02, ec); + if (ec) + return false; - bool success = (st_result & 0x01) && (st_result & 0x02) && (st_result & 0x04) && (st_result & 0x08); - if (success) { - logger_.info("Gyro Self Test passed"); - } else { - logger_.error("Gyro Self Test failed: 0x{:02X}", st_result); + bool completed = false; + for (int i = 0; i < 200; ++i) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + uint8_t reg = read_u8_from_register(static_cast(Register::GYR_CRT_CONF), ec); + if (ec) + return false; + if ((reg & 0x04) == 0) { + completed = true; + break; } + } + + if (!completed) { + logger_.error("Gyro Self Test timeout"); + return false; + } + + uint8_t st_result = + read_u8_from_register(static_cast(Register::GYR_SELF_TEST_AXES), ec); + if (ec) + return false; + + bool success = + (st_result & 0x01) && (st_result & 0x02) && (st_result & 0x04) && (st_result & 0x08); + if (success) { + logger_.info("Gyro Self Test passed"); + } else { + logger_.error("Gyro Self Test failed: 0x{:02X}", st_result); + } - return success; + return success; } /// Get Accelerometer Offsets @@ -876,7 +940,8 @@ class Bmi270 : public espp::BasePeripheral(Register::ACC_OFF_COMP_0), data, 3, ec); - if (ec) return false; + if (ec) + return false; x = static_cast(data[0]); y = static_cast(data[1]); @@ -902,15 +967,15 @@ class Bmi270 : public espp::BasePeripheral lock(base_mutex_); - // NV_CONF (0x70) Bit 3 controls Accel Offset Enable - static constexpr uint8_t ACC_OFFSET_EN_MASK = 0x08; - if (enable) { - set_bits_in_register(static_cast(Register::NV_CONF), ACC_OFFSET_EN_MASK, ec); - } else { - clear_bits_in_register(static_cast(Register::NV_CONF), ACC_OFFSET_EN_MASK, ec); - } - return !ec; + std::lock_guard lock(base_mutex_); + // NV_CONF (0x70) Bit 3 controls Accel Offset Enable + static constexpr uint8_t ACC_OFFSET_EN_MASK = 0x08; + if (enable) { + set_bits_in_register(static_cast(Register::NV_CONF), ACC_OFFSET_EN_MASK, ec); + } else { + clear_bits_in_register(static_cast(Register::NV_CONF), ACC_OFFSET_EN_MASK, ec); + } + return !ec; } /// Get Gyroscope Offsets @@ -924,7 +989,8 @@ class Bmi270 : public espp::BasePeripheral(Register::GYR_OFF_COMP_3), data, 4, ec); - if (ec) return false; + if (ec) + return false; // LSBs x = static_cast(data[0]); @@ -937,9 +1003,12 @@ class Bmi270 : public espp::BasePeripheral((data[3] & 0x30) << 4); // Sign extension for 10-bit values - if (x & 0x0200) x |= 0xFC00; - if (y & 0x0200) y |= 0xFC00; - if (z & 0x0200) z |= 0xFC00; + if (x & 0x0200) + x |= 0xFC00; + if (y & 0x0200) + y |= 0xFC00; + if (z & 0x0200) + z |= 0xFC00; return true; } @@ -962,7 +1031,8 @@ class Bmi270 : public espp::BasePeripheral(Register::GYR_OFF_COMP_6), ec); - if (ec) return false; + if (ec) + return false; uint8_t preserved_bits = current_reg & 0xC0; uint8_t data[4]; @@ -970,7 +1040,7 @@ class Bmi270 : public espp::BasePeripheral(y & 0xFF); data[2] = static_cast(z & 0xFF); data[3] = static_cast(((x >> 8) & 0x03) | ((y >> 6) & 0x0C) | ((z >> 4) & 0x30)); - + data[3] |= preserved_bits; write_many_to_register(static_cast(Register::GYR_OFF_COMP_3), data, 4, ec); @@ -982,15 +1052,16 @@ class Bmi270 : public espp::BasePeripheral lock(base_mutex_); - // GYR_OFF_COMP_6 (0x77) Bit 6 - static constexpr uint8_t GYR_OFFSET_EN_MASK = 0x40; - if (enable) { - set_bits_in_register(static_cast(Register::GYR_OFF_COMP_6), GYR_OFFSET_EN_MASK, ec); - } else { - clear_bits_in_register(static_cast(Register::GYR_OFF_COMP_6), GYR_OFFSET_EN_MASK, ec); - } - return !ec; + std::lock_guard lock(base_mutex_); + // GYR_OFF_COMP_6 (0x77) Bit 6 + static constexpr uint8_t GYR_OFFSET_EN_MASK = 0x40; + if (enable) { + set_bits_in_register(static_cast(Register::GYR_OFF_COMP_6), GYR_OFFSET_EN_MASK, ec); + } else { + clear_bits_in_register(static_cast(Register::GYR_OFF_COMP_6), GYR_OFFSET_EN_MASK, + ec); + } + return !ec; } /// Enable/Disable Gyroscope Gain Compensation @@ -998,14 +1069,14 @@ class Bmi270 : public espp::BasePeripheral lock(base_mutex_); - static constexpr uint8_t GYR_GAIN_EN_MASK = 0x80; - if (enable) { - set_bits_in_register(static_cast(Register::GYR_OFF_COMP_6), GYR_GAIN_EN_MASK, ec); - } else { - clear_bits_in_register(static_cast(Register::GYR_OFF_COMP_6), GYR_GAIN_EN_MASK, ec); - } - return !ec; + std::lock_guard lock(base_mutex_); + static constexpr uint8_t GYR_GAIN_EN_MASK = 0x80; + if (enable) { + set_bits_in_register(static_cast(Register::GYR_OFF_COMP_6), GYR_GAIN_EN_MASK, ec); + } else { + clear_bits_in_register(static_cast(Register::GYR_OFF_COMP_6), GYR_GAIN_EN_MASK, ec); + } + return !ec; } protected: @@ -1106,7 +1177,7 @@ class Bmi270 : public espp::BasePeripheral class ScopeGuard { public: - explicit ScopeGuard(F &&f) : f_(std::forward(f)), active_(true) {} + explicit ScopeGuard(F &&f) + : f_(std::forward(f)) + , active_(true) {} ~ScopeGuard() { if (active_) f_(); @@ -254,7 +256,9 @@ template class ScopeGuard { ScopeGuard(const ScopeGuard &) = delete; ScopeGuard &operator=(const ScopeGuard &) = delete; - ScopeGuard(ScopeGuard &&other) : f_(std::move(other.f_)), active_(other.active_) { + ScopeGuard(ScopeGuard &&other) + : f_(std::move(other.f_)) + , active_(other.active_) { other.active_ = false; } diff --git a/components/i2c/include/i2c_format_helpers.hpp b/components/i2c/include/i2c_format_helpers.hpp index a719427b0..56104a39a 100644 --- a/components/i2c/include/i2c_format_helpers.hpp +++ b/components/i2c/include/i2c_format_helpers.hpp @@ -18,10 +18,10 @@ template <> struct fmt::formatter { switch (p) { case I2C_NUM_0: return fmt::format_to(ctx.out(), "I2C_NUM_0"); - #ifdef I2C_NUM_1 - case I2C_NUM_1: - return fmt::format_to(ctx.out(), "I2C_NUM_1"); - #endif +#ifdef I2C_NUM_1 + case I2C_NUM_1: + return fmt::format_to(ctx.out(), "I2C_NUM_1"); +#endif default: return fmt::format_to(ctx.out(), "Unknown"); } diff --git a/components/led_strip/include/led_strip.hpp b/components/led_strip/include/led_strip.hpp old mode 100755 new mode 100644 index dd0e6c4ff..ef1bfc468 --- a/components/led_strip/include/led_strip.hpp +++ b/components/led_strip/include/led_strip.hpp @@ -69,13 +69,15 @@ class LedStrip : public BaseComponent { write_fn write; ///< Function to write data to the strip bool send_brightness{true}; ///< Whether to use the brightness value for the LEDs ByteOrder byte_order{ByteOrder::RGB}; ///< Byte order for the LEDs - std::span start_frame{}; ///< Start frame for the strip. Optional - will be sent - ///< before the first LED if not empty. Can be initialized - ///< with std::array{0x00, 0x00, ...} for compatibility. - std::span end_frame{}; ///< End frame for the strip. Optional - will be sent after - ///< the last LED if not empty. Can be initialized - ///< with std::array{0x00, 0x00, ...} for compatibility. - bool use_dma{false}; ///< Whether to use DMA-capable memory allocation + std::span + start_frame{}; ///< Start frame for the strip. Optional - will be sent + ///< before the first LED if not empty. Can be initialized + ///< with std::array{0x00, 0x00, ...} for compatibility. + std::span + end_frame{}; ///< End frame for the strip. Optional - will be sent after + ///< the last LED if not empty. Can be initialized + ///< with std::array{0x00, 0x00, ...} for compatibility. + bool use_dma{false}; ///< Whether to use DMA-capable memory allocation uint32_t dma_allocation_flags{ MALLOC_CAP_DMA}; ///< DMA allocation flags (if use_dma is true). Defaults to MALLOC_CAP_DMA. Logger::Verbosity log_level; ///< Log level for this class @@ -97,7 +99,8 @@ class LedStrip : public BaseComponent { // set the color data size pixel_size_ = send_brightness_ ? 4 : 3; - size_t data_size = num_leds_ * pixel_size_ + config.start_frame.size() + config.end_frame.size(); + size_t data_size = + num_leds_ * pixel_size_ + config.start_frame.size() + config.end_frame.size(); // Allocate memory based on DMA preference uint8_t *raw_data = nullptr; @@ -159,13 +162,13 @@ class LedStrip : public BaseComponent { /// \brief Move constructor /// \param other The other LedStrip to move from LedStrip(LedStrip &&other) noexcept - // cppcheck-suppress-begin accessMoved + // cppcheck-suppress-begin accessMoved : BaseComponent(std::move(other)) , num_leds_(other.num_leds_) , send_brightness_(other.send_brightness_) , byte_order_(other.byte_order_) , pixel_size_(other.pixel_size_) - , start_offset_(other.start_offset_) + , start_offset_(other.start_offset_) , end_offset_(other.end_offset_) , data_(other.data_) , write_(std::move(other.write_)) @@ -188,7 +191,7 @@ class LedStrip : public BaseComponent { BaseComponent::operator=(std::move(other)); // cppcheck-suppress-begin accessMoved - + // Move members from other num_leds_ = other.num_leds_; send_brightness_ = other.send_brightness_; diff --git a/components/logger/include/logger.hpp b/components/logger/include/logger.hpp index f0394aa5f..1132721a4 100644 --- a/components/logger/include/logger.hpp +++ b/components/logger/include/logger.hpp @@ -138,7 +138,7 @@ rate limit. @note Only calls that have _rate_limited suffixed will be rate limit , rate_limit_(std::move(other.rate_limit_)) , last_print_(std::move(other.last_print_)) , include_time_(other.include_time_.load()) - , level_(other.level_.load()) { } + , level_(other.level_.load()) {} /** * @brief Copy assignment operator