Add runtime BMA456 tap config with NVS persistence and goTool UI.
Expose multitap parameters over UART/ESP-NOW (like deadzone), store per node in NVS, and allow get/set from CLI, REST, and the dashboard without poll-overwrite while editing. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+92
-45
@@ -38,7 +38,7 @@ static volatile bool s_int_pending;
|
||||
static SemaphoreHandle_t s_accel_mutex;
|
||||
static bma456_tap_handler_t s_tap_handler;
|
||||
static void *s_tap_handler_ctx;
|
||||
static const bma456_tap_config_t s_tap_config = BMA456_TAP_CONFIG_DEFAULT;
|
||||
static bma456_tap_config_t s_tap_config = BMA456_TAP_CONFIG_DEFAULT;
|
||||
|
||||
static esp_err_t check_bma4(const char *api_name, int8_t rslt);
|
||||
|
||||
@@ -126,6 +126,96 @@ void bma456_set_accel_deadzone(uint32_t deadzone_lsb) {
|
||||
|
||||
uint32_t bma456_get_accel_deadzone(void) { return s_accel_deadzone; }
|
||||
|
||||
bool bma456_tap_config_valid(const bma456_tap_config_t *cfg) {
|
||||
if (cfg == NULL) {
|
||||
return false;
|
||||
}
|
||||
if (cfg->tap_sens_thres > 15u) {
|
||||
return false;
|
||||
}
|
||||
if (cfg->axis_sel > 2u) {
|
||||
return false;
|
||||
}
|
||||
if (cfg->wait_for_timeout > 1u) {
|
||||
return false;
|
||||
}
|
||||
if (!cfg->enable_single && !cfg->enable_double && !cfg->enable_triple) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void bma456_get_tap_config(bma456_tap_config_t *cfg_out) {
|
||||
if (cfg_out == NULL) {
|
||||
return;
|
||||
}
|
||||
*cfg_out = s_tap_config;
|
||||
}
|
||||
|
||||
static esp_err_t apply_tap_parameters(const bma456_tap_config_t *cfg) {
|
||||
struct bma456h_multitap_settings tap_settings = {0};
|
||||
int8_t ret = bma456h_tap_get_parameter(&tap_settings, &s_bma456);
|
||||
if (check_bma4("bma456h_tap_get_parameter", ret) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
tap_settings.tap_sens_thres = cfg->tap_sens_thres;
|
||||
tap_settings.max_gest_dur = cfg->max_gest_dur;
|
||||
tap_settings.tap_shock_dur = cfg->tap_shock_dur;
|
||||
tap_settings.quite_time_after_gest = cfg->quite_time_after_gest;
|
||||
tap_settings.wait_for_timeout = cfg->wait_for_timeout;
|
||||
tap_settings.axis_sel = cfg->axis_sel;
|
||||
ret = bma456h_tap_set_parameter(&tap_settings, &s_bma456);
|
||||
if (check_bma4("bma456h_tap_set_parameter", ret) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
/* Disable all tap kinds first, then enable the requested set. */
|
||||
uint16_t all_taps =
|
||||
BMA456H_SINGLE_TAP_EN | BMA456H_DOUBLE_TAP_EN | BMA456H_TRIPLE_TAP_EN;
|
||||
ret = bma456h_feature_enable(all_taps, BMA4_DISABLE, &s_bma456);
|
||||
if (check_bma4("bma456h_feature_enable(disable taps)", ret) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
uint16_t tap_features = 0;
|
||||
if (cfg->enable_single) {
|
||||
tap_features |= BMA456H_SINGLE_TAP_EN;
|
||||
}
|
||||
if (cfg->enable_double) {
|
||||
tap_features |= BMA456H_DOUBLE_TAP_EN;
|
||||
}
|
||||
if (cfg->enable_triple) {
|
||||
tap_features |= BMA456H_TRIPLE_TAP_EN;
|
||||
}
|
||||
ret = bma456h_feature_enable(tap_features, BMA4_ENABLE, &s_bma456);
|
||||
if (check_bma4("bma456h_feature_enable(taps)", ret) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG,
|
||||
"tap config sens=%u max_gest=%u shock=%u quiet=%u wait=%u axis=%u "
|
||||
"(single=%d double=%d triple=%d)",
|
||||
(unsigned)cfg->tap_sens_thres, (unsigned)cfg->max_gest_dur,
|
||||
(unsigned)cfg->tap_shock_dur, (unsigned)cfg->quite_time_after_gest,
|
||||
(unsigned)cfg->wait_for_timeout, (unsigned)cfg->axis_sel,
|
||||
cfg->enable_single, cfg->enable_double, cfg->enable_triple);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t bma456_set_tap_config(const bma456_tap_config_t *cfg) {
|
||||
if (!bma456_tap_config_valid(cfg)) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (s_bma456_ready) {
|
||||
esp_err_t err = apply_tap_parameters(cfg);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
s_tap_config = *cfg;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void bma456_set_tap_handler(bma456_tap_handler_t handler, void *ctx) {
|
||||
s_tap_handler = handler;
|
||||
s_tap_handler_ctx = ctx;
|
||||
@@ -259,52 +349,9 @@ static esp_err_t configure_tap_interrupt(void) {
|
||||
int8_t ret;
|
||||
const uint8_t int_line = BMA4_INTR2_MAP;
|
||||
|
||||
struct bma456h_multitap_settings tap_settings = {0};
|
||||
ret = bma456h_tap_get_parameter(&tap_settings, &s_bma456);
|
||||
if (check_bma4("bma456h_tap_get_parameter", ret) != ESP_OK) {
|
||||
if (apply_tap_parameters(&s_tap_config) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
tap_settings.tap_sens_thres = s_tap_config.tap_sens_thres;
|
||||
tap_settings.max_gest_dur = s_tap_config.max_gest_dur;
|
||||
tap_settings.tap_shock_dur = s_tap_config.tap_shock_dur;
|
||||
tap_settings.quite_time_after_gest = s_tap_config.quite_time_after_gest;
|
||||
tap_settings.wait_for_timeout = s_tap_config.wait_for_timeout;
|
||||
tap_settings.axis_sel = s_tap_config.axis_sel;
|
||||
ret = bma456h_tap_set_parameter(&tap_settings, &s_bma456);
|
||||
if (check_bma4("bma456h_tap_set_parameter", ret) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
uint16_t tap_features = 0;
|
||||
if (s_tap_config.enable_single) {
|
||||
tap_features |= BMA456H_SINGLE_TAP_EN;
|
||||
}
|
||||
if (s_tap_config.enable_double) {
|
||||
tap_features |= BMA456H_DOUBLE_TAP_EN;
|
||||
}
|
||||
if (s_tap_config.enable_triple) {
|
||||
tap_features |= BMA456H_TRIPLE_TAP_EN;
|
||||
}
|
||||
if (tap_features == 0) {
|
||||
ESP_LOGW(TAG, "tap config: no tap kinds enabled");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
ret = bma456h_feature_enable(tap_features, BMA4_ENABLE, &s_bma456);
|
||||
if (check_bma4("bma456h_feature_enable", ret) != ESP_OK) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG,
|
||||
"tap config sens=%u max_gest=%u shock=%u quiet=%u wait=%u axis=%u "
|
||||
"(single=%d double=%d triple=%d)",
|
||||
(unsigned)s_tap_config.tap_sens_thres,
|
||||
(unsigned)s_tap_config.max_gest_dur,
|
||||
(unsigned)s_tap_config.tap_shock_dur,
|
||||
(unsigned)s_tap_config.quite_time_after_gest,
|
||||
(unsigned)s_tap_config.wait_for_timeout,
|
||||
(unsigned)s_tap_config.axis_sel, s_tap_config.enable_single,
|
||||
s_tap_config.enable_double, s_tap_config.enable_triple);
|
||||
|
||||
ret = bma456h_map_interrupt(int_line, BMA456H_TAP_OUT_INT, BMA4_ENABLE,
|
||||
&s_bma456);
|
||||
|
||||
Reference in New Issue
Block a user