feat: browser simulator served by the controller, desktop build, browser bridge

- led-simulator: the rig runs the C++ led-driver as WebAssembly (sim-driver/),
  one single-threaded worker per bar, no cross-origin isolation needed. Rooms
  (one JSON per room) with an on-page editor, and a pattern gallery. The
  Python server, Pyodide boot and ring buffer are removed.
- sim-controller sim_rig.c: serves /sim/ from an embedded pack, the page's
  WebSocket and the rooms/flash API. The desktop build embeds it too.
- bridge.c: a "browser" bridge kind that carries ESP-NOW frames to the page;
  devices announced through it are marked sim.
- UI: Simulator in the Menu, split view with resize and pop-out, swipe
  between controller and rig on phones; SIM/REAL badges in Devices.
- Fades: preset pushes and Wi-Fi driver bodies always carry fade and
  beat_time, so "off" no longer inherits an earlier fade.
- Also the sequences-v2 work in progress: desktop port, bridge firmware,
  serial bridge, Wi-Fi drivers, sequence timeline, audio and API changes.
- led-driver: ESP-NOW link, beat sync, control messages, sim hooks.

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:46 +13:00
1 parent 698c1463c0
commit 581b751904
506 files changed
+42336 -3599

No files matched your search

+43 -16
View File
@@ -1,11 +1,12 @@
/*
* led-controller on the host (ESP-IDF linux target), for led-simulator.
* led-controller on the host (ESP-IDF linux target, and the desktop build), with the simulated rig.
*
* The same firmware sources as the board, minus the hardware: no Ethernet, Wi-Fi, mic or OTA.
* ESP-NOW frames go to the UDP bridge (led-simulator/bridge_sim.py) instead of the radio.
* ESP-NOW frames go to the bridges in settings instead of the radio. With none set, that is the "browser"
* bridge: the rig page this program serves at /sim/ (sim_rig.c), where every bar is the C++ led-driver.
*
* Environment:
* LED_SIM_BRIDGE simulator address, default 127.0.0.1:8766 (overrides settings "bridge")
* LED_SIM_BRIDGE one bridge address, overriding settings "bridges": "browser", host:port or /dev/...
* LED_SIM_HTTP_PORT web UI / API port, default 8080
* The store is the /db directory (mount a volume there to keep it).
*/
@@ -27,10 +28,12 @@
#include "engine.h"
#include "espnow_tx.h"
#include "bridge.h"
#include "wifi_drv.h"
#include "mic.h"
#include "ota.h"
#include "rel.h"
#include "seqplay.h"
#include "sim_rig.h"
#include "store.h"
#include "web.h"
#include "ws.h"
@@ -42,28 +45,35 @@ static esp_err_t status_get_handler(httpd_req_t *req)
beat_info_t bi;
espnow_stats_t es;
rel_stats_t rs;
char buf[1024];
char buf[3072];
char first_bridge[BRIDGE_ADDR_MAX] = "";
char bridge_addrs[BRIDGE_MAX_LINKS][BRIDGE_ADDR_MAX];
if (bridge_list(bridge_addrs, BRIDGE_MAX_LINKS) > 0) {
snprintf(first_bridge, sizeof(first_bridge), "%s", bridge_addrs[0]);
}
char bridges_json[1024];
bridge_status_json(bridges_json, sizeof(bridges_json));
beat_get_info(&bi);
espnow_tx_stats(&es);
rel_stats(&rs);
const char *br = bridge_addr();
snprintf(buf, sizeof(buf),
"{\"sim\":true,\"bridge\":\"%s\",\"ap\":false,\"ip\":\"127.0.0.1\",\"mac\":\"02:51:4d:00:00:00\",\"uptime_s\":%lld,"
"{\"sim\":true,\"bridge\":\"%s\",\"bridges\":%s,\"ap\":false,\"ip\":\"127.0.0.1\",\"mac\":\"02:51:4d:00:00:00\",\"uptime_s\":%lld,"
"\"reset_reason\":\"power_on\",\"free_heap\":0,\"free_internal\":0,"
"\"mic_rms_dbfs\":-120.0,\"mic_peak_dbfs\":-120.0,\"mic_spl_db\":0.0,"
"\"bpm\":%.1f,\"beat_confidence\":%.2f,\"beat_locked\":%s,\"beats\":%lu,\"onsets\":%lu,"
"\"meter\":%d,\"bar_beat\":%d,\"bar_sure\":%s,\"downbeats\":%lu,"
"\"espnow\":{\"beats_sent\":%lu,\"beat_send_fail\":%lu,\"bulk_sent\":%lu,"
"\"bulk_retries\":%lu,\"bulk_failed\":%lu,\"sim_dropped\":%lu},"
"\"link\":{\"ver\":%u,\"acked\":%lu,\"ack_timeouts\":%lu,\"retransmits\":%lu,\"resyncs\":%lu,\"digests\":%lu}}",
br ? br : "", (long long)(esp_timer_get_time() / 1000000),
"\"link\":{\"ver\":%u,\"acked\":%lu,\"ack_timeouts\":%lu,\"retransmits\":%lu,\"resyncs\":%lu,\"digests\":%lu},"
"\"bridge_links\":%s}",
first_bridge, bridges_json, (long long)(esp_timer_get_time() / 1000000),
bi.bpm, bi.confidence, bi.locked ? "true" : "false", (unsigned long)bi.beat_count,
(unsigned long)bi.onset_count, bi.meter, bi.bar_beat, bi.bar_sure ? "true" : "false",
(unsigned long)bi.downbeat_count,
(unsigned long)es.beats_sent, (unsigned long)es.beat_send_fail, (unsigned long)es.bulk_sent,
(unsigned long)es.bulk_retries, (unsigned long)es.bulk_failed, (unsigned long)es.sim_dropped, rel_ver(),
(unsigned long)rs.acked, (unsigned long)rs.ack_timeouts, (unsigned long)rs.retransmits,
(unsigned long)rs.resyncs, (unsigned long)rs.digests);
(unsigned long)rs.resyncs, (unsigned long)rs.digests, bridges_json);
httpd_resp_set_type(req, "application/json");
return httpd_resp_sendstr(req, buf);
}
@@ -76,10 +86,12 @@ static void start_webserver(void)
config.server_port = port ? atoi(port) : 8080;
config.ctrl_port = config.server_port + 10000;
config.uri_match_fn = httpd_uri_match_wildcard;
config.max_uri_handlers = 80;
config.max_uri_handlers = 96;
config.stack_size = 32768; // in StackType_t words on linux (8 bytes); the depth type is 16-bit
config.lru_purge_enable = true;
config.max_open_sockets = 12;
// The board's limit (lwIP) does not apply here: the rig page alone opens several connections per bar while
// its drivers load (the worker scripts, the wasm), on top of the controller's own UI. Under CONFIG_LWIP_MAX_SOCKETS - 3.
config.max_open_sockets = 48;
config.backlog_conn = 10;
if (httpd_start(&server, &config) != ESP_OK) {
ESP_LOGE(TAG, "HTTP server start failed on port %d", config.server_port);
@@ -87,13 +99,15 @@ static void start_webserver(void)
}
const httpd_uri_t status_uri = { .uri = "/status", .method = HTTP_GET, .handler = status_get_handler };
httpd_register_uri_handler(server, &status_uri);
sim_rig_register(server);
web_register(server);
audio_register(server);
audio_api_register(server);
ota_register(server);
api_register(server);
ws_register(server);
ESP_LOGI(TAG, "web UI and API on http://localhost:%d/", config.server_port);
ESP_LOGI(TAG, "web UI and API on http://localhost:%d/, the simulated rig on http://localhost:%d/sim/",
config.server_port, config.server_port);
}
static const char *settings_str(const char *key)
@@ -107,17 +121,30 @@ void app_main(void)
signal(SIGPIPE, SIG_IGN); // a browser that hangs up mid-reply is an error return, as on lwip, not a kill
ESP_ERROR_CHECK(store_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
const char *br = getenv("LED_SIM_BRIDGE");
if (!br || !br[0]) {
br = settings_str("bridge");
char bridges[BRIDGE_MAX_LINKS][BRIDGE_ADDR_MAX];
size_t nb = 0;
const char *env = getenv("LED_SIM_BRIDGE");
if (env && env[0]) {
snprintf(bridges[0], sizeof(bridges[0]), "%s", env);
nb = 1;
} else {
nb = store_bridges(bridges, BRIDGE_MAX_LINKS);
}
espnow_tx_set_bridge(br ? br : "127.0.0.1:8766");
if (nb == 0) {
snprintf(bridges[0], sizeof(bridges[0]), "%s", BRIDGE_BROWSER); // the rig page at /sim/
nb = 1;
}
espnow_tx_set_bridges(bridges, nb);
ESP_ERROR_CHECK(espnow_tx_init(NULL, NULL));
start_webserver();
wifi_drv_start_discovery(); // desktop: finds Wi-Fi drivers over mDNS; a no-op on the simulator
ESP_ERROR_CHECK(rel_init());
ESP_ERROR_CHECK(engine_init());
ESP_ERROR_CHECK(mic_start());
ESP_ERROR_CHECK(seqplay_init());
#ifdef LED_DESKTOP_ALSA
audio_api_resume_mic();
#endif
for (;;) {
vTaskDelay(pdMS_TO_TICKS(1000));
}