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:
2026-05-29 19:11:36 +02:00
co-authored by Cursor
parent ba20544762
commit 47c75110c9
35 changed files with 2409 additions and 300 deletions
+2 -1
View File
@@ -18,7 +18,8 @@ idf_component_register(
"cmd/cmd_version.c"
"cmd/cmd_client_info.c"
"cmd/cmd_accel_deadzone.c"
"cmd/cmd_accel_read.c"
"cmd/cmd_accel_snapshot.c"
"cmd/cmd_accel_stream.c"
"cmd/cmd_espnow_unicast_test.c"
"cmd/cmd_espnow_find_me.c"
"cmd/cmd_restart.c"
+12 -11
View File
@@ -115,6 +115,7 @@ Schema: `proto/esp_now_messages.proto`. Encode/decode: `esp_now_proto.c`. The ES
| `ESPNOW_UNICAST_TEST` | Master → slave | `EspNowUnicastTest` (`seq`) |
| `ESPNOW_FIND_ME` | Master → slave | `EspNowFindMe` (`client_id` filter) — LED locate sequence |
| `ESPNOW_RESTART` | Master → slave | `EspNowRestart` (`client_id` filter) — reboot slave |
| `ESPNOW_ACCEL_SAMPLE` | Slave → master | `EspNowAccelSample` (`slave_id`, `x`, `y`, `z` raw LSB) — ~every 16 ms |
| `ESPNOW_OTA_START` | Master → slave (unicast) | `EspNowOtaStart` (`total_size`) |
| `ESPNOW_OTA_PAYLOAD` | Master → slave | `EspNowOtaPayload` (`seq`, up to 200 B `data`) |
| `ESPNOW_OTA_END` | Master → slave | `EspNowOtaEnd` |
@@ -217,7 +218,7 @@ Host and master speak nanopb-encoded `UartMessage` inside UART frames (byte 0 =
| 21 | `OTA_SLAVE_PROGRESS` | Implemented (`cmd/cmd_ota_slave_progress.c`) — query per-slave ESP-NOW OTA progress |
| 22 | `FIND_ME` | Implemented (`cmd/cmd_espnow_find_me.c`) — `client_id=0` local ring, `>0` ESP-NOW to slave |
| 23 | `RESTART` | Implemented (`cmd/cmd_restart.c`) — `client_id=0` reboot master, `>0` ESP-NOW reboot slave |
| 24 | `ACCEL_READ` | Implemented (`cmd/cmd_accel_read.c`) — on-demand BMA456 XYZ (raw LSB) |
| 24 | `ACCEL_SNAPSHOT` | Implemented (`cmd/cmd_accel_snapshot.c`) — cached slave accel from ESP-NOW stream |
Regenerate C code:
@@ -311,20 +312,20 @@ Sets the **software** deadzone used by `bosch456.c` when logging accel (see [BMA
**Response:** `accel_deadzone_response` with applied `deadzone`, `success`, and `slaves_updated` (ESP-NOW count).
### ACCEL_READ command
### ACCEL_SNAPSHOT command
Read the **current** BMA456 accelerometer sample on this node (master or slave with sensor). Values are raw LSB in the configured **±2g** range; they are **not** filtered by the software deadzone (unlike periodic `ACC X=…` logs in `bosch456.c`).
Read **cached** accelerometer samples on the **master** (one entry per registered slave). Slaves send `ESPNOW_ACCEL_SAMPLE` to the master every **16 ms** (`esp_now_comm.c`); the master stores the latest value per client in `client_registry.c`.
**Request:** framed `18` (`0x18`) only, or `18` + empty `accel_read_request`.
**Request:** framed `18` (`0x18`) + optional `accel_snapshot_request` (`client_id`: `0` = all slaves, `>0` = one id).
**Response:** `accel_read_response`:
**Response:** `accel_snapshot_response.samples[]`:
| Field | Meaning |
|-------|---------|
| `success` | `true` if BMA456 is ready and I2C read succeeded |
| `x`, `y`, `z` | Raw accel LSB (`sint32`; meaningful only when `success`) |
If the sensor was not probed at boot (`bma456_is_ready()` false), `success` is `false` and axes are zero.
| `client_id` | Slave id (registry) |
| `valid` | At least one ESP-NOW sample received since boot |
| `x`, `y`, `z` | Raw BMA456 LSB (±2g) |
| `age_ms` | Ms since last sample from that slave |
Host:
@@ -332,7 +333,7 @@ Host:
go run . -port /dev/ttyUSB0 accel
```
Implementation: `bma456_read_accel()` in `bosch456.c` (mutex with the 10 Hz poll task), handler in `cmd/cmd_accel_read.c`.
External API (`serve -api-addr :8081`) polls this command every 16 ms and streams JSON over WebSocket.
### ESPNOW_UNICAST_TEST command
@@ -479,7 +480,7 @@ Target: ESP32-S3. Close serial monitor on the UART adapter port before running `
| `cmd/cmd_client_info.c/h` | CLIENT_INFO handler |
| `client_registry.c/h` | Registered slave table |
| `bosch456.c/h` | BMA456H I2C driver, accel poll, on-demand read, tap INT, deadzone filter |
| `cmd/cmd_accel_read.c` | UART `ACCEL_READ` — current accel XYZ |
| `cmd/cmd_accel_snapshot.c` | UART `ACCEL_SNAPSHOT` — cached slave accel |
| `board_input.c/h` | Taster GPIO12, LiPo ADC on GPIO1 / GPIO12 |
| `pod_settings.c/h` | NVS persistence (accel deadzone, …) |
| `led_ring.c/h` | LED ring (digit display, progress bar) |
+1 -1
View File
@@ -2,7 +2,7 @@
* BMA456H integration for Powerpod (ESP-IDF I2C master + Bosch SensorAPI).
*
* Polls accelerometer at 10 Hz; tap events arrive on BMA456_INT_GPIO.
* Accel logging is filtered in software (deadzone); see ACCEL_DEADZONE UART command.
* Accel logging is filtered in software (deadzone); slaves stream samples via ESP-NOW.
*/
#include "bosch456.h"
+79
View File
@@ -241,6 +241,85 @@ size_t client_registry_set_accel_deadzone_all(uint32_t deadzone) {
return n;
}
static void clear_client_accel(client_slot_t *slot) {
if (slot == NULL) {
return;
}
slot->info.accel_valid = false;
slot->info.accel_x = 0;
slot->info.accel_y = 0;
slot->info.accel_z = 0;
slot->info.accel_updated_at = 0;
}
esp_err_t client_registry_set_accel_stream(uint32_t client_id, bool enabled) {
for (size_t i = 0; i < CLIENT_REGISTRY_MAX; i++) {
if (!s_clients[i].active || s_clients[i].info.id != client_id) {
continue;
}
s_clients[i].info.accel_stream_enabled = enabled;
if (!enabled) {
clear_client_accel(&s_clients[i]);
}
return ESP_OK;
}
return ESP_ERR_NOT_FOUND;
}
esp_err_t client_registry_get_accel_stream(uint32_t client_id,
bool *enabled_out) {
if (enabled_out == NULL) {
return ESP_ERR_INVALID_ARG;
}
const client_info_t *info = client_registry_find_by_id(client_id);
if (info == NULL) {
return ESP_ERR_NOT_FOUND;
}
*enabled_out = info->accel_stream_enabled;
return ESP_OK;
}
size_t client_registry_set_accel_stream_all(bool enabled) {
size_t n = 0;
for (size_t i = 0; i < CLIENT_REGISTRY_MAX; i++) {
if (!s_clients[i].active) {
continue;
}
s_clients[i].info.accel_stream_enabled = enabled;
if (!enabled) {
clear_client_accel(&s_clients[i]);
}
n++;
}
return n;
}
esp_err_t client_registry_update_accel(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t slave_id, int16_t x, int16_t y,
int16_t z) {
if (mac == NULL) {
return ESP_ERR_INVALID_ARG;
}
client_slot_t *slot = find_slot(mac);
if (slot == NULL) {
return ESP_ERR_NOT_FOUND;
}
if (slot->info.id != slave_id) {
return ESP_ERR_INVALID_ARG;
}
if (!slot->info.accel_stream_enabled) {
return ESP_ERR_INVALID_STATE;
}
slot->info.accel_x = x;
slot->info.accel_y = y;
slot->info.accel_z = z;
slot->info.accel_valid = true;
slot->info.accel_updated_at = now_ms();
return ESP_OK;
}
const client_info_t *client_registry_at(size_t index) {
size_t n = 0;
for (size_t i = 0; i < CLIENT_REGISTRY_MAX; i++) {
+17
View File
@@ -21,6 +21,14 @@ typedef struct {
uint32_t version;
/** Accel deadzone in raw LSB per axis (master copy for ESP-NOW config). */
uint32_t accel_deadzone;
/** Latest accel from slave ESP-NOW stream (master only). */
bool accel_valid;
int16_t accel_x;
int16_t accel_y;
int16_t accel_z;
uint32_t accel_updated_at;
/** Host-enabled ESP-NOW accel stream to master. */
bool accel_stream_enabled;
} client_info_t;
#define CLIENT_REGISTRY_DEFAULT_ACCEL_DEADZONE 100u
@@ -63,4 +71,13 @@ esp_err_t client_registry_get_accel_deadzone(uint32_t client_id,
/** Push deadzone to all active registry entries; returns count updated. */
size_t client_registry_set_accel_deadzone_all(uint32_t deadzone);
/** Store latest accel sample from a slave (matched by sender MAC). */
esp_err_t client_registry_update_accel(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t slave_id, int16_t x, int16_t y,
int16_t z);
esp_err_t client_registry_set_accel_stream(uint32_t client_id, bool enabled);
esp_err_t client_registry_get_accel_stream(uint32_t client_id, bool *enabled_out);
size_t client_registry_set_accel_stream_all(bool enabled);
#endif
-34
View File
@@ -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);
}
-6
View File
@@ -1,6 +0,0 @@
#ifndef CMD_ACCEL_READ_H
#define CMD_ACCEL_READ_H
void cmd_accel_read_register(void);
#endif
+68
View File
@@ -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);
}
+6
View File
@@ -0,0 +1,6 @@
#ifndef CMD_ACCEL_SNAPSHOT_H
#define CMD_ACCEL_SNAPSHOT_H
void cmd_accel_snapshot_register(void);
#endif
+96
View File
@@ -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);
}
+6
View File
@@ -0,0 +1,6 @@
#ifndef CMD_ACCEL_STREAM_H
#define CMD_ACCEL_STREAM_H
void cmd_accel_stream_register(void);
#endif
+1
View File
@@ -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;
+4 -2
View File
@@ -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";
}
+123
View File
@@ -29,6 +29,7 @@
#define ESPNOW_CLIENT_TIMEOUT_MS \
(ESPNOW_HEARTBEAT_INTERVAL_MS * ESPNOW_HEARTBEAT_MISS_COUNT)
#define SLAVE_MASTER_LOST_MS (ESPNOW_HEARTBEAT_INTERVAL_MS * 5)
#define ESPNOW_ACCEL_INTERVAL_MS 16
static const uint8_t ESPNOW_BCAST[ESP_NOW_ETH_ALEN] = {0xff, 0xff, 0xff,
0xff, 0xff, 0xff};
@@ -39,6 +40,7 @@ static app_config_t s_config;
static uint8_t s_wifi_channel;
static uint8_t s_own_mac[ESP_NOW_ETH_ALEN];
static bool s_slave_joined;
static bool s_accel_stream_enabled;
static uint8_t s_master_mac[ESP_NOW_ETH_ALEN];
static uint32_t s_last_discover_ms;
@@ -111,6 +113,18 @@ static esp_err_t send_message(const uint8_t *dest_mac,
return send_message_ex(dest_mac, msg, false);
}
static esp_err_t send_accel_sample(const uint8_t *dest_mac, uint32_t slave_id,
int16_t x, int16_t y, int16_t z) {
alox_EspNowMessage msg = alox_EspNowMessage_init_zero;
msg.type = alox_EspNowMessageType_ESPNOW_ACCEL_SAMPLE;
msg.which_payload = alox_EspNowMessage_accel_sample_tag;
msg.payload.accel_sample.slave_id = slave_id;
msg.payload.accel_sample.x = x;
msg.payload.accel_sample.y = y;
msg.payload.accel_sample.z = z;
return send_message(dest_mac, &msg);
}
static esp_err_t send_message_ex(const uint8_t *dest_mac,
const alox_EspNowMessage *msg, bool wait_done) {
uint8_t buf[ESPNOW_PB_MAX_SIZE];
@@ -151,6 +165,18 @@ static esp_err_t send_message_ex(const uint8_t *dest_mac,
return err;
}
static esp_err_t send_accel_stream(const uint8_t *dest_mac, uint32_t client_id,
bool enable) {
alox_EspNowMessage msg = alox_EspNowMessage_init_zero;
msg.type = alox_EspNowMessageType_ESPNOW_SET_ACCEL_STREAM;
msg.which_payload = alox_EspNowMessage_accel_stream_tag;
msg.payload.accel_stream.enable = enable;
msg.payload.accel_stream.client_id = client_id;
return send_message(dest_mac, &msg);
}
static esp_err_t send_accel_deadzone(const uint8_t *dest_mac, uint32_t client_id,
uint32_t deadzone) {
alox_EspNowMessage msg = alox_EspNowMessage_init_zero;
@@ -338,6 +364,25 @@ esp_err_t esp_now_comm_send_unicast_test(const uint8_t mac[CLIENT_MAC_LEN],
return err;
}
esp_err_t esp_now_comm_send_accel_stream(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t client_id, bool enable) {
if (mac == NULL || !s_config.master) {
return ESP_ERR_INVALID_STATE;
}
char mac_str[18];
mac_to_str(mac, mac_str, sizeof(mac_str));
esp_err_t err = send_accel_stream(mac, client_id, enable);
if (err == ESP_OK) {
ESP_LOGI(TAG, "unicast SET_ACCEL_STREAM to %s: %s client_id=%lu", mac_str,
enable ? "on" : "off", (unsigned long)client_id);
} else {
ESP_LOGW(TAG, "unicast SET_ACCEL_STREAM to %s failed: %s", mac_str,
esp_err_to_name(err));
}
return err;
}
esp_err_t esp_now_comm_send_accel_deadzone(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t client_id, uint32_t deadzone) {
if (mac == NULL || !s_config.master) {
@@ -377,6 +422,7 @@ static void send_presence(const uint8_t *dest_mac,
static void slave_reset_join(void) {
s_slave_joined = false;
s_accel_stream_enabled = false;
memset(s_master_mac, 0, sizeof(s_master_mac));
s_last_discover_ms = 0;
}
@@ -426,6 +472,25 @@ static void handle_slave_find_me(const uint8_t *master_mac,
led_ring_find_me();
}
static void handle_slave_accel_stream(const uint8_t *master_mac,
const alox_EspNowAccelStream *cfg) {
uint32_t my_id = s_own_mac[5];
if (cfg->client_id != 0 && cfg->client_id != my_id) {
return;
}
if (s_slave_joined && !mac_equal(master_mac, s_master_mac)) {
return;
}
s_accel_stream_enabled = cfg->enable;
char mac_str[18];
mac_to_str(master_mac, mac_str, sizeof(mac_str));
ESP_LOGI(TAG, "accel stream %s from master %s (id=%lu)",
cfg->enable ? "on" : "off", mac_str, (unsigned long)my_id);
}
static void handle_slave_accel_deadzone(const uint8_t *master_mac,
const alox_EspNowAccelDeadzone *cfg) {
uint32_t my_id = s_own_mac[5];
@@ -453,6 +518,23 @@ static void handle_slave_accel_deadzone(const uint8_t *master_mac,
}
}
static void handle_master_accel_sample(const uint8_t mac[CLIENT_MAC_LEN],
const alox_EspNowAccelSample *sample) {
if (sample == NULL) {
return;
}
esp_err_t err = client_registry_update_accel(
mac, sample->slave_id, (int16_t)sample->x, (int16_t)sample->y,
(int16_t)sample->z);
if (err == ESP_ERR_NOT_FOUND) {
return;
}
if (err != ESP_OK) {
ESP_LOGW(TAG, "accel sample id mismatch from %02x:…:%02x", mac[0], mac[5]);
}
}
static void handle_client_presence(const alox_EspNowSlavePresence *presence,
const uint8_t mac[CLIENT_MAC_LEN]) {
if (presence->network != s_config.network) {
@@ -533,6 +615,30 @@ static void slave_check_master_timeout(void) {
}
}
static void slave_accel_stream_task(void *param) {
(void)param;
ESP_LOGI(TAG, "slave accel stream task (interval %u ms)",
(unsigned)ESPNOW_ACCEL_INTERVAL_MS);
while (1) {
vTaskDelay(pdMS_TO_TICKS(ESPNOW_ACCEL_INTERVAL_MS));
if (!s_slave_joined || !s_accel_stream_enabled || !bma456_is_ready()) {
continue;
}
int16_t x = 0;
int16_t y = 0;
int16_t z = 0;
if (bma456_read_accel(&x, &y, &z) != ESP_OK) {
continue;
}
(void)send_accel_sample(s_master_mac, s_own_mac[5], x, y, z);
}
}
static void slave_heartbeat_task(void *param) {
(void)param;
@@ -592,6 +698,12 @@ static void espnow_recv_cb(const esp_now_recv_info_t *info, const uint8_t *data,
case alox_EspNowMessage_accel_deadzone_tag:
handle_slave_accel_deadzone(info->src_addr, &msg.payload.accel_deadzone);
break;
case alox_EspNowMessage_accel_stream_tag:
if (!s_slave_joined || !mac_equal(info->src_addr, s_master_mac)) {
break;
}
handle_slave_accel_stream(info->src_addr, &msg.payload.accel_stream);
break;
case alox_EspNowMessage_find_me_tag:
if (!s_slave_joined || !mac_equal(info->src_addr, s_master_mac)) {
break;
@@ -639,6 +751,12 @@ static void espnow_recv_cb(const esp_now_recv_info_t *info, const uint8_t *data,
return;
}
if (msg.which_payload == alox_EspNowMessage_accel_sample_tag) {
ensure_peer(info->src_addr);
handle_master_accel_sample(info->src_addr, &msg.payload.accel_sample);
return;
}
const alox_EspNowSlavePresence *presence = esp_now_proto_get_presence(&msg);
if (presence != NULL) {
/* Registry key is the ESP-NOW sender MAC, not the optional protobuf mac field. */
@@ -739,6 +857,11 @@ esp_err_t esp_now_comm_init(const app_config_t *config) {
ESP_LOGE(TAG, "failed to create heartbeat task");
return ESP_FAIL;
}
if (xTaskCreate(slave_accel_stream_task, "espnow_accel", 4096, NULL, 5,
NULL) != pdPASS) {
ESP_LOGE(TAG, "failed to create accel stream task");
return ESP_FAIL;
}
}
return ESP_OK;
+4
View File
@@ -7,6 +7,10 @@
esp_err_t esp_now_comm_init(const app_config_t *config);
/** Master: enable/disable accel ESP-NOW stream on one slave. */
esp_err_t esp_now_comm_send_accel_stream(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t client_id, bool enable);
/** Master: unicast accel deadzone to one slave (client_id is echoed for filtering). */
esp_err_t esp_now_comm_send_accel_deadzone(const uint8_t mac[CLIENT_MAC_LEN],
uint32_t client_id, uint32_t deadzone);
+4 -2
View File
@@ -1,7 +1,8 @@
#include "app_config.h"
#include "cmd_handler.h"
#include "cmd_accel_deadzone.h"
#include "cmd_accel_read.h"
#include "cmd_accel_snapshot.h"
#include "cmd_accel_stream.h"
#include "cmd_espnow_unicast_test.h"
#include "cmd_espnow_find_me.h"
#include "cmd_restart.h"
@@ -178,7 +179,8 @@ void app_main(void) {
cmd_version_register();
cmd_client_info_register();
cmd_accel_deadzone_register();
cmd_accel_read_register();
cmd_accel_snapshot_register();
cmd_accel_stream_register();
cmd_espnow_unicast_test_register();
cmd_espnow_find_me_register();
cmd_restart_register();
+6
View File
@@ -24,6 +24,12 @@ PB_BIND(alox_EspNowSlavePresence, alox_EspNowSlavePresence, AUTO)
PB_BIND(alox_EspNowAccelDeadzone, alox_EspNowAccelDeadzone, AUTO)
PB_BIND(alox_EspNowAccelStream, alox_EspNowAccelStream, AUTO)
PB_BIND(alox_EspNowAccelSample, alox_EspNowAccelSample, AUTO)
PB_BIND(alox_EspNowOtaStart, alox_EspNowOtaStart, AUTO)
+60 -4
View File
@@ -22,7 +22,9 @@ typedef enum _alox_EspNowMessageType {
alox_EspNowMessageType_ESPNOW_OTA_END = 8,
alox_EspNowMessageType_ESPNOW_OTA_STATUS = 9,
alox_EspNowMessageType_ESPNOW_FIND_ME = 10,
alox_EspNowMessageType_ESPNOW_RESTART = 11
alox_EspNowMessageType_ESPNOW_RESTART = 11,
alox_EspNowMessageType_ESPNOW_ACCEL_SAMPLE = 12,
alox_EspNowMessageType_ESPNOW_SET_ACCEL_STREAM = 13
} alox_EspNowMessageType;
/* Struct definitions */
@@ -59,6 +61,20 @@ typedef struct _alox_EspNowAccelDeadzone {
uint32_t client_id; /* 0 = all slaves; otherwise only matching slave_id applies */
} alox_EspNowAccelDeadzone;
/* * Master → slave: enable/disable periodic accel ESP-NOW stream (~16 ms). */
typedef struct _alox_EspNowAccelStream {
bool enable;
uint32_t client_id;
} alox_EspNowAccelStream;
/* * Slave → master: latest BMA456 sample (sent ~every 16 ms). */
typedef struct _alox_EspNowAccelSample {
uint32_t slave_id;
int32_t x;
int32_t y;
int32_t z;
} alox_EspNowAccelSample;
/* Master → slave: begin OTA (erase inactive slot; slave replies ESPNOW_OTA_STATUS). */
typedef struct _alox_EspNowOtaStart {
uint32_t total_size;
@@ -98,6 +114,8 @@ typedef struct _alox_EspNowMessage {
alox_EspNowOtaStatus ota_status;
alox_EspNowFindMe find_me;
alox_EspNowRestart restart;
alox_EspNowAccelSample accel_sample;
alox_EspNowAccelStream accel_stream;
} payload;
} alox_EspNowMessage;
@@ -108,8 +126,10 @@ extern "C" {
/* Helper constants for enums */
#define _alox_EspNowMessageType_MIN alox_EspNowMessageType_ESPNOW_UNKNOWN
#define _alox_EspNowMessageType_MAX alox_EspNowMessageType_ESPNOW_RESTART
#define _alox_EspNowMessageType_ARRAYSIZE ((alox_EspNowMessageType)(alox_EspNowMessageType_ESPNOW_RESTART+1))
#define _alox_EspNowMessageType_MAX alox_EspNowMessageType_ESPNOW_SET_ACCEL_STREAM
#define _alox_EspNowMessageType_ARRAYSIZE ((alox_EspNowMessageType)(alox_EspNowMessageType_ESPNOW_SET_ACCEL_STREAM+1))
@@ -131,6 +151,8 @@ extern "C" {
#define alox_EspNowDiscover_init_default {0}
#define alox_EspNowSlavePresence_init_default {0, {{NULL}, NULL}, 0, 0, 0, 0}
#define alox_EspNowAccelDeadzone_init_default {0, 0}
#define alox_EspNowAccelStream_init_default {0, 0}
#define alox_EspNowAccelSample_init_default {0, 0, 0, 0}
#define alox_EspNowOtaStart_init_default {0}
#define alox_EspNowOtaPayload_init_default {0, {0, {0}}}
#define alox_EspNowOtaEnd_init_default {0}
@@ -142,6 +164,8 @@ extern "C" {
#define alox_EspNowDiscover_init_zero {0}
#define alox_EspNowSlavePresence_init_zero {0, {{NULL}, NULL}, 0, 0, 0, 0}
#define alox_EspNowAccelDeadzone_init_zero {0, 0}
#define alox_EspNowAccelStream_init_zero {0, 0}
#define alox_EspNowAccelSample_init_zero {0, 0, 0, 0}
#define alox_EspNowOtaStart_init_zero {0}
#define alox_EspNowOtaPayload_init_zero {0, {0, {0}}}
#define alox_EspNowOtaEnd_init_zero {0}
@@ -161,6 +185,12 @@ extern "C" {
#define alox_EspNowSlavePresence_used_tag 6
#define alox_EspNowAccelDeadzone_deadzone_tag 1
#define alox_EspNowAccelDeadzone_client_id_tag 2
#define alox_EspNowAccelStream_enable_tag 1
#define alox_EspNowAccelStream_client_id_tag 2
#define alox_EspNowAccelSample_slave_id_tag 1
#define alox_EspNowAccelSample_x_tag 2
#define alox_EspNowAccelSample_y_tag 3
#define alox_EspNowAccelSample_z_tag 4
#define alox_EspNowOtaStart_total_size_tag 1
#define alox_EspNowOtaPayload_seq_tag 1
#define alox_EspNowOtaPayload_data_tag 2
@@ -179,6 +209,8 @@ extern "C" {
#define alox_EspNowMessage_ota_status_tag 10
#define alox_EspNowMessage_find_me_tag 11
#define alox_EspNowMessage_restart_tag 12
#define alox_EspNowMessage_accel_sample_tag 13
#define alox_EspNowMessage_accel_stream_tag 14
/* Struct field encoding specification for nanopb */
#define alox_EspNowUnicastTest_FIELDLIST(X, a) \
@@ -217,6 +249,20 @@ X(a, STATIC, SINGULAR, UINT32, client_id, 2)
#define alox_EspNowAccelDeadzone_CALLBACK NULL
#define alox_EspNowAccelDeadzone_DEFAULT NULL
#define alox_EspNowAccelStream_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, BOOL, enable, 1) \
X(a, STATIC, SINGULAR, UINT32, client_id, 2)
#define alox_EspNowAccelStream_CALLBACK NULL
#define alox_EspNowAccelStream_DEFAULT NULL
#define alox_EspNowAccelSample_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, slave_id, 1) \
X(a, STATIC, SINGULAR, SINT32, x, 2) \
X(a, STATIC, SINGULAR, SINT32, y, 3) \
X(a, STATIC, SINGULAR, SINT32, z, 4)
#define alox_EspNowAccelSample_CALLBACK NULL
#define alox_EspNowAccelSample_DEFAULT NULL
#define alox_EspNowOtaStart_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, total_size, 1)
#define alox_EspNowOtaStart_CALLBACK NULL
@@ -252,7 +298,9 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,ota_payload,payload.ota_payload),
X(a, STATIC, ONEOF, MESSAGE, (payload,ota_end,payload.ota_end), 9) \
X(a, STATIC, ONEOF, MESSAGE, (payload,ota_status,payload.ota_status), 10) \
X(a, STATIC, ONEOF, MESSAGE, (payload,find_me,payload.find_me), 11) \
X(a, STATIC, ONEOF, MESSAGE, (payload,restart,payload.restart), 12)
X(a, STATIC, ONEOF, MESSAGE, (payload,restart,payload.restart), 12) \
X(a, STATIC, ONEOF, MESSAGE, (payload,accel_sample,payload.accel_sample), 13) \
X(a, STATIC, ONEOF, MESSAGE, (payload,accel_stream,payload.accel_stream), 14)
#define alox_EspNowMessage_CALLBACK NULL
#define alox_EspNowMessage_DEFAULT NULL
#define alox_EspNowMessage_payload_discover_MSGTYPE alox_EspNowDiscover
@@ -266,6 +314,8 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,restart,payload.restart), 12)
#define alox_EspNowMessage_payload_ota_status_MSGTYPE alox_EspNowOtaStatus
#define alox_EspNowMessage_payload_find_me_MSGTYPE alox_EspNowFindMe
#define alox_EspNowMessage_payload_restart_MSGTYPE alox_EspNowRestart
#define alox_EspNowMessage_payload_accel_sample_MSGTYPE alox_EspNowAccelSample
#define alox_EspNowMessage_payload_accel_stream_MSGTYPE alox_EspNowAccelStream
extern const pb_msgdesc_t alox_EspNowUnicastTest_msg;
extern const pb_msgdesc_t alox_EspNowFindMe_msg;
@@ -273,6 +323,8 @@ extern const pb_msgdesc_t alox_EspNowRestart_msg;
extern const pb_msgdesc_t alox_EspNowDiscover_msg;
extern const pb_msgdesc_t alox_EspNowSlavePresence_msg;
extern const pb_msgdesc_t alox_EspNowAccelDeadzone_msg;
extern const pb_msgdesc_t alox_EspNowAccelStream_msg;
extern const pb_msgdesc_t alox_EspNowAccelSample_msg;
extern const pb_msgdesc_t alox_EspNowOtaStart_msg;
extern const pb_msgdesc_t alox_EspNowOtaPayload_msg;
extern const pb_msgdesc_t alox_EspNowOtaEnd_msg;
@@ -286,6 +338,8 @@ extern const pb_msgdesc_t alox_EspNowMessage_msg;
#define alox_EspNowDiscover_fields &alox_EspNowDiscover_msg
#define alox_EspNowSlavePresence_fields &alox_EspNowSlavePresence_msg
#define alox_EspNowAccelDeadzone_fields &alox_EspNowAccelDeadzone_msg
#define alox_EspNowAccelStream_fields &alox_EspNowAccelStream_msg
#define alox_EspNowAccelSample_fields &alox_EspNowAccelSample_msg
#define alox_EspNowOtaStart_fields &alox_EspNowOtaStart_msg
#define alox_EspNowOtaPayload_fields &alox_EspNowOtaPayload_msg
#define alox_EspNowOtaEnd_fields &alox_EspNowOtaEnd_msg
@@ -297,6 +351,8 @@ extern const pb_msgdesc_t alox_EspNowMessage_msg;
/* alox_EspNowMessage_size depends on runtime parameters */
#define ALOX_ESP_NOW_MESSAGES_PB_H_MAX_SIZE alox_EspNowOtaPayload_size
#define alox_EspNowAccelDeadzone_size 12
#define alox_EspNowAccelSample_size 24
#define alox_EspNowAccelStream_size 8
#define alox_EspNowDiscover_size 6
#define alox_EspNowFindMe_size 6
#define alox_EspNowOtaEnd_size 0
+18
View File
@@ -17,6 +17,8 @@ enum EspNowMessageType {
ESPNOW_OTA_STATUS = 9;
ESPNOW_FIND_ME = 10;
ESPNOW_RESTART = 11;
ESPNOW_ACCEL_SAMPLE = 12;
ESPNOW_SET_ACCEL_STREAM = 13;
}
message EspNowUnicastTest {
@@ -52,6 +54,20 @@ message EspNowAccelDeadzone {
uint32 client_id = 2; // 0 = all slaves; otherwise only matching slave_id applies
}
/** Master → slave: enable/disable periodic accel ESP-NOW stream (~16 ms). */
message EspNowAccelStream {
bool enable = 1;
uint32 client_id = 2;
}
/** Slave → master: latest BMA456 sample (sent ~every 16 ms). */
message EspNowAccelSample {
uint32 slave_id = 1;
sint32 x = 2;
sint32 y = 3;
sint32 z = 4;
}
// Master → slave: begin OTA (erase inactive slot; slave replies ESPNOW_OTA_STATUS).
message EspNowOtaStart {
uint32 total_size = 1;
@@ -87,5 +103,7 @@ message EspNowMessage {
EspNowOtaStatus ota_status = 10;
EspNowFindMe find_me = 11;
EspNowRestart restart = 12;
EspNowAccelSample accel_sample = 13;
EspNowAccelStream accel_stream = 14;
}
}
+11 -2
View File
@@ -36,10 +36,19 @@ PB_BIND(alox_AccelDeadzoneRequest, alox_AccelDeadzoneRequest, AUTO)
PB_BIND(alox_AccelDeadzoneResponse, alox_AccelDeadzoneResponse, AUTO)
PB_BIND(alox_AccelReadRequest, alox_AccelReadRequest, AUTO)
PB_BIND(alox_AccelStreamRequest, alox_AccelStreamRequest, AUTO)
PB_BIND(alox_AccelReadResponse, alox_AccelReadResponse, AUTO)
PB_BIND(alox_AccelStreamResponse, alox_AccelStreamResponse, AUTO)
PB_BIND(alox_AccelSnapshotRequest, alox_AccelSnapshotRequest, AUTO)
PB_BIND(alox_AccelSample, alox_AccelSample, AUTO)
PB_BIND(alox_AccelSnapshotResponse, alox_AccelSnapshotResponse, 2)
PB_BIND(alox_EspNowUnicastTestRequest, alox_EspNowUnicastTestRequest, AUTO)
+137 -45
View File
@@ -28,7 +28,8 @@ typedef enum _alox_MessageType {
alox_MessageType_OTA_SLAVE_PROGRESS = 21,
alox_MessageType_FIND_ME = 22,
alox_MessageType_RESTART = 23,
alox_MessageType_ACCEL_READ = 24
alox_MessageType_ACCEL_SNAPSHOT = 24,
alox_MessageType_ACCEL_STREAM = 25
} alox_MessageType;
/* Struct definitions */
@@ -55,6 +56,8 @@ typedef struct _alox_ClientInfo {
uint32_t last_ping;
uint32_t last_success_ping;
uint32_t version;
/* * Master: ESP-NOW accel stream enabled for this slave. */
bool accel_stream_enabled;
} alox_ClientInfo;
typedef struct _alox_ClientInfoResponse {
@@ -89,17 +92,42 @@ typedef struct _alox_AccelDeadzoneResponse {
uint32_t slaves_updated;
} alox_AccelDeadzoneResponse;
/* Host → device: read current BMA456 accelerometer sample (raw LSB, ±2g range). */
typedef struct _alox_AccelReadRequest {
char dummy_field;
} alox_AccelReadRequest;
/* Host → master: enable/disable slave accel ESP-NOW stream (~16 ms per slave).
write=false: read; write=true: apply. client_id 0 invalid for write (use >0 or all_clients). */
typedef struct _alox_AccelStreamRequest {
bool write;
bool enable;
uint32_t client_id;
bool all_clients;
} alox_AccelStreamRequest;
typedef struct _alox_AccelReadResponse {
typedef struct _alox_AccelStreamResponse {
bool enabled;
uint32_t client_id;
bool success;
uint32_t slaves_updated;
} alox_AccelStreamResponse;
/* Host → master: read cached accel samples from slaves (only while stream enabled).
client_id 0 = all registered slaves; otherwise one slave. */
typedef struct _alox_AccelSnapshotRequest {
uint32_t client_id;
} alox_AccelSnapshotRequest;
typedef struct _alox_AccelSample {
uint32_t client_id;
bool valid;
int32_t x;
int32_t y;
int32_t z;
} alox_AccelReadResponse;
/* * Milliseconds since last ESP-NOW sample from this slave. */
uint32_t age_ms;
} alox_AccelSample;
typedef struct _alox_AccelSnapshotResponse {
pb_size_t samples_count;
alox_AccelSample samples[16];
} alox_AccelSnapshotResponse;
typedef struct _alox_EspNowUnicastTestRequest {
uint32_t client_id;
@@ -231,8 +259,10 @@ typedef struct _alox_UartMessage {
alox_EspNowFindMeResponse espnow_find_me_response;
alox_RestartRequest restart_request;
alox_RestartResponse restart_response;
alox_AccelReadRequest accel_read_request;
alox_AccelReadResponse accel_read_response;
alox_AccelSnapshotRequest accel_snapshot_request;
alox_AccelSnapshotResponse accel_snapshot_response;
alox_AccelStreamRequest accel_stream_request;
alox_AccelStreamResponse accel_stream_response;
} payload;
} alox_UartMessage;
@@ -243,8 +273,8 @@ extern "C" {
/* Helper constants for enums */
#define _alox_MessageType_MIN alox_MessageType_UNKNOWN
#define _alox_MessageType_MAX alox_MessageType_ACCEL_READ
#define _alox_MessageType_ARRAYSIZE ((alox_MessageType)(alox_MessageType_ACCEL_READ+1))
#define _alox_MessageType_MAX alox_MessageType_ACCEL_STREAM
#define _alox_MessageType_ARRAYSIZE ((alox_MessageType)(alox_MessageType_ACCEL_STREAM+1))
#define alox_UartMessage_type_ENUMTYPE alox_MessageType
@@ -271,6 +301,9 @@ extern "C" {
@@ -280,14 +313,17 @@ extern "C" {
#define alox_Ack_init_default {0}
#define alox_EchoPayload_init_default {{{NULL}, NULL}}
#define alox_VersionResponse_init_default {0, {{NULL}, NULL}, {{NULL}, NULL}}
#define alox_ClientInfo_init_default {0, 0, 0, {{NULL}, NULL}, 0, 0, 0}
#define alox_ClientInfo_init_default {0, 0, 0, {{NULL}, NULL}, 0, 0, 0, 0}
#define alox_ClientInfoResponse_init_default {{{NULL}, NULL}}
#define alox_ClientInput_init_default {0, 0, 0, 0}
#define alox_ClientInputResponse_init_default {{{NULL}, NULL}}
#define alox_AccelDeadzoneRequest_init_default {0, 0, 0, 0}
#define alox_AccelDeadzoneResponse_init_default {0, 0, 0, 0}
#define alox_AccelReadRequest_init_default {0}
#define alox_AccelReadResponse_init_default {0, 0, 0, 0}
#define alox_AccelStreamRequest_init_default {0, 0, 0, 0}
#define alox_AccelStreamResponse_init_default {0, 0, 0, 0}
#define alox_AccelSnapshotRequest_init_default {0}
#define alox_AccelSample_init_default {0, 0, 0, 0, 0, 0}
#define alox_AccelSnapshotResponse_init_default {0, {alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default, alox_AccelSample_init_default}}
#define alox_EspNowUnicastTestRequest_init_default {0, 0}
#define alox_EspNowUnicastTestResponse_init_default {0, 0}
#define alox_LedRingProgressRequest_init_default {0, 0, 0, 0, 0, 0, 0, 0, 0}
@@ -307,14 +343,17 @@ extern "C" {
#define alox_Ack_init_zero {0}
#define alox_EchoPayload_init_zero {{{NULL}, NULL}}
#define alox_VersionResponse_init_zero {0, {{NULL}, NULL}, {{NULL}, NULL}}
#define alox_ClientInfo_init_zero {0, 0, 0, {{NULL}, NULL}, 0, 0, 0}
#define alox_ClientInfo_init_zero {0, 0, 0, {{NULL}, NULL}, 0, 0, 0, 0}
#define alox_ClientInfoResponse_init_zero {{{NULL}, NULL}}
#define alox_ClientInput_init_zero {0, 0, 0, 0}
#define alox_ClientInputResponse_init_zero {{{NULL}, NULL}}
#define alox_AccelDeadzoneRequest_init_zero {0, 0, 0, 0}
#define alox_AccelDeadzoneResponse_init_zero {0, 0, 0, 0}
#define alox_AccelReadRequest_init_zero {0}
#define alox_AccelReadResponse_init_zero {0, 0, 0, 0}
#define alox_AccelStreamRequest_init_zero {0, 0, 0, 0}
#define alox_AccelStreamResponse_init_zero {0, 0, 0, 0}
#define alox_AccelSnapshotRequest_init_zero {0}
#define alox_AccelSample_init_zero {0, 0, 0, 0, 0, 0}
#define alox_AccelSnapshotResponse_init_zero {0, {alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero, alox_AccelSample_init_zero}}
#define alox_EspNowUnicastTestRequest_init_zero {0, 0}
#define alox_EspNowUnicastTestResponse_init_zero {0, 0}
#define alox_LedRingProgressRequest_init_zero {0, 0, 0, 0, 0, 0, 0, 0, 0}
@@ -343,6 +382,7 @@ extern "C" {
#define alox_ClientInfo_last_ping_tag 5
#define alox_ClientInfo_last_success_ping_tag 6
#define alox_ClientInfo_version_tag 7
#define alox_ClientInfo_accel_stream_enabled_tag 8
#define alox_ClientInfoResponse_clients_tag 1
#define alox_ClientInput_id_tag 1
#define alox_ClientInput_lage_x_tag 2
@@ -357,10 +397,22 @@ extern "C" {
#define alox_AccelDeadzoneResponse_client_id_tag 2
#define alox_AccelDeadzoneResponse_success_tag 3
#define alox_AccelDeadzoneResponse_slaves_updated_tag 4
#define alox_AccelReadResponse_success_tag 1
#define alox_AccelReadResponse_x_tag 2
#define alox_AccelReadResponse_y_tag 3
#define alox_AccelReadResponse_z_tag 4
#define alox_AccelStreamRequest_write_tag 1
#define alox_AccelStreamRequest_enable_tag 2
#define alox_AccelStreamRequest_client_id_tag 3
#define alox_AccelStreamRequest_all_clients_tag 4
#define alox_AccelStreamResponse_enabled_tag 1
#define alox_AccelStreamResponse_client_id_tag 2
#define alox_AccelStreamResponse_success_tag 3
#define alox_AccelStreamResponse_slaves_updated_tag 4
#define alox_AccelSnapshotRequest_client_id_tag 1
#define alox_AccelSample_client_id_tag 1
#define alox_AccelSample_valid_tag 2
#define alox_AccelSample_x_tag 3
#define alox_AccelSample_y_tag 4
#define alox_AccelSample_z_tag 5
#define alox_AccelSample_age_ms_tag 6
#define alox_AccelSnapshotResponse_samples_tag 1
#define alox_EspNowUnicastTestRequest_client_id_tag 1
#define alox_EspNowUnicastTestRequest_seq_tag 2
#define alox_EspNowUnicastTestResponse_success_tag 1
@@ -424,8 +476,10 @@ extern "C" {
#define alox_UartMessage_espnow_find_me_response_tag 20
#define alox_UartMessage_restart_request_tag 21
#define alox_UartMessage_restart_response_tag 22
#define alox_UartMessage_accel_read_request_tag 23
#define alox_UartMessage_accel_read_response_tag 24
#define alox_UartMessage_accel_snapshot_request_tag 23
#define alox_UartMessage_accel_snapshot_response_tag 24
#define alox_UartMessage_accel_stream_request_tag 25
#define alox_UartMessage_accel_stream_response_tag 26
/* Struct field encoding specification for nanopb */
#define alox_UartMessage_FIELDLIST(X, a) \
@@ -451,8 +505,10 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,espnow_find_me_request,payload.espno
X(a, STATIC, ONEOF, MESSAGE, (payload,espnow_find_me_response,payload.espnow_find_me_response), 20) \
X(a, STATIC, ONEOF, MESSAGE, (payload,restart_request,payload.restart_request), 21) \
X(a, STATIC, ONEOF, MESSAGE, (payload,restart_response,payload.restart_response), 22) \
X(a, STATIC, ONEOF, MESSAGE, (payload,accel_read_request,payload.accel_read_request), 23) \
X(a, STATIC, ONEOF, MESSAGE, (payload,accel_read_response,payload.accel_read_response), 24)
X(a, STATIC, ONEOF, MESSAGE, (payload,accel_snapshot_request,payload.accel_snapshot_request), 23) \
X(a, STATIC, ONEOF, MESSAGE, (payload,accel_snapshot_response,payload.accel_snapshot_response), 24) \
X(a, STATIC, ONEOF, MESSAGE, (payload,accel_stream_request,payload.accel_stream_request), 25) \
X(a, STATIC, ONEOF, MESSAGE, (payload,accel_stream_response,payload.accel_stream_response), 26)
#define alox_UartMessage_CALLBACK NULL
#define alox_UartMessage_DEFAULT NULL
#define alox_UartMessage_payload_ack_payload_MSGTYPE alox_Ack
@@ -476,8 +532,10 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,accel_read_response,payload.accel_re
#define alox_UartMessage_payload_espnow_find_me_response_MSGTYPE alox_EspNowFindMeResponse
#define alox_UartMessage_payload_restart_request_MSGTYPE alox_RestartRequest
#define alox_UartMessage_payload_restart_response_MSGTYPE alox_RestartResponse
#define alox_UartMessage_payload_accel_read_request_MSGTYPE alox_AccelReadRequest
#define alox_UartMessage_payload_accel_read_response_MSGTYPE alox_AccelReadResponse
#define alox_UartMessage_payload_accel_snapshot_request_MSGTYPE alox_AccelSnapshotRequest
#define alox_UartMessage_payload_accel_snapshot_response_MSGTYPE alox_AccelSnapshotResponse
#define alox_UartMessage_payload_accel_stream_request_MSGTYPE alox_AccelStreamRequest
#define alox_UartMessage_payload_accel_stream_response_MSGTYPE alox_AccelStreamResponse
#define alox_Ack_FIELDLIST(X, a) \
@@ -503,7 +561,8 @@ X(a, STATIC, SINGULAR, BOOL, used, 3) \
X(a, CALLBACK, SINGULAR, BYTES, mac, 4) \
X(a, STATIC, SINGULAR, UINT32, last_ping, 5) \
X(a, STATIC, SINGULAR, UINT32, last_success_ping, 6) \
X(a, STATIC, SINGULAR, UINT32, version, 7)
X(a, STATIC, SINGULAR, UINT32, version, 7) \
X(a, STATIC, SINGULAR, BOOL, accel_stream_enabled, 8)
#define alox_ClientInfo_CALLBACK pb_default_field_callback
#define alox_ClientInfo_DEFAULT NULL
@@ -543,18 +602,42 @@ X(a, STATIC, SINGULAR, UINT32, slaves_updated, 4)
#define alox_AccelDeadzoneResponse_CALLBACK NULL
#define alox_AccelDeadzoneResponse_DEFAULT NULL
#define alox_AccelReadRequest_FIELDLIST(X, a) \
#define alox_AccelStreamRequest_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, BOOL, write, 1) \
X(a, STATIC, SINGULAR, BOOL, enable, 2) \
X(a, STATIC, SINGULAR, UINT32, client_id, 3) \
X(a, STATIC, SINGULAR, BOOL, all_clients, 4)
#define alox_AccelStreamRequest_CALLBACK NULL
#define alox_AccelStreamRequest_DEFAULT NULL
#define alox_AccelReadRequest_CALLBACK NULL
#define alox_AccelReadRequest_DEFAULT NULL
#define alox_AccelStreamResponse_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, BOOL, enabled, 1) \
X(a, STATIC, SINGULAR, UINT32, client_id, 2) \
X(a, STATIC, SINGULAR, BOOL, success, 3) \
X(a, STATIC, SINGULAR, UINT32, slaves_updated, 4)
#define alox_AccelStreamResponse_CALLBACK NULL
#define alox_AccelStreamResponse_DEFAULT NULL
#define alox_AccelReadResponse_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, BOOL, success, 1) \
X(a, STATIC, SINGULAR, SINT32, x, 2) \
X(a, STATIC, SINGULAR, SINT32, y, 3) \
X(a, STATIC, SINGULAR, SINT32, z, 4)
#define alox_AccelReadResponse_CALLBACK NULL
#define alox_AccelReadResponse_DEFAULT NULL
#define alox_AccelSnapshotRequest_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, client_id, 1)
#define alox_AccelSnapshotRequest_CALLBACK NULL
#define alox_AccelSnapshotRequest_DEFAULT NULL
#define alox_AccelSample_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, client_id, 1) \
X(a, STATIC, SINGULAR, BOOL, valid, 2) \
X(a, STATIC, SINGULAR, SINT32, x, 3) \
X(a, STATIC, SINGULAR, SINT32, y, 4) \
X(a, STATIC, SINGULAR, SINT32, z, 5) \
X(a, STATIC, SINGULAR, UINT32, age_ms, 6)
#define alox_AccelSample_CALLBACK NULL
#define alox_AccelSample_DEFAULT NULL
#define alox_AccelSnapshotResponse_FIELDLIST(X, a) \
X(a, STATIC, REPEATED, MESSAGE, samples, 1)
#define alox_AccelSnapshotResponse_CALLBACK NULL
#define alox_AccelSnapshotResponse_DEFAULT NULL
#define alox_AccelSnapshotResponse_samples_MSGTYPE alox_AccelSample
#define alox_EspNowUnicastTestRequest_FIELDLIST(X, a) \
X(a, STATIC, SINGULAR, UINT32, client_id, 1) \
@@ -669,8 +752,11 @@ extern const pb_msgdesc_t alox_ClientInput_msg;
extern const pb_msgdesc_t alox_ClientInputResponse_msg;
extern const pb_msgdesc_t alox_AccelDeadzoneRequest_msg;
extern const pb_msgdesc_t alox_AccelDeadzoneResponse_msg;
extern const pb_msgdesc_t alox_AccelReadRequest_msg;
extern const pb_msgdesc_t alox_AccelReadResponse_msg;
extern const pb_msgdesc_t alox_AccelStreamRequest_msg;
extern const pb_msgdesc_t alox_AccelStreamResponse_msg;
extern const pb_msgdesc_t alox_AccelSnapshotRequest_msg;
extern const pb_msgdesc_t alox_AccelSample_msg;
extern const pb_msgdesc_t alox_AccelSnapshotResponse_msg;
extern const pb_msgdesc_t alox_EspNowUnicastTestRequest_msg;
extern const pb_msgdesc_t alox_EspNowUnicastTestResponse_msg;
extern const pb_msgdesc_t alox_LedRingProgressRequest_msg;
@@ -698,8 +784,11 @@ extern const pb_msgdesc_t alox_OtaSlaveProgressResponse_msg;
#define alox_ClientInputResponse_fields &alox_ClientInputResponse_msg
#define alox_AccelDeadzoneRequest_fields &alox_AccelDeadzoneRequest_msg
#define alox_AccelDeadzoneResponse_fields &alox_AccelDeadzoneResponse_msg
#define alox_AccelReadRequest_fields &alox_AccelReadRequest_msg
#define alox_AccelReadResponse_fields &alox_AccelReadResponse_msg
#define alox_AccelStreamRequest_fields &alox_AccelStreamRequest_msg
#define alox_AccelStreamResponse_fields &alox_AccelStreamResponse_msg
#define alox_AccelSnapshotRequest_fields &alox_AccelSnapshotRequest_msg
#define alox_AccelSample_fields &alox_AccelSample_msg
#define alox_AccelSnapshotResponse_fields &alox_AccelSnapshotResponse_msg
#define alox_EspNowUnicastTestRequest_fields &alox_EspNowUnicastTestRequest_msg
#define alox_EspNowUnicastTestResponse_fields &alox_EspNowUnicastTestResponse_msg
#define alox_LedRingProgressRequest_fields &alox_LedRingProgressRequest_msg
@@ -723,11 +812,14 @@ extern const pb_msgdesc_t alox_OtaSlaveProgressResponse_msg;
/* alox_ClientInfo_size depends on runtime parameters */
/* alox_ClientInfoResponse_size depends on runtime parameters */
/* alox_ClientInputResponse_size depends on runtime parameters */
#define ALOX_UART_MESSAGES_PB_H_MAX_SIZE alox_OtaSlaveProgressResponse_size
#define ALOX_UART_MESSAGES_PB_H_MAX_SIZE alox_AccelSnapshotResponse_size
#define alox_AccelDeadzoneRequest_size 16
#define alox_AccelDeadzoneResponse_size 20
#define alox_AccelReadRequest_size 0
#define alox_AccelReadResponse_size 20
#define alox_AccelSample_size 32
#define alox_AccelSnapshotRequest_size 6
#define alox_AccelSnapshotResponse_size 544
#define alox_AccelStreamRequest_size 12
#define alox_AccelStreamResponse_size 16
#define alox_Ack_size 0
#define alox_ClientInput_size 22
#define alox_EspNowFindMeRequest_size 6
+41 -10
View File
@@ -22,7 +22,8 @@ enum MessageType {
OTA_SLAVE_PROGRESS = 21;
FIND_ME = 22;
RESTART = 23;
ACCEL_READ = 24;
ACCEL_SNAPSHOT = 24;
ACCEL_STREAM = 25;
}
message UartMessage {
@@ -49,8 +50,10 @@ message UartMessage {
EspNowFindMeResponse espnow_find_me_response = 20;
RestartRequest restart_request = 21;
RestartResponse restart_response = 22;
AccelReadRequest accel_read_request = 23;
AccelReadResponse accel_read_response = 24;
AccelSnapshotRequest accel_snapshot_request = 23;
AccelSnapshotResponse accel_snapshot_response = 24;
AccelStreamRequest accel_stream_request = 25;
AccelStreamResponse accel_stream_response = 26;
}
}
@@ -75,6 +78,8 @@ message ClientInfo {
uint32 last_ping = 5;
uint32 last_success_ping = 6;
uint32 version = 7;
/** Master: ESP-NOW accel stream enabled for this slave. */
bool accel_stream_enabled = 8;
}
message ClientInfoResponse {
@@ -109,14 +114,40 @@ message AccelDeadzoneResponse {
uint32 slaves_updated = 4;
}
// Host → device: read current BMA456 accelerometer sample (raw LSB, ±2g range).
message AccelReadRequest {}
// Host → master: enable/disable slave accel ESP-NOW stream (~16 ms per slave).
// write=false: read; write=true: apply. client_id 0 invalid for write (use >0 or all_clients).
message AccelStreamRequest {
bool write = 1;
bool enable = 2;
uint32 client_id = 3;
bool all_clients = 4;
}
message AccelReadResponse {
bool success = 1;
sint32 x = 2;
sint32 y = 3;
sint32 z = 4;
message AccelStreamResponse {
bool enabled = 1;
uint32 client_id = 2;
bool success = 3;
uint32 slaves_updated = 4;
}
// Host → master: read cached accel samples from slaves (only while stream enabled).
// client_id 0 = all registered slaves; otherwise one slave.
message AccelSnapshotRequest {
uint32 client_id = 1;
}
message AccelSample {
uint32 client_id = 1;
bool valid = 2;
sint32 x = 3;
sint32 y = 4;
sint32 z = 5;
/** Milliseconds since last ESP-NOW sample from this slave. */
uint32 age_ms = 6;
}
message AccelSnapshotResponse {
repeated AccelSample samples = 1 [(nanopb).max_count = 16];
}
message EspNowUnicastTestRequest {
+6 -2
View File
@@ -9,8 +9,12 @@
#define UART_NUM UART_NUM_1
#define UART_BAUD_RATE 921600
#define UART_TXD_PIN 3
#define UART_RXD_PIN 2
// #define UART_TXD_PIN 3
// #define UART_RXD_PIN 2
#define UART_TXD_PIN 2
#define UART_RXD_PIN 3
#define UART_BUF_SIZE 2048
#define START_MARKER 0xAA