feat: faster, correct preset editor; default presets use their own pattern

- Editing a preset can change its pattern (the dropdown was disabled).
- on, off and rainbow are built-in patterns and are always listed, so the
  editor no longer falls back to the first pattern (aurora) for them.
- The editor loads the pattern names once and only the selected pattern's
  definition (new GET /patterns/names and GET /patterns/<name>).
- A shared GET cache (fetch_cache.js) reuses profile, palette, zone, preset,
  group and pattern lookups; any write clears it and entries expire at 30 s.
- The seeded rainbow preset uses the rainbow pattern.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Sonnet 5.5 committed 2026-11-10 01:46:11 +13:00
1 parent d4118072f6
commit 38162d4b89
9 files changed
+189 -81

No files matched your search

+56 -3
View File
@@ -139,10 +139,24 @@ static esp_err_t write_file(const char *base, const char *code, size_t len)
return ok ? ESP_OK : ESP_FAIL;
}
// GET /patterns and /patterns/definitions: stored metadata plus {} for every other pattern file
static esp_err_t list_patterns(httpd_req_t *req)
// Patterns the driver has built in (no .py file); the seed presets use them
static const struct {
const char *name, *json;
} BUILTIN[] = {
{ "on", "{\"min_delay\":10,\"max_delay\":10000,\"max_colors\":1}" },
{ "off", "{\"min_delay\":10,\"max_delay\":10000,\"max_colors\":0}" },
{ "rainbow", "{\"n1\":\"Step Rate\",\"min_delay\":10,\"max_delay\":10000,\"max_colors\":0}" },
};
// Stored metadata plus the built-ins and {} for every other pattern file, as {name: {...}}
static cJSON *all_patterns(void)
{
cJSON *o = cJSON_Duplicate(store_coll("pattern"), true);
for (size_t i = 0; i < sizeof(BUILTIN) / sizeof(BUILTIN[0]); i++) {
if (!cJSON_GetObjectItem(o, BUILTIN[i].name)) {
cJSON_AddItemToObject(o, BUILTIN[i].name, cJSON_Parse(BUILTIN[i].json));
}
}
cJSON *files = file_names(), *it;
cJSON_ArrayForEach(it, files)
{
@@ -151,7 +165,38 @@ static esp_err_t list_patterns(httpd_req_t *req)
}
}
cJSON_Delete(files);
return api_json(req, "200 OK", o, true);
return o;
}
// GET /patterns and /patterns/definitions: every pattern with its definition
static esp_err_t list_patterns(httpd_req_t *req)
{
return api_json(req, "200 OK", all_patterns(), true);
}
// GET /patterns/names: just the names, for a pattern picker that loads definitions one at a time
static esp_err_t list_pattern_names(httpd_req_t *req)
{
cJSON *all = all_patterns(), *names = cJSON_CreateArray(), *it;
cJSON_ArrayForEach(it, all)
{
cJSON_AddItemToArray(names, cJSON_CreateString(it->string));
}
cJSON_Delete(all);
return api_json(req, "200 OK", names, true);
}
// GET /patterns/<name>: one pattern's definition ({} when it has none)
static esp_err_t get_pattern(httpd_req_t *req, const char *name)
{
const cJSON *rec = cJSON_GetObjectItem(store_coll("pattern"), name);
for (size_t i = 0; !rec && i < sizeof(BUILTIN) / sizeof(BUILTIN[0]); i++) {
if (!strcmp(BUILTIN[i].name, name)) {
cJSON *o = cJSON_Parse(BUILTIN[i].json);
return api_json(req, "200 OK", o, true);
}
}
return api_json(req, "200 OK", rec ? (cJSON *)rec : cJSON_CreateObject(), !rec);
}
// POST /patterns {name, data: {...}, ...}: data (plus any other keys) becomes the record under name
@@ -452,6 +497,14 @@ bool patterns_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret)
*ret = list_patterns(req);
return true;
}
if (method == HTTP_GET && n == 2 && !strcmp(seg[1], "names")) {
*ret = list_pattern_names(req);
return true;
}
if (method == HTTP_GET && n == 2 && strcmp(seg[1], "ota") && strcmp(seg[1], "upload") && strcmp(seg[1], "driver")) {
*ret = get_pattern(req, seg[1]);
return true;
}
if (method != HTTP_POST) {
return false;
}