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>
163 lines
5.1 KiB
C
163 lines
5.1 KiB
C
#include "bosch456.h"
|
|
#include "client_registry.h"
|
|
#include "cmd_tap_config.h"
|
|
#include "esp_log.h"
|
|
#include "esp_now_comm.h"
|
|
#include "pod_settings.h"
|
|
#include "uart_cmd.h"
|
|
|
|
static const char *TAG = "[TAP_CFG]";
|
|
|
|
static void fill_response_fields(alox_TapConfigResponse *out,
|
|
const bma456_tap_config_t *cfg) {
|
|
out->tap_sens_thres = cfg->tap_sens_thres;
|
|
out->max_gest_dur = cfg->max_gest_dur;
|
|
out->tap_shock_dur = cfg->tap_shock_dur;
|
|
out->quite_time_after_gest = cfg->quite_time_after_gest;
|
|
out->wait_for_timeout = cfg->wait_for_timeout;
|
|
out->axis_sel = cfg->axis_sel;
|
|
out->enable_single = cfg->enable_single;
|
|
out->enable_double = cfg->enable_double;
|
|
out->enable_triple = cfg->enable_triple;
|
|
}
|
|
|
|
static void reply(uint32_t client_id, bool success, uint32_t slaves_updated,
|
|
const bma456_tap_config_t *cfg) {
|
|
alox_UartMessage response;
|
|
uart_cmd_init_response(&response, alox_MessageType_TAP_CONFIG,
|
|
alox_UartMessage_tap_config_response_tag);
|
|
response.payload.tap_config_response.client_id = client_id;
|
|
response.payload.tap_config_response.success = success;
|
|
response.payload.tap_config_response.slaves_updated = slaves_updated;
|
|
if (cfg != NULL) {
|
|
fill_response_fields(&response.payload.tap_config_response, cfg);
|
|
}
|
|
uart_cmd_send(&response, TAG);
|
|
}
|
|
|
|
static void req_to_cfg(const alox_TapConfigRequest *req,
|
|
bma456_tap_config_t *cfg) {
|
|
cfg->tap_sens_thres = (uint16_t)req->tap_sens_thres;
|
|
cfg->max_gest_dur = (uint16_t)req->max_gest_dur;
|
|
cfg->tap_shock_dur = (uint16_t)req->tap_shock_dur;
|
|
cfg->quite_time_after_gest = (uint16_t)req->quite_time_after_gest;
|
|
cfg->wait_for_timeout = (uint16_t)req->wait_for_timeout;
|
|
cfg->axis_sel = (uint16_t)req->axis_sel;
|
|
cfg->enable_single = req->enable_single;
|
|
cfg->enable_double = req->enable_double;
|
|
cfg->enable_triple = req->enable_triple;
|
|
}
|
|
|
|
static void apply_local_tap_config(const bma456_tap_config_t *cfg) {
|
|
if (bma456_set_tap_config(cfg) != ESP_OK) {
|
|
ESP_LOGW(TAG, "local tap config apply failed");
|
|
return;
|
|
}
|
|
if (pod_settings_save_tap_config(cfg) != ESP_OK) {
|
|
ESP_LOGW(TAG, "tap config applied but not saved to NVS");
|
|
}
|
|
}
|
|
|
|
static esp_err_t push_tap_config_to_slave(const client_info_t *client,
|
|
const bma456_tap_config_t *cfg) {
|
|
if (client == NULL || cfg == NULL) {
|
|
return ESP_ERR_INVALID_ARG;
|
|
}
|
|
|
|
esp_err_t err = client_registry_set_tap_config(client->id, cfg);
|
|
if (err != ESP_OK) {
|
|
return err;
|
|
}
|
|
|
|
return esp_now_comm_send_tap_config(client->mac, client->id, cfg);
|
|
}
|
|
|
|
static void handle_tap_config(const uint8_t *data, size_t len) {
|
|
alox_UartMessage uart_msg;
|
|
alox_TapConfigRequest req = alox_TapConfigRequest_init_zero;
|
|
static const bma456_tap_config_t k_default = BMA456_TAP_CONFIG_DEFAULT;
|
|
|
|
if (uart_cmd_decode(data, len, &uart_msg) != ESP_OK) {
|
|
ESP_LOGW(TAG, "decode failed");
|
|
reply(0, false, 0, &k_default);
|
|
return;
|
|
}
|
|
|
|
const alox_TapConfigRequest *req_ptr =
|
|
UART_CMD_REQ(&uart_msg, alox_UartMessage_tap_config_request_tag,
|
|
tap_config_request);
|
|
if (req_ptr != NULL) {
|
|
req = *req_ptr;
|
|
}
|
|
|
|
bma456_tap_config_t cfg;
|
|
req_to_cfg(&req, &cfg);
|
|
|
|
if (req.write) {
|
|
if (!bma456_tap_config_valid(&cfg)) {
|
|
ESP_LOGW(TAG, "invalid tap config");
|
|
reply(req.client_id, false, 0, &cfg);
|
|
return;
|
|
}
|
|
|
|
if (req.all_clients) {
|
|
size_t n = client_registry_set_tap_config_all(&cfg);
|
|
uint32_t sent = 0;
|
|
|
|
for (size_t i = 0; i < client_registry_count(); i++) {
|
|
const client_info_t *client = client_registry_at(i);
|
|
if (client == NULL) {
|
|
continue;
|
|
}
|
|
if (esp_now_comm_send_tap_config(client->mac, client->id, &cfg) ==
|
|
ESP_OK) {
|
|
sent++;
|
|
}
|
|
}
|
|
|
|
if (bma456_is_ready()) {
|
|
apply_local_tap_config(&cfg);
|
|
}
|
|
|
|
ESP_LOGI(TAG, "set tap config via unicast to %u/%u slaves",
|
|
(unsigned)sent, (unsigned)n);
|
|
reply(0, sent > 0 || bma456_is_ready(), sent, &cfg);
|
|
return;
|
|
}
|
|
|
|
if (req.client_id == 0) {
|
|
apply_local_tap_config(&cfg);
|
|
ESP_LOGI(TAG, "set local tap config (no ESP-NOW; use -client or -all "
|
|
"for slaves)");
|
|
reply(0, true, 0, &cfg);
|
|
return;
|
|
}
|
|
|
|
const client_info_t *client = client_registry_find_by_id(req.client_id);
|
|
if (client == NULL) {
|
|
ESP_LOGW(TAG, "client id %lu not found", (unsigned long)req.client_id);
|
|
reply(req.client_id, false, 0, &cfg);
|
|
return;
|
|
}
|
|
|
|
esp_err_t err = push_tap_config_to_slave(client, &cfg);
|
|
reply(req.client_id, err == ESP_OK, err == ESP_OK ? 1 : 0, &cfg);
|
|
return;
|
|
}
|
|
|
|
if (req.all_clients || req.client_id == 0) {
|
|
bma456_tap_config_t local;
|
|
bma456_get_tap_config(&local);
|
|
reply(0, true, 0, &local);
|
|
return;
|
|
}
|
|
|
|
bma456_tap_config_t cached;
|
|
esp_err_t err = client_registry_get_tap_config(req.client_id, &cached);
|
|
reply(req.client_id, err == ESP_OK, 0, err == ESP_OK ? &cached : &k_default);
|
|
}
|
|
|
|
void cmd_tap_config_register(void) {
|
|
uart_cmd_register(alox_MessageType_TAP_CONFIG, handle_tap_config);
|
|
}
|