80#ifndef COMPONENT_HANDLER_BNO08X_HANDLER_H_
81#define COMPONENT_HANDLER_BNO08X_HANDLER_H_
89#include "core/hf-core-drivers/external/hf-bno08x-driver/inc/bno08x.hpp"
90#include "core/hf-core-drivers/external/hf-bno08x-driver/inc/bno08x_comm_interface.hpp"
91#include "base/BaseI2c.h"
92#include "base/BaseSpi.h"
93#include "base/BaseGpio.h"
141 default:
return "Unknown error";
164 BaseGpio* reset_gpio =
nullptr,
165 BaseGpio* int_gpio =
nullptr) noexcept
174 bool Open() noexcept;
176 void Close() noexcept;
178 int Write(const uint8_t* data, uint32_t length) noexcept;
180 int Read(uint8_t* data, uint32_t length) noexcept;
184 void Delay(uint32_t ms) noexcept;
189 void GpioSet(bno08x::CtrlPin pin, bno08x::GpioSignal signal) noexcept;
212 BaseGpio* reset_gpio =
nullptr,
213 BaseGpio* int_gpio =
nullptr,
214 BaseGpio* wake_gpio =
nullptr) noexcept
223 bool Open() noexcept;
225 void Close() noexcept;
227 int Write(const uint8_t* data, uint32_t length) noexcept;
229 int Read(uint8_t* data, uint32_t length) noexcept;
233 void Delay(uint32_t ms) noexcept;
238 void GpioSet(bno08x::CtrlPin pin, bno08x::GpioSignal signal) noexcept;
244 BaseGpio* wake_gpio_;
261 virtual
bool Begin() noexcept = 0;
262 virtual
void Update() noexcept = 0;
263 virtual
bool EnableSensor(BNO085Sensor sensor, uint32_t interval_ms,
264 float sensitivity) noexcept = 0;
265 virtual
bool DisableSensor(BNO085Sensor sensor) noexcept = 0;
266 virtual
void SetCallback(SensorCallback cb) noexcept = 0;
267 virtual
bool HasNewData(BNO085Sensor sensor) const noexcept = 0;
268 virtual SensorEvent GetLatest(BNO085Sensor sensor) const noexcept = 0;
269 virtual
int GetLastError() const noexcept = 0;
270 virtual
void HardwareReset(uint32_t lowMs) noexcept = 0;
271 virtual
void SetBootPin(
bool state) noexcept = 0;
272 virtual
void SetWakePin(
bool state) noexcept = 0;
273 virtual
void SelectInterface(BNO085Interface iface) noexcept = 0;
286template <typename CommType>
293 : comm_(std::move(comm)), driver_(comm_) {}
295 bool Begin() noexcept
override {
return driver_.Begin(); }
296 void Update() noexcept
override { driver_.Update(); }
299 float sensitivity)
noexcept override {
300 return driver_.EnableSensor(sensor, interval_ms, sensitivity);
304 return driver_.DisableSensor(sensor);
308 driver_.SetCallback(std::move(cb));
311 bool HasNewData(BNO085Sensor sensor)
const noexcept override {
312 return driver_.HasNewData(sensor);
315 SensorEvent
GetLatest(BNO085Sensor sensor)
const noexcept override {
316 return driver_.GetLatest(sensor);
320 return driver_.GetLastError();
324 driver_.HardwareReset(lowMs);
328 driver_.SetBootPin(state);
332 driver_.SetWakePin(state);
336 driver_.SelectInterface(iface);
340 return comm_.GetInterfaceType();
360 uint64_t timestamp_us{0};
373 uint64_t timestamp_us{0};
385 uint64_t timestamp_us{0};
400 uint64_t timestamp_us{0};
408 bool tap_detected{
false};
409 bool double_tap{
false};
410 uint8_t tap_direction{0};
411 uint32_t step_count{0};
412 bool step_detected{
false};
413 bool shake_detected{
false};
414 bool pickup_detected{
false};
415 bool significant_motion{
false};
416 uint8_t stability_class{0};
417 uint64_t timestamp_us{0};
425 uint8_t accelerometer_accuracy{0};
426 uint8_t gyroscope_accuracy{0};
427 uint8_t magnetometer_accuracy{0};
428 uint8_t rotation_accuracy{0};
429 bool fully_calibrated{
false};
440 bool enable_accelerometer{
true};
441 bool enable_gyroscope{
true};
442 bool enable_magnetometer{
true};
443 bool enable_rotation_vector{
true};
444 bool enable_linear_acceleration{
false};
445 bool enable_gravity{
false};
446 bool enable_game_rotation{
false};
449 bool enable_tap_detector{
false};
450 bool enable_step_counter{
false};
451 bool enable_step_detector{
false};
452 bool enable_shake_detector{
false};
453 bool enable_pickup_detector{
false};
454 bool enable_significant_motion{
false};
455 bool enable_stability_classifier{
false};
458 uint32_t accelerometer_interval_ms{50};
459 uint32_t gyroscope_interval_ms{50};
460 uint32_t magnetometer_interval_ms{100};
461 uint32_t rotation_interval_ms{50};
462 uint32_t linear_accel_interval_ms{50};
463 uint32_t gravity_interval_ms{100};
464 uint32_t game_rotation_interval_ms{50};
499 BaseGpio* reset_gpio =
nullptr,
500 BaseGpio* int_gpio =
nullptr)
noexcept;
512 BaseGpio* reset_gpio =
nullptr,
513 BaseGpio* int_gpio =
nullptr,
514 BaseGpio* wake_gpio =
nullptr)
noexcept;
544 bool EnsureInitialized() noexcept;
556 bool IsInitialized() const noexcept;
584 void SetSensorCallback(SensorCallback callback) noexcept;
589 void ClearSensorCallback() noexcept;
607 BNO085Interface GetInterfaceType() const noexcept;
640 template <
typename Fn>
641 auto visitDriver(Fn&& fn)
noexcept ->
decltype(fn(std::declval<IBno08xDriverOps&>())) {
642 using ReturnType =
decltype(fn(std::declval<IBno08xDriverOps&>()));
643 MutexLockGuard lock(handler_mutex_);
644 if (!lock.IsLocked() || !ensureInitializedLocked() || !driver_ops_) {
645 if constexpr (std::is_void_v<ReturnType>) {
651 return fn(*driver_ops_);
664 int GetLastDriverError() const noexcept;
670 const
char* GetDescription() const noexcept;
681 void DumpDiagnostics() const noexcept;
690 mutable RtosMutex handler_mutex_;
691 bool initialized_{
false};
695 char description_[64]{};
704 bool ensureInitializedLocked() noexcept;
709 bool applyConfigLocked() noexcept;
714 static
Bno08xError mapDriverError(
int sh2_error) noexcept;
732 BaseGpio* reset_gpio =
nullptr,
733 BaseGpio* int_gpio =
nullptr)
noexcept {
734 return std::make_unique<Bno08xHandler>(i2c_device, config, reset_gpio, int_gpio);
749 BaseGpio* reset_gpio =
nullptr,
750 BaseGpio* int_gpio =
nullptr,
751 BaseGpio* wake_gpio =
nullptr)
noexcept {
752 return std::make_unique<Bno08xHandler>(spi_device, config, reset_gpio, int_gpio, wake_gpio);
std::unique_ptr< Bno08xHandler > CreateBno08xHandlerI2c(BaseI2c &i2c_device, const Bno08xConfig &config=Bno08xConfig{}, BaseGpio *reset_gpio=nullptr, BaseGpio *int_gpio=nullptr) noexcept
Create BNO08x handler with I2C interface.
Definition Bno08xHandler.h:729
std::unique_ptr< Bno08xHandler > CreateBno08xHandlerSpi(BaseSpi &spi_device, const Bno08xConfig &config=Bno08xConfig{}, BaseGpio *reset_gpio=nullptr, BaseGpio *int_gpio=nullptr, BaseGpio *wake_gpio=nullptr) noexcept
Create BNO08x handler with SPI interface.
Definition Bno08xHandler.h:746
constexpr const char * Bno08xErrorToString(Bno08xError error) noexcept
Convert Bno08xError to string for debugging.
Definition Bno08xHandler.h:124
Bno08xError
BNO08x handler error codes for consistent error reporting.
Definition Bno08xHandler.h:103
@ NOT_INITIALIZED
Handler or sensor not initialized.
@ TIMEOUT
Operation timed out.
@ MUTEX_LOCK_FAILED
Could not acquire handler mutex.
@ INVALID_INTERFACE
Wrong interface type for operation.
@ COMMUNICATION_FAILED
I2C/SPI or SH-2 I/O error.
@ UNSUPPORTED_OPERATION
Feature not supported.
@ HARDWARE_ERROR
GPIO or hardware fault.
@ CALIBRATION_FAILED
Calibration request failed.
@ FIRMWARE_UPDATE_FAILED
DFU transfer failed.
@ SUCCESS
Operation completed successfully.
@ SENSOR_NOT_ENABLED
Sensor not enabled for read.
@ DATA_NOT_AVAILABLE
No new data for requested sensor.
@ SENSOR_NOT_RESPONDING
Hub/sensor did not respond.
@ INITIALIZATION_FAILED
Begin() or config apply failed.
@ INVALID_PARAMETER
Bad argument (e.g. invalid sensor)
Concrete type-erased driver wrapper.
Definition Bno08xHandler.h:287
bool HasNewData(BNO085Sensor sensor) const noexcept override
Definition Bno08xHandler.h:311
void SetCallback(SensorCallback cb) noexcept override
Definition Bno08xHandler.h:307
void HardwareReset(uint32_t lowMs) noexcept override
Definition Bno08xHandler.h:323
BNO085< CommType > driver_
BNO085 driver instance (mutable: GetLatest clears internal flag)
Definition Bno08xHandler.h:345
void SetWakePin(bool state) noexcept override
Definition Bno08xHandler.h:331
void Update() noexcept override
Definition Bno08xHandler.h:296
void SetBootPin(bool state) noexcept override
Definition Bno08xHandler.h:327
SensorEvent GetLatest(BNO085Sensor sensor) const noexcept override
Definition Bno08xHandler.h:315
bool EnableSensor(BNO085Sensor sensor, uint32_t interval_ms, float sensitivity) noexcept override
Definition Bno08xHandler.h:298
bool DisableSensor(BNO085Sensor sensor) noexcept override
Definition Bno08xHandler.h:303
Bno08xDriverImpl(CommType &&comm) noexcept
Construct with a pre-built comm adapter (moved in).
Definition Bno08xHandler.h:292
int GetLastError() const noexcept override
Definition Bno08xHandler.h:319
bool Begin() noexcept override
Definition Bno08xHandler.h:295
void SelectInterface(BNO085Interface iface) noexcept override
Definition Bno08xHandler.h:335
CommType comm_
CRTP comm adapter (must outlive driver_)
Definition Bno08xHandler.h:344
BNO085Interface GetInterfaceType() noexcept override
Definition Bno08xHandler.h:339
Unified handler for BNO08x IMU sensor family.
Definition Bno08xHandler.h:484
~Bno08xHandler() noexcept=default
const IBno08xDriverOps * GetDriver() const noexcept
Definition Bno08xHandler.h:634
BNO085Interface interface_type_
I2C or SPI.
Definition Bno08xHandler.h:693
SensorCallback user_callback_
User's sensor callback.
Definition Bno08xHandler.h:694
auto visitDriver(Fn &&fn) noexcept -> decltype(fn(std::declval< IBno08xDriverOps & >()))
Visit the underlying IMU driver under handler mutex protection.
Definition Bno08xHandler.h:641
I2C CRTP communication adapter for BNO08x.
Definition Bno08xHandler.h:155
uint32_t GetTimeUs() noexcept
Definition Bno08xHandler.cpp:109
BaseGpio * int_gpio_
Definition Bno08xHandler.h:194
void GpioSet(bno08x::CtrlPin pin, bno08x::GpioSignal signal) noexcept
Definition Bno08xHandler.cpp:113
BaseI2c & i2c_
Definition Bno08xHandler.h:192
BaseGpio * reset_gpio_
Definition Bno08xHandler.h:193
void Close() noexcept
Definition Bno08xHandler.cpp:60
void Delay(uint32_t ms) noexcept
Definition Bno08xHandler.cpp:105
bool DataAvailable() noexcept
Definition Bno08xHandler.cpp:94
int Read(uint8_t *data, uint32_t length) noexcept
Definition Bno08xHandler.cpp:74
bool Open() noexcept
Definition Bno08xHandler.cpp:37
BNO085Interface GetInterfaceType() noexcept
Definition Bno08xHandler.h:171
int Write(const uint8_t *data, uint32_t length) noexcept
Definition Bno08xHandler.cpp:64
HalI2cBno08xComm(BaseI2c &i2c, BaseGpio *reset_gpio=nullptr, BaseGpio *int_gpio=nullptr) noexcept
Construct I2C adapter.
Definition Bno08xHandler.h:163
SPI CRTP communication adapter for BNO08x.
Definition Bno08xHandler.h:202
HalSpiBno08xComm(BaseSpi &spi, BaseGpio *reset_gpio=nullptr, BaseGpio *int_gpio=nullptr, BaseGpio *wake_gpio=nullptr) noexcept
Construct SPI adapter.
Definition Bno08xHandler.h:211
BNO085Interface GetInterfaceType() noexcept
Definition Bno08xHandler.h:220
Internal abstract interface for type-erasing the BNO085<CommType> template.
Definition Bno08xHandler.h:257
virtual ~IBno08xDriverOps() noexcept=default
Activity and gesture detection data.
Definition Bno08xHandler.h:407
Sensor calibration status.
Definition Bno08xHandler.h:424
BNO08x configuration structure.
Definition Bno08xHandler.h:438
Euler angles derived from quaternion.
Definition Bno08xHandler.h:380
Complete IMU sensor data structure.
Definition Bno08xHandler.h:392
Bno08xVector3 gravity
Gravity vector.
Definition Bno08xHandler.h:397
Bno08xVector3 gyroscope
Calibrated angular velocity (rad/s)
Definition Bno08xHandler.h:394
Bno08xVector3 acceleration
Calibrated acceleration (m/s^2)
Definition Bno08xHandler.h:393
Bno08xVector3 magnetometer
Calibrated magnetic field (uT)
Definition Bno08xHandler.h:395
Bno08xQuaternion rotation
Orientation quaternion.
Definition Bno08xHandler.h:398
Bno08xVector3 linear_acceleration
Linear acceleration (no gravity)
Definition Bno08xHandler.h:396
Bno08xEulerAngles euler
Euler angles.
Definition Bno08xHandler.h:399
Enhanced quaternion with timestamp and accuracy.
Definition Bno08xHandler.h:367
Enhanced vector with timestamp and accuracy.
Definition Bno08xHandler.h:355