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:
1 parent
698c1463c0
commit
581b751904
506 files changed
+42336
-3599
No files matched your search
+111
-12
@@ -16,6 +16,7 @@
|
||||
#include "espnow_tx.h"
|
||||
#include "rel.h"
|
||||
#include "store.h"
|
||||
#include "wifi_drv.h"
|
||||
|
||||
#define IDENTIFY_OFF_MS 2000
|
||||
|
||||
@@ -93,12 +94,40 @@ static const char *normalize_device(cJSON *dev, const char *mac)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Copy of a device with "connected": null for ESP-NOW (no link state), false for Wi-Fi (the
|
||||
// controller keeps no Wi-Fi driver connections)
|
||||
// A Wi-Fi driver's id is its mDNS hostname: the address without ".local", lower case, 12 characters or fewer
|
||||
static bool wifi_id(const cJSON *body, char id[13])
|
||||
{
|
||||
char buf[64];
|
||||
if (!trimmed(cJSON_GetObjectItem(body, "address"), buf, sizeof(buf))) {
|
||||
return false;
|
||||
}
|
||||
size_t n = strlen(buf);
|
||||
if (n > 6 && !strcmp(buf + n - 6, ".local")) {
|
||||
buf[n - 6] = 0;
|
||||
n -= 6;
|
||||
}
|
||||
if (n == 0 || n > 12) {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
if (!isalnum((unsigned char)buf[i]) && buf[i] != '-') {
|
||||
return false;
|
||||
}
|
||||
buf[i] = (char)tolower((unsigned char)buf[i]);
|
||||
}
|
||||
memcpy(id, buf, n);
|
||||
id[n] = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Copy of a device with "connected": null for ESP-NOW (no link state), and for Wi-Fi whether its
|
||||
// WebSocket to the driver is up
|
||||
static cJSON *with_connected(const cJSON *dev)
|
||||
{
|
||||
cJSON *o = cJSON_Duplicate(dev, true);
|
||||
api_set(o, "connected", is_espnow(dev) ? cJSON_CreateNull() : cJSON_CreateFalse());
|
||||
const cJSON *id = cJSON_GetObjectItem(dev, "id");
|
||||
bool up = cJSON_IsString(id) && wifi_drv_connected(id->valuestring);
|
||||
api_set(o, "connected", is_espnow(dev) ? cJSON_CreateNull() : cJSON_CreateBool(up));
|
||||
return o;
|
||||
}
|
||||
|
||||
@@ -125,9 +154,12 @@ static esp_err_t create_device(httpd_req_t *req)
|
||||
bool wifi = cJSON_IsString(tr) && !strcmp(tr->valuestring, "wifi");
|
||||
if (!trimmed(cJSON_GetObjectItem(body, "name"), name, sizeof(name))) {
|
||||
err = "name is required";
|
||||
} else if (!api_mac(cJSON_GetObjectItem(body, "mac"), mac) &&
|
||||
(wifi || !api_mac(cJSON_GetObjectItem(body, "address"), mac))) {
|
||||
err = "mac is required (12 hex digits); for Wi-Fi include mac plus IP in address";
|
||||
} else if (wifi) {
|
||||
if (!wifi_id(body, mac)) {
|
||||
err = "address must be the driver's hostname (letters, digits and '-', 12 characters or fewer)";
|
||||
}
|
||||
} else if (!api_mac(cJSON_GetObjectItem(body, "mac"), mac) && !api_mac(cJSON_GetObjectItem(body, "address"), mac)) {
|
||||
err = "mac is required (12 hex digits)";
|
||||
}
|
||||
cJSON_DeleteItemFromObject(body, "id");
|
||||
cJSON_DeleteItemFromObject(body, "connected");
|
||||
@@ -135,6 +167,11 @@ static esp_err_t create_device(httpd_req_t *req)
|
||||
if (!err) {
|
||||
err = normalize_device(body, mac);
|
||||
}
|
||||
if (!err && wifi) {
|
||||
char addr[WIFI_DRV_ID_MAX + 8];
|
||||
snprintf(addr, sizeof(addr), "%s.local", mac);
|
||||
api_set(body, "address", cJSON_CreateString(addr));
|
||||
}
|
||||
if (err) {
|
||||
cJSON_Delete(body);
|
||||
return api_error(req, "400 Bad Request", err);
|
||||
@@ -204,14 +241,48 @@ static esp_err_t push_to(const char *mac, cJSON *msg)
|
||||
return err;
|
||||
}
|
||||
|
||||
// Settings a Wi-Fi driver takes at /api/hardware. Its name is its hostname and its id, so it is not sent.
|
||||
static esp_err_t push_wifi_hw(const char *address, cJSON *dc)
|
||||
{
|
||||
char host[64];
|
||||
snprintf(host, sizeof(host), "%s", address);
|
||||
size_t n = strlen(host);
|
||||
if (n > 6 && !strcmp(host + n - 6, ".local")) {
|
||||
host[n - 6] = 0;
|
||||
}
|
||||
cJSON *body = cJSON_CreateObject();
|
||||
const cJSON *v = cJSON_GetObjectItem(dc, "color_order");
|
||||
if (cJSON_IsString(v)) {
|
||||
cJSON_AddStringToObject(body, "rgb_order", v->valuestring);
|
||||
}
|
||||
v = cJSON_GetObjectItem(dc, "max_brightness");
|
||||
if (cJSON_IsNumber(v)) {
|
||||
cJSON_AddNumberToObject(body, "max_brightness", v->valuedouble);
|
||||
}
|
||||
v = cJSON_GetObjectItem(dc, "num_leds");
|
||||
if (cJSON_IsNumber(v)) {
|
||||
cJSON_AddNumberToObject(body, "num_leds", v->valuedouble);
|
||||
}
|
||||
cJSON_Delete(dc);
|
||||
if (!body->child) {
|
||||
cJSON_Delete(body);
|
||||
return ESP_OK;
|
||||
}
|
||||
wifi_drv_post(host, "/api/hardware", body); // takes ownership of body
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Send changed settings to an ESP-NOW driver; takes ownership of dc
|
||||
static esp_err_t push_device_config(const cJSON *dev, cJSON *dc)
|
||||
{
|
||||
const cJSON *addr = cJSON_GetObjectItem(dev, "address");
|
||||
if (!is_espnow(dev) || !cJSON_IsString(addr)) {
|
||||
if (!cJSON_IsString(addr)) {
|
||||
cJSON_Delete(dc);
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (!is_espnow(dev)) {
|
||||
return push_wifi_hw(addr->valuestring, dc);
|
||||
}
|
||||
cJSON *msg = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(msg, "v", "1");
|
||||
cJSON_AddItemToObject(msg, "device_config", dc);
|
||||
@@ -314,8 +385,9 @@ esp_err_t api_identify_group(const char *group_id)
|
||||
|
||||
static esp_err_t identify(httpd_req_t *req, const cJSON *dev, const char *mac)
|
||||
{
|
||||
if (!is_espnow(dev)) {
|
||||
return api_error(req, "503 Service Unavailable", "Wi-Fi driver not connected");
|
||||
if (!is_espnow(dev)) { // a Wi-Fi driver is identified over its own link (its id is mac)
|
||||
wifi_drv_identify(mac);
|
||||
return api_message(req, "200 OK", "Identify sent");
|
||||
}
|
||||
if (identify_start(mac, NULL) != ESP_OK) {
|
||||
return api_error(req, "503 Service Unavailable", "Identify send failed");
|
||||
@@ -441,7 +513,7 @@ static esp_err_t device_brightness(httpd_req_t *req, const char *mac)
|
||||
}
|
||||
|
||||
// POST /devices/<id>/driver-config {name, num_leds, color_order, startup_mode}
|
||||
static esp_err_t device_driver_config(httpd_req_t *req, const char *mac)
|
||||
static esp_err_t device_driver_config(httpd_req_t *req, const cJSON *dev, const char *mac)
|
||||
{
|
||||
cJSON *body = api_body_opt(req);
|
||||
if (!body) {
|
||||
@@ -453,6 +525,15 @@ static esp_err_t device_driver_config(httpd_req_t *req, const char *mac)
|
||||
if (!dc) {
|
||||
return api_error(req, "400 Bad Request", "Provide at least one of name, num_leds, color_order, startup_mode");
|
||||
}
|
||||
if (!is_espnow(dev)) {
|
||||
const cJSON *addr = cJSON_GetObjectItem(dev, "address");
|
||||
if (!cJSON_IsString(addr)) {
|
||||
cJSON_Delete(dc);
|
||||
return api_error(req, "500 Internal Server Error", "device has no address");
|
||||
}
|
||||
push_wifi_hw(addr->valuestring, dc);
|
||||
return api_message(req, "200 OK", "driver-config sent");
|
||||
}
|
||||
cJSON *msg = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(msg, "v", "1");
|
||||
cJSON_AddItemToObject(msg, "device_config", dc);
|
||||
@@ -588,7 +669,9 @@ static esp_err_t ping_devices(httpd_req_t *req)
|
||||
cJSON_ArrayForEach(d, store_coll("device"))
|
||||
{
|
||||
unsigned m[6];
|
||||
if (d->string && sscanf(d->string, "%2x%2x%2x%2x%2x%2x", &m[0], &m[1], &m[2], &m[3], &m[4], &m[5]) == 6) {
|
||||
const cJSON *tr = cJSON_GetObjectItem(d, "transport");
|
||||
bool wifi = cJSON_IsString(tr) && !strcmp(tr->valuestring, "wifi"); // ids are hostnames, not MACs
|
||||
if (!wifi && d->string && sscanf(d->string, "%2x%2x%2x%2x%2x%2x", &m[0], &m[1], &m[2], &m[3], &m[4], &m[5]) == 6) {
|
||||
uint8_t mac[6] = { m[0], m[1], m[2], m[3], m[4], m[5] };
|
||||
esp_err_t e = espnow_tx_send(mac, pkt, sizeof(pkt));
|
||||
err = e != ESP_OK ? e : err;
|
||||
@@ -613,6 +696,10 @@ static esp_err_t ping_devices(httpd_req_t *req)
|
||||
if (err != ESP_OK) {
|
||||
return api_error(req, "503 Service Unavailable", "Send failed");
|
||||
}
|
||||
wifi_drv_ping_t wr[WIFI_DRV_PING_MAX];
|
||||
store_unlock(); // Wi-Fi drivers answer over their own links; this waits up to timeout_s
|
||||
int wn = wifi_drv_ping_all((int)(timeout_s * 1000), wr, WIFI_DRV_PING_MAX);
|
||||
store_lock();
|
||||
|
||||
cJSON *o = cJSON_CreateObject(), *responses;
|
||||
cJSON_AddTrueToObject(o, "ok");
|
||||
@@ -636,6 +723,18 @@ static esp_err_t ping_devices(httpd_req_t *req)
|
||||
}
|
||||
registered += register_from_ping(hex, got[i].name, got[i].cfg);
|
||||
}
|
||||
for (int i = 0; i < wn; i++) {
|
||||
if (wr[i].rtt_ms < 0) {
|
||||
continue;
|
||||
}
|
||||
const cJSON *dev = cJSON_GetObjectItem(store_coll("device"), wr[i].id);
|
||||
const cJSON *name = dev ? cJSON_GetObjectItem(dev, "name") : NULL;
|
||||
cJSON *r = cJSON_AddObjectToObject(responses, wr[i].id);
|
||||
cJSON_AddStringToObject(r, "mac", wr[i].id);
|
||||
cJSON_AddStringToObject(r, "name", cJSON_IsString(name) ? name->valuestring : wr[i].id);
|
||||
cJSON_AddNumberToObject(r, "rtt_ms", wr[i].rtt_ms);
|
||||
cJSON_AddStringToObject(r, "transport", "wifi");
|
||||
}
|
||||
cJSON_AddNumberToObject(o, "registered", registered);
|
||||
if (registered) {
|
||||
store_save("device");
|
||||
@@ -868,7 +967,7 @@ bool devices_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
|
||||
} else if (method == HTTP_POST && !strcmp(seg[2], "brightness")) {
|
||||
*ret = device_brightness(req, mac);
|
||||
} else if (method == HTTP_POST && !strcmp(seg[2], "driver-config")) {
|
||||
*ret = device_driver_config(req, mac);
|
||||
*ret = device_driver_config(req, dev, mac);
|
||||
} else if (method == HTTP_POST && !strcmp(seg[2], "patterns")) {
|
||||
*ret = api_error(req, "400 Bad Request", "Pattern OTA push is not supported for ESP-NOW devices");
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user