33#include "../../../SensorBuildOpt.h"
34#if !SENSORLIB_EXCLUDE_BMA456H
37#include "../../../bosch/bma4xx/bma456h.h"
197 if (feature & BMA456H_STEP_COUNTER_EN) {
198 if (bma456h_step_detector_enable(BMA4_ENABLE,
dev.get()) != 0) {
203 if (bma456h_feature_enable(feature, enable,
dev.get()) != 0) {
233 bool enableStepCounter(
bool enable, uint16_t step_counter_wm = 1,
bool reset_counter =
false)
override
236 if (bma456h_step_counter_set_watermark(step_counter_wm,
dev.get()) != 0) {
242 if (bma456h_reset_step_counter(
dev.get()) != 0) {
258 return bma456h_step_counter_get_watermark(&step_counter_wm,
dev.get()) == 0;
273 if (!
enableInterrupt(pin_map, BMA456H_STEP_CNTR_INT, interrupt_enable)) {
291 if (!
enableInterrupt(pin_map, BMA456H_TAP_OUT_INT, interrupt_enable)) {
294 return enableFeature(BMA456H_SINGLE_TAP_EN | BMA456H_DOUBLE_TAP_EN | BMA456H_TRIPLE_TAP_EN, enable);
308 if (!
enableInterrupt(pin_map, BMA456H_ACTIVITY_INT, interrupt_enable)) {
324 bool interrupt_enable =
false,
327 uint16_t interrupt_sources = 0;
328 uint16_t feature = 0;
329 if (cfg.
x_axis) feature |= BMA456H_ANY_MOTION_X_AXIS_EN;
330 if (cfg.
y_axis) feature |= BMA456H_ANY_MOTION_Y_AXIS_EN;
331 if (cfg.
z_axis) feature |= BMA456H_ANY_MOTION_Z_AXIS_EN;
334 interrupt_sources |= BMA456H_ANY_MOT_INT;
337 if (!
enableInterrupt(pin_map, BMA456H_ANY_MOT_INT, interrupt_enable)) {
354 bool interrupt_enable =
false,
357 uint16_t feature = 0;
358 if (cfg.
x_axis) feature |= BMA456H_NO_MOTION_X_AXIS_EN;
359 if (cfg.
y_axis) feature |= BMA456H_NO_MOTION_Y_AXIS_EN;
360 if (cfg.
z_axis) feature |= BMA456H_NO_MOTION_Z_AXIS_EN;
503 bool configMotion(
bool any_motion, uint16_t threshold, uint16_t duration)
override
507 if (threshold > 16380) {
508 SENSORLIB_LOG_E(
"Threshold %u exceeds recommended maximum (16380)", threshold);
511 if (duration > 16383) {
512 SENSORLIB_LOG_E(
"Duration %u exceeds recommended maximum (16383)", duration);
515 struct bma456h_any_no_mot_config any_mot_config = {0, 0, 0, 0};
518 rslt = bma456h_get_any_mot_config(&any_mot_config,
dev.get());
524 rslt = bma456h_get_no_mot_config(&any_mot_config,
dev.get());
531 any_mot_config.threshold = threshold;
532 any_mot_config.duration = duration;
534 SENSORLIB_LOG_D(
"Configuring motion detection: threshold=%u LSB (%.2f mG), duration=%u LSB (%.2f ms at 50Hz)",
535 threshold, threshold * 0.48f,
536 duration, duration * 20.0f);
539 if (bma456h_set_any_mot_config(&any_mot_config,
dev.get()) != 0) {
544 if (bma456h_set_no_mot_config(&any_mot_config,
dev.get()) != 0) {
565 uint16_t threshold_lsb =
static_cast<uint16_t
>(threshold_mg / 0.48f + 0.5f);
566 uint16_t duration_lsb =
static_cast<uint16_t
>(duration_ms / 20.0f + 0.5f);
569 threshold_mg, threshold_lsb, duration_ms, duration_lsb);
587 uint16_t threshold_lsb =
static_cast<uint16_t
>(threshold_mg / 0.48f + 0.5f);
588 uint16_t duration_lsb =
static_cast<uint16_t
>(duration_ms / 20.0f + 0.5f);
591 threshold_mg, threshold_lsb, duration_ms, duration_lsb);
606 struct bma456h_any_no_mot_config any_mot_config;
607 if (bma456h_get_any_mot_config(&any_mot_config,
dev.get()) != 0) {
612 threshold_mg = any_mot_config.threshold * 0.48f;
613 duration_ms = any_mot_config.duration * 20.0f;
615 SENSORLIB_LOG_D(
"Current config: threshold=%u LSB (%.2f mg), duration=%u LSB (%.2f ms)",
616 any_mot_config.threshold, threshold_mg,
617 any_mot_config.duration, duration_ms);
632 struct bma456h_any_no_mot_config no_mot_config;
633 if (bma456h_get_no_mot_config(&no_mot_config,
dev.get()) != 0) {
638 threshold_mg = no_mot_config.threshold * 0.48f;
639 duration_ms = no_mot_config.duration * 20.0f;
641 SENSORLIB_LOG_D(
"Current no-motion config: threshold=%u LSB (%.2f mg), duration=%u LSB (%.2f ms)",
642 no_mot_config.threshold, threshold_mg,
643 no_mot_config.duration, duration_ms);
656 uint32_t step_count = 0;
657 if (bma456h_step_counter_output(&step_count,
dev.get()) != 0) {
671 return bma456h_reset_step_counter(
dev.get()) == 0;
682 bma456h_out_state activity_type = {0, 0, 0, 0};
683 if (bma456h_output_state(&activity_type,
dev.get()) != 0) {
687 return static_cast<ActivityType>(activity_type.activity_type);
698 bma456h_out_state tap_type = { 0, 0, 0, 0 };
699 if (bma456h_output_state(&tap_type,
dev.get()) != 0) {
703 if (tap_type.triple_tap) {
705 }
else if (tap_type.double_tap) {
707 }
else if (tap_type.single_tap) {
722 if (bma4_read_int_status(&
_status,
dev.get()) != 0) {
737 uint32_t step_count = 0;
738 if (bma456h_step_counter_output(&step_count,
dev.get()) == 0) {
772 bool boschInitImpl()
override
774 int8_t rslt = bma456h_init(
dev.get());
779 rslt = bma456h_write_config_file(
dev.get());
781 SENSORLIB_LOG_E(
"bma456h_write_config_file failed with code %d", rslt);
InterruptPinMap
Enumeration of interrupt pin mapping options.
#define SENSORLIB_LOG_E(...)
#define SENSORLIB_LOG_D(...)
void setCallbacks(const EventCallbacks &cbs)
Set all callbacks at once.
void setOnTapCallback(std::function< void(TapType)> cb)
Set the callback for tap events.
void setOnStepDetectedCallback(std::function< void()> cb)
Set the callback for step detection events.
void setOnActivityCallback(std::function< void(ActivityType)> cb)
Set the callback for activity events.
BMA4XXCapability::Capability getCapabilities() const override
Get supported capabilities.
bool enableTapDetector(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable or disable tap detection.
ActivityType getActivityType()
Get the current activity type.
bool getNoMotionConfig(float &threshold_mg, float &duration_ms)
Get current no-motion detection configuration.
bool configAnyMotion(uint16_t threshold, uint16_t duration) override
Configure any-motion detection parameters with detailed unit conversion.
bool configNoMotion(uint16_t threshold, uint16_t duration) override
Configure no-motion detection parameters with detailed unit conversion.
void update() override
Update sensor state and process interrupts.
bool getAnyMotionConfig(float &threshold_mg, float &duration_ms)
Get current motion detection configuration.
void setOnAnyMotionCallback(std::function< void(void)> cb)
Set the any-motion callback for motion events.
void setOnDataReadyCallback(std::function< void(AccelerometerData)> cb)
Set the callback for data ready events.
bool enableStepCounter(bool enable, uint16_t step_counter_wm=1, bool reset_counter=false) override
Enable or disable step counter.
EventCallbacks & getCallbacks()
Get the current callbacks.
bool enableDataReady(bool enable=true, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable data ready interrupt.
bool configAnyMotionInPhysicalUnits(float threshold_mg, float duration_ms) override
Configure any-motion detection using physical units.
void setOnStepCountCallback(std::function< void(uint32_t)> cb)
Set the callback for step count events.
TapType getTapType()
Get the detected tap type.
uint32_t getStepCount()
Get the current step count.
~SensorBMA456H()=default
Destructor for the SensorBMA456H class.
bool enableFeature(uint16_t feature, bool enable)
Enable or disable a specific feature.
bool resetStepCounter()
Reset the step counter.
bool enableStepDetector(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable or disable step detector.
bool enableAnyMotionDetection(const MotionAxesConfig &cfg, bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable or disable any-motion detection.
bool getStepCounterWatermark(uint16_t &step_counter_wm) const
Get the current step counter watermark.
SensorBMA456H()
Constructor for the SensorBMA456H class.
bool enableNoMotionDetection(const MotionAxesConfig &cfg, bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable or disable no-motion detection.
const EventCallbacks & getCallbacks() const
Get the current callbacks (const version)
bool configMotion(bool any_motion, uint16_t threshold, uint16_t duration) override
Configure any-motion or 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 enableActivityRecognition(bool enable, bool interrupt_enable=false, InterruptPinMap pin_map=InterruptPinMap::PIN1) override
Enable or disable activity recognition.
void setOnNoMotionCallback(std::function< void(void)> cb)
Set the callback for no-motion events.
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
ActivityType
Activity detection type.
TapType
Tap detection type.
Structure representing accelerometer data.
Event callbacks for the sensor.
std::function< void(uint32_t)> onStepCount
Callback for step count updates.
std::function< void(void)> onAnyMotion
Callback for any motion detection.
std::function< void(ActivityType)> onActivity
Callback for activity recognition events.
std::function< void(AccelerometerData &)> onDataReady
Callback for new accelerometer data.
std::function< void(void)> onNoMotion
Callback for no-motion detection.
std::function< void(TapType)> onTap
Callback for tap detection events.
std::function< void()> onStepDetected
Callback for step detection events.
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)