Add LED web UI with AP, Wi-Fi client, mDNS and patterns

Brings the led_strip example into the cpp app, targeting the ESP32-C3:
- Access point led-XXXXXX (open) plus a Wi-Fi client configured from the
  web Settings tab, with credentials stored in NVS and reconnect backoff
- mDNS as led.local with an HTTP service on port 80
- HTTP server with an embedded web UI and a JSON API for patterns and Wi-Fi
- Pattern runner for solid, rainbow, chase, breathe, blink, pulse,
  transition and circle on a 119-LED WS2812 strip (GPIO 10)

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Haiku 5.5 committed 2026-11-11 00:02:49 +13:00
1 parent df43820acd
commit 89f996096c
20 files changed
+1539 -18

No files matched your search

+188
View File
@@ -0,0 +1,188 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/
#include <cstring>
#include "esp_log.h"
#include "esp_timer.h"
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/task.h"
#include "patterns.h"
#include "ws2812.h"
#define RMT_LED_STRIP_GPIO_NUM GPIO_NUM_10
#define EXAMPLE_LED_NUMBERS 119
#define FRAME_MS 20
static const char *TAG = "patterns";
static const char *const s_names[PM_COUNT] = {
"solid", "rainbow", "chase", "breathe", "blink", "pulse", "transition", "circle",
};
static WS2812 *s_strip;
static SemaphoreHandle_t s_lock;
static pattern_params_t s_p = { PM_CHASE, { 255, 0, 0 }, { 0, 0, 0 }, 1000, 16 };
const char *patterns_mode_name(pattern_mode_t m)
{
return (m >= 0 && m < PM_COUNT) ? s_names[m] : "";
}
pattern_mode_t patterns_mode_from_name(const char *name)
{
for (int i = 0; i < PM_COUNT; i++) {
if (strcmp(name, s_names[i]) == 0) {
return (pattern_mode_t)i;
}
}
return PM_COUNT;
}
void patterns_set(const pattern_params_t &p)
{
pattern_params_t q = p;
if (q.speed_ms < 10) q.speed_ms = 10;
if (q.speed_ms > 10000) q.speed_ms = 10000;
if (q.mode >= PM_COUNT) q.mode = PM_SOLID;
xSemaphoreTake(s_lock, portMAX_DELAY);
s_p = q;
xSemaphoreGive(s_lock);
}
pattern_params_t patterns_get(void)
{
xSemaphoreTake(s_lock, portMAX_DELAY);
pattern_params_t p = s_p;
xSemaphoreGive(s_lock);
return p;
}
// Linear blend from a (k=0) to b (k=255)
static void lerp(const uint8_t *a, const uint8_t *b, uint32_t k, uint8_t *out)
{
for (int i = 0; i < 3; i++) {
out[i] = (uint8_t)(a[i] + ((int)b[i] - a[i]) * (int)k / 255);
}
}
// Draw one frame of the pattern at time t (ms since the start of the pattern)
static void render(const pattern_params_t &p, uint32_t t, WS2812 &s)
{
const size_t n = s.size();
const uint32_t sp = p.speed_ms;
uint8_t col[3];
switch (p.mode) {
case PM_SOLID:
s.fill(p.c1[0], p.c1[1], p.c1[2]);
break;
case PM_RAINBOW: {
uint32_t off = (t / sp) % 360;
for (size_t i = 0; i < n; i++) {
uint32_t r, g, b;
WS2812::hsv2rgb((i * 360 / n + off) % 360, 100, 100, &r, &g, &b);
s.set(i, r, g, b);
}
break;
}
case PM_CHASE: {
// Every third LED lit, moving one step per speed_ms; hue shifts each full cycle
uint32_t step = t / sp;
uint32_t phase = step % 3;
uint32_t off = ((step / 3) * 60) % 360;
s.fill(0, 0, 0);
for (size_t j = phase; j < n; j += 3) {
uint32_t r, g, b;
WS2812::hsv2rgb((j * 360 / n + off) % 360, 100, 100, &r, &g, &b);
s.set(j, r, g, b);
}
break;
}
case PM_BREATHE: {
uint32_t period = sp * 2 < 500 ? 500 : sp * 2;
uint32_t ph = t % period;
uint32_t k = ph * 2 < period ? (ph * 2 * 255) / period : ((period - ph) * 2 * 255) / period;
lerp(p.c2, p.c1, k, col);
s.fill(col[0], col[1], col[2]);
break;
}
case PM_BLINK:
if ((t / sp) % 2 == 0) {
s.fill(p.c1[0], p.c1[1], p.c1[2]);
} else {
s.fill(p.c2[0], p.c2[1], p.c2[2]);
}
break;
case PM_PULSE: {
// Attack over the first 10% of the period, then a linear decay back to c2
uint32_t period = sp * 2 < 200 ? 200 : sp * 2;
uint32_t frac = ((t % period) * 1000) / period;
uint32_t k = frac < 100 ? frac * 255 / 100 : (1000 - frac) * 255 / 900;
lerp(p.c2, p.c1, k, col);
s.fill(col[0], col[1], col[2]);
break;
}
case PM_TRANSITION: {
// c1 -> c2 over speed_ms, then back again
uint32_t period = 2 * sp;
uint32_t ph = t % period;
uint32_t k = ph < sp ? ph * 255 / sp : (period - ph) * 255 / sp;
lerp(p.c1, p.c2, k, col);
s.fill(col[0], col[1], col[2]);
break;
}
case PM_CIRCLE: {
// A segment a third of the strip long, moving around the ring
size_t head = (t / sp) % n;
size_t len = n / 3 < 1 ? 1 : n / 3;
for (size_t i = 0; i < n; i++) {
size_t dist = (head + n - i) % n;
if (dist < len) {
s.set(i, p.c1[0], p.c1[1], p.c1[2]);
} else {
s.set(i, p.c2[0], p.c2[1], p.c2[2]);
}
}
break;
}
default:
break;
}
}
static void patterns_task(void *arg)
{
(void)arg;
const uint32_t t0 = (uint32_t)(esp_timer_get_time() / 1000);
while (true) {
pattern_params_t p = patterns_get();
uint32_t now = (uint32_t)(esp_timer_get_time() / 1000);
s_strip->setBrightness(p.brightness);
render(p, now - t0, *s_strip);
esp_err_t err = s_strip->write();
if (err != ESP_OK) {
ESP_LOGW(TAG, "LED write failed: %s", esp_err_to_name(err));
}
vTaskDelay(pdMS_TO_TICKS(FRAME_MS));
}
}
void patterns_start(void)
{
s_lock = xSemaphoreCreateMutex();
s_strip = new WS2812(RMT_LED_STRIP_GPIO_NUM, EXAMPLE_LED_NUMBERS);
xTaskCreate(patterns_task, "patterns", 4096, NULL, 5, NULL);
ESP_LOGI(TAG, "Started %d LEDs on GPIO %d", EXAMPLE_LED_NUMBERS, (int)RMT_LED_STRIP_GPIO_NUM);
}