Stream slave accel via ESP-NOW with master snapshot cache.
Slaves push BMA456 samples at 16ms when enabled; the master caches per client and exposes ACCEL_SNAPSHOT and ACCEL_STREAM over UART. goTool adds dashboard stream controls, HTTP accel-stream routes, and an external WebSocket API with per-connection receive/interval and slave stream commands. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
#include "bosch456.h"
|
||||
#include "cmd_accel_read.h"
|
||||
#include "uart_cmd.h"
|
||||
|
||||
static const char *TAG = "[ACCEL_READ]";
|
||||
|
||||
static void reply(bool success, int16_t x, int16_t y, int16_t z) {
|
||||
alox_UartMessage response;
|
||||
uart_cmd_init_response(&response, alox_MessageType_ACCEL_READ,
|
||||
alox_UartMessage_accel_read_response_tag);
|
||||
response.payload.accel_read_response.success = success;
|
||||
response.payload.accel_read_response.x = x;
|
||||
response.payload.accel_read_response.y = y;
|
||||
response.payload.accel_read_response.z = z;
|
||||
uart_cmd_send(&response, TAG);
|
||||
}
|
||||
|
||||
static void handle_accel_read(const uint8_t *data, size_t len) {
|
||||
(void)data;
|
||||
(void)len;
|
||||
|
||||
int16_t x = 0;
|
||||
int16_t y = 0;
|
||||
int16_t z = 0;
|
||||
if (bma456_read_accel(&x, &y, &z) == ESP_OK) {
|
||||
reply(true, x, y, z);
|
||||
return;
|
||||
}
|
||||
reply(false, 0, 0, 0);
|
||||
}
|
||||
|
||||
void cmd_accel_read_register(void) {
|
||||
uart_cmd_register(alox_MessageType_ACCEL_READ, handle_accel_read);
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
#ifndef CMD_ACCEL_READ_H
|
||||
#define CMD_ACCEL_READ_H
|
||||
|
||||
void cmd_accel_read_register(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,68 @@
|
||||
#include "client_registry.h"
|
||||
#include "cmd_accel_snapshot.h"
|
||||
#include "uart_cmd.h"
|
||||
|
||||
static const char *TAG = "[ACCEL_SNAP]";
|
||||
|
||||
static void fill_accel_snapshot(alox_AccelSnapshotResponse *out,
|
||||
uint32_t filter_client_id) {
|
||||
if (out == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
out->samples_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 (!client->accel_stream_enabled) {
|
||||
continue;
|
||||
}
|
||||
if (out->samples_count >=
|
||||
sizeof(out->samples) / sizeof(out->samples[0])) {
|
||||
break;
|
||||
}
|
||||
|
||||
alox_AccelSample *sample = &out->samples[out->samples_count++];
|
||||
sample->client_id = client->id;
|
||||
sample->valid = client->accel_valid;
|
||||
sample->x = client->accel_x;
|
||||
sample->y = client->accel_y;
|
||||
sample->z = client->accel_z;
|
||||
if (client->accel_valid) {
|
||||
sample->age_ms = client_registry_ms_since(client->accel_updated_at);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_accel_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_AccelSnapshotRequest *snap_req = UART_CMD_REQ(
|
||||
&req, alox_UartMessage_accel_snapshot_request_tag, accel_snapshot_request);
|
||||
if (snap_req != NULL) {
|
||||
filter_client_id = snap_req->client_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
alox_UartMessage response;
|
||||
uart_cmd_init_response(&response, alox_MessageType_ACCEL_SNAPSHOT,
|
||||
alox_UartMessage_accel_snapshot_response_tag);
|
||||
fill_accel_snapshot(&response.payload.accel_snapshot_response, filter_client_id);
|
||||
|
||||
uart_cmd_send(&response, TAG);
|
||||
}
|
||||
|
||||
void cmd_accel_snapshot_register(void) {
|
||||
uart_cmd_register(alox_MessageType_ACCEL_SNAPSHOT, handle_accel_snapshot);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef CMD_ACCEL_SNAPSHOT_H
|
||||
#define CMD_ACCEL_SNAPSHOT_H
|
||||
|
||||
void cmd_accel_snapshot_register(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,96 @@
|
||||
#include "client_registry.h"
|
||||
#include "cmd_accel_stream.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_now_comm.h"
|
||||
#include "uart_cmd.h"
|
||||
|
||||
static const char *TAG = "[ACCEL_STREAM]";
|
||||
|
||||
static void reply(bool enabled, uint32_t client_id, bool success,
|
||||
uint32_t slaves_updated) {
|
||||
alox_UartMessage response;
|
||||
uart_cmd_init_response(&response, alox_MessageType_ACCEL_STREAM,
|
||||
alox_UartMessage_accel_stream_response_tag);
|
||||
response.payload.accel_stream_response.enabled = enabled;
|
||||
response.payload.accel_stream_response.client_id = client_id;
|
||||
response.payload.accel_stream_response.success = success;
|
||||
response.payload.accel_stream_response.slaves_updated = slaves_updated;
|
||||
uart_cmd_send(&response, TAG);
|
||||
}
|
||||
|
||||
static esp_err_t push_stream_to_slave(const client_info_t *client, bool enable) {
|
||||
if (client == NULL) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
esp_err_t err = client_registry_set_accel_stream(client->id, enable);
|
||||
if (err != ESP_OK) {
|
||||
return err;
|
||||
}
|
||||
return esp_now_comm_send_accel_stream(client->mac, client->id, enable);
|
||||
}
|
||||
|
||||
static void handle_accel_stream(const uint8_t *data, size_t len) {
|
||||
alox_UartMessage uart_msg;
|
||||
alox_AccelStreamRequest req = alox_AccelStreamRequest_init_zero;
|
||||
|
||||
if (uart_cmd_decode(data, len, &uart_msg) == ESP_OK) {
|
||||
const alox_AccelStreamRequest *req_ptr = UART_CMD_REQ(
|
||||
&uart_msg, alox_UartMessage_accel_stream_request_tag, accel_stream_request);
|
||||
if (req_ptr != NULL) {
|
||||
req = *req_ptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (req.write) {
|
||||
if (req.all_clients) {
|
||||
size_t n = client_registry_set_accel_stream_all(req.enable);
|
||||
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_accel_stream(client->mac, client->id,
|
||||
req.enable) == ESP_OK) {
|
||||
sent++;
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "accel stream %s for %u/%u slaves",
|
||||
req.enable ? "on" : "off", (unsigned)sent, (unsigned)n);
|
||||
reply(req.enable, 0, sent > 0, sent);
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.client_id == 0) {
|
||||
ESP_LOGW(TAG, "client_id required (or all_clients)");
|
||||
reply(req.enable, 0, false, 0);
|
||||
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.enable, req.client_id, false, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
esp_err_t err = push_stream_to_slave(client, req.enable);
|
||||
reply(req.enable, req.client_id, err == ESP_OK, err == ESP_OK ? 1u : 0u);
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.all_clients || req.client_id == 0) {
|
||||
reply(false, 0, false, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
bool enabled = false;
|
||||
esp_err_t err = client_registry_get_accel_stream(req.client_id, &enabled);
|
||||
reply(enabled, req.client_id, err == ESP_OK, 0);
|
||||
}
|
||||
|
||||
void cmd_accel_stream_register(void) {
|
||||
uart_cmd_register(alox_MessageType_ACCEL_STREAM, handle_accel_stream);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef CMD_ACCEL_STREAM_H
|
||||
#define CMD_ACCEL_STREAM_H
|
||||
|
||||
void cmd_accel_stream_register(void);
|
||||
|
||||
#endif
|
||||
@@ -27,6 +27,7 @@ static bool encode_clients_list(pb_ostream_t *stream, const pb_field_t *field,
|
||||
proto.last_success_ping =
|
||||
client_registry_ms_since(client->last_success_ping_at);
|
||||
proto.version = client->version;
|
||||
proto.accel_stream_enabled = client->accel_stream_enabled;
|
||||
proto.mac.funcs.encode = uart_cmd_encode_bytes;
|
||||
proto.mac.arg = &mac;
|
||||
|
||||
|
||||
@@ -48,8 +48,10 @@ static const char *message_type_name(uint16_t id) {
|
||||
return "FIND_ME";
|
||||
case alox_MessageType_RESTART:
|
||||
return "RESTART";
|
||||
case alox_MessageType_ACCEL_READ:
|
||||
return "ACCEL_READ";
|
||||
case alox_MessageType_ACCEL_SNAPSHOT:
|
||||
return "ACCEL_SNAPSHOT";
|
||||
case alox_MessageType_ACCEL_STREAM:
|
||||
return "ACCEL_STREAM";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user