feat: ESP-NOW link, beat sync and control messages in the C++ driver

- espnow_link, ctrl_msg and beat_sync: the reliable link, envelopes and
  beat-synced timing.
- ctrl_msg: accept an envelope with the trailing save byte.
- patterns_frame() and espnow_link_poll() split out of their tasks, so the
  WebAssembly simulator can drive one frame and one link pass from a timer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Opus 5.5 committed 2026-11-16 19:32:36 +13:00
1 parent 94e300f6ad
commit 24dd5ffb15
30 files changed
+2159 -158

No files matched your search

+92 -2
View File
@@ -10,6 +10,7 @@
#include "mdns.h"
#include "esp_http_server.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_netif.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
@@ -19,7 +20,9 @@
#include "esp_wifi.h"
#include "net.h"
#include "ota.h"
#include "ctrl_msg.h"
#include "ddp.h"
#include "espnow_link.h"
#include "patterns.h"
#include "effects.h"
#include "presets.h"
@@ -246,6 +249,12 @@ static esp_err_t status_get(api_req_t *req)
cJSON_AddStringToObject(o, "ap_ssid", ap);
cJSON_AddBoolToObject(o, "ap_on", net_ap_on());
cJSON_AddStringToObject(o, "hostname", n.hostname);
// The station MAC is the one the board uses on ESP-NOW too, so the controller ties the two records together
uint8_t mac[6];
esp_read_mac(mac, ESP_MAC_WIFI_STA);
char mac_hex[13];
snprintf(mac_hex, sizeof(mac_hex), "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
cJSON_AddStringToObject(o, "mac", mac_hex);
cJSON_AddBoolToObject(o, "sta_connected", up);
cJSON_AddStringToObject(o, "sta_ip", ip);
cJSON_AddStringToObject(o, "selected", presets_selected());
@@ -268,11 +277,17 @@ static esp_err_t pattern_post(api_req_t *req)
preset_t p;
patterns_get(&p);
bool ok = preset_from_json(in, &p);
// "select": the controller's preset select: restart the pattern, and fade in if the preset says so
bool select = cJSON_IsTrue(cJSON_GetObjectItem(in, "select"));
cJSON_Delete(in);
if (!ok) {
return send_error(req, "400 Bad Request", "unknown pattern");
}
patterns_set(p);
if (select) {
patterns_select(p);
} else {
patterns_set(p);
}
return send_json(req, cJSON_CreateObject());
}
@@ -318,13 +333,17 @@ static esp_err_t wifi_post(api_req_t *req)
strncpy(c.pass, pw, sizeof(c.pass) - 1);
cJSON_Delete(in);
net_cfg_t before;
settings_load_net(&before);
esp_err_t err = net_sta_connect(&c);
if (err != ESP_OK) {
ESP_LOGW(TAG, "Connect to %s failed: %s", c.ssid, esp_err_to_name(err));
return send_error(req, "500 Internal Server Error", esp_err_to_name(err));
}
cJSON *o = cJSON_CreateObject();
cJSON_AddStringToObject(o, "message", "saved; connecting");
cJSON_AddStringToObject(o, "message", before.espnow_enabled
? "saved; ESP-NOW is off, restarting into Wi-Fi mode"
: "saved; connecting");
return send_json(req, o);
}
@@ -690,6 +709,7 @@ static esp_err_t presets_get(api_req_t *req)
cJSON_AddStringToObject(o, "selected", presets_selected());
cJSON_AddStringToObject(o, "startup", presets_startup());
cJSON_AddNumberToObject(o, "max", PRESETS_MAX);
cJSON_AddNumberToObject(o, "dropped", presets_dropped());
return send_json(req, o);
}
@@ -1155,6 +1175,74 @@ typedef struct {
api_fn_t fn;
} api_route_t;
// GET /api/espnow: the ESP-NOW link to led-controller: its settings, the controller, the groups, the counters
static esp_err_t espnow_get(api_req_t *req)
{
net_cfg_t n;
settings_load_net(&n);
espnow_stats_t st;
espnow_link_stats(&st);
cJSON *o = cJSON_CreateObject();
cJSON_AddBoolToObject(o, "enabled", n.espnow_enabled);
cJSON_AddBoolToObject(o, "running", st.running);
cJSON_AddNumberToObject(o, "channel", n.espnow_channel);
uint8_t radio = 0;
wifi_second_chan_t second;
if (esp_wifi_get_channel(&radio, &second) == ESP_OK) {
cJSON_AddNumberToObject(o, "radio_channel", radio); // the channel ESP-NOW actually uses
}
char ctrl[24] = "";
if (st.master_seen) {
snprintf(ctrl, sizeof(ctrl), "%02x:%02x:%02x:%02x:%02x:%02x", st.master[0], st.master[1], st.master[2],
st.master[3], st.master[4], st.master[5]);
}
cJSON_AddStringToObject(o, "controller", ctrl);
cJSON_AddItemToObject(o, "groups", ctrl_groups_json());
cJSON *c = cJSON_AddObjectToObject(o, "counters");
cJSON_AddNumberToObject(c, "msgs", st.msgs);
cJSON_AddNumberToObject(c, "dups", st.dups);
cJSON_AddNumberToObject(c, "acks", st.acks);
cJSON_AddNumberToObject(c, "resyncs", st.resyncs);
cJSON_AddNumberToObject(c, "pings", st.pings);
cJSON_AddNumberToObject(c, "beats", st.beats);
cJSON_AddNumberToObject(c, "dropped", st.dropped);
cJSON_AddNumberToObject(c, "frames", st.frames);
cJSON_AddNumberToObject(c, "sent", st.sent);
cJSON_AddNumberToObject(c, "send_fail", st.send_fail);
cJSON_AddNumberToObject(c, "ver", st.ver);
return send_json(req, o);
}
// POST /api/espnow {"enabled": bool, "channel": 1-13}. Saved now; the link starts or moves channel on restart.
static esp_err_t espnow_post(api_req_t *req)
{
cJSON *in = read_json(req);
if (!in) {
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
}
net_cfg_t n;
settings_load_net(&n);
cJSON *v = cJSON_GetObjectItem(in, "enabled");
if (cJSON_IsBool(v)) {
n.espnow_enabled = cJSON_IsTrue(v);
}
v = cJSON_GetObjectItem(in, "channel");
if (cJSON_IsNumber(v)) {
if (v->valueint < 1 || v->valueint > 13) {
cJSON_Delete(in);
return send_error(req, "400 Bad Request", "channel must be 1-13");
}
n.espnow_channel = (uint8_t)v->valueint;
}
cJSON_Delete(in);
if (settings_save_net(&n) != ESP_OK) {
return send_error(req, "500 Internal Server Error", "could not save");
}
cJSON *o = cJSON_CreateObject();
cJSON_AddStringToObject(o, "message", "saved, restart to apply");
return send_json(req, o);
}
static const api_route_t s_api[] = {
{"/api/status", HTTP_GET, status_get},
{"/api/pattern", HTTP_POST, pattern_post},
@@ -1164,6 +1252,8 @@ static const api_route_t s_api[] = {
{"/api/network", HTTP_GET, network_get},
{"/api/network", HTTP_POST, network_post},
{"/api/ddp", HTTP_GET, ddp_get},
{"/api/espnow", HTTP_GET, espnow_get},
{"/api/espnow", HTTP_POST, espnow_post},
{"/api/hardware", HTTP_GET, hardware_get},
{"/api/hardware", HTTP_POST, hardware_post},
{"/api/presets", HTTP_GET, presets_get},