Port Soft-AP web UI and LED engine onto Pico 2 W C firmware.

Serve the ESP32 frontend over httpserver/WebSocket, drive eight WS2812 strips with PIO/DMA patterns on core1, and keep Wi-Fi control on core0.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
jimmyandCursor committed 2026-07-20 22:03:52 +12:00
1 parent b783d97f13
commit b3e9fc9663
28 files changed
+5123 -14

No files matched your search

+166
View File
@@ -0,0 +1,166 @@
#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
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);
}
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);
}
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);
}
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 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);
/* broadcast selection so other clients update */
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[CMD_DATA_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[CMD_DATA_MAX];
if (json_get_object(obj, "data", data, sizeof(data))) {
snprintf(cmd.data, sizeof(cmd.data), "%s", data);
} else {
/* data may be inline in edit object without nested key in some payloads */
snprintf(cmd.data, sizeof(cmd.data), "%s", obj);
}
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.ival = 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);
}