sim: fixture editor for DDP and Art-Net/DMX fixtures

The rig sim receives Art-Net (UDP 6454, answers ArtPoll) and DDP (each port the
room's DDP strips use) in sim_net.c and passes changed universes and pixels to
the page over /sim/ws, now with a kind byte in front of each binary frame.

Rooms gain fixture types: ddp strips, artnet pixel strips, and artnet cans and
movers that read named channels from the room's dmx_definitions (the qlcplus
stage map's format). Overlapping DMX channels are refused. Cans and movers are
decoded and drawn as in the qlcplus stage simulator (dmx.js, dmx-draw.js).

In Edit mode: pick a type to add, edit port/offset/universe/address/colour
order/definition, see a can's or mover's live channels, make a controller
output aimed at the sim and send its test. The room dialog lists DMX
definitions, with presets, a channel editor, and .qxf and stage map import.

GET /sim/api/dmx shows what the receiver has had. Tests: test/api/test_sim_net.py
and test/browser/dmx_decode.test.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Opus 5.5 committed 2026-11-17 18:13:58 +13:00
1 parent 2fdd55323b
commit 35207a868a
16 files changed
+2859 -118

No files matched your search

+322 -58
View File
@@ -6,7 +6,8 @@
* without threads, so the page needs no cross-origin isolation: any address works
* WS /sim/ws the page's radio. One page at a time: a new one takes over.
* page -> here binary src_mac(6) + ESP-NOW packet a driver transmitted (bridge_browser_rx)
* here -> page binary dst_mac(6) + ESP-NOW packet the controller transmitted (sim_rig_tx)
* here -> page binary 0x00 + dst_mac(6) + ESP-NOW packet the controller transmitted (sim_rig_tx)
* here -> page binary 0x01 / 0x02 + ... an Art-Net universe or DDP port's pixels (sim_net.h)
* page -> here text {"t":"flash","mac":"..","data":"<NVS JSON>"} a driver committed its flash
* here -> page text {"t":"takeover"} another page opened the rig
* GET /sim/api/config the active room, its strips, the room list and the bridge
@@ -16,11 +17,17 @@
* GET|PUT|DELETE /sim/api/rooms/<name>
* POST /sim/api/room {name}: switch the rig to that room
* POST /sim/api/next-mac {taken: [mac, ...]}: a MAC no room or the page uses
* GET /sim/api/dmx[?data=1] what the Art-Net and DDP receiver has had (sim_net.h); data=1 adds the bytes
*
* Rooms follow the stage maps of the qlcplus simulator: one JSON file per room in <db>/sim/rooms/, a size in
* metres, strip types ("definitions") and "fixtures" keyed by id, each one LED bar: x, y (metres) is pixel 0,
* "direction" the way it runs (degrees clockwise from +y), "mac" the driver. Each bar's flash is
* <db>/sim/flash/<mac>.json, so a bar keeps its presets in every room it appears in.
*
* A fixture's "type" is "espnow" (the default: a bar with a driver), "ddp" (a strip fed by DDP: port, offset
* in pixels, color_order) or "artnet" (universe, DMX address 1-512, and kind "pixels" with color_order, or
* "can"/"mover" with dmx_def naming one of the room's "dmx_definitions": {manufacturer, model, mode, channels},
* plus zero and pan_reverse for movers). Art-Net fixtures may not share DMX channels.
*/
#include "auth.h"
#include "sim_rig.h"
@@ -39,12 +46,15 @@
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "sim_net.h"
#include "store.h"
static const char *TAG = "sim_rig";
#define MAX_FIXTURES 64
#define MAX_LEDS 512 // FX_LEDS_MAX in led-driver/main/effects.h
#define MAX_NET_LEDS SIM_NET_DDP_PIXELS // DDP and Art-Net strips: no driver, only the buffer
#define MAX_DMX_DEFS 32
#define FLASH_MAX (512 * 1024)
#define FRAME_MAX (FLASH_MAX + 1024)
#define DEFAULT_PITCH_M (1.0 / 60) // 60 LEDs per metre
@@ -299,6 +309,249 @@ static double num_in(const cJSON *o, const char *key, double def, double lo, dou
return v->valuedouble;
}
static const char *const COLOR_ORDERS[] = { "rgb", "rbg", "grb", "gbr", "brg", "bgr" };
static bool color_order_ok(const char *s)
{
for (size_t i = 0; i < sizeof(COLOR_ORDERS) / sizeof(COLOR_ORDERS[0]); i++) {
if (s && !strcmp(s, COLOR_ORDERS[i])) {
return true;
}
}
return false;
}
// A DMX definition or channel name: 1-max printable characters
static bool text_ok(const char *s, size_t max)
{
size_t n = s ? strlen(s) : 0;
if (n == 0 || n > max) {
return false;
}
for (size_t i = 0; i < n; i++) {
if ((unsigned char)s[i] < 0x20 || s[i] == 0x7f) {
return false;
}
}
return true;
}
static const char *str_or(const cJSON *o, const char *key, const char *def)
{
const cJSON *v = cJSON_GetObjectItem(o, key);
return cJSON_IsString(v) ? v->valuestring : def;
}
// dmx_definitions: {name: {manufacturer, model, mode, channels: [name, ...]}}, the qlcplus stage map's definitions
static void validate_dmx_defs(const cJSON *in, cJSON *out, char *err, size_t errn)
{
if (!cJSON_IsObject(in)) {
snprintf(err, errn, "dmx_definitions must be an object keyed by name");
return;
}
if (cJSON_GetArraySize(in) > MAX_DMX_DEFS) {
snprintf(err, errn, "at most %d DMX definitions in a room", MAX_DMX_DEFS);
return;
}
const cJSON *d;
cJSON_ArrayForEach(d, in)
{
if (!text_ok(d->string, 60) || !cJSON_IsObject(d)) {
snprintf(err, errn, "DMX definition names are 1-60 characters, each an object");
return;
}
const cJSON *ch = cJSON_GetObjectItem(d, "channels"), *c;
int n = cJSON_GetArraySize(ch);
if (!cJSON_IsArray(ch) || n < 1 || n > 512) {
snprintf(err, errn, "DMX definition %s: channels is a list of 1-512 names", d->string);
return;
}
cJSON *o = cJSON_AddObjectToObject(out, d->string);
const char *fields[] = { "manufacturer", "model", "mode" };
for (int i = 0; i < 3; i++) {
const char *v = str_or(d, fields[i], "");
cJSON_AddStringToObject(o, fields[i], text_ok(v, 60) ? v : "");
}
cJSON *chans = cJSON_AddArrayToObject(o, "channels");
cJSON_ArrayForEach(c, ch)
{
if (!cJSON_IsString(c) || !text_ok(c->valuestring, 40)) {
snprintf(err, errn, "DMX definition %s: channel names are 1-40 characters", d->string);
return;
}
cJSON_AddItemToArray(chans, cJSON_CreateString(c->valuestring));
}
}
}
// One fixture into fixtures (canonical form); err set when it is not valid
static void validate_fixture(const cJSON *f, const cJSON *defs, const cJSON *dmx_defs, cJSON *fixtures, char *err,
size_t errn)
{
const char *fid = f->string;
if (!id_ok(fid) || !cJSON_IsObject(f)) {
snprintf(err, errn, "fixture ids are 1-40 letters, digits or -_.#, each an object");
return;
}
const char *type = str_or(f, "type", "espnow");
const char *kind = str_or(f, "kind", "pixels");
bool espnow = !strcmp(type, "espnow"), ddp = !strcmp(type, "ddp"), artnet = !strcmp(type, "artnet");
if (!espnow && !ddp && !artnet) {
snprintf(err, errn, "fixture %s: type is espnow, ddp or artnet", fid);
return;
}
bool pixels = !artnet || !strcmp(kind, "pixels");
if (artnet && !pixels && strcmp(kind, "can") && strcmp(kind, "mover")) {
snprintf(err, errn, "fixture %s: kind is pixels, can or mover", fid);
return;
}
const cJSON *def = cJSON_GetObjectItem(f, "def");
if (pixels && (!cJSON_IsString(def) || !cJSON_GetObjectItem(defs, def->valuestring))) {
snprintf(err, errn, "fixture %s: unknown strip type", fid);
return;
}
char m[18] = "";
if (espnow) {
const cJSON *mac = cJSON_GetObjectItem(f, "mac");
if (!cJSON_IsString(mac) || !mac_ok(mac->valuestring)) {
snprintf(err, errn, "strip %s: mac must be like 02:51:4d:00:00:01", fid);
return;
}
snprintf(m, sizeof(m), "%s", mac->valuestring);
for (char *c = m; *c; c++) {
*c = (char)tolower((unsigned char)*c);
}
const cJSON *other;
cJSON_ArrayForEach(other, fixtures)
{
const cJSON *om = cJSON_GetObjectItem(other, "mac");
if (cJSON_IsString(om) && !strcmp(om->valuestring, m)) {
snprintf(err, errn, "strip %s: mac %s is used twice in this room", fid, m);
return;
}
}
}
const char *dmx_def = str_or(f, "dmx_def", "");
if (artnet && !pixels && !cJSON_GetObjectItem(dmx_defs, dmx_def)) {
snprintf(err, errn, "fixture %s: dmx_def must name one of the room's DMX definitions", fid);
return;
}
char what[80];
cJSON *o = cJSON_AddObjectToObject(fixtures, fid);
const char *label = str_or(f, "label", "");
char lab[48];
snprintf(lab, sizeof(lab), "%s", label[0] ? label : fid);
cJSON_AddStringToObject(o, "label", lab);
cJSON_AddStringToObject(o, "type", type);
if (pixels) {
cJSON_AddStringToObject(o, "def", def->valuestring);
}
if (espnow) {
cJSON_AddStringToObject(o, "mac", m);
}
snprintf(what, sizeof(what), "%s x", fid);
cJSON_AddNumberToObject(o, "x", round_to(num_in(f, "x", 0, -1000, 1000, what, err, errn), 0.001));
snprintf(what, sizeof(what), "%s y", fid);
cJSON_AddNumberToObject(o, "y", round_to(num_in(f, "y", 0, -1000, 1000, what, err, errn), 0.001));
snprintf(what, sizeof(what), "%s direction", fid);
cJSON_AddNumberToObject(o, "direction",
round_to(wrap_deg(num_in(f, "direction", 90, -3600, 3600, what, err, errn)), 0.1));
const cJSON *leds = cJSON_GetObjectItem(f, "leds");
if (pixels && leds && !cJSON_IsNull(leds)) {
snprintf(what, sizeof(what), "%s leds", fid);
cJSON_AddNumberToObject(o, "leds", (int)num_in(f, "leds", 1, 1, espnow ? MAX_LEDS : MAX_NET_LEDS, what, err, errn));
}
if (!espnow && pixels) {
const char *order = str_or(f, "color_order", "rgb");
if (!color_order_ok(order)) {
snprintf(err, errn, "fixture %s: color_order is one of rgb, rbg, grb, gbr, brg, bgr", fid);
return;
}
cJSON_AddStringToObject(o, "color_order", order);
}
if (ddp) {
snprintf(what, sizeof(what), "%s port", fid);
cJSON_AddNumberToObject(o, "port", (int)num_in(f, "port", 4048, 1, 65535, what, err, errn));
snprintf(what, sizeof(what), "%s offset", fid);
int off = (int)num_in(f, "offset", 0, 0, SIM_NET_DDP_PIXELS - 1, what, err, errn);
cJSON_AddNumberToObject(o, "offset", off);
}
if (artnet) {
cJSON_AddStringToObject(o, "kind", kind);
snprintf(what, sizeof(what), "%s universe", fid);
cJSON_AddNumberToObject(o, "universe", (int)num_in(f, "universe", 0, 0, 32767, what, err, errn));
snprintf(what, sizeof(what), "%s address", fid);
cJSON_AddNumberToObject(o, "address", (int)num_in(f, "address", 1, 1, 512, what, err, errn));
if (!pixels) {
cJSON_AddStringToObject(o, "dmx_def", dmx_def);
snprintf(what, sizeof(what), "%s zero", fid);
cJSON_AddNumberToObject(o, "zero", round_to(wrap_deg(num_in(f, "zero", 0, -3600, 3600, what, err, errn)), 0.1));
cJSON_AddBoolToObject(o, "pan_reverse", cJSON_IsTrue(cJSON_GetObjectItem(f, "pan_reverse")));
}
}
}
static int fixture_leds(const cJSON *room, const cJSON *f);
// The DMX slots an Art-Net fixture uses: up to 32 runs of [start, end) in a universe. Pixels fill the first
// universe from the address, then 170 a universe from channel 1, the way an Art-Net output sends them.
static int dmx_runs(const cJSON *room, const cJSON *f, int uni[32], int start[32], int end[32])
{
int u = cJSON_GetObjectItem(f, "universe")->valueint, a0 = cJSON_GetObjectItem(f, "address")->valueint - 1;
if (strcmp(str_or(f, "kind", "pixels"), "pixels")) {
const cJSON *d = cJSON_GetObjectItem(cJSON_GetObjectItem(room, "dmx_definitions"), str_or(f, "dmx_def", ""));
uni[0] = u;
start[0] = a0;
end[0] = a0 + cJSON_GetArraySize(cJSON_GetObjectItem(d, "channels"));
return 1;
}
int left = fixture_leds(room, f), n = 0;
int first = (512 - a0) / 3;
while (left > 0 && n < 32) {
int take = n == 0 ? first : 170;
take = take < left ? take : left;
uni[n] = u + n;
start[n] = n == 0 ? a0 : 0;
end[n] = start[n] + take * 3;
left -= take;
n++;
}
return n;
}
// Art-Net fixtures must fit in their universes and not share slots
static void dmx_overlaps(const cJSON *room, char *err, size_t errn)
{
const cJSON *fixtures = cJSON_GetObjectItem(room, "fixtures"), *a, *b;
cJSON_ArrayForEach(a, fixtures)
{
if (strcmp(str_or(a, "type", ""), "artnet")) {
continue;
}
int ua[32], sa[32], ea[32], na = dmx_runs(room, a, ua, sa, ea);
if (ea[0] > 512) {
snprintf(err, errn, "fixture %s: its %d channels from %d run past channel 512", a->string, ea[0] - sa[0],
sa[0] + 1);
return;
}
for (b = a->next; b; b = b->next) {
if (strcmp(str_or(b, "type", ""), "artnet")) {
continue;
}
int ub[32], sb[32], eb[32], nb = dmx_runs(room, b, ub, sb, eb);
for (int i = 0; i < na; i++) {
for (int k = 0; k < nb; k++) {
if (ua[i] == ub[k] && sa[i] < eb[k] && sb[k] < ea[i]) {
snprintf(err, errn, "fixtures %s and %s share DMX channels in universe %d", a->string,
b->string, ua[i]);
return;
}
}
}
}
}
}
// The room in canonical form (a new object), or NULL with the reason in err
static cJSON *validate_room(const cJSON *raw, const char *name, char *err, size_t errn)
{
@@ -343,69 +596,28 @@ static cJSON *validate_room(const cJSON *raw, const char *name, char *err, size_
cJSON_AddNumberToObject(o, "leds", (int)leds);
cJSON_AddNumberToObject(o, "pitch_m", round_to(pitch, 0.00001));
}
const cJSON *dd_in = cJSON_GetObjectItem(raw, "dmx_definitions");
cJSON *dmx_defs = cJSON_AddObjectToObject(room, "dmx_definitions");
if (!err[0] && dd_in && !cJSON_IsNull(dd_in)) {
validate_dmx_defs(dd_in, dmx_defs, err, errn);
}
const cJSON *fx_in = cJSON_GetObjectItem(raw, "fixtures"), *f;
cJSON *fixtures = cJSON_AddObjectToObject(room, "fixtures");
if (!err[0] && fx_in && !cJSON_IsObject(fx_in)) {
snprintf(err, errn, "fixtures must be an object keyed by id");
}
if (!err[0] && cJSON_GetArraySize(fx_in) > MAX_FIXTURES) {
snprintf(err, errn, "at most %d strips in a room", MAX_FIXTURES);
snprintf(err, errn, "at most %d fixtures in a room", MAX_FIXTURES);
}
cJSON_ArrayForEach(f, fx_in)
{
if (err[0]) {
break;
}
const char *fid = f->string;
if (!id_ok(fid) || !cJSON_IsObject(f)) {
snprintf(err, errn, "strip ids are 1-40 letters, digits or -_.#, each an object");
break;
}
const cJSON *def = cJSON_GetObjectItem(f, "def"), *mac = cJSON_GetObjectItem(f, "mac");
const cJSON *label = cJSON_GetObjectItem(f, "label");
if (!cJSON_IsString(def) || !cJSON_GetObjectItem(defs, def->valuestring)) {
snprintf(err, errn, "strip %s: unknown strip type", fid);
break;
}
if (!cJSON_IsString(mac) || !mac_ok(mac->valuestring)) {
snprintf(err, errn, "strip %s: mac must be like 02:51:4d:00:00:01", fid);
break;
}
char m[18];
snprintf(m, sizeof(m), "%s", mac->valuestring);
for (char *c = m; *c; c++) {
*c = (char)tolower((unsigned char)*c);
}
const cJSON *other;
cJSON_ArrayForEach(other, fixtures)
{
const cJSON *om = cJSON_GetObjectItem(other, "mac");
if (cJSON_IsString(om) && !strcmp(om->valuestring, m)) {
snprintf(err, errn, "strip %s: mac %s is used twice in this room", fid, m);
}
}
if (err[0]) {
break;
}
char what[80];
cJSON *o = cJSON_AddObjectToObject(fixtures, fid);
char lab[48];
snprintf(lab, sizeof(lab), "%s", cJSON_IsString(label) && label->valuestring[0] ? label->valuestring : fid);
cJSON_AddStringToObject(o, "label", lab);
cJSON_AddStringToObject(o, "def", def->valuestring);
cJSON_AddStringToObject(o, "mac", m);
snprintf(what, sizeof(what), "%s x", fid);
cJSON_AddNumberToObject(o, "x", round_to(num_in(f, "x", 0, -1000, 1000, what, err, errn), 0.001));
snprintf(what, sizeof(what), "%s y", fid);
cJSON_AddNumberToObject(o, "y", round_to(num_in(f, "y", 0, -1000, 1000, what, err, errn), 0.001));
snprintf(what, sizeof(what), "%s direction", fid);
cJSON_AddNumberToObject(o, "direction",
round_to(wrap_deg(num_in(f, "direction", 90, -3600, 3600, what, err, errn)), 0.1));
const cJSON *leds = cJSON_GetObjectItem(f, "leds");
if (leds && !cJSON_IsNull(leds)) {
snprintf(what, sizeof(what), "%s leds", fid);
cJSON_AddNumberToObject(o, "leds", (int)num_in(f, "leds", 1, 1, MAX_LEDS, what, err, errn));
}
validate_fixture(f, defs, dmx_defs, fixtures, err, errn);
}
if (!err[0]) {
dmx_overlaps(room, err, errn);
}
if (err[0]) {
cJSON_Delete(room);
@@ -567,7 +779,11 @@ static const cJSON *fixture_by_mac(const cJSON *room, const char *mac12)
const cJSON *f;
cJSON_ArrayForEach(f, cJSON_GetObjectItem(room, "fixtures"))
{
const char *m = cJSON_GetObjectItem(f, "mac")->valuestring;
const cJSON *mj = cJSON_GetObjectItem(f, "mac");
if (!cJSON_IsString(mj)) {
continue; // a DDP or Art-Net fixture
}
const char *m = mj->valuestring;
char h[13];
int k = 0;
for (const char *c = m; *c && k < 12; c++) {
@@ -614,8 +830,8 @@ static bool send_frame(int fd, httpd_ws_type_t type, const uint8_t *data, size_t
return httpd_ws_send_frame_async(s_hd, fd, &f) == ESP_OK;
}
// Called by bridge.c for the "browser" bridge: dst_mac(6) + packet
bool sim_rig_tx(const uint8_t *frame, size_t len)
// One binary frame to the page, its first byte the kind (0 ESP-NOW, 1 Art-Net, 2 DDP)
bool sim_rig_page_send(const uint8_t *frame, size_t len)
{
if (!s_tx_mu) {
return false;
@@ -629,6 +845,44 @@ bool sim_rig_tx(const uint8_t *frame, size_t len)
return ok;
}
// Called by bridge.c for the "browser" bridge: dst_mac(6) + packet, sent to the page behind a 0 byte
bool sim_rig_tx(const uint8_t *frame, size_t len)
{
uint8_t buf[1 + 6 + 256];
if (len + 1 > sizeof(buf)) {
return false;
}
buf[0] = 0;
memcpy(buf + 1, frame, len);
return sim_rig_page_send(buf, len + 1);
}
// The receiver listens on the DDP ports the room's DDP fixtures use
static void room_ports(const cJSON *room)
{
uint16_t ports[SIM_NET_MAX_DDP_PORTS];
int n = 0;
const cJSON *f;
cJSON_ArrayForEach(f, cJSON_GetObjectItem(room, "fixtures"))
{
const cJSON *p = cJSON_GetObjectItem(f, "port");
if (!strcmp(str_or(f, "type", ""), "ddp") && cJSON_IsNumber(p) && n < SIM_NET_MAX_DDP_PORTS) {
ports[n++] = (uint16_t)p->valueint;
}
}
sim_net_set_ddp_ports(ports, n);
}
static void active_room_ports(void)
{
char err[160];
cJSON *room = load_room(s_room, err, sizeof(err));
if (room) {
room_ports(room);
}
cJSON_Delete(room);
}
static esp_err_t ws_open(httpd_req_t *req)
{
if (auth_ws_admit(req, "sim", AUTH_VIEW) != ESP_OK) {
@@ -794,9 +1048,9 @@ static void next_mac(const cJSON *taken, char out[18])
const cJSON *f, *fixtures = room ? cJSON_GetObjectItem(room, "fixtures") : NULL;
cJSON_ArrayForEach(f, fixtures)
{
const char *m = cJSON_GetObjectItem(f, "mac")->valuestring;
if (all && len + 20 < cap) {
len += (size_t)snprintf(all + len, cap - len, "%s,", m);
const cJSON *m = cJSON_GetObjectItem(f, "mac");
if (all && cJSON_IsString(m) && len + 20 < cap) {
len += (size_t)snprintf(all + len, cap - len, "%s,", m->valuestring);
}
}
cJSON_Delete(room);
@@ -870,6 +1124,10 @@ static esp_err_t api_handler(httpd_req_t *req)
cJSON_AddStringToObject(o, "mac", mac);
ret = api_json(req, "200 OK", o, true);
}
} else if (!strcmp(a, "dmx") && !b[0] && m == HTTP_GET) {
char q[32] = "";
bool data = httpd_req_get_url_query_str(req, q, sizeof(q)) == ESP_OK && strstr(q, "data=1");
ret = api_json(req, "200 OK", sim_net_status(data), true);
} else if (!strcmp(a, "rooms") && !b[0] && m == HTTP_GET) {
ret = rooms_info(req);
} else if (!strcmp(a, "rooms") && !b[0] && m == HTTP_POST) {
@@ -939,6 +1197,9 @@ static esp_err_t api_handler(httpd_req_t *req)
ret = api_error(req, "500 Internal Server Error", "could not write the room");
cJSON_Delete(room);
} else {
if (!strcmp(b, s_room)) {
room_ports(room);
}
cJSON *o = cJSON_CreateObject();
cJSON_AddItemToObject(o, "room", room);
ret = api_json(req, "200 OK", o, true);
@@ -967,6 +1228,7 @@ static esp_err_t api_handler(httpd_req_t *req)
} else {
snprintf(s_room, sizeof(s_room), "%s", nm->valuestring);
save_active();
active_room_ports();
ESP_LOGI(TAG, "room: %s", s_room);
ret = api_json(req, "200 OK", config_json(), true);
}
@@ -989,6 +1251,8 @@ esp_err_t sim_rig_register(httpd_handle_t server)
return ESP_ERR_NO_MEM;
}
load_active();
sim_net_start();
active_room_ports();
const httpd_uri_t ws = { .uri = "/sim/ws", .method = HTTP_GET, .handler = ws_handler, .is_websocket = true,
.handle_ws_control_frames = false, .ws_post_handshake_cb = ws_open };
esp_err_t err = httpd_register_uri_handler(server, &ws);