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

+55 -4
View File
@@ -8,6 +8,7 @@
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_netif.h"
#include "esp_system.h"
#include "esp_timer.h"
#include "esp_wifi.h"
#include "mdns.h"
@@ -15,7 +16,7 @@
static const char *TAG = "net";
#define AP_CHANNEL 1
#define AP_CHANNEL 1 // the access point's channel when ESP-NOW is off; otherwise the ESP-NOW channel
#define AP_MAX_CLIENTS 4
#define RETRY_MAX_MS 30000
@@ -59,7 +60,7 @@ static void apply_ap(bool on)
wifi_config_t ap = {};
strncpy((char *)ap.ap.ssid, ssid, sizeof(ap.ap.ssid) - 1);
ap.ap.ssid_len = strlen(ssid);
ap.ap.channel = AP_CHANNEL;
ap.ap.channel = n.espnow_enabled ? n.espnow_channel : AP_CHANNEL;
ap.ap.max_connection = AP_MAX_CLIENTS;
// A password of 8-63 characters is WPA2; anything else is an open network
const size_t plen = strlen(n.ap_pass);
@@ -96,6 +97,15 @@ static void ap_timer_cb(void *arg)
static esp_err_t apply_client_ip(const net_cfg_t *c);
// The Wi-Fi client and ESP-NOW share the radio, and a client that searches for its network hops channels and
// breaks ESP-NOW, so only one is on: with ESP-NOW enabled the client stays off (no connect, no retries).
static bool client_allowed(void)
{
net_cfg_t n;
settings_load_net(&n);
return !n.espnow_enabled;
}
static void reconnect_cb(void *arg)
{
(void)arg;
@@ -105,7 +115,9 @@ static void reconnect_cb(void *arg)
if (apply_client_ip(&s_pending_net) != ESP_OK) {
ESP_LOGW(TAG, "Client address could not be applied");
}
esp_wifi_connect();
if (client_allowed()) {
esp_wifi_connect();
}
}
static void ap_switch_later(bool on, uint64_t delay_us)
@@ -147,6 +159,9 @@ static esp_err_t apply_client_ip(const net_cfg_t *c)
static void retry_cb(void *arg)
{
(void)arg;
if (!client_allowed()) {
return;
}
esp_wifi_connect();
}
@@ -166,7 +181,11 @@ static void on_wifi_event(void *arg, esp_event_base_t base, int32_t id, void *da
n.use_static = false;
apply_client_ip(&n);
}
esp_wifi_connect();
if (!n.espnow_enabled) {
esp_wifi_connect();
} else {
ESP_LOGI(TAG, "ESP-NOW is on: the Wi-Fi client stays off");
}
}
break;
}
@@ -265,6 +284,8 @@ esp_err_t net_start(void)
}
ESP_ERROR_CHECK(esp_wifi_start());
// No modem sleep: the radio stays awake so the AP never holds DDP unicast until a beacon
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));
// mDNS: <hostname>.local, with an HTTP service on port 80
ESP_ERROR_CHECK(mdns_init());
@@ -294,12 +315,42 @@ bool net_sta_status(char *ip, size_t ip_len)
return true;
}
// Restart after a Wi-Fi change, so the reply goes out first
static void restart_cb(void *arg)
{
(void)arg;
esp_restart();
}
esp_err_t net_sta_connect(const wifi_creds_t *c)
{
esp_err_t err = settings_save_wifi(c);
if (err != ESP_OK) {
return err;
}
net_cfg_t n;
settings_load_net(&n);
if (n.espnow_enabled) {
// Wi-Fi and ESP-NOW share the radio, so Wi-Fi mode means ESP-NOW off. Restart into it: the
// saved credentials are joined at boot.
n.espnow_enabled = false;
err = settings_save_net(&n);
if (err != ESP_OK) {
return err;
}
static esp_timer_handle_t restart_timer;
if (!restart_timer) {
esp_timer_create_args_t a = {};
a.callback = restart_cb;
a.name = "wifi_restart";
err = esp_timer_create(&a, &restart_timer);
if (err != ESP_OK) {
return err;
}
}
ESP_LOGI(TAG, "Wi-Fi set: ESP-NOW off, restarting into Wi-Fi mode");
return esp_timer_start_once(restart_timer, 1500000);
}
esp_timer_stop(s_retry_timer);
esp_wifi_disconnect();
s_retries = 0;