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
+306
-1
@@ -6,11 +6,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "api_util.h"
|
||||
#include "bridge.h"
|
||||
#include "envelope.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
#include "espnow_tx.h"
|
||||
#include "rel.h"
|
||||
#include "wifi_drv.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "store.h"
|
||||
@@ -276,6 +278,180 @@ static bool driver_in_zone(const uint8_t mac[6], const char *name, const char *z
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Wi-Fi drivers (transport "wifi") get the preset as a JSON API call, not an ESP-NOW frame */
|
||||
static bool is_wifi(const cJSON *dev)
|
||||
{
|
||||
const cJSON *tr = cJSON_GetObjectItem(dev, "transport");
|
||||
return cJSON_IsString(tr) && !strcmp(tr->valuestring, "wifi");
|
||||
}
|
||||
|
||||
// Whether the Wi-Fi device (its id and record) is in group gid: a group id, "z<zone>" or "d<mac>"
|
||||
static bool wifi_in_group(const cJSON *dev, const char *id, const char *gid)
|
||||
{
|
||||
if (gid[0] == 'z') {
|
||||
const cJSON *name = cJSON_GetObjectItem(dev, "name"), *names = zone_names(gid + 1);
|
||||
return cJSON_IsString(name) && names && name_seen(names, cJSON_GetArraySize(names), name->valuestring);
|
||||
}
|
||||
if (gid[0] == 'd') {
|
||||
return false; // a driver's own group is an ESP-NOW MAC
|
||||
}
|
||||
const cJSON *group = cJSON_GetObjectItem(store_coll("group"), gid);
|
||||
const cJSON *list = group ? cJSON_GetObjectItem(group, "devices") : NULL, *it;
|
||||
cJSON_ArrayForEach(it, list)
|
||||
{
|
||||
if (cJSON_IsString(it) && !strcmp(it->valuestring, id)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Wi-Fi drivers in the zone's groups get its preset (payload), or off when payload is NULL
|
||||
static void wifi_zone(const char *zone_id, const cJSON *payload)
|
||||
{
|
||||
char t[ZONE_TARGETS_MAX][GROUP_ID_MAX];
|
||||
int n = engine_zone_targets(zone_id, t, ZONE_TARGETS_MAX);
|
||||
const cJSON *d;
|
||||
cJSON_ArrayForEach(d, store_coll("device"))
|
||||
{
|
||||
if (!is_wifi(d)) {
|
||||
continue;
|
||||
}
|
||||
bool in = false;
|
||||
for (int i = 0; i < n && !in; i++) {
|
||||
in = wifi_in_group(d, d->string, t[i]);
|
||||
}
|
||||
if (!in) {
|
||||
continue;
|
||||
}
|
||||
cJSON *body = NULL;
|
||||
if (payload) {
|
||||
body = wifi_drv_preset_body(payload);
|
||||
} else {
|
||||
body = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(body, "pattern", "off");
|
||||
}
|
||||
if (body) {
|
||||
wifi_drv_post(d->string, "/api/pattern", body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Whether a Wi-Fi device is addressed by a message: its targets (ids), else its groups, else all of them
|
||||
static bool wifi_selected(const cJSON *dev, const char *id, const cJSON *targets, const cJSON *groups)
|
||||
{
|
||||
if (cJSON_IsArray(targets) && cJSON_GetArraySize(targets) > 0) {
|
||||
const cJSON *t;
|
||||
cJSON_ArrayForEach(t, targets)
|
||||
{
|
||||
if (cJSON_IsString(t) && !strcmp(t->valuestring, id)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (cJSON_IsArray(groups) && groups->child) {
|
||||
const cJSON *g;
|
||||
cJSON_ArrayForEach(g, groups)
|
||||
{
|
||||
if (cJSON_IsString(g) && wifi_in_group(dev, id, g->valuestring)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// The Wi-Fi part of a v1 message: select (["preset", step], or {group: preset}) becomes /api/pattern,
|
||||
// b becomes /api/brightness. The preset travels with the select, so a driver never runs a stale one.
|
||||
static void wifi_push(const cJSON *m, const cJSON *targets)
|
||||
{
|
||||
const cJSON *groups = cJSON_GetObjectItem(m, "groups");
|
||||
const cJSON *sel = cJSON_GetObjectItem(m, "select");
|
||||
const cJSON *presets = cJSON_GetObjectItem(m, "presets");
|
||||
const cJSON *b = cJSON_GetObjectItem(m, "b");
|
||||
const cJSON *d;
|
||||
cJSON_ArrayForEach(d, store_coll("device"))
|
||||
{
|
||||
const char *id = d->string;
|
||||
if (!is_wifi(d) || !wifi_selected(d, id, targets, groups)) {
|
||||
continue;
|
||||
}
|
||||
const char *name = NULL;
|
||||
if (cJSON_IsArray(sel)) {
|
||||
const cJSON *p0 = cJSON_GetArrayItem(sel, 0);
|
||||
name = cJSON_IsString(p0) ? p0->valuestring : NULL;
|
||||
} else if (cJSON_IsObject(sel)) {
|
||||
const cJSON *it;
|
||||
cJSON_ArrayForEach(it, sel)
|
||||
{
|
||||
const cJSON *v = cJSON_IsArray(it) ? cJSON_GetArrayItem(it, 0) : it;
|
||||
// A key is "*", the device's id or name, or a group the device is in
|
||||
const cJSON *dn = cJSON_GetObjectItem(d, "name");
|
||||
bool named = !strcmp(it->string, id) || (cJSON_IsString(dn) && !strcmp(it->string, dn->valuestring));
|
||||
if (cJSON_IsString(v) && (!strcmp(it->string, "*") || named || wifi_in_group(d, id, it->string))) {
|
||||
name = v->valuestring;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (cJSON_IsNumber(b)) {
|
||||
cJSON *body = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(body, "brightness", b->valuedouble);
|
||||
wifi_drv_post(id, "/api/brightness", body);
|
||||
continue;
|
||||
}
|
||||
if (!name) {
|
||||
continue;
|
||||
}
|
||||
cJSON *body = NULL, *stored = NULL;
|
||||
if (!strcmp(name, "off")) {
|
||||
body = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(body, "pattern", "off");
|
||||
cJSON_AddNumberToObject(body, "fade", 0); // not the fade of the preset it replaces
|
||||
} else {
|
||||
const cJSON *pv = presets ? cJSON_GetObjectItem(presets, name) : NULL;
|
||||
if (!pv) {
|
||||
stored = engine_preset_payload(name);
|
||||
pv = stored;
|
||||
}
|
||||
body = wifi_drv_preset_body(pv);
|
||||
cJSON_Delete(stored);
|
||||
}
|
||||
if (body) {
|
||||
cJSON_AddTrueToObject(body, "select"); // a select restarts the pattern and fades in (fade)
|
||||
wifi_drv_post(id, "/api/pattern", body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Wi-Fi drivers keep their own preset store: each preset sent is saved on the drivers it is addressed to
|
||||
// (every Wi-Fi device when the send has no targets), and def becomes their startup preset
|
||||
static void wifi_store_presets(const cJSON *presets, const cJSON *targets, const char *def)
|
||||
{
|
||||
const cJSON *d;
|
||||
cJSON_ArrayForEach(d, store_coll("device"))
|
||||
{
|
||||
if (!is_wifi(d) || !wifi_selected(d, d->string, targets, NULL)) {
|
||||
continue;
|
||||
}
|
||||
const cJSON *p;
|
||||
cJSON_ArrayForEach(p, presets)
|
||||
{
|
||||
cJSON *body = wifi_drv_preset_body(p);
|
||||
if (body) {
|
||||
cJSON_AddStringToObject(body, "name", p->string);
|
||||
wifi_drv_post(d->string, "/api/presets", body);
|
||||
}
|
||||
}
|
||||
if (def && def[0] && cJSON_GetObjectItem(presets, def)) {
|
||||
cJSON *body = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(body, "name", def);
|
||||
wifi_drv_post(d->string, "/api/presets/startup", body);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Send one envelope frame to every group the zone plays to, as GROUP_CMDs
|
||||
static esp_err_t emit_zone(const char *zone_id, const uint8_t *frame, size_t len, bool commit)
|
||||
{
|
||||
@@ -340,6 +516,7 @@ static esp_err_t play_locked(const char *zone_id, const char *preset_id)
|
||||
if (!payload) {
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
wifi_zone(zone_id, payload);
|
||||
uint8_t frame[ENV_FRAME_MAX];
|
||||
env_blob_t pb;
|
||||
env_blob_init(&pb);
|
||||
@@ -359,6 +536,13 @@ static esp_err_t play_locked(const char *zone_id, const char *preset_id)
|
||||
return ret;
|
||||
}
|
||||
|
||||
void engine_zone_note(const char *zone_id, const char *preset_id)
|
||||
{
|
||||
store_lock();
|
||||
zone_cur_set(zone_id, preset_id);
|
||||
store_unlock();
|
||||
}
|
||||
|
||||
const char *engine_zone_current(const char *zone_id)
|
||||
{
|
||||
for (int i = 0; i < 16; i++) {
|
||||
@@ -398,6 +582,7 @@ esp_err_t engine_stop(const char *zone_id)
|
||||
store_lock();
|
||||
rel_bump_ver();
|
||||
esp_err_t ret = select_zone(zone_id, "off");
|
||||
wifi_zone(zone_id, NULL);
|
||||
if (ret == ESP_OK) {
|
||||
zone_cur_set(zone_id, "off");
|
||||
}
|
||||
@@ -666,6 +851,7 @@ esp_err_t engine_push(const cJSON *messages, const cJSON *targets)
|
||||
if (!cJSON_IsObject(m)) {
|
||||
continue;
|
||||
}
|
||||
wifi_push(m, targets);
|
||||
if (!cJSON_IsArray(targets) || cJSON_GetArraySize(targets) == 0) {
|
||||
rel_bump_ver(); // a broadcast state change: drivers that miss it resync from the digest
|
||||
}
|
||||
@@ -731,6 +917,7 @@ esp_err_t engine_send_presets(const cJSON *ids, bool save, const char *def, cons
|
||||
if (!cJSON_IsArray(targets) || cJSON_GetArraySize(targets) == 0) {
|
||||
rel_bump_ver();
|
||||
}
|
||||
wifi_store_presets(presets, targets, def);
|
||||
ret = push_binary(msg, targets);
|
||||
// The startup preset needs the driver names (the driver checks it is addressed); targeted sends only
|
||||
if (ret == ESP_OK && def && cJSON_GetObjectItem(presets, def) && cJSON_IsArray(targets)) {
|
||||
@@ -957,6 +1144,94 @@ void engine_groups_push_all(int *total, int *sent, int *failed)
|
||||
}
|
||||
|
||||
// A driver said hello at boot: register it, then tell it which zones it belongs to
|
||||
// Groups that list key from list to instead
|
||||
static void groups_rename(const char *from, const char *to)
|
||||
{
|
||||
cJSON *g;
|
||||
cJSON_ArrayForEach(g, store_coll("group"))
|
||||
{
|
||||
cJSON *list = cJSON_GetObjectItem(g, "devices");
|
||||
for (int i = 0; list && i < cJSON_GetArraySize(list); i++) {
|
||||
cJSON *it = cJSON_GetArrayItem(list, i);
|
||||
if (cJSON_IsString(it) && !strcmp(it->valuestring, from)) {
|
||||
cJSON_ReplaceItemInArray(list, i, cJSON_CreateString(to));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Move a device to a new key and transport, keeping its fields. Groups that listed the old key list the
|
||||
// new one. ESP-NOW devices are keyed by MAC and Wi-Fi devices by hostname, so a board that changes
|
||||
// mode changes key. Caller holds the store lock.
|
||||
static void device_rekey(const char *from, const char *to, const char *transport, const char *address)
|
||||
{
|
||||
cJSON *devs = store_coll("device");
|
||||
cJSON *old = cJSON_GetObjectItem(devs, from);
|
||||
if (!old || !strcmp(from, to)) {
|
||||
return;
|
||||
}
|
||||
cJSON *rec = cJSON_Duplicate(old, 1);
|
||||
api_set(rec, "id", cJSON_CreateString(to));
|
||||
api_set(rec, "transport", cJSON_CreateString(transport));
|
||||
api_set(rec, "address", cJSON_CreateString(address));
|
||||
cJSON_DeleteItemFromObject(devs, from);
|
||||
cJSON_DeleteItemFromObject(devs, to); // a record discovery made for the same board
|
||||
cJSON_AddItemToObject(devs, to, rec);
|
||||
groups_rename(from, to);
|
||||
store_save("device");
|
||||
store_save("group");
|
||||
ESP_LOGI(TAG, "device %s is now %s (%s)", from, transport, to);
|
||||
}
|
||||
|
||||
// Keep the record under key to (its settings and zones) and drop from, which is the same board on the other
|
||||
// transport. Falls back to a move when there is no record under to.
|
||||
static void device_fold(const char *from, const char *to, const char *transport, const char *address)
|
||||
{
|
||||
cJSON *devs = store_coll("device");
|
||||
cJSON *keep = cJSON_GetObjectItem(devs, to);
|
||||
if (!keep) {
|
||||
device_rekey(from, to, transport, address);
|
||||
return;
|
||||
}
|
||||
// The configured record wins: its settings and zones go onto the record discovery made
|
||||
const cJSON *old = cJSON_GetObjectItem(devs, from);
|
||||
for (const cJSON *c = old ? old->child : NULL; c; c = c->next) {
|
||||
if (c->string && strcmp(c->string, "id") && strcmp(c->string, "transport") &&
|
||||
strcmp(c->string, "address") && strcmp(c->string, "mac")) {
|
||||
api_set(keep, c->string, cJSON_Duplicate(c, 1));
|
||||
}
|
||||
}
|
||||
cJSON_DeleteItemFromObject(devs, from);
|
||||
api_set(keep, "transport", cJSON_CreateString(transport));
|
||||
api_set(keep, "address", cJSON_CreateString(address));
|
||||
groups_rename(from, to);
|
||||
store_save("device");
|
||||
store_save("group");
|
||||
ESP_LOGI(TAG, "device %s is now %s (%s), settings kept", from, transport, to);
|
||||
}
|
||||
|
||||
// A Wi-Fi driver reported its MAC (its status). Remember it on its record. An ESP-NOW record with the same
|
||||
// MAC is the same board, now on Wi-Fi: it becomes the Wi-Fi device under the hostname.
|
||||
void engine_wifi_mac(const char *host, const char *mac)
|
||||
{
|
||||
store_lock();
|
||||
cJSON *devs = store_coll("device");
|
||||
const cJSON *esp = cJSON_GetObjectItem(devs, mac);
|
||||
const cJSON *et = esp ? cJSON_GetObjectItem(esp, "transport") : NULL;
|
||||
if (cJSON_IsString(et) && !strcmp(et->valuestring, "espnow")) {
|
||||
char addr[WIFI_DRV_ID_MAX + 8];
|
||||
snprintf(addr, sizeof(addr), "%s.local", host);
|
||||
device_fold(mac, host, "wifi", addr);
|
||||
}
|
||||
cJSON *rec = cJSON_GetObjectItem(devs, host);
|
||||
const cJSON *m = rec ? cJSON_GetObjectItem(rec, "mac") : NULL;
|
||||
if (rec && !(cJSON_IsString(m) && !strcmp(m->valuestring, mac))) {
|
||||
api_set(rec, "mac", cJSON_CreateString(mac));
|
||||
store_save("device");
|
||||
}
|
||||
store_unlock();
|
||||
}
|
||||
|
||||
static void on_announce(const uint8_t mac[6], const uint8_t *pkt, int len)
|
||||
{
|
||||
char name[48] = "";
|
||||
@@ -992,8 +1267,27 @@ static void on_announce(const uint8_t mac[6], const uint8_t *pkt, int len)
|
||||
snprintf(hex, sizeof(hex), "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||
store_lock();
|
||||
cJSON *devs = store_coll("device");
|
||||
// A board that was a Wi-Fi device (keyed by hostname) and is back on ESP-NOW: same MAC, new key
|
||||
if (!cJSON_GetObjectItem(devs, hex)) {
|
||||
const cJSON *d;
|
||||
char from[WIFI_DRV_ID_MAX + 1] = "";
|
||||
cJSON_ArrayForEach(d, devs)
|
||||
{
|
||||
const cJSON *m = cJSON_GetObjectItem(d, "mac");
|
||||
if (cJSON_IsString(m) && !strcmp(m->valuestring, hex)) {
|
||||
snprintf(from, sizeof(from), "%s", d->string);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (from[0]) {
|
||||
device_rekey(from, hex, "espnow", hex);
|
||||
}
|
||||
}
|
||||
cJSON *rec = cJSON_GetObjectItem(devs, hex);
|
||||
const cJSON *old = cJSON_GetObjectItem(rec, "name");
|
||||
// A driver heard on the browser bridge is one of the rig page's simulated drivers: "sim": true (a real
|
||||
// board has no "sim" key)
|
||||
const bool sim = bridge_mac_is_browser(mac);
|
||||
if (!rec) {
|
||||
rec = cJSON_AddObjectToObject(devs, hex);
|
||||
cJSON_AddStringToObject(rec, "id", hex);
|
||||
@@ -1002,8 +1296,11 @@ static void on_announce(const uint8_t mac[6], const uint8_t *pkt, int len)
|
||||
cJSON_AddStringToObject(rec, "transport", "espnow");
|
||||
cJSON_AddStringToObject(rec, "address", hex);
|
||||
api_apply_driver_cfg(rec, cfg, cfg_len);
|
||||
if (sim) {
|
||||
cJSON_AddTrueToObject(rec, "sim");
|
||||
}
|
||||
store_save("device");
|
||||
ESP_LOGI(TAG, "new driver %s (%s)", name, hex);
|
||||
ESP_LOGI(TAG, "new %sdriver %s (%s)", sim ? "simulated " : "", name, hex);
|
||||
} else {
|
||||
bool changed = false;
|
||||
if (!cJSON_IsString(old) || strcmp(old->valuestring, name) != 0) {
|
||||
@@ -1013,6 +1310,13 @@ static void on_announce(const uint8_t mac[6], const uint8_t *pkt, int len)
|
||||
if (api_apply_driver_cfg(rec, cfg, cfg_len)) {
|
||||
changed = true;
|
||||
}
|
||||
if (sim != cJSON_IsTrue(cJSON_GetObjectItem(rec, "sim"))) {
|
||||
cJSON_DeleteItemFromObject(rec, "sim");
|
||||
if (sim) {
|
||||
cJSON_AddTrueToObject(rec, "sim");
|
||||
}
|
||||
changed = true;
|
||||
}
|
||||
if (changed) {
|
||||
store_save("device");
|
||||
}
|
||||
@@ -1065,6 +1369,7 @@ static void resync_driver(const uint8_t mac[6])
|
||||
|
||||
esp_err_t engine_init(void)
|
||||
{
|
||||
wifi_drv_init();
|
||||
rel_set_resync_cb(resync_driver);
|
||||
rel_set_announce_cb(on_announce);
|
||||
return ESP_OK;
|
||||
|
||||
Reference in new issue
Block a user