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>
97 lines
3.0 KiB
C
97 lines
3.0 KiB
C
#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);
|
|
}
|