The ESP-IDF project, docs, tools, tests and web UI move from v2/ to the root, and the Makefile, README, build scripts and test runner drop the v2/ prefix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
188 lines
4.9 KiB
C
188 lines
4.9 KiB
C
#include "envelope.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
void env_blob_init(env_blob_t *b)
|
|
{
|
|
b->data[0] = 0;
|
|
b->len = 1;
|
|
}
|
|
|
|
static bool put(env_blob_t *b, const void *src, size_t n)
|
|
{
|
|
if (b->len + n > ENV_BLOB_MAX) {
|
|
return false;
|
|
}
|
|
memcpy(b->data + b->len, src, n);
|
|
b->len += n;
|
|
return true;
|
|
}
|
|
|
|
static bool put_u8(env_blob_t *b, unsigned v)
|
|
{
|
|
uint8_t x = v;
|
|
return put(b, &x, 1);
|
|
}
|
|
|
|
static bool put_le16(env_blob_t *b, unsigned v)
|
|
{
|
|
uint8_t x[2] = { v & 255, (v >> 8) & 255 };
|
|
return put(b, x, 2);
|
|
}
|
|
|
|
// Python truthiness of a JSON value
|
|
static bool truthy(const cJSON *v)
|
|
{
|
|
if (!v || cJSON_IsNull(v) || cJSON_IsFalse(v)) {
|
|
return false;
|
|
}
|
|
if (cJSON_IsNumber(v)) {
|
|
return v->valuedouble != 0;
|
|
}
|
|
if (cJSON_IsString(v)) {
|
|
return v->valuestring[0] != 0;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
static const cJSON *get2(const cJSON *o, const char *a, const char *b)
|
|
{
|
|
const cJSON *v = cJSON_GetObjectItem(o, a);
|
|
return truthy(v) ? v : cJSON_GetObjectItem(o, b);
|
|
}
|
|
|
|
static int clamp(double v, int lo, int hi)
|
|
{
|
|
int i = (int)v;
|
|
return i < lo ? lo : (i > hi ? hi : i);
|
|
}
|
|
|
|
static int num_or(const cJSON *v, int dflt)
|
|
{
|
|
return cJSON_IsNumber(v) ? (int)v->valuedouble : (cJSON_IsBool(v) ? cJSON_IsTrue(v) : dflt);
|
|
}
|
|
|
|
// Same rule as the Python packer: 6+ hex digits or an [r,g,b] list; anything else is skipped
|
|
static bool color_rgb(const cJSON *c, uint8_t rgb[3])
|
|
{
|
|
if (cJSON_IsString(c)) {
|
|
const char *s = c->valuestring;
|
|
while (*s == ' ') {
|
|
s++;
|
|
}
|
|
while (*s == '#') {
|
|
s++;
|
|
}
|
|
if (strlen(s) < 6) {
|
|
return false;
|
|
}
|
|
char full[7];
|
|
memcpy(full, s, 6);
|
|
full[6] = 0;
|
|
char *end;
|
|
unsigned long v = strtoul(full, &end, 16);
|
|
if (*end) {
|
|
return false;
|
|
}
|
|
rgb[0] = v >> 16;
|
|
rgb[1] = v >> 8;
|
|
rgb[2] = v;
|
|
return true;
|
|
}
|
|
if (cJSON_IsArray(c) && cJSON_GetArraySize(c) >= 3) {
|
|
for (int i = 0; i < 3; i++) {
|
|
rgb[i] = (uint8_t)num_or(cJSON_GetArrayItem(c, i), 0);
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool env_add_preset(env_blob_t *b, const char *name, const cJSON *p)
|
|
{
|
|
size_t save_len = b->len;
|
|
uint8_t save_count = b->data[0];
|
|
|
|
const cJSON *pat = get2(p, "p", "pattern");
|
|
const char *pattern = cJSON_IsString(pat) ? pat->valuestring : "off";
|
|
const cJSON *cols = get2(p, "c", "colors");
|
|
int ncol = cJSON_IsArray(cols) ? cJSON_GetArraySize(cols) : 0;
|
|
size_t nl = strlen(name), pl = strlen(pattern);
|
|
if (nl > 250 || pl > 250 || ncol > 255) {
|
|
return false;
|
|
}
|
|
|
|
uint8_t rgbs[255][3];
|
|
int nvalid = 0;
|
|
for (int i = 0; i < ncol; i++) {
|
|
if (color_rgb(cJSON_GetArrayItem(cols, i), rgbs[nvalid])) {
|
|
nvalid++;
|
|
}
|
|
}
|
|
bool ok = put_u8(b, nl) && put(b, name, nl) && put_u8(b, pl) && put(b, pattern, pl) && put_u8(b, nvalid);
|
|
for (int i = 0; ok && i < nvalid; i++) {
|
|
ok = put(b, rgbs[i], 3);
|
|
}
|
|
const cJSON *d = get2(p, "d", "delay"), *br = get2(p, "b", "brightness");
|
|
const cJSON *a = cJSON_GetObjectItem(p, "a");
|
|
if (!a) {
|
|
a = cJSON_GetObjectItem(p, "auto");
|
|
}
|
|
int auto_ = a ? truthy(a) : 1;
|
|
ok = ok && put_le16(b, clamp(num_or(d, 100), 0, 65535)) && put_u8(b, clamp(num_or(br, 127), 0, 255)) &&
|
|
put_u8(b, auto_);
|
|
for (int i = 1; ok && i <= 6; i++) {
|
|
char key[4] = { 'n', '0' + i, 0 };
|
|
ok = put_le16(b, (uint16_t)(int16_t)clamp(num_or(cJSON_GetObjectItem(p, key), 0), -32768, 32767));
|
|
}
|
|
if (!ok) {
|
|
b->len = save_len;
|
|
b->data[0] = save_count;
|
|
return false;
|
|
}
|
|
b->data[0]++;
|
|
return true;
|
|
}
|
|
|
|
bool env_add_select(env_blob_t *b, const char *device, const char *preset, int step)
|
|
{
|
|
size_t save_len = b->len;
|
|
size_t dl = strlen(device), pl = strlen(preset);
|
|
if (dl > 250 || pl > 250 || (step > 65535)) {
|
|
return false;
|
|
}
|
|
bool ok = put_u8(b, dl) && put(b, device, dl) && put_u8(b, pl) && put(b, preset, pl) && put_u8(b, step >= 0);
|
|
if (ok && step >= 0) {
|
|
ok = put_le16(b, step);
|
|
}
|
|
if (!ok) {
|
|
b->len = save_len;
|
|
return false;
|
|
}
|
|
b->data[0]++;
|
|
return true;
|
|
}
|
|
|
|
size_t env_frame(uint8_t *out, int brightness, const env_blob_t *presets, const env_blob_t *select)
|
|
{
|
|
size_t lp = presets && presets->data[0] ? presets->len : 0;
|
|
size_t ls = select && select->data[0] ? select->len : 0;
|
|
int wire = 255;
|
|
if (brightness >= 0) {
|
|
wire = brightness > 255 ? 127 : (brightness * 127 + 127) / 255; // 0-127 scales to 0-255
|
|
}
|
|
out[0] = 2;
|
|
out[1] = wire;
|
|
out[2] = lp;
|
|
out[3] = ls;
|
|
out[4] = 0;
|
|
if (lp) {
|
|
memcpy(out + 5, presets->data, lp);
|
|
}
|
|
if (ls) {
|
|
memcpy(out + 5 + lp, select->data, ls);
|
|
}
|
|
return 5 + lp + ls;
|
|
}
|