Files
led-hoop/c/src/web.c
T
jimmyandClaude Opus 5.5 bf0429c505 Embed gzipped web assets at build time and type LED edit commands.
Generate the web UI headers from c/web/ via scripts/embed_file.py (minify + gzip) and serve them with Content-Encoding, replacing the checked-in web_*.h blobs. Move the LED driver/presets into lib/leds, and parse preset edits into a typed cmd_preset_t on core0 instead of passing raw JSON to core1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 22:18:10 +13:00

207 lines
6.2 KiB
C

#include <stdio.h>
#include <string.h>
#include "httpserver.h"
#include "web.h"
#include "web_index_html.h"
#include "web_main_js.h"
#include "web_styles_css.h"
#include "flash_store.h"
#include "mini_json.h"
#include "cmd_queue.h"
#define WS_MSG_MAX 2048
#define JSON_OBJ_MAX 1024
static httpserver_t *g_http;
static char g_buttons_json[FLASH_STORE_MAX];
static void buttons_load(void) {
size_t n = 0;
if (!flash_store_load("buttons", g_buttons_json, sizeof(g_buttons_json), &n)) {
snprintf(g_buttons_json, sizeof(g_buttons_json), "{\"buttons\":[]}");
}
}
static void on_index(httpserver_conn_t *conn, const char *path, const char *query) {
(void)path;
(void)query;
httpserver_send_static(conn, 200, "text/html; charset=utf-8",
web_index_html, web_index_html_len, "gzip");
}
static void on_main_js(httpserver_conn_t *conn, const char *path, const char *query) {
(void)path;
(void)query;
httpserver_send_static(conn, 200, "application/javascript; charset=utf-8",
web_main_js, web_main_js_len, "gzip");
}
static void on_styles_css(httpserver_conn_t *conn, const char *path, const char *query) {
(void)path;
(void)query;
httpserver_send_static(conn, 200, "text/css; charset=utf-8",
web_styles_css, web_styles_css_len, "gzip");
}
static void on_get_buttons(httpserver_conn_t *conn, const char *path, const char *query) {
(void)path;
(void)query;
httpserver_send(conn, 200, "application/json", g_buttons_json, strlen(g_buttons_json));
}
static void on_post_buttons(httpserver_conn_t *conn, const char *path, const char *query) {
(void)path;
(void)query;
size_t len = 0;
const char *body = httpserver_request_body(conn, &len);
if (!body || len == 0 || len >= sizeof(g_buttons_json)) {
httpserver_send(conn, 500, "application/json", "{\"ok\":false}", 12);
return;
}
memcpy(g_buttons_json, body, len);
g_buttons_json[len] = '\0';
if (!flash_store_save("buttons", g_buttons_json, len)) {
httpserver_send(conn, 500, "application/json", "{\"ok\":false,\"error\":\"save failed\"}", 34);
return;
}
httpserver_send(conn, 200, "application/json", "{\"ok\":true}", 11);
}
static void on_ws_open(httpserver_conn_t *ws) {
(void)ws;
printf("ws: open\n");
}
static void push_select(const char *name) {
led_cmd_t cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.type = CMD_SELECT;
snprintf(cmd.name, sizeof(cmd.name), "%s", name);
cmd_queue_push(&cmd);
}
static void parse_preset_fields(const char *json, cmd_preset_t *p) {
memset(p, 0, sizeof(*p));
p->d = 100;
p->b = 127;
snprintf(p->pattern, sizeof(p->pattern), "off");
char s[CMD_PATTERN_MAX];
if (json_get_string(json, "p", s, sizeof(s))) {
snprintf(p->pattern, sizeof(p->pattern), "%s", s);
}
json_get_int(json, "d", &p->d);
json_get_int(json, "b", &p->b);
json_get_int(json, "n1", &p->n1);
json_get_int(json, "n2", &p->n2);
json_get_int(json, "n3", &p->n3);
json_get_int(json, "n4", &p->n4);
json_get_int(json, "n5", &p->n5);
json_get_int(json, "n6", &p->n6);
json_get_int(json, "n7", &p->n7);
json_get_int(json, "n8", &p->n8);
uint8_t colors[CMD_COLORS_MAX][3];
size_t n = 0;
if (json_get_rgb_array(json, "c", colors, CMD_COLORS_MAX, &n) && n > 0) {
p->c_count = (uint8_t)n;
for (size_t i = 0; i < n; i++) {
p->c[i].r = colors[i][0];
p->c[i].g = colors[i][1];
p->c[i].b = colors[i][2];
}
} else {
p->c_count = 1;
p->c[0].r = 255;
p->c[0].g = 255;
p->c[0].b = 255;
}
}
static void on_ws_msg(httpserver_conn_t *ws, const uint8_t *data, size_t len, bool text) {
(void)text;
char msg[WS_MSG_MAX + 1];
if (len >= sizeof(msg)) {
len = sizeof(msg) - 1;
}
memcpy(msg, data, len);
msg[len] = '\0';
printf("ws: %s\n", msg);
char name[CMD_NAME_MAX];
if (json_get_select_name(msg, name, sizeof(name))) {
push_select(name);
char out[96];
int n = snprintf(out, sizeof(out), "{\"select\":\"%s\"}", name);
if (g_http) {
httpserver_ws_broadcast(g_http, out, (size_t)n, true);
} else {
httpserver_ws_send(ws, out, (size_t)n, true);
}
return;
}
char obj[JSON_OBJ_MAX];
if (json_get_object(msg, "preset_edit", obj, sizeof(obj))) {
led_cmd_t cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.type = CMD_EDIT;
if (!json_get_string(obj, "name", cmd.name, sizeof(cmd.name))) {
return;
}
char data[JSON_OBJ_MAX];
if (json_get_object(obj, "data", data, sizeof(data))) {
parse_preset_fields(data, &cmd.u.edit);
} else {
parse_preset_fields(obj, &cmd.u.edit);
}
cmd_queue_push(&cmd);
return;
}
if (json_get_string(msg, "preset_delete", name, sizeof(name))) {
led_cmd_t cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.type = CMD_DELETE;
snprintf(cmd.name, sizeof(cmd.name), "%s", name);
cmd_queue_push(&cmd);
return;
}
bool save = false;
if (json_get_bool(msg, "preset_save", &save) && save) {
led_cmd_t cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.type = CMD_SAVE;
cmd_queue_push(&cmd);
return;
}
int b = 0;
if (json_get_int(msg, "b", &b)) {
led_cmd_t cmd;
memset(&cmd, 0, sizeof(cmd));
cmd.type = CMD_BRIGHTNESS;
cmd.u.brightness = b;
cmd_queue_push(&cmd);
}
}
static void on_ws_close(httpserver_conn_t *ws) {
(void)ws;
printf("ws: close\n");
}
bool web_start(void) {
buttons_load();
g_http = httpserver_create();
httpserver_route(g_http, "GET", "/", on_index);
httpserver_route(g_http, "GET", "/static/main.js", on_main_js);
httpserver_route(g_http, "GET", "/static/styles.css", on_styles_css);
httpserver_route(g_http, "GET", "/api/buttons", on_get_buttons);
httpserver_route(g_http, "POST", "/api/buttons", on_post_buttons);
httpserver_websocket(g_http, "/ws", on_ws_open, on_ws_msg, on_ws_close);
return httpserver_listen(g_http, 80);
}