#include "dmx_wire.h" #include #include #include #include #include static void put16(uint8_t *p, unsigned v) { p[0] = v >> 8; p[1] = v; } size_t dmx_artnet_packet(uint8_t *pkt, uint8_t seq, int uni, const uint8_t *data, size_t n) { n = n > DMX_CHANNELS ? DMX_CHANNELS : n; size_t dl = n + (n & 1); dl = dl < 2 ? 2 : dl; memcpy(pkt, "Art-Net\0", 8); uint8_t h[10] = { 0x00, 0x50, 0, 14, seq, 0, uni & 0xFF, (uni >> 8) & 0x7F, dl >> 8, dl & 0xFF }; memcpy(pkt + 8, h, 10); memset(pkt + 18, 0, dl); memcpy(pkt + 18, data, n); return 18 + dl; } size_t dmx_sacn_packet(uint8_t *pkt, const uint8_t cid[16], const char *source, uint8_t seq, int uni, const uint8_t *data, size_t n) { n = n > DMX_CHANNELS ? DMX_CHANNELS : n; size_t total = 126 + n; memset(pkt, 0, 126); put16(pkt, 0x0010); memcpy(pkt + 4, "ASC-E1.17\0\0\0", 12); put16(pkt + 16, 0x7000 | (total - 16)); pkt[21] = 0x04; memcpy(pkt + 22, cid, 16); put16(pkt + 38, 0x7000 | (total - 38)); pkt[43] = 0x02; snprintf((char *)pkt + 44, 64, "%s", source); pkt[108] = 100; pkt[111] = seq; put16(pkt + 113, uni); put16(pkt + 115, 0x7000 | (total - 115)); pkt[117] = 0x02; pkt[118] = 0xA1; put16(pkt + 121, 0x0001); put16(pkt + 123, n + 1); memcpy(pkt + 126, data, n); return total; } uint32_t dmx_sacn_group(int uni) { return 0xEFFF0000u | (uni & 0xFFFF); } // "Red 3" -> DMX_ROLE_RED, 3; "Red" -> DMX_ROLE_RED, 0 (no number); other names -> FIXED static int colour_role(const char *name, int *num) { static const struct { const char *word; int role; } WORDS[] = { { "red", DMX_ROLE_RED }, { "green", DMX_ROLE_GREEN }, { "blue", DMX_ROLE_BLUE }, { "white", DMX_ROLE_WHITE } }; *num = 0; for (size_t i = 0; i < sizeof(WORDS) / sizeof(WORDS[0]); i++) { size_t w = strlen(WORDS[i].word); if (strncasecmp(name, WORDS[i].word, w) != 0) { continue; } const char *rest = name + w; if (!*rest) { return WORDS[i].role; } if (*rest != ' ' || !isdigit((unsigned char)rest[1])) { continue; } char *end; long n = strtol(rest + 1, &end, 10); if (*end || n < 1 || n > 9999) { continue; } *num = (int)n; return WORDS[i].role; } return DMX_ROLE_FIXED; } static bool is_any(const char *name, const char *const *list, size_t n) { for (size_t i = 0; i < n; i++) { if (!strcasecmp(name, list[i])) { return true; } } return false; } int dmx_def_map(const cJSON *def, dmx_slot_t *slots, int max, int *pixels, bool *white) { static const char *const DIMMERS[] = { "Dimmer", "Intensity", "Master Dimmer", "Center Brightness" }; static const char *const STROBES[] = { "Strobe", "Shutter", "Center Strobe" }; const cJSON *chans = cJSON_GetObjectItem(def, "channels"); const cJSON *defaults = cJSON_GetObjectItem(def, "defaults"); *pixels = 1; *white = false; if (!cJSON_IsArray(chans)) { return 0; } // The numbers of the pixel groups, in order: "Red 1".."Red 7" are pixels 0..6; plain "Red" is number 0 int nums[DMX_PIXELS_MAX], nnums = 0; int count = 0; const cJSON *c; cJSON_ArrayForEach(c, chans) { if (count >= max) { break; } const char *name = cJSON_IsString(c) ? c->valuestring : ""; while (isspace((unsigned char)*name)) { name++; } char trimmed[48]; snprintf(trimmed, sizeof(trimmed), "%s", name); for (int k = (int)strlen(trimmed) - 1; k >= 0 && isspace((unsigned char)trimmed[k]); k--) { trimmed[k] = 0; } dmx_slot_t *s = &slots[count++]; memset(s, 0, sizeof(*s)); int num; int role = colour_role(trimmed, &num); if (role != DMX_ROLE_FIXED) { int at = -1; for (int k = 0; k < nnums; k++) { at = nums[k] == num ? k : at; } if (at < 0 && nnums < DMX_PIXELS_MAX) { nums[nnums++] = num; } s->role = (uint8_t)role; s->pixel = (uint8_t)num; // the group's number for now; its index once all are known *white = *white || role == DMX_ROLE_WHITE; } else if (is_any(trimmed, DIMMERS, sizeof(DIMMERS) / sizeof(DIMMERS[0]))) { s->role = DMX_ROLE_DIMMER; } else if (is_any(trimmed, STROBES, sizeof(STROBES) / sizeof(STROBES[0]))) { s->role = DMX_ROLE_STROBE; } else { const cJSON *d = cJSON_GetObjectItem(defaults, trimmed); int v = cJSON_IsNumber(d) ? d->valueint : 0; s->value = (uint8_t)(v < 0 ? 0 : v > 255 ? 255 : v); } } // Sort the group numbers, then turn each slot's number into its pixel index for (int i = 1; i < nnums; i++) { for (int k = i; k > 0 && nums[k - 1] > nums[k]; k--) { int t = nums[k]; nums[k] = nums[k - 1]; nums[k - 1] = t; } } for (int i = 0; i < count; i++) { if (slots[i].role < DMX_ROLE_RED || slots[i].role > DMX_ROLE_WHITE) { continue; } int idx = 0; for (int k = 0; k < nnums; k++) { idx = nums[k] == slots[i].pixel ? k : idx; } slots[i].pixel = (uint8_t)idx; } *pixels = nnums ? nnums : 1; return count; }