Add BMA456 tap detection with ESP-NOW notify and host snapshot API.

Slaves forward configured tap kinds to the master; goTool exposes CLI, dashboard, REST, and WebSocket with separate notify vs receive and 2s display cache.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-05-29 20:42:57 +02:00
co-authored by Cursor
parent 3cb0b5bbe9
commit a8d4d42920
34 changed files with 3138 additions and 241 deletions
+3
View File
@@ -28,6 +28,9 @@ static bool encode_clients_list(pb_ostream_t *stream, const pb_field_t *field,
client_registry_ms_since(client->last_success_ping_at);
proto.version = client->version;
proto.accel_stream_enabled = client->accel_stream_enabled;
proto.tap_notify_single = client->tap_notify_single;
proto.tap_notify_double = client->tap_notify_double;
proto.tap_notify_triple = client->tap_notify_triple;
proto.mac.funcs.encode = uart_cmd_encode_bytes;
proto.mac.arg = &mac;
+4
View File
@@ -54,6 +54,10 @@ static const char *message_type_name(uint16_t id) {
return "ACCEL_STREAM";
case alox_MessageType_BATTERY_STATUS:
return "BATTERY_STATUS";
case alox_MessageType_TAP_NOTIFY:
return "TAP_NOTIFY";
case alox_MessageType_TAP_SNAPSHOT:
return "TAP_SNAPSHOT";
default:
return "UNKNOWN";
}
+110
View File
@@ -0,0 +1,110 @@
#include "client_registry.h"
#include "cmd_tap_notify.h"
#include "esp_log.h"
#include "esp_now_comm.h"
#include "uart_cmd.h"
static const char *TAG = "[TAP_NOTIFY]";
static void reply(uint32_t client_id, bool success, uint32_t slaves_updated,
bool single, bool double_tap, bool triple) {
alox_UartMessage response;
uart_cmd_init_response(&response, alox_MessageType_TAP_NOTIFY,
alox_UartMessage_tap_notify_response_tag);
response.payload.tap_notify_response.client_id = client_id;
response.payload.tap_notify_response.success = success;
response.payload.tap_notify_response.slaves_updated = slaves_updated;
response.payload.tap_notify_response.single = single;
response.payload.tap_notify_response.double_tap = double_tap;
response.payload.tap_notify_response.triple = triple;
uart_cmd_send(&response, TAG);
}
static esp_err_t push_tap_notify_to_slave(const client_info_t *client,
bool single, bool double_tap,
bool triple) {
if (client == NULL) {
return ESP_ERR_INVALID_ARG;
}
esp_err_t err =
client_registry_set_tap_notify(client->id, single, double_tap, triple);
if (err != ESP_OK) {
return err;
}
return esp_now_comm_send_tap_notify(client->mac, client->id, single,
double_tap, triple);
}
static void handle_tap_notify(const uint8_t *data, size_t len) {
alox_UartMessage uart_msg;
alox_TapNotifyRequest req = alox_TapNotifyRequest_init_zero;
if (uart_cmd_decode(data, len, &uart_msg) == ESP_OK) {
const alox_TapNotifyRequest *req_ptr = UART_CMD_REQ(
&uart_msg, alox_UartMessage_tap_notify_request_tag, tap_notify_request);
if (req_ptr != NULL) {
req = *req_ptr;
}
}
if (req.write) {
if (req.all_clients) {
size_t n = client_registry_set_tap_notify_all(req.single, req.double_tap,
req.triple);
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_notify(client->mac, client->id, req.single,
req.double_tap,
req.triple) == ESP_OK) {
sent++;
}
}
ESP_LOGI(TAG, "tap notify single=%d double=%d triple=%d for %u/%u slaves",
req.single, req.double_tap, req.triple, (unsigned)sent,
(unsigned)n);
reply(0, sent > 0, sent, req.single, req.double_tap, req.triple);
return;
}
if (req.client_id == 0) {
ESP_LOGW(TAG, "client_id required (or all_clients)");
reply(0, false, 0, req.single, req.double_tap, req.triple);
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, req.single, req.double_tap, req.triple);
return;
}
esp_err_t err =
push_tap_notify_to_slave(client, req.single, req.double_tap, req.triple);
reply(req.client_id, err == ESP_OK, err == ESP_OK ? 1u : 0u, req.single,
req.double_tap, req.triple);
return;
}
if (req.all_clients || req.client_id == 0) {
reply(0, false, 0, false, false, false);
return;
}
bool single = false;
bool double_tap = false;
bool triple = false;
esp_err_t err = client_registry_get_tap_notify(req.client_id, &single,
&double_tap, &triple);
reply(req.client_id, err == ESP_OK, 0, single, double_tap, triple);
}
void cmd_tap_notify_register(void) {
uart_cmd_register(alox_MessageType_TAP_NOTIFY, handle_tap_notify);
}
+6
View File
@@ -0,0 +1,6 @@
#ifndef CMD_TAP_NOTIFY_H
#define CMD_TAP_NOTIFY_H
void cmd_tap_notify_register(void);
#endif
+88
View File
@@ -0,0 +1,88 @@
#include "client_registry.h"
#include "cmd_tap_snapshot.h"
#include "uart_cmd.h"
static const char *TAG = "[TAP_SNAP]";
static alox_TapKind tap_kind_from_registry(uint32_t kind) {
switch (kind) {
case 1:
return alox_TapKind_TAP_SINGLE;
case 2:
return alox_TapKind_TAP_DOUBLE;
case 3:
return alox_TapKind_TAP_TRIPLE;
default:
return alox_TapKind_TAP_NONE;
}
}
static bool tap_notify_any(const client_info_t *client) {
return client != NULL &&
(client->tap_notify_single || client->tap_notify_double ||
client->tap_notify_triple);
}
static void fill_tap_snapshot(alox_TapSnapshotResponse *out,
uint32_t filter_client_id) {
if (out == NULL) {
return;
}
out->events_count = 0;
size_t count = client_registry_count();
for (size_t i = 0; i < count; i++) {
const client_info_t *client = client_registry_at(i);
if (client == NULL) {
continue;
}
if (filter_client_id != 0 && client->id != filter_client_id) {
continue;
}
if (!tap_notify_any(client)) {
continue;
}
if (out->events_count >= sizeof(out->events) / sizeof(out->events[0])) {
break;
}
uint32_t kind = 0;
uint32_t age_ms = 0;
if (!client_registry_take_tap(client->id, &kind, &age_ms)) {
continue;
}
alox_TapEvent *event = &out->events[out->events_count++];
event->client_id = client->id;
event->valid = true;
event->kind = tap_kind_from_registry(kind);
event->age_ms = age_ms;
}
}
static void handle_tap_snapshot(const uint8_t *data, size_t len) {
uint32_t filter_client_id = 0;
if (len > 0) {
alox_UartMessage req;
if (uart_cmd_decode(data, len, &req) == ESP_OK) {
alox_TapSnapshotRequest *snap_req = UART_CMD_REQ(
&req, alox_UartMessage_tap_snapshot_request_tag, tap_snapshot_request);
if (snap_req != NULL) {
filter_client_id = snap_req->client_id;
}
}
}
alox_UartMessage response;
uart_cmd_init_response(&response, alox_MessageType_TAP_SNAPSHOT,
alox_UartMessage_tap_snapshot_response_tag);
fill_tap_snapshot(&response.payload.tap_snapshot_response, filter_client_id);
uart_cmd_send(&response, TAG);
}
void cmd_tap_snapshot_register(void) {
uart_cmd_register(alox_MessageType_TAP_SNAPSHOT, handle_tap_snapshot);
}
+6
View File
@@ -0,0 +1,6 @@
#ifndef CMD_TAP_SNAPSHOT_H
#define CMD_TAP_SNAPSHOT_H
void cmd_tap_snapshot_register(void);
#endif