Files
powerpods/main/bosch456.h
T
skrueckenandCursor 8f90dfe82f Send ESP-NOW accel only on deadzone change and harden BLE+WiFi OTA coex.
Quiet pods no longer spam 100 Hz samples; mute wifi logs and throttle BLE
notifies during slave distribution so the wifi task stack does not panic.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-02 00:55:50 +01:00

113 lines
4.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#ifndef BOSCH456_H
#define BOSCH456_H
/**
* Powerpod driver for Bosch BMA456H (hearable variant) on the shared I2C bus.
*
* Vendor API: components/bma456 (bma4.c + bma456h.c only).
* Implementation: bosch456.c
*/
#include "driver/i2c_types.h"
#include "esp_err.h"
#include <stdbool.h>
#include <stdint.h>
/** 7-bit I2C address (SDO low). */
#define BMA456_I2C_ADDR 0x18
/** Sensor interrupt line → ESP32 GPIO (active high, rising edge). */
#define BMA456_INT_GPIO 10
/** Software filter: emit accel only when |axis - last| > deadzone (raw LSB). */
#define BMA456_DEFAULT_ACCEL_DEADZONE 100u
/**
* BMA456H multitap tuning (see BST-BMA456-AN000).
*
* Time fields use register units: value × 5 ms (e.g. 100 → 500 ms).
* tap_sens_thres: 0 = most sensitive … 15 = least (~78 mg per LSB).
* axis_sel: 0 = X, 1 = Y, 2 = Z.
* wait_for_timeout: 0 = report immediately, 1 = wait max_gest_dur for classification.
*/
typedef struct {
uint16_t tap_sens_thres;
uint16_t max_gest_dur;
uint16_t tap_shock_dur;
uint16_t quite_time_after_gest;
uint16_t wait_for_timeout;
uint16_t axis_sel;
bool enable_single;
bool enable_double;
bool enable_triple;
} bma456_tap_config_t;
/** Edit these values for the compile-time default (overridden by NVS if set). */
#define BMA456_TAP_CONFIG_DEFAULT \
{ \
.tap_sens_thres = 5, /* sensitive; 0=max, Bosch default=9 */ \
.max_gest_dur = 100, /* 500 ms window for double/triple */ \
.tap_shock_dur = 6, /* 30 ms debounce after each impulse */ \
.quite_time_after_gest = 60, /* 300 ms min gap between gestures */ \
.wait_for_timeout = 0, /* 0 = faster single-tap response */ \
.axis_sel = 2, /* Z axis */ \
.enable_single = true, \
.enable_double = true, \
.enable_triple = true, \
}
/** True if cfg fields are within sensor limits and at least one tap kind is on. */
bool bma456_tap_config_valid(const bma456_tap_config_t *cfg);
/** Copy current runtime tap config (compile default until set/NVS-applied). */
void bma456_get_tap_config(bma456_tap_config_t *cfg_out);
/**
* Apply tap config to the sensor (and keep RAM copy).
* Returns ESP_ERR_INVALID_ARG if invalid. When the sensor is not ready, only
* the RAM copy is updated (ESP_OK).
*/
esp_err_t bma456_set_tap_config(const bma456_tap_config_t *cfg);
/**
* Probe and configure the sensor on bus_handle (100 kHz device).
* On failure the device is removed and ESP_ERR_NOT_FOUND / ESP_FAIL is returned;
* firmware continues without a sensor (see bma456_is_ready()).
*/
esp_err_t init_bma456(i2c_master_bus_handle_t bus_handle);
bool bma456_is_ready(void);
void bma456_set_accel_deadzone(uint32_t deadzone_lsb);
uint32_t bma456_get_accel_deadzone(void);
/**
* ESP-NOW stream filter: true if sample exceeds deadzone vs last streamed
* sample (or no baseline yet). On true, commits (x,y,z) as stream baseline.
* Independent of the log filter baseline.
*/
bool bma456_accel_stream_take_if_changed(int16_t x, int16_t y, int16_t z);
/** Force next stream take to accept (call when accel stream is enabled). */
void bma456_accel_stream_reset_baseline(void);
/** Log accel when any axis moved more than deadzone since last reported sample. */
void bma456_report_accel_if_changed(int16_t x, int16_t y, int16_t z);
/** Tap kinds from BMA456H multitap output. */
typedef enum {
BMA456_TAP_SINGLE = 1,
BMA456_TAP_DOUBLE = 2,
BMA456_TAP_TRIPLE = 3,
} bma456_tap_kind_t;
typedef void (*bma456_tap_handler_t)(bma456_tap_kind_t kind, void *ctx);
/** Optional callback invoked from sensor task on tap interrupt (may be NULL). */
void bma456_set_tap_handler(bma456_tap_handler_t handler, void *ctx);
/** On-demand read of current accel XYZ (raw LSB). Returns ESP_ERR_INVALID_STATE if sensor not ready. */
esp_err_t bma456_read_accel(int16_t *x, int16_t *y, int16_t *z);
#endif