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>
This commit is contained in:
jimmyandClaude Opus 5.5 committed 2026-11-08 22:18:10 +13:00
1 parent b3e9fc9663
commit bf0429c505
19 files changed
+395 -2205

No files matched your search

+71 -34
View File
@@ -53,7 +53,10 @@ struct httpserver_conn {
/* streaming static response */
const uint8_t *static_body;
size_t static_len;
size_t static_sent;
size_t static_sent; /* bytes queued via tcp_write */
size_t tx_acked; /* bytes ACKed (from tcp_sent) */
size_t tx_total; /* hdr_len + static_len */
bool write_done;
char hdr[HS_HDR_BUF];
int hdr_len;
int hdr_sent;
@@ -364,43 +367,60 @@ static bool parse_request_line(const char *req, char *method, size_t method_len,
}
static err_t pump_static(httpserver_conn_t *conn) {
if (!conn->pcb) {
if (!conn->pcb || !conn->static_body) {
return ERR_OK;
}
while (conn->hdr_sent < conn->hdr_len) {
u16_t left = (u16_t)(conn->hdr_len - conn->hdr_sent);
u16_t space = tcp_sndbuf(conn->pcb);
if (space == 0) {
return ERR_OK;
}
u16_t n = left < space ? left : space;
if (n > HS_SEND_CHUNK) {
n = HS_SEND_CHUNK;
}
if (tcp_write(conn->pcb, conn->hdr + conn->hdr_sent, n, TCP_WRITE_FLAG_COPY) != ERR_OK) {
return close_conn(conn, conn->pcb, ERR_ABRT);
}
conn->hdr_sent += n;
}
while (conn->static_sent < conn->static_len) {
u16_t left = (u16_t)(conn->static_len - conn->static_sent);
size_t left = (size_t)(conn->hdr_len - conn->hdr_sent);
u16_t space = tcp_sndbuf(conn->pcb);
if (space == 0) {
tcp_output(conn->pcb);
return ERR_OK;
}
u16_t n = left < space ? left : space;
u16_t n = left < space ? (u16_t)left : space;
if (n > HS_SEND_CHUNK) {
n = HS_SEND_CHUNK;
}
/* body is const flash — may use COPY for safety across chips */
if (tcp_write(conn->pcb, conn->static_body + conn->static_sent, n, TCP_WRITE_FLAG_COPY) != ERR_OK) {
err_t err = tcp_write(conn->pcb, conn->hdr + conn->hdr_sent, n, TCP_WRITE_FLAG_COPY);
if (err == ERR_MEM) {
tcp_output(conn->pcb);
return ERR_OK;
}
if (err != ERR_OK) {
return close_conn(conn, conn->pcb, ERR_ABRT);
}
conn->hdr_sent += n;
}
while (conn->static_sent < conn->static_len) {
size_t left = conn->static_len - conn->static_sent;
u16_t space = tcp_sndbuf(conn->pcb);
if (space == 0) {
tcp_output(conn->pcb);
return ERR_OK;
}
u16_t n = left < space ? (u16_t)left : space;
if (n > HS_SEND_CHUNK) {
n = HS_SEND_CHUNK;
}
err_t err = tcp_write(conn->pcb, conn->static_body + conn->static_sent, n, TCP_WRITE_FLAG_COPY);
if (err == ERR_MEM) {
tcp_output(conn->pcb);
return ERR_OK;
}
if (err != ERR_OK) {
return close_conn(conn, conn->pcb, ERR_ABRT);
}
conn->static_sent += n;
}
tcp_output(conn->pcb);
if (conn->static_sent >= conn->static_len && conn->hdr_sent >= conn->hdr_len) {
conn->write_done = true;
/* Only close after peer has ACKed every queued byte — closing earlier
* truncates the response and triggers ERR_CONTENT_LENGTH_MISMATCH. */
if (conn->tx_acked >= conn->tx_total) {
return close_conn(conn, conn->pcb, ERR_OK);
}
return ERR_OK;
@@ -572,10 +592,10 @@ static err_t try_dispatch(httpserver_conn_t *conn) {
static err_t on_sent(void *arg, struct tcp_pcb *pcb, u16_t len) {
httpserver_conn_t *conn = arg;
(void)pcb;
(void)len;
if (conn->mode != CONN_HTTP) {
return ERR_OK;
}
conn->tx_acked += len;
if (conn->static_body) {
return pump_static(conn);
}
@@ -613,10 +633,11 @@ static err_t on_poll(void *arg, struct tcp_pcb *pcb) {
if (conn->mode == CONN_WS) {
return ERR_OK;
}
if (conn->static_body && conn->static_sent < conn->static_len) {
/* Keep pumping / waiting for ACK on static responses */
if (conn->static_body) {
return pump_static(conn);
}
if (conn->dispatched && !conn->static_body) {
if (conn->dispatched) {
return close_conn(conn, pcb, ERR_OK);
}
return close_conn(conn, pcb, ERR_OK);
@@ -708,30 +729,46 @@ bool httpserver_listen(httpserver_t *s, uint16_t port) {
}
void httpserver_send_static(httpserver_conn_t *conn, int status, const char *content_type,
const uint8_t *body, size_t body_len) {
const uint8_t *body, size_t body_len,
const char *content_encoding) {
if (!conn || conn->mode != CONN_HTTP || !conn->pcb) {
return;
}
if (!content_type) {
content_type = "application/octet-stream";
}
conn->hdr_len = snprintf(conn->hdr, sizeof(conn->hdr),
"HTTP/1.1 %d %s\r\n"
"Content-Type: %s\r\n"
"Content-Length: %u\r\n"
"Connection: close\r\n"
"\r\n",
status, status_text(status), content_type, (unsigned)body_len);
if (content_encoding && content_encoding[0]) {
conn->hdr_len = snprintf(conn->hdr, sizeof(conn->hdr),
"HTTP/1.1 %d %s\r\n"
"Content-Type: %s\r\n"
"Content-Encoding: %s\r\n"
"Content-Length: %u\r\n"
"Connection: close\r\n"
"\r\n",
status, status_text(status), content_type, content_encoding,
(unsigned)body_len);
} else {
conn->hdr_len = snprintf(conn->hdr, sizeof(conn->hdr),
"HTTP/1.1 %d %s\r\n"
"Content-Type: %s\r\n"
"Content-Length: %u\r\n"
"Connection: close\r\n"
"\r\n",
status, status_text(status), content_type, (unsigned)body_len);
}
conn->hdr_sent = 0;
conn->static_body = body ? body : (const uint8_t *)"";
conn->static_len = body_len;
conn->static_sent = 0;
conn->tx_acked = 0;
conn->tx_total = (size_t)conn->hdr_len + body_len;
conn->write_done = false;
pump_static(conn);
}
void httpserver_send(httpserver_conn_t *conn, int status, const char *content_type,
const void *body, size_t body_len) {
httpserver_send_static(conn, status, content_type, (const uint8_t *)body, body_len);
httpserver_send_static(conn, status, content_type, (const uint8_t *)body, body_len, NULL);
}
void httpserver_send_html(httpserver_conn_t *conn, const char *html) {
+4 -2
View File
@@ -31,9 +31,11 @@ bool httpserver_listen(httpserver_t *s, uint16_t port);
void httpserver_send(httpserver_conn_t *conn, int status, const char *content_type,
const void *body, size_t body_len);
/* Stream a large const body (no copy of entire file into conn buffer). */
/* Stream a large const body (no copy of entire file into conn buffer).
* content_encoding may be NULL, or e.g. "gzip". */
void httpserver_send_static(httpserver_conn_t *conn, int status, const char *content_type,
const uint8_t *body, size_t body_len);
const uint8_t *body, size_t body_len,
const char *content_encoding);
void httpserver_send_html(httpserver_conn_t *conn, const char *html);
+26
View File
@@ -0,0 +1,26 @@
#pragma once
#include <array>
#include <cstdint>
#include <string>
#include <vector>
struct RGB {
uint8_t r = 0;
uint8_t g = 0;
uint8_t b = 0;
};
class Preset {
public:
std::string p = "off";
int d = 100;
int b = 127;
std::vector<RGB> c{{255, 255, 255}};
bool a = true;
int n1 = 0, n2 = 0, n3 = 0, n4 = 0;
int n5 = 0, n6 = 0, n7 = 0, n8 = 0;
Preset() = default;
explicit Preset(std::string pattern) : p(std::move(pattern)) {}
};
+97
View File
@@ -0,0 +1,97 @@
#include "leds/presets.hpp"
#include <algorithm>
#include "patterns/patterns.hpp"
namespace {
struct StripConfig { uint pin, count, midpoint; };
constexpr StripConfig kConfig[] = {
{6, 291, 291 / 2}, {29, 290, 290 / 2 - 1}, {3, 283, 283 / 2},
{28, 278, 278 / 2 - 1}, {2, 278, 275 / 2}, {0, 283, 278 / 2 - 1},
{4, 290, 283 / 2}, {7, 291, 290 / 2 - 1},
};
}
Presets::Presets() {
for (size_t i = 0; i < kStripCount; ++i) {
strips[i] = std::make_unique<WS2812B>(kConfig[i].count, kConfig[i].pin, i);
strip_midpoints[i] = kConfig[i].midpoint;
scale_map[i].resize(kConfig[0].count);
for (size_t led = 0; led < scale_map[i].size(); ++led)
scale_map[i][led] = led * kConfig[i].count / kConfig[0].count;
}
}
Presets::~Presets() = default;
std::unique_ptr<Pattern> Presets::make_pattern(const std::string& name) const {
#define MAKE(name) if (name == #name) return std::make_unique<name>();
if (name == "off" || name == "on") return nullptr;
if (name == "blink") return std::make_unique<Blink>();
if (name == "rainbow") return std::make_unique<Rainbow>();
if (name == "pulse") return std::make_unique<Pulse>();
if (name == "transition") return std::make_unique<Transition>();
if (name == "chase") return std::make_unique<Chase>();
if (name == "circle") return std::make_unique<Circle>();
if (name == "roll") return std::make_unique<Roll>();
if (name == "calibration") return std::make_unique<Calibration>();
if (name == "test") return std::make_unique<Test>();
if (name == "grab") return std::make_unique<Grab>();
if (name == "spin") return std::make_unique<Spin>();
if (name == "lift") return std::make_unique<Lift>();
if (name == "flare") return std::make_unique<Flare>();
if (name == "hook") return std::make_unique<Hook>();
if (name == "pose") return std::make_unique<Pose>();
if (name == "point") return std::make_unique<Point>();
if (name == "scale_test") return std::make_unique<ScaleTest>();
return nullptr;
#undef MAKE
}
bool Presets::select(const std::string& name, int next_step) {
auto saved = presets.find(name);
Preset* preset = saved == presets.end() ? nullptr : &saved->second;
const std::string pattern = preset ? preset->p : name;
if (pattern == "off") { off(); current.reset(); selected = name; step = 0; return true; }
if (pattern == "on") {
static Preset default_on("on");
on(preset ? preset : &default_on); current.reset(); selected = name; return true;
}
current = make_pattern(pattern);
if (!current) return false;
static Preset built_in;
if (!preset) { built_in = Preset(pattern); preset = &built_in; }
if (next_step >= 0) step = next_step;
else if (selected != name) step = 0;
current->start(*this, preset);
selected = name;
return true;
}
bool Presets::edit(const std::string& name, const Preset& preset) { presets[name] = preset; return true; }
bool Presets::remove(const std::string& name) { return presets.erase(name) != 0; }
void Presets::tick() { if (current && !current->tick()) current.reset(); }
void Presets::fill(RGB color) { for (auto& strip : strips) strip->fill(color); }
void Presets::fill_n(RGB color, int first, int last) {
first = std::max(0, first); last = std::min<int>(kConfig[0].count, last);
for (int i = first; i < last; ++i) for (size_t s = 0; s < kStripCount; ++s) set(s, i, color);
}
bool Presets::set(size_t strip, int index, RGB color) {
if (strip >= kStripCount || index < 0 || index >= static_cast<int>(scale_map[strip].size())) return false;
strips[strip]->set(scale_map[strip][index], color); return true;
}
RGB Presets::apply_brightness(RGB color, int local) const {
const int effective = std::clamp(local, 0, 255) * b / 255;
return {static_cast<uint8_t>(color.r * effective / 255), static_cast<uint8_t>(color.g * effective / 255),
static_cast<uint8_t>(color.b * effective / 255)};
}
void Presets::show_all() { for (auto& strip : strips) strip->show(); }
void Presets::show_all_parallel() {
uint32_t mask = 0;
for (auto& strip : strips) { strip->prepare_show(); mask |= 1u << strip->dma_channel; }
dma_start_channel_mask(mask);
}
void Presets::off() { fill({0, 0, 0}); show_all_parallel(); }
void Presets::on(Preset* preset) {
const RGB color = preset && !preset->c.empty() ? preset->c[0] : RGB{255, 255, 255};
fill(apply_brightness(color, preset ? preset->b : 255)); show_all_parallel();
}
uint16_t Presets::strip_length(size_t strip) const { return strip < kStripCount ? strips[strip]->num_leds : 0; }
+46
View File
@@ -0,0 +1,46 @@
#pragma once
#include <array>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "leds/preset.hpp"
#include "leds/ws2812.hpp"
class Pattern;
class Presets {
public:
static constexpr size_t kStripCount = 8;
Presets();
~Presets();
bool select(const std::string& name, int step = -1);
bool edit(const std::string& name, const Preset& preset);
bool remove(const std::string& name);
void tick();
void fill(RGB color);
void fill_n(RGB color, int first, int last);
bool set(size_t strip, int reference_index, RGB color);
RGB apply_brightness(RGB color, int local = 255) const;
void show_all();
void show_all_parallel();
void off();
void on(Preset* preset);
uint16_t strip_length(size_t strip) const;
std::array<std::unique_ptr<WS2812B>, kStripCount> strips;
std::array<uint16_t, kStripCount> strip_midpoints{};
std::array<std::vector<uint16_t>, kStripCount> scale_map;
std::unordered_map<std::string, Preset> presets;
std::unique_ptr<Pattern> current;
std::string selected;
int step = 0;
int last_roll_head = 0;
uint8_t b = 255;
private:
std::unique_ptr<Pattern> make_pattern(const std::string& name) const;
};
+84
View File
@@ -0,0 +1,84 @@
#include "leds/ws2812.hpp"
#include "hardware/clocks.h"
#include "hardware/gpio.h"
#include "leds/preset.hpp"
namespace {
constexpr uint T1 = 2, T2 = 5, T3 = 3;
const uint16_t ws2812_instructions[] = {
static_cast<uint16_t>(pio_encode_out(pio_x, 1) | pio_encode_sideset(1, 0) | pio_encode_delay(T3 - 1)),
static_cast<uint16_t>(pio_encode_jmp_not_x(3) | pio_encode_sideset(1, 1) | pio_encode_delay(T1 - 1)),
static_cast<uint16_t>(pio_encode_jmp(0) | pio_encode_sideset(1, 1) | pio_encode_delay(T2 - 1)),
static_cast<uint16_t>(pio_encode_nop() | pio_encode_sideset(1, 0) | pio_encode_delay(T2 - 1)),
};
const pio_program ws2812_program = {
.instructions = ws2812_instructions, .length = 4, .origin = -1,
};
bool pio_program_added[2] = {};
uint pio_offsets[2] = {};
uint program_offset(PIO pio) {
const uint index = pio == pio0 ? 0 : 1;
if (!pio_program_added[index]) {
pio_offsets[index] = pio_add_program(pio, &ws2812_program);
pio_program_added[index] = true;
}
return pio_offsets[index];
}
} // namespace
WS2812B::WS2812B(uint16_t count, uint pin, uint strip_index, float level)
: num_leds(count), brightness(level), buffer(static_cast<size_t>(count) * 3),
pio(strip_index < 4 ? pio0 : pio1), sm(strip_index % 4), pin_(pin) {
const uint offset = program_offset(pio);
pio_gpio_init(pio, pin_);
pio_sm_set_consecutive_pindirs(pio, sm, pin_, 1, true);
pio_sm_config config = pio_get_default_sm_config();
sm_config_set_wrap(&config, offset, offset + 3);
sm_config_set_sideset(&config, 1, false, false);
sm_config_set_sideset_pins(&config, pin_);
sm_config_set_out_shift(&config, false, true, 8);
sm_config_set_fifo_join(&config, PIO_FIFO_JOIN_TX);
sm_config_set_clkdiv(&config, static_cast<float>(clock_get_hz(clk_sys)) / 8000000.0f);
pio_sm_init(pio, sm, offset, &config);
pio_sm_set_enabled(pio, sm, true);
dma_channel = dma_claim_unused_channel(true);
dma_channel_config dma_config = dma_channel_get_default_config(dma_channel);
channel_config_set_transfer_data_size(&dma_config, DMA_SIZE_8);
channel_config_set_dreq(&dma_config, pio_get_dreq(pio, sm, true));
channel_config_set_read_increment(&dma_config, true);
channel_config_set_write_increment(&dma_config, false);
dma_channel_configure(dma_channel, &dma_config, &pio->txf[sm], nullptr,
buffer.size(), false);
}
WS2812B::~WS2812B() {
if (dma_channel >= 0) dma_channel_unclaim(dma_channel);
}
void WS2812B::set(uint16_t index, RGB color) {
if (index >= num_leds) return;
const size_t pos = static_cast<size_t>(index) * 3;
buffer[pos] = static_cast<uint8_t>(color.g * brightness);
buffer[pos + 1] = static_cast<uint8_t>(color.r * brightness);
buffer[pos + 2] = static_cast<uint8_t>(color.b * brightness);
}
void WS2812B::fill(RGB color) {
for (uint16_t i = 0; i < num_leds; ++i) set(i, color);
}
void WS2812B::prepare_show(const uint8_t* source, size_t offset) {
if (!source) source = buffer.data();
dma_channel_set_read_addr(dma_channel, source + offset, false);
dma_channel_set_trans_count(dma_channel, buffer.size(), false);
}
void WS2812B::trigger() { dma_channel_start(dma_channel); }
void WS2812B::show(const uint8_t* source, size_t offset) {
prepare_show(source, offset);
trigger();
}
bool WS2812B::busy() const { return dma_channel_is_busy(dma_channel); }
+34
View File
@@ -0,0 +1,34 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <vector>
#include "hardware/dma.h"
#include "hardware/pio.h"
struct RGB;
class WS2812B {
public:
WS2812B(uint16_t num_leds, uint pin, uint strip_index, float brightness = 1.0f);
~WS2812B();
WS2812B(const WS2812B&) = delete;
WS2812B& operator=(const WS2812B&) = delete;
void fill(RGB color);
void set(uint16_t index, RGB color);
void show(const uint8_t* buffer = nullptr, size_t offset = 0);
void prepare_show(const uint8_t* buffer = nullptr, size_t offset = 0);
void trigger();
bool busy() const;
uint16_t num_leds;
float brightness;
std::vector<uint8_t> buffer;
int dma_channel = -1;
PIO pio = pio0;
uint sm = 0;
private:
uint pin_;
};