33#include "../../../SensorBuildOpt.h"
34#if !SENSORLIB_EXCLUDE_BMA422
37#include "../../../bosch/bma4xx/bma422_an.h"
162 bool interrupt_enable =
false,
166 struct bma422_an_any_no_mot_config any_mot_config = { 0, 0, 0};
168 rslt = bma422_an_get_any_mot_config(&any_mot_config,
dev.get());
174 any_mot_config.axes_en = 0x00;
176 if (cfg.
x_axis) any_mot_config.axes_en |= BMA422_AN_X_AXIS_EN;
177 if (cfg.
y_axis) any_mot_config.axes_en |= BMA422_AN_Y_AXIS_EN;
178 if (cfg.
z_axis) any_mot_config.axes_en |= BMA422_AN_Z_AXIS_EN;
181 if (!
enableInterrupt(pin_map, BMA422_AN_ANY_MOT_INT, interrupt_enable)) {
185 rslt = bma422_an_set_any_mot_config(&any_mot_config,
dev.get());
199 bool interrupt_enable =
false,
203 struct bma422_an_any_no_mot_config no_mot_config = { 0, 0, 0};
204 rslt = bma422_an_get_no_mot_config(&no_mot_config,
dev.get());
209 no_mot_config.axes_en = 0x00;
211 if (cfg.
x_axis) no_mot_config.axes_en |= BMA422_AN_X_AXIS_EN;
212 if (cfg.
y_axis) no_mot_config.axes_en |= BMA422_AN_Y_AXIS_EN;
213 if (cfg.
z_axis) no_mot_config.axes_en |= BMA422_AN_Z_AXIS_EN;
216 if (!
enableInterrupt(pin_map, BMA422_AN_NO_MOT_INT, interrupt_enable)) {
220 rslt = bma422_an_set_no_mot_config(&no_mot_config,
dev.get());
359 bool configMotion(
bool any_motion, uint16_t threshold, uint16_t duration)
override
363 if (threshold > 16380) {
364 SENSORLIB_LOG_E(
"Threshold %u exceeds recommended maximum (16380)", threshold);
367 if (duration > 16383) {
368 SENSORLIB_LOG_E(
"Duration %u exceeds recommended maximum (16383)", duration);
371 struct bma422_an_any_no_mot_config any_mot_config = {0, 0, 0};
374 rslt = bma422_an_get_any_mot_config(&any_mot_config,
dev.get());
380 rslt = bma422_an_get_no_mot_config(&any_mot_config,
dev.get());
387 any_mot_config.threshold = threshold;
388 any_mot_config.duration = duration;
390 SENSORLIB_LOG_D(
"Configuring motion detection: threshold=%u LSB (%.2f mG), duration=%u LSB (%.2f ms at 50Hz)",
391 threshold, threshold * 0.48f,
392 duration, duration * 20.0f);
395 if (bma422_an_set_any_mot_config(&any_mot_config,
dev.get()) != 0) {
400 if (bma422_an_set_no_mot_config(&any_mot_config,
dev.get()) != 0) {
421 uint16_t threshold_lsb =
static_cast<uint16_t
>(threshold_mg / 0.48f + 0.5f);
422 uint16_t duration_lsb =
static_cast<uint16_t
>(duration_ms / 20.0f + 0.5f);
425 threshold_mg, threshold_lsb, duration_ms, duration_lsb);
443 uint16_t threshold_lsb =
static_cast<uint16_t
>(threshold_mg / 0.48f + 0.5f);
444 uint16_t duration_lsb =
static_cast<uint16_t
>(duration_ms / 20.0f + 0.5f);
447 threshold_mg, threshold_lsb, duration_ms, duration_lsb);
462 struct bma422_an_any_no_mot_config any_mot_config;
463 if (bma422_an_get_any_mot_config(&any_mot_config,
dev.get()) != 0) {
468 threshold_mg = any_mot_config.threshold * 0.48f;
469 duration_ms = any_mot_config.duration * 20.0f;
471 SENSORLIB_LOG_D(
"Current config: threshold=%u LSB (%.2f mg), duration=%u LSB (%.2f ms)",
472 any_mot_config.threshold, threshold_mg,
473 any_mot_config.duration, duration_ms);
488 struct bma422_an_any_no_mot_config no_mot_config;
489 if (bma422_an_get_no_mot_config(&no_mot_config,
dev.get()) != 0) {
494 threshold_mg = no_mot_config.threshold * 0.48f;
495 duration_ms = no_mot_config.duration * 20.0f;
497 SENSORLIB_LOG_D(
"Current no-motion config: threshold=%u LSB (%.2f mg), duration=%u LSB (%.2f ms)",
498 no_mot_config.threshold, threshold_mg,
499 no_mot_config.duration, duration_ms);
513 if (bma4_read_int_status(&
_status,
dev.get()) != 0) {
545 bool boschInitImpl()
override
547 int8_t rslt = bma422_an_init(
dev.get());
552 rslt = bma422_an_write_config_file(
dev.get());
554 SENSORLIB_LOG_E(
"bma422_an_write_config_file failed with code %d", rslt);
InterruptPinMap
Enumeration of interrupt pin mapping options.
#define SENSORLIB_LOG_E(...)
#define SENSORLIB_LOG_D(...)
~SensorBMA422()=default
Destructor for the SensorBMA422 class.
void setOnNoMotionCallback(std::function< void(void)> cb)
Set the callback for no-motion events.
void setCallbacks(const EventCallbacks &cbs)
Set all callbacks at once.
bool enableAnyMotionDetection(const MotionAxesConfig &cfg, bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable or disable any-motion detection.
const EventCallbacks & getCallbacks() const
Get the current callbacks (const version)
void update() override
Update sensor state and process interrupts.
bool getNoMotionConfig(float &threshold_mg, float &duration_ms)
Get current no-motion detection configuration.
void setOnDataReadyCallback(std::function< void(AccelerometerData)> cb)
Set the callback for data ready events.
SensorBMA422()
Constructor for the SensorBMA422 class.
void setOnAnyMotionCallback(std::function< void(void)> cb)
Set the any-motion callback for motion events.
bool configNoMotion(uint16_t threshold, uint16_t duration) override
Configure no-motion detection parameters with detailed unit conversion.
bool configNoMotionInPhysicalUnits(float threshold_mg, float duration_ms) override
Configure no-motion detection using physical units.
bool getAnyMotionConfig(float &threshold_mg, float &duration_ms)
Get current motion detection configuration.
bool configAnyMotionInPhysicalUnits(float threshold_mg, float duration_ms) override
Configure any-motion detection using physical units.
BMA4XXCapability::Capability getCapabilities() const override
Get supported capabilities.
bool enableDataReady(bool enable=true, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable data ready interrupt.
bool configAnyMotion(uint16_t threshold, uint16_t duration) override
Configure any-motion detection parameters with detailed unit conversion.
EventCallbacks & getCallbacks()
Get the current callbacks.
bool enableNoMotionDetection(const MotionAxesConfig &cfg, bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable or disable no-motion detection.
bool configMotion(bool any_motion, uint16_t threshold, uint16_t duration) override
Configure any-motion or no-motion detection parameters with detailed unit conversion.
uint8_t _remap_reg_offset
bool enableInterrupt(InterruptPinMap pin_map, uint16_t interrupt_sources, bool enable)
Enable or disable specific interrupt sources on a pin.
bool readData(AccelerometerData &data) override
Get the accelerometer data.
std::unique_ptr< struct bma4_dev > dev
Structure representing accelerometer data.
Event callbacks for the sensor.
std::function< void(void)> onAnyMotion
Callback for any motion detection.
std::function< void(void)> onNoMotion
Callback for no-motion detection.
std::function< void(AccelerometerData &)> onDataReady
Callback for new accelerometer data.
Motion axes configuration.
uint8_t z_axis
Enable motion detection on Z-axis (1=enabled, 0=disabled)
uint8_t y_axis
Enable motion detection on Y-axis (1=enabled, 0=disabled)
uint8_t x_axis
Enable motion detection on X-axis (1=enabled, 0=disabled)