feat: web UI with AP, Wi-Fi client, mDNS, presets, OTA and DDP
- Wi-Fi access point led-XXXXXX plus a client network, configurable mDNS name and static client address - Preset tiles with an editor, drag to reorder, default presets, global brightness, Run and Edit modes, collapsible menu on phones - 35 patterns ported from led-driver, up to 512 LEDs, colour order, max brightness and data pin settings - Over-the-air updates (POST /ota) with rollback, and a CLI in tools/ota.py - DDP receiver on UDP 4048 with tools/ddp_test.py - LED refresh every 10 ms; lwIP sockets raised to 16 so mDNS, HTTP and DDP can run together Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
89f996096c
commit
9a04d28ecf
32 files changed
+6210
-398
No files matched your search
@@ -73,3 +73,8 @@ help:
|
|||||||
@echo clean Remove build outputs
|
@echo clean Remove build outputs
|
||||||
@echo fullclean Delete the build directory entirely
|
@echo fullclean Delete the build directory entirely
|
||||||
@echo help Show this help
|
@echo help Show this help
|
||||||
|
|
||||||
|
# Over-the-air update over Wi-Fi (the access point or the home network): make ota HOST=<ip or name>
|
||||||
|
ota: HOST ?= led.local
|
||||||
|
ota: build
|
||||||
|
python3 tools/ota.py $(HOST)
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
dependencies:
|
||||||
|
espressif/cjson:
|
||||||
|
component_hash: e788323270d90738662d66fffa910bfe1fba019bba087f01557e70c40485b469
|
||||||
|
dependencies:
|
||||||
|
- name: idf
|
||||||
|
require: private
|
||||||
|
version: '>=5.0'
|
||||||
|
source:
|
||||||
|
registry_url: https://components.espressif.com/
|
||||||
|
type: service
|
||||||
|
version: 1.7.19~2
|
||||||
|
espressif/mdns:
|
||||||
|
component_hash: 2fa43caa78cc170842255bc7bf71f306bc815cae12e9496bfb12372423019ac7
|
||||||
|
dependencies:
|
||||||
|
- name: idf
|
||||||
|
require: private
|
||||||
|
version: '>=5.0'
|
||||||
|
source:
|
||||||
|
registry_url: https://components.espressif.com/
|
||||||
|
type: service
|
||||||
|
version: 1.15.1
|
||||||
|
idf:
|
||||||
|
source:
|
||||||
|
type: idf
|
||||||
|
version: 6.1.0
|
||||||
|
direct_dependencies:
|
||||||
|
- espressif/cjson
|
||||||
|
- espressif/mdns
|
||||||
|
manifest_hash: d0a7e36d6cc4abacaa550a978cbc8f50812589b67060a807571ef39c1e829dfd
|
||||||
|
target: esp32c3
|
||||||
|
version: 3.0.0
|
||||||
@@ -97,6 +97,13 @@ Presets define LED patterns with their configuration. Each preset has a name and
|
|||||||
- **`n3`**: Tail movement rate (LEDs per second)
|
- **`n3`**: Tail movement rate (LEDs per second)
|
||||||
- **`n4`**: Minimum length
|
- **`n4`**: Minimum length
|
||||||
|
|
||||||
|
#### Gradient Wipe (`gradient_wipe`)
|
||||||
|
One-shot: lights LEDs from `n1` to `n2` (backwards if `n1 > n2`) with a gradient from the first to the second colour, then stops.
|
||||||
|
- **`n1`**: First LED
|
||||||
|
- **`n2`**: Last LED
|
||||||
|
- **`delay`**: Total time in milliseconds to sweep from `n1` to `n2`
|
||||||
|
- **`background`**: Colour for the rest of the strip
|
||||||
|
|
||||||
## Select Messages
|
## Select Messages
|
||||||
|
|
||||||
Select messages control which preset is active on which device. The format uses a list to support step synchronization.
|
Select messages control which preset is active on which device. The format uses a list to support step synchronization.
|
||||||
|
|||||||
+3
-2
@@ -1,5 +1,6 @@
|
|||||||
idf_component_register(SRCS "main.cpp" "ws2812.cpp" "net.cpp" "settings.cpp" "web.cpp" "patterns.cpp"
|
idf_component_register(SRCS "main.cpp" "ws2812.cpp" "net.cpp" "settings.cpp" "web.cpp" "patterns.cpp" "presets.cpp"
|
||||||
|
"effects_util.cpp" "effects_a.cpp" "effects_b.cpp" "effects_rig.cpp" "ota.c" "ddp.cpp"
|
||||||
PRIV_REQUIRES esp_driver_rmt esp_driver_gpio esp_wifi esp_netif esp_event
|
PRIV_REQUIRES esp_driver_rmt esp_driver_gpio esp_wifi esp_netif esp_event
|
||||||
esp_timer nvs_flash esp_http_server cjson mdns esp_hw_support
|
esp_timer nvs_flash esp_http_server cjson mdns esp_hw_support app_update esp_app_format lwip
|
||||||
INCLUDE_DIRS "."
|
INCLUDE_DIRS "."
|
||||||
EMBED_TXTFILES "../web/index.html" "../web/app.js" "../web/style.css")
|
EMBED_TXTFILES "../web/index.html" "../web/app.js" "../web/style.css")
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "ddp.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/task.h"
|
||||||
|
#include "lwip/sockets.h"
|
||||||
|
#include "patterns.h"
|
||||||
|
|
||||||
|
static const char *TAG = "ddp";
|
||||||
|
|
||||||
|
// Header: flags, sequence, data type, destination, data offset (4), data length (2); with the timecode
|
||||||
|
// flag, 4 more bytes follow. Version 1 is flags bits 6-7 = 01.
|
||||||
|
#define DDP_FLAG_VER1 0x40
|
||||||
|
#define DDP_VER_MASK 0xC0
|
||||||
|
#define DDP_FLAG_TIMECODE 0x10
|
||||||
|
#define DDP_HEADER 10
|
||||||
|
|
||||||
|
static uint32_t be32(const uint8_t *p)
|
||||||
|
{
|
||||||
|
return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | p[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint16_t be16(const uint8_t *p)
|
||||||
|
{
|
||||||
|
return (uint16_t)((p[0] << 8) | p[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ddp_task(void *arg)
|
||||||
|
{
|
||||||
|
(void)arg;
|
||||||
|
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
||||||
|
if (sock < 0) {
|
||||||
|
ESP_LOGE(TAG, "socket failed");
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
struct sockaddr_in addr = {};
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons(DDP_PORT);
|
||||||
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
|
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||||
|
ESP_LOGE(TAG, "bind to port %d failed", DDP_PORT);
|
||||||
|
close(sock);
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
ESP_LOGI(TAG, "DDP listening on UDP port %d", DDP_PORT);
|
||||||
|
|
||||||
|
uint8_t buf[1500];
|
||||||
|
while (true) {
|
||||||
|
int n = recv(sock, buf, sizeof(buf), 0);
|
||||||
|
if (n < DDP_HEADER) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
uint8_t flags = buf[0];
|
||||||
|
if ((flags & DDP_VER_MASK) != DDP_FLAG_VER1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
size_t hdr = DDP_HEADER + ((flags & DDP_FLAG_TIMECODE) ? 4 : 0);
|
||||||
|
if ((size_t)n < hdr) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
uint8_t dtype = buf[2];
|
||||||
|
if (dtype != 0x00 && dtype != 0x01) {
|
||||||
|
continue; // only 8-bit RGB
|
||||||
|
}
|
||||||
|
uint32_t offset = be32(buf + 4);
|
||||||
|
size_t len = be16(buf + 8);
|
||||||
|
if (len > (size_t)n - hdr) {
|
||||||
|
len = (size_t)n - hdr;
|
||||||
|
}
|
||||||
|
patterns_ddp_write(offset, buf + hdr, len, (flags & 0x01) != 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t ddp_start(void)
|
||||||
|
{
|
||||||
|
BaseType_t ok = xTaskCreate(ddp_task, "ddp", 4096, NULL, 4, NULL);
|
||||||
|
return ok == pdPASS ? ESP_OK : ESP_ERR_NO_MEM;
|
||||||
|
}
|
||||||
+14
@@ -0,0 +1,14 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* DDP (Distributed Display Protocol) receiver: RGB pixels over UDP, port 4048. Packets fill the strip
|
||||||
|
* while they keep arriving (see patterns.cpp).
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "esp_err.h"
|
||||||
|
|
||||||
|
#define DDP_PORT 4048
|
||||||
|
|
||||||
|
/** @brief Start listening for DDP packets. */
|
||||||
|
esp_err_t ddp_start(void);
|
||||||
+149
@@ -0,0 +1,149 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Pattern engine shared by the effect files. Ports the MicroPython led-driver patterns: each pattern
|
||||||
|
* gets the same preset fields (colours, delay d, brightness b, auto a, reverse, n1..n6) and renders into
|
||||||
|
* a shadow frame, which patterns.cpp shows (with crossfade) on the strip.
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "patterns.h"
|
||||||
|
|
||||||
|
#define FX_LEDS_MAX 512 // largest strip the engine supports (per-LED buffers are sized at run time)
|
||||||
|
|
||||||
|
struct rgb {
|
||||||
|
int r, g, b;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Per-frame view handed to a pattern
|
||||||
|
struct fx_t {
|
||||||
|
int n; // LEDs in the strip
|
||||||
|
const preset_t *pr; // the running preset (copy)
|
||||||
|
uint32_t now; // ms now
|
||||||
|
uint32_t t; // ms since the pattern started
|
||||||
|
rgb *px; // shadow frame, physical order; persists between frames (n entries)
|
||||||
|
};
|
||||||
|
|
||||||
|
// Per-pattern state. Patterns run one at a time, so one instance is reset on each select.
|
||||||
|
struct flake_t { int pos, ci, wj; };
|
||||||
|
struct star_t { int h, ci; };
|
||||||
|
struct spark_t { int p, lv, ci; };
|
||||||
|
struct bug_t { int pos, ph; };
|
||||||
|
struct drop_t { int pos, age; };
|
||||||
|
|
||||||
|
struct st_t {
|
||||||
|
bool done; // one-shot finished: hold the frame
|
||||||
|
bool started; // one-time setup done
|
||||||
|
uint32_t last, aux; // timers
|
||||||
|
uint32_t t0; // start time
|
||||||
|
int ci; // colour index
|
||||||
|
int phase, dir, pause; // animation state
|
||||||
|
int head, tail; // moving head / tail
|
||||||
|
int p1, p2; // comet heads
|
||||||
|
int cnt; // counters
|
||||||
|
int sc; // chase step count
|
||||||
|
int stage; // state machine stage
|
||||||
|
int flash_idx;
|
||||||
|
int rise; // flame
|
||||||
|
int cj[8]; // flame cluster jitter
|
||||||
|
uint32_t breath_origin, last_cluster, next_spark, spark_start;
|
||||||
|
int spark_dur;
|
||||||
|
bool spark_active;
|
||||||
|
int breath_ms, lo, hi, gap_lo, gap_hi;
|
||||||
|
bool sparks_on;
|
||||||
|
int centers[32];
|
||||||
|
int ncent;
|
||||||
|
int *glow; // per-LED buffers, n entries each (see frame_alloc)
|
||||||
|
flake_t flakes[64];
|
||||||
|
int nflakes;
|
||||||
|
star_t stars[8];
|
||||||
|
int nstars;
|
||||||
|
spark_t sparks[32];
|
||||||
|
int nsparks;
|
||||||
|
bug_t bugs[16];
|
||||||
|
int nbugs;
|
||||||
|
drop_t drops[32];
|
||||||
|
int ndrops;
|
||||||
|
uint8_t *on, *next_on;
|
||||||
|
int *col_i, *next_ci;
|
||||||
|
int *light, *dark;
|
||||||
|
int nlight, ndark;
|
||||||
|
float *m; // rig: position of each LED along the pattern
|
||||||
|
int *shade; // rig: shade of each LED
|
||||||
|
};
|
||||||
|
|
||||||
|
extern st_t g_st;
|
||||||
|
|
||||||
|
// Allocate the per-LED buffers for n LEDs (frame and pattern state). False if out of memory, in which
|
||||||
|
// case the old buffers are kept. Call with the pattern lock held.
|
||||||
|
bool frame_alloc(int n);
|
||||||
|
|
||||||
|
// Reset the pattern state for a new pattern, keeping the buffers and clearing them.
|
||||||
|
void st_reset(void);
|
||||||
|
|
||||||
|
// ---- helpers ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
int clampi(int v, int lo, int hi);
|
||||||
|
int fdiv(int a, int b); // floor division, as Python's //
|
||||||
|
int pmod(int a, int m); // non-negative modulo, as Python's %
|
||||||
|
int nz(int v, int def); // v when v > 0, else def
|
||||||
|
rgb mk(int r, int g, int b); // clamped
|
||||||
|
rgb ab(rgb c, int level); // c scaled by level/255
|
||||||
|
rgb mix(rgb a, rgb b, int k); // a -> b, k 0..255
|
||||||
|
int sgn(const fx_t &f, int v); // v, negated when reversed
|
||||||
|
int led_at(const fx_t &f, int i); // logical index -> physical (reverse)
|
||||||
|
void put(fx_t &f, int phys, rgb c);
|
||||||
|
void putl(fx_t &f, int logical, rgb c);
|
||||||
|
void fill(fx_t &f, rgb c);
|
||||||
|
bool due(uint32_t now, uint32_t &last, uint32_t d); // true (and last += d) once d ms have passed
|
||||||
|
uint32_t dur(const fx_t &f, int v); // ms, or quarter beats with beat_time
|
||||||
|
int get_colours(const fx_t &f, const rgb *def, int ndef, rgb *out); // preset colours or the default
|
||||||
|
int rnd(int lo, int hi); // random integer, inclusive
|
||||||
|
rgb grad_at(const rgb *cols, int ncols, float t); // colour at t (0..1) along the list
|
||||||
|
rgb wheel(int pos); // hue wheel, 0..255 -> colour
|
||||||
|
|
||||||
|
// Colour table for the rig patterns: shades of the colour list, at LEVELS brightness steps
|
||||||
|
#define RIG_SHADES 16
|
||||||
|
#define RIG_LEVELS 16
|
||||||
|
rgb rig_entry(const fx_t &f, const rgb *cols, int ncols, int shades, int shade, int level);
|
||||||
|
|
||||||
|
// Pattern entry points (return true once a one-shot has finished)
|
||||||
|
bool fx_off(fx_t &f, st_t &s);
|
||||||
|
bool fx_on(fx_t &f, st_t &s);
|
||||||
|
bool fx_blink(fx_t &f, st_t &s);
|
||||||
|
bool fx_aurora(fx_t &f, st_t &s);
|
||||||
|
bool fx_bar_graph(fx_t &f, st_t &s);
|
||||||
|
bool fx_blizzard(fx_t &f, st_t &s);
|
||||||
|
bool fx_candle_glow(fx_t &f, st_t &s);
|
||||||
|
bool fx_chase(fx_t &f, st_t &s);
|
||||||
|
bool fx_circle(fx_t &f, st_t &s);
|
||||||
|
bool fx_clock_sweep(fx_t &f, st_t &s);
|
||||||
|
bool fx_colour_cycle(fx_t &f, st_t &s);
|
||||||
|
bool fx_flicker(fx_t &f, st_t &s);
|
||||||
|
bool fx_gradient_wipe(fx_t &f, st_t &s);
|
||||||
|
bool fx_hit(fx_t &f, st_t &s);
|
||||||
|
bool fx_icicles(fx_t &f, st_t &s);
|
||||||
|
bool fx_keyframes(fx_t &f, st_t &s);
|
||||||
|
bool fx_orbit(fx_t &f, st_t &s);
|
||||||
|
bool fx_pulse(fx_t &f, st_t &s);
|
||||||
|
bool fx_transition(fx_t &f, st_t &s);
|
||||||
|
bool fx_strobe_burst(fx_t &f, st_t &s);
|
||||||
|
bool fx_plasma(fx_t &f, st_t &s);
|
||||||
|
|
||||||
|
bool fx_meteor(fx_t &f, st_t &s);
|
||||||
|
bool fx_particles(fx_t &f, st_t &s);
|
||||||
|
bool fx_sparkle(fx_t &f, st_t &s);
|
||||||
|
bool fx_rain_drops(fx_t &f, st_t &s);
|
||||||
|
bool fx_rime(fx_t &f, st_t &s);
|
||||||
|
bool fx_twinkle(fx_t &f, st_t &s);
|
||||||
|
bool fx_radiate(fx_t &f, st_t &s);
|
||||||
|
bool fx_palette_morph(fx_t &f, st_t &s);
|
||||||
|
bool fx_flame(fx_t &f, st_t &s);
|
||||||
|
|
||||||
|
bool fx_field_wave(fx_t &f, st_t &s);
|
||||||
|
bool fx_grow(fx_t &f, st_t &s);
|
||||||
|
bool fx_ripple(fx_t &f, st_t &s);
|
||||||
|
bool fx_spin(fx_t &f, st_t &s);
|
||||||
|
bool fx_sweep(fx_t &f, st_t &s);
|
||||||
@@ -0,0 +1,897 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Basic and colour patterns, ported from the MicroPython led-driver (patterns folder).
|
||||||
|
*/
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
#include "effects.h"
|
||||||
|
|
||||||
|
static const rgb WHITE = { 255, 255, 255 };
|
||||||
|
static const rgb BLACK = { 0, 0, 0 };
|
||||||
|
|
||||||
|
static bool first_call(st_t &s)
|
||||||
|
{
|
||||||
|
if (s.started) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
s.started = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- off, on, blink ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_off(fx_t &f, st_t &)
|
||||||
|
{
|
||||||
|
fill(f, BLACK);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fx_on(fx_t &f, st_t &)
|
||||||
|
{
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, &WHITE, 1, c);
|
||||||
|
fill(f, ab(c[0], f.pr->brightness));
|
||||||
|
(void)nc;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fx_blink(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, &WHITE, 1, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
s.stage = 1; // on
|
||||||
|
}
|
||||||
|
uint32_t d = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
if (due(f.now, s.last, d)) {
|
||||||
|
if (s.stage) {
|
||||||
|
fill(f, ab(c[s.ci % nc], b));
|
||||||
|
s.ci++;
|
||||||
|
} else {
|
||||||
|
fill(f, bgc);
|
||||||
|
}
|
||||||
|
s.stage = !s.stage;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- aurora ------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_aurora(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb bands_def[3] = { { 40, 200, 140 }, { 80, 120, 255 }, { 160, 80, 220 } };
|
||||||
|
const rgb north_def[3] = { { 20, 55, 120 }, { 60, 140, 220 }, { 180, 220, 255 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS + 1];
|
||||||
|
bool northern = f.pr->n[6] == 1;
|
||||||
|
int nc = get_colours(f, northern ? north_def : bands_def, 3, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int nled = f.n;
|
||||||
|
uint32_t d = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
|
||||||
|
if (!northern) {
|
||||||
|
int bands = nz(f.pr->n[1], 3);
|
||||||
|
int shimmer = clampi(nz(f.pr->n[2], 40), 0, 255);
|
||||||
|
if (!due(f.now, s.last, d)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < nled; i++) {
|
||||||
|
int idx = (((i * bands) / (nled > 0 ? nled : 1)) + s.phase / 32) % nc;
|
||||||
|
rgb col = ab(c[idx], b);
|
||||||
|
int w = 255 - abs(128 - ((i * 8 + s.phase) & 255)) * 2;
|
||||||
|
w = clampi(w + shimmer, 0, 255);
|
||||||
|
rgb px = { col.r * w / 255, col.g * w / 255, col.b * w / 255 };
|
||||||
|
putl(f, i, px);
|
||||||
|
}
|
||||||
|
s.phase = pmod(s.phase + sgn(f, 1), 256);
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Northern wave: a sine of brightness along the strip, blended through the colours
|
||||||
|
int period = nz(f.pr->n[1], 20);
|
||||||
|
if (period < 4) period = 4;
|
||||||
|
int contrast = clampi(nz(f.pr->n[2], 200), 1, 255);
|
||||||
|
int drift = nz(f.pr->n[3], 2);
|
||||||
|
if (nc < 2) {
|
||||||
|
c[nc] = mk(120, 180, 255);
|
||||||
|
nc++;
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, d)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
const float twopi = 6.2831853f;
|
||||||
|
for (int i = 0; i < nled; i++) {
|
||||||
|
float t = (i * twopi / period) + (s.phase * twopi / 256.0f);
|
||||||
|
float w = (sinf(t) + 1.0f) * 0.5f;
|
||||||
|
int u = (int)(w * (nc - 1) * 256.0f);
|
||||||
|
int fi = u >> 8;
|
||||||
|
int frac = u & 255;
|
||||||
|
if (fi >= nc - 1) {
|
||||||
|
fi = nc - 2;
|
||||||
|
frac = 255;
|
||||||
|
}
|
||||||
|
rgb peak = ab(mix(c[fi], c[fi + 1], frac), b);
|
||||||
|
int mixf = (int)(w * contrast * 2);
|
||||||
|
mixf = mixf >> 1;
|
||||||
|
if (mixf > 255) mixf = 255;
|
||||||
|
putl(f, i, mix(bgc, peak, mixf));
|
||||||
|
}
|
||||||
|
s.phase = pmod(s.phase + sgn(f, drift), 256);
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- bar graph ---------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_bar_graph(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[2] = { { 0, 255, 0 }, { 255, 80, 0 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 2, c);
|
||||||
|
(void)nc;
|
||||||
|
uint32_t d = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
if (!due(f.now, s.last, d)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int level = clampi(f.pr->n[1], 0, 100);
|
||||||
|
int target = (f.n * level) / 100;
|
||||||
|
rgb lit = ab(c[0], b);
|
||||||
|
rgb unlit = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
for (int i = 0; i < f.n; i++) {
|
||||||
|
put(f, i, i < target ? lit : unlit);
|
||||||
|
}
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- blizzard ----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_blizzard(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[3] = { { 255, 255, 255 }, { 200, 230, 255 }, { 180, 210, 255 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 3, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int nled = f.n;
|
||||||
|
int density = nz(f.pr->n[1], 90);
|
||||||
|
int speed = nz(f.pr->n[2], 2);
|
||||||
|
int wraw = f.pr->n[3];
|
||||||
|
int wind = wraw <= 0 ? 0 : clampi(fdiv(wraw - 128, 20), -4, 4);
|
||||||
|
uint32_t d = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
if (!due(f.now, s.last, d)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
for (int i = 0; i < nled; i++) {
|
||||||
|
put(f, i, bgc);
|
||||||
|
}
|
||||||
|
if (rnd(0, 255) < density && s.nflakes < 64) {
|
||||||
|
flake_t fl = { nled - 1, rnd(0, nc - 1), wind == 0 ? 0 : rnd(-1, 1) };
|
||||||
|
s.flakes[s.nflakes++] = fl;
|
||||||
|
}
|
||||||
|
int keep = 0;
|
||||||
|
for (int k = 0; k < s.nflakes; k++) {
|
||||||
|
flake_t fl = s.flakes[k];
|
||||||
|
int p = fl.pos;
|
||||||
|
int lateral = wind + fl.wj;
|
||||||
|
p -= sgn(f, speed);
|
||||||
|
p += sgn(f, lateral);
|
||||||
|
if (p < -2 || p >= nled + 2) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int pi = clampi(p, 0, nled - 1);
|
||||||
|
putl(f, pi, ab(c[fl.ci], b));
|
||||||
|
s.flakes[keep] = { p, fl.ci, fl.wj };
|
||||||
|
keep++;
|
||||||
|
}
|
||||||
|
s.nflakes = keep;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- candle glow -------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_candle_glow(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[3] = { { 255, 140, 40 }, { 255, 200, 120 }, { 255, 90, 20 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 3, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int nled = f.n;
|
||||||
|
if (first_call(s)) {
|
||||||
|
int nc_candles = clampi(nz(f.pr->n[1], 4), 1, nled);
|
||||||
|
if (nc_candles > 32) nc_candles = 32;
|
||||||
|
s.ncent = nc_candles;
|
||||||
|
for (int k = 0; k < nc_candles; k++) {
|
||||||
|
s.centers[k] = rnd(0, nled > 0 ? nled - 1 : 0);
|
||||||
|
}
|
||||||
|
s.last = f.now;
|
||||||
|
}
|
||||||
|
int width = nz(f.pr->n[2], 3);
|
||||||
|
int flicker = clampi(nz(f.pr->n[3], 90), 1, 255);
|
||||||
|
uint32_t d = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
if (!due(f.now, s.last, d)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
for (int i = 0; i < nled; i++) {
|
||||||
|
put(f, i, bgc);
|
||||||
|
}
|
||||||
|
int base_lo = 180 - flicker / 2;
|
||||||
|
if (base_lo < 40) base_lo = 40;
|
||||||
|
for (int ci = 0; ci < s.ncent; ci++) {
|
||||||
|
int centre = s.centers[ci];
|
||||||
|
rgb warmth = c[ci % nc];
|
||||||
|
int pulse = clampi(base_lo + rnd(0, flicker), 0, 255);
|
||||||
|
for (int off = -width; off <= width; off++) {
|
||||||
|
int idx = centre + off;
|
||||||
|
if (idx < 0 || idx >= nled) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int dist = abs(off);
|
||||||
|
int fall = ((width - dist + 1) * 256) / (width + 1);
|
||||||
|
int fac = (fall * pulse) / 256;
|
||||||
|
rgb px = { warmth.r * fac / 255, warmth.g * fac / 255, warmth.b * fac / 255 };
|
||||||
|
rgb lit = ab(px, b);
|
||||||
|
rgb o = f.px[idx];
|
||||||
|
f.px[idx] = { o.r > lit.r ? o.r : lit.r, o.g > lit.g ? o.g : lit.g, o.b > lit.b ? o.b : lit.b };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- chase -------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static void chase_draw(fx_t &f, int pos, int n1, int n2, int n3, int n4, rgb c0, rgb c1)
|
||||||
|
{
|
||||||
|
int seg = n1 + n2;
|
||||||
|
(void)n3;
|
||||||
|
(void)n4;
|
||||||
|
for (int i = 0; i < f.n; i++) {
|
||||||
|
int rel = pmod(i - pos, seg);
|
||||||
|
putl(f, i, rel < n1 ? c0 : c1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fx_chase(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int nled = f.n;
|
||||||
|
if (f.pr->n[6] == 1) {
|
||||||
|
// Marquee: dashes that scroll
|
||||||
|
const rgb def[1] = { WHITE };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 1, c);
|
||||||
|
(void)nc;
|
||||||
|
int on_len = nz(f.pr->n[1], 3);
|
||||||
|
int off_len = nz(f.pr->n[2], 2);
|
||||||
|
int step = abs(sgn(f, nz(f.pr->n[3], 1)));
|
||||||
|
if (step < 1) step = 1;
|
||||||
|
int period = on_len + off_len;
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
}
|
||||||
|
uint32_t d = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
if (!due(f.now, s.last, d)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb col = ab(c[0], b);
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
for (int i = 0; i < nled; i++) {
|
||||||
|
int m = pmod(i + s.phase, period);
|
||||||
|
putl(f, i, m < on_len ? col : bgc);
|
||||||
|
}
|
||||||
|
s.phase = pmod(s.phase + sgn(f, step), period);
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Chase: two colours moving along the strip, n1/n2 segment lengths, n3/n4 steps
|
||||||
|
const rgb def[2] = { WHITE, WHITE };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 2, c);
|
||||||
|
rgb color0 = ab(c[0], b);
|
||||||
|
rgb color1 = ab(nc > 1 ? c[1] : c[0], b);
|
||||||
|
int n1 = nz(f.pr->n[1], 1);
|
||||||
|
int n2 = nz(f.pr->n[2], 1);
|
||||||
|
int n3 = sgn(f, f.pr->n[3]);
|
||||||
|
int n4 = sgn(f, f.pr->n[4]);
|
||||||
|
int seg = n1 + n2;
|
||||||
|
int max_pos = nled + seg;
|
||||||
|
auto position = [&](int sc) {
|
||||||
|
int pos;
|
||||||
|
if (sc % 2 == 0) {
|
||||||
|
pos = (sc / 2) * (n3 + n4) + n3;
|
||||||
|
} else {
|
||||||
|
pos = ((sc + 1) / 2) * (n3 + n4);
|
||||||
|
}
|
||||||
|
return pmod(pos, max_pos);
|
||||||
|
};
|
||||||
|
if (!f.pr->autorun) {
|
||||||
|
chase_draw(f, position(s.sc), n1, n2, n3, n4, color0, color1);
|
||||||
|
s.sc = (s.sc + 1) % 2;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
uint32_t dur_ms = f.pr->delay < 10 ? 10 : f.pr->delay;
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now - dur_ms; // the first step is immediate
|
||||||
|
}
|
||||||
|
if ((int32_t)(f.now - s.last) >= (int32_t)dur_ms) {
|
||||||
|
chase_draw(f, position(s.sc), n1, n2, n3, n4, color0, color1);
|
||||||
|
s.sc = (s.sc + 1) % 2;
|
||||||
|
s.last += dur_ms;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- circle ------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_circle(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
rgb color0, color1;
|
||||||
|
if (f.pr->ncol == 0) {
|
||||||
|
color0 = color1 = ab(WHITE, b);
|
||||||
|
} else {
|
||||||
|
color0 = ab(mk(f.pr->col[0][0], f.pr->col[0][1], f.pr->col[0][2]), b);
|
||||||
|
color1 = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
}
|
||||||
|
int nled = f.n;
|
||||||
|
int head_rate = nz(f.pr->n[1], 1);
|
||||||
|
int max_length = nz(f.pr->n[2], 1);
|
||||||
|
int tail_rate = nz(f.pr->n[3], 1);
|
||||||
|
int min_length = f.pr->n[4] > 0 ? f.pr->n[4] : 0;
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.aux = f.now; // head timer
|
||||||
|
s.last = f.now; // tail timer
|
||||||
|
s.phase = 0; // 0 growing, 1 shrinking, 2 off
|
||||||
|
}
|
||||||
|
// Draw the ring: background, then the segment from the tail to the head
|
||||||
|
int seg = pmod(s.head - s.tail, nled);
|
||||||
|
if (seg == 0 && s.head != s.tail) {
|
||||||
|
seg = nled;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < nled; i++) {
|
||||||
|
put(f, i, color1);
|
||||||
|
}
|
||||||
|
for (int i = 0; i <= seg; i++) {
|
||||||
|
put(f, (s.tail + i) % nled, color0);
|
||||||
|
}
|
||||||
|
// Head moves at n1 LEDs per second
|
||||||
|
uint32_t head_delay = 1000u / (uint32_t)head_rate;
|
||||||
|
if ((int32_t)(f.now - s.aux) >= (int32_t)head_delay) {
|
||||||
|
s.aux += head_delay;
|
||||||
|
s.head = (s.head + 1) % nled;
|
||||||
|
}
|
||||||
|
// Tail behaviour by phase
|
||||||
|
if (s.phase == 0) {
|
||||||
|
if (seg >= max_length) {
|
||||||
|
s.phase = 1;
|
||||||
|
}
|
||||||
|
} else if (s.phase == 1) {
|
||||||
|
uint32_t tail_delay = 1000u / (uint32_t)tail_rate;
|
||||||
|
if ((int32_t)(f.now - s.last) >= (int32_t)tail_delay) {
|
||||||
|
s.last += tail_delay;
|
||||||
|
s.tail = (s.tail + 1) % nled;
|
||||||
|
int cur = pmod(s.head - s.tail, nled);
|
||||||
|
if (cur == 0 && s.head != s.tail) {
|
||||||
|
cur = nled;
|
||||||
|
}
|
||||||
|
if (min_length == 0 && cur <= 1) {
|
||||||
|
s.phase = 2;
|
||||||
|
} else if (min_length > 0 && cur <= min_length) {
|
||||||
|
s.phase = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
s.tail = s.head;
|
||||||
|
s.phase = 0;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- clock sweep -------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_clock_sweep(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[2] = { WHITE, { 60, 60, 60 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
get_colours(f, def, 2, c);
|
||||||
|
int width = nz(f.pr->n[1], 1);
|
||||||
|
int marker = f.pr->n[2] > 0 ? f.pr->n[2] : 0;
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
uint32_t d = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, d)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
rgb fg = ab(c[0], b);
|
||||||
|
rgb dim = { bgc.r * 2 / 3, bgc.g * 2 / 3, bgc.b * 2 / 3 };
|
||||||
|
for (int i = 0; i < f.n; i++) {
|
||||||
|
put(f, i, bgc);
|
||||||
|
if (marker > 0 && i % marker == 0) {
|
||||||
|
put(f, i, dim);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int w = 0; w < width; w++) {
|
||||||
|
put(f, (s.phase + w) % f.n, fg);
|
||||||
|
}
|
||||||
|
s.phase = (s.phase + 1) % (f.n > 0 ? f.n : 1);
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- colour cycle ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static void gradient_render(fx_t &f, const rgb *cols, int nc, int phase)
|
||||||
|
{
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int nled = f.n;
|
||||||
|
if (nc == 1) {
|
||||||
|
fill(f, ab(cols[0], b));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int full_span = nc * 256;
|
||||||
|
int phase_shift = (phase * full_span) / 256;
|
||||||
|
for (int i = 0; i < nled; i++) {
|
||||||
|
int pos = (((i * full_span) / nled) + phase_shift) % full_span;
|
||||||
|
int idx = pos / 256;
|
||||||
|
int frac = pos & 255;
|
||||||
|
rgb c1 = cols[idx];
|
||||||
|
rgb c2 = cols[(idx + 1) % nc];
|
||||||
|
rgb blended = mix(c1, c2, frac);
|
||||||
|
putl(f, i, ab(blended, b));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rainbow_render(fx_t &f, int phase)
|
||||||
|
{
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int nled = f.n;
|
||||||
|
for (int i = 0; i < nled; i++) {
|
||||||
|
int rc = (i * 256) / (nled > 0 ? nled : 1) + phase;
|
||||||
|
putl(f, i, ab(wheel(rc & 255), b));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fx_colour_cycle(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
int step_amount = nz(f.pr->n[1], 1);
|
||||||
|
if (f.pr->n[6] == 1) {
|
||||||
|
// Fixed spectrum wheel, scrolling
|
||||||
|
if (!f.pr->autorun) {
|
||||||
|
rainbow_render(f, s.phase);
|
||||||
|
s.phase = pmod(s.phase + sgn(f, step_amount), 256);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
uint32_t d = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
if (!due(f.now, s.last, d)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rainbow_render(f, s.phase);
|
||||||
|
s.phase = pmod(s.phase + sgn(f, step_amount), 256);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const rgb def[2] = { { 255, 0, 0 }, { 0, 0, 255 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 2, c);
|
||||||
|
if (!f.pr->autorun) {
|
||||||
|
gradient_render(f, c, nc, s.phase);
|
||||||
|
s.phase = pmod(s.phase + sgn(f, step_amount), 256);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
uint32_t d = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
if (!due(f.now, s.last, d)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
gradient_render(f, c, nc, s.phase);
|
||||||
|
s.phase = pmod(s.phase + sgn(f, step_amount), 256);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- flicker -----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_flicker(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, &WHITE, 1, c);
|
||||||
|
int lo = clampi(f.pr->n[1], 0, 255);
|
||||||
|
int hi = clampi(f.pr->brightness, 0, 255);
|
||||||
|
if (lo > hi) {
|
||||||
|
int t = lo;
|
||||||
|
lo = hi;
|
||||||
|
hi = t;
|
||||||
|
}
|
||||||
|
if (!f.pr->autorun) {
|
||||||
|
int level = rnd(lo, hi);
|
||||||
|
fill(f, ab(c[s.ci % nc], level));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
uint32_t d = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, d)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int level = rnd(lo, hi);
|
||||||
|
fill(f, ab(c[s.ci % nc], level));
|
||||||
|
s.ci++;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- gradient wipe (always one-shot) -----------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_gradient_wipe(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, &WHITE, 1, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
rgb c1 = c[0];
|
||||||
|
rgb c2 = nc > 1 ? c[1] : c[0];
|
||||||
|
int last = f.n > 0 ? f.n - 1 : 0;
|
||||||
|
int n1 = clampi(f.pr->n[1], 0, last);
|
||||||
|
int n2 = clampi(f.pr->n[2], 0, last);
|
||||||
|
int count = abs(n2 - n1) + 1;
|
||||||
|
int step = n2 >= n1 ? 1 : -1;
|
||||||
|
uint32_t duration = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
if (first_call(s)) {
|
||||||
|
fill(f, ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b));
|
||||||
|
}
|
||||||
|
if (s.cnt >= count) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
int lit = (int)((f.t * (uint32_t)count) / duration) + 1;
|
||||||
|
if (lit > count) {
|
||||||
|
lit = count;
|
||||||
|
}
|
||||||
|
for (int k = s.cnt; k < lit; k++) {
|
||||||
|
rgb col;
|
||||||
|
if (count == 1) {
|
||||||
|
col = c1;
|
||||||
|
} else {
|
||||||
|
float t = (float)k / (count - 1);
|
||||||
|
col = { (int)(c1.r + (c2.r - c1.r) * t), (int)(c1.g + (c2.g - c1.g) * t),
|
||||||
|
(int)(c1.b + (c2.b - c1.b) * t) };
|
||||||
|
}
|
||||||
|
putl(f, n1 + k * step, ab(col, b));
|
||||||
|
}
|
||||||
|
if (lit > s.cnt) {
|
||||||
|
s.cnt = lit;
|
||||||
|
}
|
||||||
|
return s.cnt >= count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- hit ---------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_hit(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, &WHITE, 1, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.aux = f.now;
|
||||||
|
}
|
||||||
|
for (int guard = 0; guard < 2; guard++) {
|
||||||
|
int attack = (int)dur(f, f.pr->n[1]);
|
||||||
|
int hold = (int)dur(f, f.pr->n[2]);
|
||||||
|
int decay = (int)dur(f, f.pr->n[3]);
|
||||||
|
int gap = f.pr->autorun ? (int)dur(f, (int)f.pr->delay) : 0;
|
||||||
|
rgb base = c[s.ci % nc];
|
||||||
|
int t = (int)(f.now - s.aux);
|
||||||
|
if (t < attack) {
|
||||||
|
rgb sc = { base.r * t / attack, base.g * t / attack, base.b * t / attack };
|
||||||
|
fill(f, ab(sc, b));
|
||||||
|
} else if (t < attack + hold) {
|
||||||
|
fill(f, ab(base, b));
|
||||||
|
} else if (t < attack + hold + decay) {
|
||||||
|
int left = decay - (t - attack - hold);
|
||||||
|
rgb sc = { base.r * left / decay, base.g * left / decay, base.b * left / decay };
|
||||||
|
rgb v = ab(sc, b);
|
||||||
|
rgb m = { v.r > bgc.r ? v.r : bgc.r, v.g > bgc.g ? v.g : bgc.g, v.b > bgc.b ? v.b : bgc.b };
|
||||||
|
fill(f, m);
|
||||||
|
} else if (!f.pr->autorun) {
|
||||||
|
fill(f, f.pr->n[4] == 1 ? ab(base, b) : bgc);
|
||||||
|
return true;
|
||||||
|
} else if (t < attack + hold + decay + gap) {
|
||||||
|
fill(f, bgc);
|
||||||
|
} else {
|
||||||
|
s.ci = (s.ci + 1) % nc;
|
||||||
|
s.aux = f.now;
|
||||||
|
continue; // the next flash starts on this frame
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- icicles -----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_icicles(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[3] = { { 240, 248, 255 }, { 160, 210, 255 }, { 255, 255, 255 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 3, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int nled = f.n;
|
||||||
|
int spacing = nz(f.pr->n[1], 12);
|
||||||
|
int cap = nled / 4;
|
||||||
|
if (cap > 14) cap = 14;
|
||||||
|
if (cap < 3) cap = 3;
|
||||||
|
int max_len = nz(f.pr->n[2], cap);
|
||||||
|
if (max_len < 2) max_len = 2;
|
||||||
|
if (max_len > (nled > 2 ? nled : 2)) max_len = nled > 2 ? nled : 2;
|
||||||
|
int span = max_len * 2;
|
||||||
|
int phase_step = nz(f.pr->n[3], 1);
|
||||||
|
uint32_t d = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, d)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
for (int i = 0; i < nled; i++) {
|
||||||
|
put(f, i, bgc);
|
||||||
|
}
|
||||||
|
int aidx = 0;
|
||||||
|
for (int anchor = 0; anchor < nled; anchor += spacing) {
|
||||||
|
int tri_i = pmod(s.phase + aidx * 5, span);
|
||||||
|
int ic_len = tri_i <= max_len ? tri_i : span - tri_i;
|
||||||
|
rgb tip = ab(c[aidx % nc], b);
|
||||||
|
for (int k = 0; k < ic_len; k++) {
|
||||||
|
int idx = anchor + k;
|
||||||
|
if (idx >= nled) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int br = ((k + 1) * 255) / (ic_len > 1 ? ic_len : 1);
|
||||||
|
rgb px = { (tip.r * br + bgc.r * (255 - br)) / 255, (tip.g * br + bgc.g * (255 - br)) / 255,
|
||||||
|
(tip.b * br + bgc.b * (255 - br)) / 255 };
|
||||||
|
putl(f, idx, px);
|
||||||
|
}
|
||||||
|
aidx++;
|
||||||
|
}
|
||||||
|
s.phase = pmod(s.phase + sgn(f, phase_step), span);
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- keyframes ---------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static float ease_t(float t, int mode)
|
||||||
|
{
|
||||||
|
if (t <= 0) return 0;
|
||||||
|
if (t >= 1) return 1;
|
||||||
|
switch (mode) {
|
||||||
|
case 1: return t * t * (3 - 2 * t); // in-out
|
||||||
|
case 2: return 0; // step
|
||||||
|
case 3: return t * t; // in
|
||||||
|
case 4: return 1 - (1 - t) * (1 - t); // out
|
||||||
|
default: return t; // linear
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fx_keyframes(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[1] = { WHITE };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 1, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
(void)s;
|
||||||
|
int total = (int)dur(f, (int)f.pr->delay);
|
||||||
|
if (total < 1) total = 1;
|
||||||
|
bool loop = f.pr->autorun;
|
||||||
|
int segs = loop ? nc : (nc - 1 > 1 ? nc - 1 : 1);
|
||||||
|
int t = (int)f.t;
|
||||||
|
if (!loop && t >= total) {
|
||||||
|
if (f.pr->n[2] == 1) {
|
||||||
|
fill(f, ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b));
|
||||||
|
} else if (f.pr->n[2] == 2) {
|
||||||
|
fill(f, BLACK);
|
||||||
|
} else {
|
||||||
|
fill(f, ab(c[nc - 1], b));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
float x = ((float)(t % total) / total) * segs;
|
||||||
|
int i = (int)x;
|
||||||
|
rgb a = c[i % nc];
|
||||||
|
rgb bb = c[(i + 1) % nc];
|
||||||
|
float k = ease_t(x - i, f.pr->n[1]);
|
||||||
|
rgb m = { (int)(a.r + (bb.r - a.r) * k), (int)(a.g + (bb.g - a.g) * k), (int)(a.b + (bb.b - a.b) * k) };
|
||||||
|
fill(f, ab(m, b));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- orbit -------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_orbit(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[3] = { WHITE, { 0, 180, 255 }, { 255, 0, 120 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 3, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int nled = f.n;
|
||||||
|
int orbits = nz(f.pr->n[1], 3);
|
||||||
|
int speed = nz(f.pr->n[2], 1);
|
||||||
|
uint32_t d = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, d)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
for (int i = 0; i < nled; i++) {
|
||||||
|
put(f, i, bgc);
|
||||||
|
}
|
||||||
|
for (int k = 0; k < orbits; k++) {
|
||||||
|
int idx = ((s.phase * (k + 1)) / 8 + (k * nled) / orbits) % (nled > 0 ? nled : 1);
|
||||||
|
put(f, idx, ab(c[k % nc], b));
|
||||||
|
}
|
||||||
|
s.phase = (s.phase + speed) & 255;
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- pulse -------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_pulse(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, &WHITE, 1, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
bool manual = !f.pr->autorun;
|
||||||
|
if (first_call(s)) {
|
||||||
|
fill(f, bgc);
|
||||||
|
s.aux = f.now; // cycle start
|
||||||
|
}
|
||||||
|
int attack = nz(f.pr->n[1], 0);
|
||||||
|
int hold = nz(f.pr->n[2], 0);
|
||||||
|
int decay = nz(f.pr->n[3], 0);
|
||||||
|
int delay = manual ? 0 : (int)f.pr->delay;
|
||||||
|
int total = attack + hold + decay + delay;
|
||||||
|
if (total <= 0) total = 1;
|
||||||
|
int elapsed = (int)(f.now - s.aux);
|
||||||
|
rgb base = c[s.ci % nc];
|
||||||
|
if (elapsed < attack && attack > 0) {
|
||||||
|
rgb col = { (int)(base.r * ((float)elapsed / attack)), (int)(base.g * ((float)elapsed / attack)),
|
||||||
|
(int)(base.b * ((float)elapsed / attack)) };
|
||||||
|
fill(f, ab(col, b));
|
||||||
|
} else if (elapsed < attack + hold) {
|
||||||
|
fill(f, ab(base, b));
|
||||||
|
} else if (elapsed < attack + hold + decay && decay > 0) {
|
||||||
|
float fac = 1.0f - (float)(elapsed - attack - hold) / decay;
|
||||||
|
if (fac < 0) fac = 0;
|
||||||
|
rgb col = { (int)(base.r * fac), (int)(base.g * fac), (int)(base.b * fac) };
|
||||||
|
fill(f, ab(col, b));
|
||||||
|
} else if (elapsed < total) {
|
||||||
|
fill(f, bgc);
|
||||||
|
} else {
|
||||||
|
int nclr = nc > 1 ? nc : 1;
|
||||||
|
s.ci = (s.ci + 1) % nclr;
|
||||||
|
if (manual) {
|
||||||
|
fill(f, bgc);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
s.aux = f.now;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- transition --------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_transition(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, &WHITE, 1, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
if (nc == 1) {
|
||||||
|
fill(f, ab(c[0], b));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.aux = f.now;
|
||||||
|
}
|
||||||
|
int duration = f.pr->delay < 10 ? 10 : (int)f.pr->delay;
|
||||||
|
int elapsed = (int)(f.now - s.aux);
|
||||||
|
if (elapsed >= duration) {
|
||||||
|
if (!f.pr->autorun) {
|
||||||
|
fill(f, ab(c[(s.ci + 1) % nc], b));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
s.ci = (s.ci + 1) % nc;
|
||||||
|
s.aux = f.now;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb c1 = c[s.ci % nc];
|
||||||
|
rgb c2 = c[(s.ci + 1) % nc];
|
||||||
|
float factor = (float)elapsed / duration;
|
||||||
|
rgb m = { (int)(c1.r + (c2.r - c1.r) * factor), (int)(c1.g + (c2.g - c1.g) * factor),
|
||||||
|
(int)(c1.b + (c2.b - c1.b) * factor) };
|
||||||
|
fill(f, ab(m, b));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- strobe burst ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_strobe_burst(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, &WHITE, 1, c);
|
||||||
|
(void)nc;
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.stage = 0; // 0 flash on, 1 flash off, 2 cooldown
|
||||||
|
s.flash_idx = 0;
|
||||||
|
s.aux = f.now; // state start
|
||||||
|
}
|
||||||
|
int count = nz(f.pr->n[1], 3);
|
||||||
|
int gap = nz(f.pr->n[2], 60);
|
||||||
|
int cooldown = nz(f.pr->n[3], 400);
|
||||||
|
int on_ms = (int)f.pr->delay / 2;
|
||||||
|
if (on_ms < 1) on_ms = 1;
|
||||||
|
rgb col = ab(c[0], b);
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
int since = (int)(f.now - s.aux);
|
||||||
|
if (s.stage == 0) {
|
||||||
|
fill(f, col);
|
||||||
|
if (since >= on_ms) {
|
||||||
|
s.stage = 1;
|
||||||
|
s.aux += on_ms;
|
||||||
|
}
|
||||||
|
} else if (s.stage == 1) {
|
||||||
|
fill(f, bgc);
|
||||||
|
if (since >= gap) {
|
||||||
|
s.flash_idx++;
|
||||||
|
if (s.flash_idx >= count) {
|
||||||
|
if (!f.pr->autorun) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
s.stage = 2;
|
||||||
|
s.flash_idx = 0;
|
||||||
|
s.aux += gap;
|
||||||
|
} else {
|
||||||
|
s.stage = 0;
|
||||||
|
s.aux += gap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fill(f, bgc);
|
||||||
|
if (since >= cooldown) {
|
||||||
|
s.stage = 0;
|
||||||
|
s.aux += cooldown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- plasma ------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_plasma(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int nled = f.n;
|
||||||
|
int scale = nz(f.pr->n[1], 6);
|
||||||
|
int speed = nz(f.pr->n[2], 2);
|
||||||
|
int contrast = nz(f.pr->n[3], 2);
|
||||||
|
uint32_t d = f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, d)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int t = s.phase;
|
||||||
|
for (int i = 0; i < nled; i++) {
|
||||||
|
int v = (i * scale + t) & 255;
|
||||||
|
int v2 = ((i * scale) / contrast - t * 2) & 255;
|
||||||
|
put(f, i, ab(wheel((v + v2) & 255), b));
|
||||||
|
}
|
||||||
|
s.phase = pmod(s.phase + speed, 256);
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
@@ -0,0 +1,894 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Moving lights, particles and sparkles, ported from the MicroPython led-driver (patterns folder).
|
||||||
|
*/
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
#include "effects.h"
|
||||||
|
|
||||||
|
static const rgb WHITE = { 255, 255, 255 };
|
||||||
|
|
||||||
|
static bool first_call(st_t &s)
|
||||||
|
{
|
||||||
|
if (s.started) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
s.started = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t period_of(const fx_t &f)
|
||||||
|
{
|
||||||
|
return f.pr->delay < 1 ? 1 : f.pr->delay;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fade every LED's stored colour towards black (the shadow frame persists between frames)
|
||||||
|
static void fade_frame(fx_t &f, int amount)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < f.n; i++) {
|
||||||
|
rgb c = f.px[i];
|
||||||
|
f.px[i] = { c.r * amount / 255, c.g * amount / 255, c.b * amount / 255 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- meteor / comet / scanner ------------------------------------------------------------------
|
||||||
|
|
||||||
|
static bool meteor_run(fx_t &f, st_t &s, const rgb *cols, int nc)
|
||||||
|
{
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int tail = nz(f.pr->n[1], 8);
|
||||||
|
int speed = nz(f.pr->n[2], 1);
|
||||||
|
int fade_amount = clampi(nz(f.pr->n[3], 192), 1, 255);
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
s.dir = f.pr->reverse ? -1 : 1;
|
||||||
|
s.head = f.pr->reverse ? (f.n > 0 ? f.n - 1 : 0) : 0;
|
||||||
|
s.ci = 0;
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, period_of(f))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
fade_frame(f, fade_amount);
|
||||||
|
rgb lit = ab(cols[s.ci % nc], b);
|
||||||
|
if (s.head >= 0 && s.head < f.n) {
|
||||||
|
putl(f, s.head, lit);
|
||||||
|
}
|
||||||
|
s.head += sgn(f, s.dir * speed);
|
||||||
|
if (s.head >= f.n + tail) {
|
||||||
|
s.head = f.n - 1;
|
||||||
|
s.dir = -1;
|
||||||
|
s.ci++;
|
||||||
|
} else if (s.head < -tail) {
|
||||||
|
s.head = 0;
|
||||||
|
s.dir = 1;
|
||||||
|
s.ci++;
|
||||||
|
}
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool comet_run(fx_t &f, st_t &s, const rgb *cols, int nc)
|
||||||
|
{
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int nled = f.n;
|
||||||
|
int tail = nz(f.pr->n[1], 6);
|
||||||
|
int speed = nz(f.pr->n[2], 1);
|
||||||
|
int gap = f.pr->n[3] > 0 ? f.pr->n[3] : 0;
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
if (f.pr->reverse) {
|
||||||
|
s.p1 = nled - 1;
|
||||||
|
s.p2 = gap;
|
||||||
|
} else {
|
||||||
|
s.p1 = 0;
|
||||||
|
s.p2 = nled - 1 - gap;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, period_of(f))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
for (int i = 0; i < nled; i++) {
|
||||||
|
put(f, i, bgc);
|
||||||
|
}
|
||||||
|
rgb c1 = ab(cols[0], b);
|
||||||
|
rgb c2 = ab(nc > 1 ? cols[1] : cols[0], b);
|
||||||
|
int denom = tail > 1 ? tail : 1;
|
||||||
|
for (int t = 0; t < tail; t++) {
|
||||||
|
int i1 = s.p1 - t;
|
||||||
|
if (i1 >= 0 && i1 < nled) {
|
||||||
|
int k = (255 * (tail - t)) / denom;
|
||||||
|
putl(f, i1, { c1.r * k / 255, c1.g * k / 255, c1.b * k / 255 });
|
||||||
|
}
|
||||||
|
int i2 = s.p2 + t;
|
||||||
|
if (i2 >= 0 && i2 < nled) {
|
||||||
|
int k = (255 * (tail - t)) / denom;
|
||||||
|
putl(f, i2, { c2.r * k / 255, c2.g * k / 255, c2.b * k / 255 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.p1 += sgn(f, speed);
|
||||||
|
s.p2 -= sgn(f, speed);
|
||||||
|
if (s.p1 - tail > nled && s.p2 + tail < 0) {
|
||||||
|
s.p1 = 0;
|
||||||
|
s.p2 = nled - 1 - gap;
|
||||||
|
}
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool scanner_run(fx_t &f, st_t &s, const rgb *cols, int nc)
|
||||||
|
{
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int nled = f.n;
|
||||||
|
int width = nz(f.pr->n[1], 4);
|
||||||
|
int end_pause = f.pr->n[2] > 0 ? f.pr->n[2] : 0;
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
s.ci = 0;
|
||||||
|
s.pause = 0;
|
||||||
|
s.dir = f.pr->reverse ? -1 : 1;
|
||||||
|
s.head = f.pr->reverse ? (nled > 0 ? nled - 1 : 0) : 0; // centre
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, period_of(f))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb base = ab(cols[s.ci % nc], b);
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
int denom = width > 1 ? width : 1;
|
||||||
|
for (int i = 0; i < nled; i++) {
|
||||||
|
int dist = abs(i - s.head);
|
||||||
|
if (dist > width) {
|
||||||
|
putl(f, i, bgc);
|
||||||
|
} else {
|
||||||
|
int sc = ((width - dist) * 255) / denom;
|
||||||
|
putl(f, i, { base.r * sc / 255, base.g * sc / 255, base.b * sc / 255 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (s.pause > 0) {
|
||||||
|
s.pause--;
|
||||||
|
} else {
|
||||||
|
s.head += sgn(f, s.dir);
|
||||||
|
if (s.head >= nled - 1) {
|
||||||
|
s.head = nled - 1;
|
||||||
|
s.dir = -1;
|
||||||
|
s.pause = end_pause;
|
||||||
|
s.ci++;
|
||||||
|
} else if (s.head <= 0) {
|
||||||
|
s.head = 0;
|
||||||
|
s.dir = 1;
|
||||||
|
s.pause = end_pause;
|
||||||
|
s.ci++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fx_meteor(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[1] = { WHITE };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 1, c);
|
||||||
|
switch (f.pr->n[6]) {
|
||||||
|
case 1:
|
||||||
|
return comet_run(f, s, c, nc);
|
||||||
|
case 2:
|
||||||
|
return scanner_run(f, s, c, nc);
|
||||||
|
default:
|
||||||
|
return meteor_run(f, s, c, nc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- particles: snowfall and starfall ----------------------------------------------------------
|
||||||
|
|
||||||
|
static bool snow_run(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[2] = { WHITE, { 180, 220, 255 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 2, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int density = nz(f.pr->n[1], 20);
|
||||||
|
int speed = abs(sgn(f, nz(f.pr->n[2], 1)));
|
||||||
|
if (speed < 1) speed = 1;
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, period_of(f))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (rnd(0, 255) < density && s.nflakes < 64) {
|
||||||
|
s.flakes[s.nflakes++] = { f.n - 1, rnd(0, nc - 1), 0 };
|
||||||
|
}
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
for (int i = 0; i < f.n; i++) {
|
||||||
|
put(f, i, bgc);
|
||||||
|
}
|
||||||
|
int keep = 0;
|
||||||
|
for (int k = 0; k < s.nflakes; k++) {
|
||||||
|
flake_t fl = s.flakes[k];
|
||||||
|
if (fl.pos >= 0 && fl.pos < f.n) {
|
||||||
|
putl(f, fl.pos, ab(c[fl.ci], b));
|
||||||
|
}
|
||||||
|
fl.pos -= sgn(f, speed);
|
||||||
|
if (fl.pos >= -1) {
|
||||||
|
s.flakes[keep++] = fl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.nflakes = keep;
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool starfall_run(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[3] = { WHITE, { 200, 230, 255 }, { 255, 248, 220 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 3, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int rate = clampi(nz(f.pr->n[1], 14), 1, 255);
|
||||||
|
int speed = abs(sgn(f, nz(f.pr->n[2], 2)));
|
||||||
|
if (speed < 1) speed = 1;
|
||||||
|
int tail = nz(f.pr->n[3], 10);
|
||||||
|
if (tail < 2) tail = 2;
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, period_of(f))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
for (int i = 0; i < f.n; i++) {
|
||||||
|
put(f, i, bgc);
|
||||||
|
}
|
||||||
|
if (s.nstars < 4 && rnd(0, 255) < rate) {
|
||||||
|
int extra = f.n / 2;
|
||||||
|
if (extra > 8) extra = 8;
|
||||||
|
int top = f.n - 1 + rnd(0, extra);
|
||||||
|
s.stars[s.nstars++] = { top, rnd(0, nc - 1) };
|
||||||
|
}
|
||||||
|
int keep = 0;
|
||||||
|
for (int k = 0; k < s.nstars; k++) {
|
||||||
|
star_t st = s.stars[k];
|
||||||
|
for (int t = 0; t < tail; t++) {
|
||||||
|
int idx = st.h + t;
|
||||||
|
if (idx < 0 || idx >= f.n) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int fade = 255 - (t * 255) / (tail - 1 > 1 ? tail - 1 : 1);
|
||||||
|
rgb base = c[st.ci];
|
||||||
|
rgb lit = ab({ base.r * fade / 255, base.g * fade / 255, base.b * fade / 255 }, b);
|
||||||
|
int pix = led_at(f, idx);
|
||||||
|
rgb o = f.px[pix];
|
||||||
|
f.px[pix] = { o.r > lit.r ? o.r : lit.r, o.g > lit.g ? o.g : lit.g, o.b > lit.b ? o.b : lit.b };
|
||||||
|
}
|
||||||
|
st.h -= sgn(f, speed);
|
||||||
|
if (st.h >= -tail) {
|
||||||
|
s.stars[keep++] = st;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.nstars = keep;
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fx_particles(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
return f.pr->n[6] == 1 ? starfall_run(f, s) : snow_run(f, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- sparkle: trail, ice burst, fireflies ------------------------------------------------------
|
||||||
|
|
||||||
|
static bool sparkle_trail(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[1] = { { 120, 120, 255 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 1, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int density = nz(f.pr->n[1], 24);
|
||||||
|
int decay = clampi(nz(f.pr->n[2], 210), 1, 255);
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, period_of(f))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
fade_frame(f, decay);
|
||||||
|
int sparks = f.n * density / 255;
|
||||||
|
if (sparks < 1) sparks = 1;
|
||||||
|
for (int k = 0; k < sparks; k++) {
|
||||||
|
int idx = rnd(0, f.n > 0 ? f.n - 1 : 0);
|
||||||
|
put(f, idx, ab(c[rnd(0, nc - 1)], b));
|
||||||
|
}
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool sparkle_ice(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[3] = { { 240, 248, 255 }, { 200, 235, 255 }, { 255, 255, 255 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 3, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int rate = clampi(nz(f.pr->n[1], 55), 1, 255);
|
||||||
|
int decay = clampi(nz(f.pr->n[2], 140), 1, 255);
|
||||||
|
int halo = clampi(f.pr->n[3], 0, 3);
|
||||||
|
const int cap = 28;
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, period_of(f))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
for (int i = 0; i < f.n; i++) {
|
||||||
|
put(f, i, bgc);
|
||||||
|
}
|
||||||
|
int keep = 0;
|
||||||
|
for (int k = 0; k < s.nsparks; k++) {
|
||||||
|
spark_t sp = s.sparks[k];
|
||||||
|
sp.lv -= decay;
|
||||||
|
if (sp.lv > 0) {
|
||||||
|
s.sparks[keep++] = sp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.nsparks = keep;
|
||||||
|
if (s.nsparks < cap && rnd(0, 255) < rate) {
|
||||||
|
s.sparks[s.nsparks++] = { rnd(0, f.n > 0 ? f.n - 1 : 0), 255, rnd(0, nc - 1) };
|
||||||
|
}
|
||||||
|
for (int k = 0; k < s.nsparks; k++) {
|
||||||
|
spark_t sp = s.sparks[k];
|
||||||
|
rgb base = c[sp.ci];
|
||||||
|
for (int off = -halo; off <= halo; off++) {
|
||||||
|
int idx = sp.p + off;
|
||||||
|
if (idx < 0 || idx >= f.n) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int dist = abs(off);
|
||||||
|
int fac = dist == 0 ? sp.lv : (sp.lv * (halo - dist + 1)) / (halo + 1);
|
||||||
|
rgb lit = ab({ base.r * fac / 255, base.g * fac / 255, base.b * fac / 255 }, b);
|
||||||
|
rgb o = f.px[idx];
|
||||||
|
f.px[idx] = { o.r + lit.r > 255 ? 255 : o.r + lit.r, o.g + lit.g > 255 ? 255 : o.g + lit.g,
|
||||||
|
o.b + lit.b > 255 ? 255 : o.b + lit.b };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool sparkle_fireflies(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[2] = { { 255, 210, 80 }, { 120, 255, 120 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 2, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int count = nz(f.pr->n[1], 6);
|
||||||
|
if (count > 16) count = 16;
|
||||||
|
int speed = nz(f.pr->n[2], 8);
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
s.nbugs = count;
|
||||||
|
for (int k = 0; k < count; k++) {
|
||||||
|
s.bugs[k] = { rnd(0, f.n > 0 ? f.n - 1 : 0), rnd(0, 255) };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, period_of(f))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
for (int i = 0; i < f.n; i++) {
|
||||||
|
put(f, i, bgc);
|
||||||
|
}
|
||||||
|
for (int k = 0; k < s.nbugs; k++) {
|
||||||
|
bug_t &bg = s.bugs[k];
|
||||||
|
int tri = 255 - abs(128 - bg.ph) * 2;
|
||||||
|
rgb col = ab(c[bg.pos % nc], b);
|
||||||
|
put(f, bg.pos, { col.r * tri / 255, col.g * tri / 255, col.b * tri / 255 });
|
||||||
|
bg.ph = (bg.ph + speed) & 255;
|
||||||
|
if (rnd(0, 31) == 0) {
|
||||||
|
bg.pos = rnd(0, f.n > 0 ? f.n - 1 : 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fx_sparkle(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
switch (f.pr->n[6]) {
|
||||||
|
case 1:
|
||||||
|
return sparkle_ice(f, s);
|
||||||
|
case 2:
|
||||||
|
return sparkle_fireflies(f, s);
|
||||||
|
default:
|
||||||
|
return sparkle_trail(f, s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- rain drops --------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_rain_drops(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[1] = { { 120, 180, 255 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 1, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int rate = nz(f.pr->n[1], 32);
|
||||||
|
int width = nz(f.pr->n[2], 3);
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, period_of(f))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
for (int i = 0; i < f.n; i++) {
|
||||||
|
put(f, i, bgc);
|
||||||
|
}
|
||||||
|
if (rnd(0, 255) < rate && s.ndrops < 32) {
|
||||||
|
s.drops[s.ndrops++] = { rnd(0, f.n > 0 ? f.n - 1 : 0), 0 };
|
||||||
|
}
|
||||||
|
int keep = 0;
|
||||||
|
int denom = width + 1;
|
||||||
|
for (int k = 0; k < s.ndrops; k++) {
|
||||||
|
drop_t dr = s.drops[k];
|
||||||
|
for (int off = -width; off <= width; off++) {
|
||||||
|
int idx = dr.pos + off;
|
||||||
|
if (idx < 0 || idx >= f.n) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int lvl = 255 - (abs(off) * 255 / (denom > 1 ? denom : 1) + dr.age * 40);
|
||||||
|
if (lvl < 0) lvl = 0;
|
||||||
|
rgb base = ab(c[dr.age % nc], b);
|
||||||
|
put(f, idx, { base.r * lvl / 255, base.g * lvl / 255, base.b * lvl / 255 });
|
||||||
|
}
|
||||||
|
dr.age++;
|
||||||
|
if (dr.age < 8) {
|
||||||
|
s.drops[keep++] = dr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.ndrops = keep;
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- rime: slow frost build-up -----------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_rime(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[3] = { { 220, 235, 255 }, WHITE, { 185, 220, 255 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 3, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int num = f.n;
|
||||||
|
if (num <= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int chill = clampi(nz(f.pr->n[1], 36), 1, 255);
|
||||||
|
int melt = clampi(nz(f.pr->n[2], 12), 1, 255);
|
||||||
|
int spark_cap = clampi(nz(f.pr->n[3], 3), 1, num);
|
||||||
|
if (first_call(s)) {
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
s.glow[i] = 0;
|
||||||
|
}
|
||||||
|
s.last = f.now;
|
||||||
|
}
|
||||||
|
if (!due(f.now, s.last, period_of(f))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
s.glow[i] = s.glow[i] > melt ? s.glow[i] - melt : 0;
|
||||||
|
}
|
||||||
|
int spawned = 0;
|
||||||
|
int tries = spark_cap + num / 8;
|
||||||
|
for (int t = 0; t < tries; t++) {
|
||||||
|
if (spawned >= spark_cap) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (rnd(0, 255) >= chill) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int j = rnd(0, num - 1);
|
||||||
|
int g = s.glow[j] + rnd(80, 200);
|
||||||
|
s.glow[j] = g > 255 ? 255 : g;
|
||||||
|
spawned++;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
int g = clampi(s.glow[i], 0, 255);
|
||||||
|
rgb hi = ab(c[i % nc], b);
|
||||||
|
rgb px = { (hi.r * g + bgc.r * (255 - g)) / 255, (hi.g * g + bgc.g * (255 - g)) / 255,
|
||||||
|
(hi.b * g + bgc.b * (255 - g)) / 255 };
|
||||||
|
put(f, i, px);
|
||||||
|
}
|
||||||
|
return !f.pr->autorun;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- twinkle -----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static const rgb kCool[6] = {
|
||||||
|
{ 120, 200, 255 }, { 80, 140, 255 }, { 180, 120, 255 },
|
||||||
|
{ 100, 220, 240 }, { 160, 200, 255 }, { 90, 180, 220 },
|
||||||
|
};
|
||||||
|
|
||||||
|
static int cluster_lo(const fx_t &f, int *hi_out)
|
||||||
|
{
|
||||||
|
int lo = f.pr->n[3];
|
||||||
|
int hi = f.pr->n[4];
|
||||||
|
int num = f.n;
|
||||||
|
if (lo <= 0 && hi <= 0) {
|
||||||
|
lo = 1;
|
||||||
|
hi = num < 4 ? num : 4;
|
||||||
|
} else {
|
||||||
|
if (lo <= 0) lo = 1;
|
||||||
|
if (hi <= 0) hi = lo;
|
||||||
|
if (hi < lo) {
|
||||||
|
int t = lo;
|
||||||
|
lo = hi;
|
||||||
|
hi = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lo = clampi(lo, 1, num > 0 ? num : 1);
|
||||||
|
hi = clampi(hi, lo, num > 0 ? num : 1);
|
||||||
|
*hi_out = hi;
|
||||||
|
return lo;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cluster_len(const fx_t &f)
|
||||||
|
{
|
||||||
|
int hi;
|
||||||
|
int lo = cluster_lo(f, &hi);
|
||||||
|
return lo == hi ? lo : rnd(lo, hi);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cluster_base(const fx_t &f, int start, int k)
|
||||||
|
{
|
||||||
|
k = clampi(k, 0, f.n);
|
||||||
|
if (k <= 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int b = start < f.n - k ? start : f.n - k;
|
||||||
|
return b < 0 ? 0 : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fx_twinkle(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
rgb palette[PATTERN_MAX_COLOURS];
|
||||||
|
int np = get_colours(f, kCool, 6, palette);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int num = f.n;
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
if (num <= 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (first_call(s)) {
|
||||||
|
int dens0 = clampi(f.pr->n[2] > 0 ? f.pr->n[2] : 128, 0, 255);
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
s.on[i] = rnd(0, 255) < dens0;
|
||||||
|
s.col_i[i] = rnd(0, np - 1);
|
||||||
|
}
|
||||||
|
s.last = f.now;
|
||||||
|
}
|
||||||
|
if (!f.pr->autorun) {
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
put(f, i, s.on[i] ? ab(palette[s.col_i[i] % np], b) : bgc);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
uint32_t d = period_of(f);
|
||||||
|
if (!due(f.now, s.last, d)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int rate = clampi(f.pr->n[1] > 0 ? f.pr->n[1] : 48, 1, 255);
|
||||||
|
int dens = clampi(f.pr->n[2] > 0 ? f.pr->n[2] : 128, 0, 255);
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
s.next_on[i] = s.on[i];
|
||||||
|
s.next_ci[i] = s.col_i[i];
|
||||||
|
}
|
||||||
|
s.nlight = 0;
|
||||||
|
s.ndark = 0;
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
if (rnd(0, 255) < rate) {
|
||||||
|
int r = rnd(0, 255);
|
||||||
|
if (!s.on[i]) {
|
||||||
|
if (r < dens) s.light[s.nlight++] = i;
|
||||||
|
} else {
|
||||||
|
if (r < 255 - dens) s.dark[s.ndark++] = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int k = 0; k < s.ndark; k++) {
|
||||||
|
int start = s.dark[k];
|
||||||
|
int len = cluster_len(f);
|
||||||
|
int base = cluster_base(f, start, len);
|
||||||
|
for (int dj = 0; dj < len; dj++) {
|
||||||
|
s.next_on[base + dj] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int k = 0; k < s.nlight; k++) {
|
||||||
|
int start = s.light[k];
|
||||||
|
int len = cluster_len(f);
|
||||||
|
int base = cluster_base(f, start, len);
|
||||||
|
for (int dj = 0; dj < len; dj++) {
|
||||||
|
s.next_on[base + dj] = 1;
|
||||||
|
s.next_ci[base + dj] = rnd(0, np - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
put(f, i, s.next_on[i] ? ab(palette[s.next_ci[i] % np], b) : bgc);
|
||||||
|
s.on[i] = s.next_on[i];
|
||||||
|
s.col_i[i] = s.next_ci[i];
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- radiate: pulses from nodes ----------------------------------------------------------------
|
||||||
|
|
||||||
|
static int radiate_front(int age, int outward, int ret, int maxd)
|
||||||
|
{
|
||||||
|
if (age <= outward) {
|
||||||
|
return (age * maxd + outward - 1) / outward;
|
||||||
|
}
|
||||||
|
if (age <= outward + ret) {
|
||||||
|
int rem = ret - (age - outward);
|
||||||
|
return (rem * maxd + ret - 1) / ret;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fx_radiate(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[1] = { WHITE };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 1, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
rgb off_c = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
int spacing = nz(f.pr->n[1], 1);
|
||||||
|
int outward = nz(f.pr->n[2], 1);
|
||||||
|
int ret = nz(f.pr->n[3], 1);
|
||||||
|
int maxd = spacing / 2;
|
||||||
|
int life = outward + ret;
|
||||||
|
int num = f.n;
|
||||||
|
|
||||||
|
if (!f.pr->autorun) {
|
||||||
|
// One pulse, then hold
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.aux = f.now;
|
||||||
|
}
|
||||||
|
int age = (int)(f.now - s.aux);
|
||||||
|
if (age < 1) age = 1;
|
||||||
|
int front = radiate_front(age, outward, ret, maxd);
|
||||||
|
if (front < 0) front = 0;
|
||||||
|
rgb lit = ab(c[s.ci % nc], b);
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
int offset = (i + spacing / 2) % spacing;
|
||||||
|
int dist = offset < spacing - offset ? offset : spacing - offset;
|
||||||
|
put(f, i, dist <= front ? lit : off_c);
|
||||||
|
}
|
||||||
|
if (age >= life) {
|
||||||
|
s.ci++;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t delay = period_of(f);
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.last = f.now;
|
||||||
|
s.ncent = 1;
|
||||||
|
s.centers[0] = (int)f.now;
|
||||||
|
}
|
||||||
|
if ((int32_t)(f.now - s.last) >= (int32_t)delay) {
|
||||||
|
s.ncent = 1;
|
||||||
|
s.centers[0] = (int)f.now;
|
||||||
|
s.last += delay;
|
||||||
|
s.ci++;
|
||||||
|
}
|
||||||
|
int kept = 0;
|
||||||
|
for (int k = 0; k < s.ncent; k++) {
|
||||||
|
if ((int)(f.now - s.centers[k]) < life) {
|
||||||
|
s.centers[kept++] = s.centers[k];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.ncent = kept;
|
||||||
|
rgb lit = ab(c[s.ci % nc], b);
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
int offset = (i + spacing / 2) % spacing;
|
||||||
|
int dist = offset < spacing - offset ? offset : spacing - offset;
|
||||||
|
bool on = false;
|
||||||
|
for (int k = 0; k < s.ncent && !on; k++) {
|
||||||
|
int age = (int)(f.now - s.centers[k]);
|
||||||
|
if (age <= 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
int front = radiate_front(age, outward, ret, maxd);
|
||||||
|
if (front < 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (dist <= front) {
|
||||||
|
on = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
put(f, i, on ? lit : off_c);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- palette morph -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_palette_morph(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[3] = { { 255, 0, 0 }, { 0, 255, 0 }, { 0, 0, 255 } };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 3, c);
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
int num = f.n;
|
||||||
|
if (nc < 2) {
|
||||||
|
fill(f, ab(c[0], b));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int morph = nz(f.pr->n[1], 1200);
|
||||||
|
if (morph < 50) morph = 50;
|
||||||
|
int warp = nz(f.pr->n[2], 3);
|
||||||
|
int turb = nz(f.pr->n[3], 24);
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.aux = f.now; // morph start
|
||||||
|
s.last = f.now; // frame timer
|
||||||
|
s.ci = 0; // base colour index
|
||||||
|
s.phase = 0;
|
||||||
|
}
|
||||||
|
uint32_t delay = period_of(f);
|
||||||
|
if (!due(f.now, s.last, delay)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int age = (int)(f.now - s.aux);
|
||||||
|
int t = age < morph ? (age * 255) / morph : 255;
|
||||||
|
rgb a = c[s.ci % nc];
|
||||||
|
rgb bb = c[(s.ci + 1) % nc];
|
||||||
|
rgb anchor = mix(a, bb, t);
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
int pos = (i * 256) / (num > 0 ? num : 1);
|
||||||
|
int wobble = (((pos * turb) / 32) + s.phase + (t / 2)) & 255;
|
||||||
|
int breath = 255 - abs(128 - wobble) * 2;
|
||||||
|
int local = (pos + breath / 3 + t / 4) % 256;
|
||||||
|
int idx = (s.ci + ((local * nc) / 256)) % nc;
|
||||||
|
int frac = (local * nc) & 255;
|
||||||
|
rgb grad = mix(c[idx], c[(idx + 1) % nc], frac);
|
||||||
|
rgb out = mix(grad, anchor, 80);
|
||||||
|
put(f, i, ab(out, b));
|
||||||
|
}
|
||||||
|
if (age >= morph) {
|
||||||
|
s.ci = (s.ci + 1) % nc;
|
||||||
|
s.aux = f.now;
|
||||||
|
if (!f.pr->autorun) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.phase = (s.phase + warp) & 255;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- flame -------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static int tri255(uint32_t elapsed, uint32_t period)
|
||||||
|
{
|
||||||
|
if (period < 400) period = 400;
|
||||||
|
uint32_t p = elapsed % period;
|
||||||
|
uint32_t half = period >> 1;
|
||||||
|
if (half == 0) return 128;
|
||||||
|
if (p < half) return (int)((p * 255) / half);
|
||||||
|
return (int)(((period - p) * 255) / (period - half));
|
||||||
|
}
|
||||||
|
|
||||||
|
static rgb palette_sample(const rgb *pal, int n, int pos)
|
||||||
|
{
|
||||||
|
if (n == 1) return pal[0];
|
||||||
|
int span = (n - 1) * pos;
|
||||||
|
int seg = span >> 8;
|
||||||
|
if (seg >= n - 1) return pal[n - 1];
|
||||||
|
int frac = span & 0xFF;
|
||||||
|
rgb a = pal[seg];
|
||||||
|
rgb b = pal[seg + 1];
|
||||||
|
rgb o = { a.r + (((b.r - a.r) * frac) >> 8), a.g + (((b.g - a.g) * frac) >> 8),
|
||||||
|
a.b + (((b.b - a.b) * frac) >> 8) };
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void flame_frame(fx_t &f, st_t &s, const rgb *pal, int np, uint32_t breath_el, bool spark_on)
|
||||||
|
{
|
||||||
|
int num = f.n;
|
||||||
|
int denom = num > 1 ? num - 1 : 1;
|
||||||
|
int breathe = tri255(breath_el, (uint32_t)s.breath_ms);
|
||||||
|
int base_level = s.lo + (((s.hi - s.lo) * breathe) >> 8);
|
||||||
|
int micro = 232 + rnd(0, 35);
|
||||||
|
int level = clampi((base_level * micro) >> 8, s.lo, s.hi);
|
||||||
|
int boost = 0;
|
||||||
|
rgb white = { 0, 0, 0 };
|
||||||
|
if (spark_on && s.spark_dur > 0) {
|
||||||
|
int el = (int)(f.now - s.spark_start);
|
||||||
|
if (el < 0) el = 0;
|
||||||
|
if (el < s.spark_dur) {
|
||||||
|
int env = 255 - (el * 255) / s.spark_dur;
|
||||||
|
boost = (env * 90) >> 8;
|
||||||
|
white = { (env * 55) >> 8, (env * 50) >> 8, (env * 40) >> 8 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int cell = num >> 3;
|
||||||
|
if (cell < 1) cell = 1;
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
int h = (i * 256) / denom;
|
||||||
|
int flow = (h + s.rise + (i / cell) * 17) & 255;
|
||||||
|
int pos = (flow + s.cj[(i >> 2) & 7]) & 255;
|
||||||
|
rgb c = palette_sample(pal, np, pos);
|
||||||
|
if (boost) {
|
||||||
|
c = { clampi(c.r + white.r + ((boost * 3) >> 2), 0, 255), clampi(c.g + white.g + (boost >> 1), 0, 255),
|
||||||
|
clampi(c.b + white.b + (boost >> 2), 0, 255) };
|
||||||
|
}
|
||||||
|
put(f, i, ab(c, level));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fx_flame(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[4] = { { 90, 8, 8 }, { 200, 40, 12 }, { 255, 120, 30 }, { 255, 220, 140 } };
|
||||||
|
rgb pal[PATTERN_MAX_COLOURS];
|
||||||
|
int np = get_colours(f, def, 4, pal);
|
||||||
|
int lo = clampi(f.pr->n[1], 0, 255);
|
||||||
|
int hi = clampi(f.pr->brightness, 0, 255);
|
||||||
|
if (lo > hi) {
|
||||||
|
int t = lo;
|
||||||
|
lo = hi;
|
||||||
|
hi = t;
|
||||||
|
}
|
||||||
|
int bp = f.pr->n[2];
|
||||||
|
int breath_ms = bp > 0 ? bp : 2500;
|
||||||
|
if (breath_ms < 800) breath_ms = 800;
|
||||||
|
int gap_lo = f.pr->n[3];
|
||||||
|
int gap_hi = f.pr->n[4];
|
||||||
|
if (gap_lo == 0 && gap_hi == 0) {
|
||||||
|
gap_lo = 10000;
|
||||||
|
gap_hi = 30000;
|
||||||
|
} else {
|
||||||
|
if (gap_lo < 500) gap_lo = 500;
|
||||||
|
}
|
||||||
|
if (gap_hi < gap_lo) gap_hi = gap_lo;
|
||||||
|
uint32_t delay = f.pr->delay < 16 ? 16 : f.pr->delay;
|
||||||
|
|
||||||
|
if (first_call(s)) {
|
||||||
|
s.breath_ms = breath_ms;
|
||||||
|
s.lo = lo;
|
||||||
|
s.hi = hi;
|
||||||
|
s.rise = rnd(0, 255);
|
||||||
|
for (int k = 0; k < 8; k++) {
|
||||||
|
s.cj[k] = rnd(-18, 18);
|
||||||
|
}
|
||||||
|
s.last = f.now; // last frame
|
||||||
|
s.breath_origin = f.now;
|
||||||
|
s.last_cluster = f.now;
|
||||||
|
s.spark_active = false;
|
||||||
|
s.spark_dur = 0;
|
||||||
|
s.next_spark = f.now + (uint32_t)rnd(gap_lo, gap_hi);
|
||||||
|
}
|
||||||
|
s.lo = lo;
|
||||||
|
s.hi = hi;
|
||||||
|
s.breath_ms = breath_ms;
|
||||||
|
|
||||||
|
if (!f.pr->autorun) {
|
||||||
|
flame_frame(f, s, pal, np, 0, false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if ((int32_t)(f.now - s.last) < (int32_t)delay) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
s.last += delay;
|
||||||
|
s.rise = (s.rise + rnd(-10, 12)) & 255;
|
||||||
|
if ((int32_t)(f.now - s.last_cluster) >= (int32_t)(delay * 4)) {
|
||||||
|
s.last_cluster = f.now;
|
||||||
|
for (int k = 0; k < 8; k++) {
|
||||||
|
s.cj[k] = rnd(-18, 18);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (s.spark_active) {
|
||||||
|
if ((int)(f.now - s.spark_start) >= s.spark_dur) {
|
||||||
|
s.spark_active = false;
|
||||||
|
s.next_spark = f.now + (uint32_t)rnd(gap_lo, gap_hi);
|
||||||
|
}
|
||||||
|
} else if ((int32_t)(f.now - s.next_spark) >= 0) {
|
||||||
|
s.spark_active = true;
|
||||||
|
s.spark_start = f.now;
|
||||||
|
s.spark_dur = rnd(180, 360);
|
||||||
|
}
|
||||||
|
flame_frame(f, s, pal, np, f.now - s.breath_origin, s.spark_active);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
@@ -0,0 +1,295 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Patterns that span the rig, ported from the MicroPython led-driver (rig.py and patterns folder).
|
||||||
|
* The rig geometry is the MicroPython default for a lone strip: the bar runs from (-1, 0) to (1, 0), so
|
||||||
|
* LED i sits at x = -1 + 2 (i + 0.5) / n. The rig placement (place) is not configurable on this board.
|
||||||
|
*/
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
#include "effects.h"
|
||||||
|
|
||||||
|
static const rgb WHITE = { 255, 255, 255 };
|
||||||
|
|
||||||
|
static bool first_call(st_t &s)
|
||||||
|
{
|
||||||
|
if (s.started) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
s.started = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Position of LED i on the bar
|
||||||
|
static float led_x(int i, int n)
|
||||||
|
{
|
||||||
|
return -1.0f + 2.0f * (i + 0.5f) / n;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Distance from the centre, 0..1
|
||||||
|
static float led_r(int i, int n)
|
||||||
|
{
|
||||||
|
float x = led_x(i, n);
|
||||||
|
return x < 0 ? -x : x;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Angle round the centre, 0..1 of a turn: the left half is at 1/2, the right at 0
|
||||||
|
static float led_a(int i, int n)
|
||||||
|
{
|
||||||
|
return led_x(i, n) < 0 ? 0.5f : 0.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Position of LED i along a direction across the rig, 0..1
|
||||||
|
static float along(int i, int n, float angle_deg)
|
||||||
|
{
|
||||||
|
float rad = angle_deg * 3.14159265f / 180.0f;
|
||||||
|
return (led_x(i, n) * cosf(rad) + 1.0f) / 2.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int shade_of(float p)
|
||||||
|
{
|
||||||
|
int s = (int)(p * (RIG_SHADES - 1) + 0.5f);
|
||||||
|
return s < 0 ? 0 : (s > RIG_SHADES - 1 ? RIG_SHADES - 1 : s);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shade of a brightness-free sine: 0..15
|
||||||
|
static int sin_shade(int i)
|
||||||
|
{
|
||||||
|
float v = (sinf(i * 6.2831853f / 256.0f) + 1.0f) / 2.0f * (RIG_SHADES - 1) + 0.5f;
|
||||||
|
return (int)v;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- field wave --------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_field_wave(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[1] = { WHITE };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 1, c);
|
||||||
|
int n = f.n;
|
||||||
|
int kind = f.pr->n[3];
|
||||||
|
int lam = nz(f.pr->n[2], 1000);
|
||||||
|
uint32_t period = dur(f, (int)f.pr->delay);
|
||||||
|
if (period < 1) period = 1;
|
||||||
|
(void)first_call(s);
|
||||||
|
uint32_t t = f.t;
|
||||||
|
bool done = !f.pr->autorun && t >= period;
|
||||||
|
int ph = done ? 256 : (int)((t % period) * 256 / period);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
int shade;
|
||||||
|
if (kind == 3) {
|
||||||
|
// Plasma: two sines of the position, summed
|
||||||
|
// Lone bar: y is always 0, so the second sine is taken at -p13
|
||||||
|
int px = (int)(led_x(i, n) * 3 * 256000 / (lam * 6.283f));
|
||||||
|
int p13 = ph * 13 / 10;
|
||||||
|
float v = sinf(((px + ph) & 255) * 6.2831853f / 256.0f)
|
||||||
|
+ sinf(((-p13) & 255) * 6.2831853f / 256.0f); // -2..2
|
||||||
|
shade = sin_shade(((int)(v * 41) + ph) & 255);
|
||||||
|
} else {
|
||||||
|
float m = kind == 1 ? led_r(i, n) : (kind == 2 ? led_a(i, n) : along(i, n, (float)f.pr->n[1]));
|
||||||
|
int mi = (int)(m * 256000 / lam);
|
||||||
|
shade = sin_shade((mi - ph) & 255);
|
||||||
|
}
|
||||||
|
put(f, i, rig_entry(f, c, nc, RIG_SHADES, shade, RIG_LEVELS - 1));
|
||||||
|
}
|
||||||
|
return done;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- grow --------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_grow(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[1] = { WHITE };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 1, c);
|
||||||
|
int n = f.n;
|
||||||
|
int mode = f.pr->n[1];
|
||||||
|
const int top = RIG_LEVELS - 1;
|
||||||
|
const float edge = 0.06f;
|
||||||
|
(void)first_call(s);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
float m;
|
||||||
|
if (mode == 1) {
|
||||||
|
m = 1 - led_r(i, n);
|
||||||
|
} else if (mode == 2) {
|
||||||
|
m = led_a(i, n);
|
||||||
|
} else if (mode == 3) {
|
||||||
|
m = along(i, n, (float)f.pr->n[3]);
|
||||||
|
} else {
|
||||||
|
m = led_r(i, n);
|
||||||
|
}
|
||||||
|
s.m[i] = m;
|
||||||
|
s.shade[i] = shade_of(m);
|
||||||
|
}
|
||||||
|
uint32_t total = dur(f, (int)f.pr->delay);
|
||||||
|
if (total < 1) total = 1;
|
||||||
|
bool empties = f.pr->n[2] == 1 || f.pr->n[2] == 2 || f.pr->autorun;
|
||||||
|
int phases = empties ? 2 : 1;
|
||||||
|
uint32_t t = f.t;
|
||||||
|
if (!f.pr->autorun && t >= total * phases) {
|
||||||
|
if (f.pr->n[2] == 3 || empties) {
|
||||||
|
fill(f, ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), f.pr->brightness));
|
||||||
|
} else {
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
put(f, i, rig_entry(f, c, nc, RIG_SHADES, s.shade[i], top));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
float ph;
|
||||||
|
if (f.pr->autorun) {
|
||||||
|
ph = (float)(t % (total * phases)) / total;
|
||||||
|
} else {
|
||||||
|
ph = (float)t / total;
|
||||||
|
if (ph > phases) ph = (float)phases;
|
||||||
|
}
|
||||||
|
float front;
|
||||||
|
bool rising;
|
||||||
|
if (ph <= 1) {
|
||||||
|
front = ph * (1 + edge);
|
||||||
|
rising = true;
|
||||||
|
} else if (f.pr->n[2] == 2) {
|
||||||
|
front = (ph - 1) * (1 + edge);
|
||||||
|
rising = false;
|
||||||
|
} else {
|
||||||
|
front = (2 - ph) * (1 + edge);
|
||||||
|
rising = true;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
float v = (rising ? (front - s.m[i]) : (s.m[i] - front)) / edge;
|
||||||
|
int lv = v <= 0 ? 0 : (v >= 1 ? top : (int)(v * top));
|
||||||
|
put(f, i, rig_entry(f, c, nc, RIG_SHADES, s.shade[i], lv));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- ripple ------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_ripple(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[1] = { WHITE };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 1, c);
|
||||||
|
int n = f.n;
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
const int top = RIG_LEVELS - 1;
|
||||||
|
(void)first_call(s);
|
||||||
|
uint32_t period = dur(f, (int)f.pr->delay);
|
||||||
|
if (period < 1) period = 1;
|
||||||
|
float hw = (f.pr->n[1] > 0 ? f.pr->n[1] : 150) / 2000.0f;
|
||||||
|
int rings = f.pr->autorun ? nz(f.pr->n[2], 1) : 1;
|
||||||
|
if (rings > 8) rings = 8;
|
||||||
|
float fade = (f.pr->n[4] > 0 ? f.pr->n[4] : 0) / 1000.0f;
|
||||||
|
uint32_t t = f.t;
|
||||||
|
if (!f.pr->autorun && t >= period) {
|
||||||
|
fill(f, ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
float base = f.pr->autorun ? (float)(t % period) / period : (float)t / period;
|
||||||
|
float span = 1 + 4 * hw;
|
||||||
|
float rads[8];
|
||||||
|
for (int k = 0; k < rings; k++) {
|
||||||
|
float x = base + (float)k / rings;
|
||||||
|
x = x - floorf(x);
|
||||||
|
rads[k] = -2 * hw + x * span;
|
||||||
|
}
|
||||||
|
int shade = (int)((t / period) % (uint32_t)nc);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
float r = (f.pr->n[3] == 1) ? (1 - led_r(i, n)) : led_r(i, n);
|
||||||
|
float best = 0;
|
||||||
|
for (int k = 0; k < rings; k++) {
|
||||||
|
float v = 1 - fabsf(r - rads[k]) / hw;
|
||||||
|
if (v > best) best = v;
|
||||||
|
}
|
||||||
|
if (fade > 0 && best > 0) {
|
||||||
|
best *= 1 - fade * r;
|
||||||
|
if (best < 0) best = 0;
|
||||||
|
}
|
||||||
|
put(f, i, rig_entry(f, c, nc, nc, shade, (int)(best * top)));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- spin --------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_spin(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[1] = { WHITE };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 1, c);
|
||||||
|
int n = f.n;
|
||||||
|
const int top = RIG_LEVELS - 1;
|
||||||
|
(void)first_call(s);
|
||||||
|
uint32_t period = dur(f, (int)f.pr->delay);
|
||||||
|
if (period < 1) period = 1;
|
||||||
|
float span = 1.0f / (f.pr->n[1] > 0 ? f.pr->n[1] : 1);
|
||||||
|
float width = (f.pr->n[2] > 0 ? f.pr->n[2] : 20) / 360.0f;
|
||||||
|
float tail = (f.pr->n[3] > 0 ? f.pr->n[3] : 90) / 360.0f;
|
||||||
|
uint32_t t = f.t;
|
||||||
|
if (!f.pr->autorun && t >= period) {
|
||||||
|
fill(f, ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), f.pr->brightness));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
float rot = fmodf((float)t / period, 1.0f);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
float ang = f.pr->n[4] == 1 ? 1 - led_a(i, n) : led_a(i, n);
|
||||||
|
float x = rot - ang;
|
||||||
|
float behind = x - floorf(x / span) * span;
|
||||||
|
if (behind < width) {
|
||||||
|
put(f, i, rig_entry(f, c, nc, RIG_SHADES, 0, top));
|
||||||
|
} else {
|
||||||
|
float fr = (behind - width) / tail;
|
||||||
|
if (fr >= 1) {
|
||||||
|
put(f, i, rig_entry(f, c, nc, RIG_SHADES, 0, 0));
|
||||||
|
} else {
|
||||||
|
int shade = (int)(fr * (RIG_SHADES - 1));
|
||||||
|
int lv = (int)((1 - fr) * top);
|
||||||
|
put(f, i, rig_entry(f, c, nc, RIG_SHADES, shade, lv));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- sweep -------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool fx_sweep(fx_t &f, st_t &s)
|
||||||
|
{
|
||||||
|
const rgb def[1] = { WHITE };
|
||||||
|
rgb c[PATTERN_MAX_COLOURS];
|
||||||
|
int nc = get_colours(f, def, 1, c);
|
||||||
|
int n = f.n;
|
||||||
|
const int top = RIG_LEVELS - 1;
|
||||||
|
(void)first_call(s);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
s.m[i] = along(i, n, (float)f.pr->n[1]);
|
||||||
|
s.shade[i] = shade_of(s.m[i]);
|
||||||
|
}
|
||||||
|
uint32_t total = dur(f, (int)f.pr->delay);
|
||||||
|
if (total < 1) total = 1;
|
||||||
|
float hw = (f.pr->n[2] > 0 ? f.pr->n[2] : 200) / 2000.0f;
|
||||||
|
float trail = (f.pr->n[3] > 0 ? f.pr->n[3] : 0) / 1000.0f;
|
||||||
|
bool keep = f.pr->n[4] == 1;
|
||||||
|
uint32_t t = f.t;
|
||||||
|
bool done = !f.pr->autorun && t >= total;
|
||||||
|
float p = done ? 1.0f : (float)(t % total) / total;
|
||||||
|
float head = -hw + p * (1 + 2 * hw + trail);
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
float d = s.m[i] - head;
|
||||||
|
float k;
|
||||||
|
if (d > hw) {
|
||||||
|
k = 0;
|
||||||
|
} else if (d >= -hw) {
|
||||||
|
k = (keep && d < 0) ? 1.0f : 1 - fabsf(d) / hw;
|
||||||
|
} else if (keep) {
|
||||||
|
k = 1;
|
||||||
|
} else if (trail > 0) {
|
||||||
|
k = 1 + (d + hw) / trail;
|
||||||
|
if (k < 0) k = 0;
|
||||||
|
} else {
|
||||||
|
k = 0;
|
||||||
|
}
|
||||||
|
put(f, i, rig_entry(f, c, nc, RIG_SHADES, s.shade[i], (int)(k * top)));
|
||||||
|
}
|
||||||
|
return done;
|
||||||
|
}
|
||||||
@@ -0,0 +1,185 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Helpers shared by the pattern files: integer maths that matches Python's // and %, colour mixing,
|
||||||
|
* random numbers and the rig colour table.
|
||||||
|
*/
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "esp_random.h"
|
||||||
|
#include "effects.h"
|
||||||
|
|
||||||
|
int clampi(int v, int lo, int hi)
|
||||||
|
{
|
||||||
|
return v < lo ? lo : (v > hi ? hi : v);
|
||||||
|
}
|
||||||
|
|
||||||
|
int fdiv(int a, int b)
|
||||||
|
{
|
||||||
|
int q = a / b;
|
||||||
|
if ((a % b != 0) && ((a < 0) != (b < 0))) {
|
||||||
|
q--;
|
||||||
|
}
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
|
int pmod(int a, int m)
|
||||||
|
{
|
||||||
|
int r = a % m;
|
||||||
|
return r < 0 ? r + m : r;
|
||||||
|
}
|
||||||
|
|
||||||
|
int nz(int v, int def)
|
||||||
|
{
|
||||||
|
return v > 0 ? v : def;
|
||||||
|
}
|
||||||
|
|
||||||
|
rgb mk(int r, int g, int b)
|
||||||
|
{
|
||||||
|
rgb c = { clampi(r, 0, 255), clampi(g, 0, 255), clampi(b, 0, 255) };
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
rgb ab(rgb c, int level)
|
||||||
|
{
|
||||||
|
rgb o = { fdiv(c.r * level, 255), fdiv(c.g * level, 255), fdiv(c.b * level, 255) };
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
rgb mix(rgb a, rgb b, int k)
|
||||||
|
{
|
||||||
|
rgb o = {
|
||||||
|
a.r + fdiv((b.r - a.r) * k, 255),
|
||||||
|
a.g + fdiv((b.g - a.g) * k, 255),
|
||||||
|
a.b + fdiv((b.b - a.b) * k, 255),
|
||||||
|
};
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sgn(const fx_t &f, int v)
|
||||||
|
{
|
||||||
|
return f.pr->reverse ? -v : v;
|
||||||
|
}
|
||||||
|
|
||||||
|
int led_at(const fx_t &f, int i)
|
||||||
|
{
|
||||||
|
return (f.pr->reverse && i >= 0 && i < f.n) ? f.n - 1 - i : i;
|
||||||
|
}
|
||||||
|
|
||||||
|
void put(fx_t &f, int phys, rgb c)
|
||||||
|
{
|
||||||
|
if (phys >= 0 && phys < f.n) {
|
||||||
|
f.px[phys] = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void putl(fx_t &f, int logical, rgb c)
|
||||||
|
{
|
||||||
|
put(f, led_at(f, logical), c);
|
||||||
|
}
|
||||||
|
|
||||||
|
void fill(fx_t &f, rgb c)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < f.n; i++) {
|
||||||
|
f.px[i] = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool due(uint32_t now, uint32_t &last, uint32_t d)
|
||||||
|
{
|
||||||
|
if ((int32_t)(now - last) >= (int32_t)d) {
|
||||||
|
last += d;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The beat is 500 ms here (there is no beat source on this board)
|
||||||
|
uint32_t dur(const fx_t &f, int v)
|
||||||
|
{
|
||||||
|
if (v < 0) {
|
||||||
|
v = 0;
|
||||||
|
}
|
||||||
|
return f.pr->beat_time ? (uint32_t)(v * 500 / 4) : (uint32_t)v;
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_colours(const fx_t &f, const rgb *def, int ndef, rgb *out)
|
||||||
|
{
|
||||||
|
if (f.pr->ncol > 0) {
|
||||||
|
int n = f.pr->ncol > PATTERN_MAX_COLOURS ? PATTERN_MAX_COLOURS : f.pr->ncol;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
out[i] = mk(f.pr->col[i][0], f.pr->col[i][1], f.pr->col[i][2]);
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < ndef; i++) {
|
||||||
|
out[i] = def[i];
|
||||||
|
}
|
||||||
|
return ndef;
|
||||||
|
}
|
||||||
|
|
||||||
|
int rnd(int lo, int hi)
|
||||||
|
{
|
||||||
|
if (hi <= lo) {
|
||||||
|
return lo;
|
||||||
|
}
|
||||||
|
return lo + (int)(esp_random() % (uint32_t)(hi - lo + 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
rgb grad_at(const rgb *cols, int m, float t)
|
||||||
|
{
|
||||||
|
if (m == 1 || t <= 0) {
|
||||||
|
return cols[0];
|
||||||
|
}
|
||||||
|
if (t >= 1) {
|
||||||
|
return cols[m - 1];
|
||||||
|
}
|
||||||
|
float x = t * (m - 1);
|
||||||
|
int j = (int)x;
|
||||||
|
float f = x - j;
|
||||||
|
rgb a = cols[j];
|
||||||
|
rgb b = cols[j + 1];
|
||||||
|
rgb o = { (int)(a.r + (b.r - a.r) * f), (int)(a.g + (b.g - a.g) * f), (int)(a.b + (b.b - a.b) * f) };
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Colour of one entry of the rig table: a shade of the colour list at a brightness level (0..15),
|
||||||
|
// over the background. Mirrors rig.Lut in the MicroPython driver.
|
||||||
|
rgb rig_entry(const fx_t &f, const rgb *cols, int ncols, int shades, int shade, int level)
|
||||||
|
{
|
||||||
|
int b = f.pr->brightness;
|
||||||
|
rgb bgc = ab(mk(f.pr->bg[0], f.pr->bg[1], f.pr->bg[2]), b);
|
||||||
|
rgb c = (shades > 1) ? grad_at(cols, ncols, (float)shade / (shades - 1)) : cols[0];
|
||||||
|
rgb sc = { fdiv((int)c.r * b, 255), fdiv((int)c.g * b, 255), fdiv((int)c.b * b, 255) };
|
||||||
|
int top = RIG_LEVELS - 1;
|
||||||
|
rgb o = {
|
||||||
|
bgc.r + fdiv((sc.r - bgc.r) * level, top),
|
||||||
|
bgc.g + fdiv((sc.g - bgc.g) * level, top),
|
||||||
|
bgc.b + fdiv((sc.b - bgc.b) * level, top),
|
||||||
|
};
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hue wheel, 0..255 -> colour
|
||||||
|
rgb wheel(int pos)
|
||||||
|
{
|
||||||
|
pos &= 255;
|
||||||
|
rgb c;
|
||||||
|
if (pos < 85) {
|
||||||
|
c.r = pos * 3;
|
||||||
|
c.g = 255 - pos * 3;
|
||||||
|
c.b = 0;
|
||||||
|
} else if (pos < 170) {
|
||||||
|
pos -= 85;
|
||||||
|
c.r = 255 - pos * 3;
|
||||||
|
c.g = 0;
|
||||||
|
c.b = pos * 3;
|
||||||
|
} else {
|
||||||
|
pos -= 170;
|
||||||
|
c.r = 0;
|
||||||
|
c.g = pos * 3;
|
||||||
|
c.b = 255 - pos * 3;
|
||||||
|
}
|
||||||
|
return c;
|
||||||
|
}
|
||||||
+10
-1
@@ -4,8 +4,11 @@
|
|||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
|
|
||||||
|
#include "ddp.h"
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
#include "ota.h"
|
||||||
#include "patterns.h"
|
#include "patterns.h"
|
||||||
|
#include "presets.h"
|
||||||
#include "web.h"
|
#include "web.h"
|
||||||
|
|
||||||
static const char *TAG = "example";
|
static const char *TAG = "example";
|
||||||
@@ -20,9 +23,15 @@ extern "C" void app_main(void)
|
|||||||
}
|
}
|
||||||
ESP_ERROR_CHECK(err);
|
ESP_ERROR_CHECK(err);
|
||||||
|
|
||||||
patterns_start();
|
hw_cfg_t hw;
|
||||||
|
settings_load_hw(&hw);
|
||||||
|
patterns_start(hw);
|
||||||
|
ESP_ERROR_CHECK(presets_init());
|
||||||
|
presets_apply_startup();
|
||||||
ESP_ERROR_CHECK(net_start());
|
ESP_ERROR_CHECK(net_start());
|
||||||
ESP_ERROR_CHECK(web_start());
|
ESP_ERROR_CHECK(web_start());
|
||||||
|
ESP_ERROR_CHECK(ddp_start());
|
||||||
|
ota_confirm_later();
|
||||||
|
|
||||||
char ssid[16];
|
char ssid[16];
|
||||||
net_ap_ssid(ssid, sizeof(ssid));
|
net_ap_ssid(ssid, sizeof(ssid));
|
||||||
|
|||||||
+66
-4
@@ -17,7 +17,6 @@ static const char *TAG = "net";
|
|||||||
|
|
||||||
#define AP_CHANNEL 1
|
#define AP_CHANNEL 1
|
||||||
#define AP_MAX_CLIENTS 4
|
#define AP_MAX_CLIENTS 4
|
||||||
#define MDNS_HOSTNAME "led"
|
|
||||||
#define RETRY_MAX_MS 30000
|
#define RETRY_MAX_MS 30000
|
||||||
|
|
||||||
static esp_netif_t *s_ap_netif;
|
static esp_netif_t *s_ap_netif;
|
||||||
@@ -37,6 +36,35 @@ static void apply_sta_config(const wifi_creds_t *c)
|
|||||||
esp_wifi_set_config(WIFI_IF_STA, &cfg);
|
esp_wifi_set_config(WIFI_IF_STA, &cfg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Client address: DHCP, or the static address from settings. Called before the client connects.
|
||||||
|
static esp_err_t apply_client_ip(const net_cfg_t *c)
|
||||||
|
{
|
||||||
|
if (!c->use_static) {
|
||||||
|
esp_err_t err = esp_netif_dhcpc_start(s_sta_netif);
|
||||||
|
// Already running is fine
|
||||||
|
return (err == ESP_OK || err == ESP_ERR_ESP_NETIF_DHCP_ALREADY_STARTED) ? ESP_OK : err;
|
||||||
|
}
|
||||||
|
esp_netif_ip_info_t info = {};
|
||||||
|
if (esp_netif_str_to_ip4(c->ip, &info.ip) != ESP_OK ||
|
||||||
|
esp_netif_str_to_ip4(c->netmask, &info.netmask) != ESP_OK ||
|
||||||
|
esp_netif_str_to_ip4(c->gateway, &info.gw) != ESP_OK) {
|
||||||
|
return ESP_ERR_INVALID_ARG;
|
||||||
|
}
|
||||||
|
esp_netif_dhcpc_stop(s_sta_netif); // ignore "not started"
|
||||||
|
esp_err_t err = esp_netif_set_ip_info(s_sta_netif, &info);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
esp_netif_dns_info_t dns = {};
|
||||||
|
const char *dns_str = c->dns[0] ? c->dns : c->gateway;
|
||||||
|
if (esp_netif_str_to_ip4(dns_str, &dns.ip.u_addr.ip4) == ESP_OK) {
|
||||||
|
dns.ip.type = ESP_IPADDR_TYPE_V4;
|
||||||
|
esp_netif_set_dns_info(s_sta_netif, ESP_NETIF_DNS_MAIN, &dns);
|
||||||
|
}
|
||||||
|
ESP_LOGI(TAG, "Client static IP %s/%s gw %s", c->ip, c->netmask, c->gateway);
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
static void retry_cb(void *arg)
|
static void retry_cb(void *arg)
|
||||||
{
|
{
|
||||||
(void)arg;
|
(void)arg;
|
||||||
@@ -52,6 +80,13 @@ static void on_wifi_event(void *arg, esp_event_base_t base, int32_t id, void *da
|
|||||||
case WIFI_EVENT_STA_START: {
|
case WIFI_EVENT_STA_START: {
|
||||||
wifi_creds_t c;
|
wifi_creds_t c;
|
||||||
if (settings_load_wifi(&c) == ESP_OK && c.ssid[0]) {
|
if (settings_load_wifi(&c) == ESP_OK && c.ssid[0]) {
|
||||||
|
net_cfg_t n;
|
||||||
|
settings_load_net(&n);
|
||||||
|
if (apply_client_ip(&n) != ESP_OK) {
|
||||||
|
ESP_LOGW(TAG, "Static IP settings are invalid; using DHCP");
|
||||||
|
n.use_static = false;
|
||||||
|
apply_client_ip(&n);
|
||||||
|
}
|
||||||
esp_wifi_connect();
|
esp_wifi_connect();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -133,12 +168,14 @@ esp_err_t net_start(void)
|
|||||||
ESP_ERROR_CHECK(esp_wifi_start());
|
ESP_ERROR_CHECK(esp_wifi_start());
|
||||||
ESP_LOGI(TAG, "AP %s started", ssid);
|
ESP_LOGI(TAG, "AP %s started", ssid);
|
||||||
|
|
||||||
// mDNS: led.local, with an HTTP service on port 80
|
// mDNS: <hostname>.local, with an HTTP service on port 80
|
||||||
|
net_cfg_t n;
|
||||||
|
settings_load_net(&n);
|
||||||
ESP_ERROR_CHECK(mdns_init());
|
ESP_ERROR_CHECK(mdns_init());
|
||||||
ESP_ERROR_CHECK(mdns_hostname_set(MDNS_HOSTNAME));
|
ESP_ERROR_CHECK(mdns_hostname_set(n.hostname));
|
||||||
ESP_ERROR_CHECK(mdns_instance_name_set("LED controller"));
|
ESP_ERROR_CHECK(mdns_instance_name_set("LED controller"));
|
||||||
ESP_ERROR_CHECK(mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0));
|
ESP_ERROR_CHECK(mdns_service_add(NULL, "_http", "_tcp", 80, NULL, 0));
|
||||||
ESP_LOGI(TAG, "mDNS: %s.local", MDNS_HOSTNAME);
|
ESP_LOGI(TAG, "mDNS: %s.local", n.hostname);
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,6 +204,31 @@ esp_err_t net_sta_connect(const wifi_creds_t *c)
|
|||||||
return esp_wifi_connect();
|
return esp_wifi_connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
esp_err_t net_save_network(const net_cfg_t *c)
|
||||||
|
{
|
||||||
|
esp_err_t err = settings_save_net(c);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
err = mdns_hostname_set(c->hostname);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
// Reconnect with the new address settings if a client is configured
|
||||||
|
wifi_creds_t w;
|
||||||
|
if (settings_load_wifi(&w) != ESP_OK || !w.ssid[0]) {
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
esp_timer_stop(s_retry_timer);
|
||||||
|
esp_wifi_disconnect();
|
||||||
|
s_retries = 0;
|
||||||
|
err = apply_client_ip(c);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
return esp_wifi_connect();
|
||||||
|
}
|
||||||
|
|
||||||
esp_err_t net_scan(wifi_ap_record_t *recs, uint16_t *count)
|
esp_err_t net_scan(wifi_ap_record_t *recs, uint16_t *count)
|
||||||
{
|
{
|
||||||
esp_err_t err = esp_wifi_scan_start(NULL, true);
|
esp_err_t err = esp_wifi_scan_start(NULL, true);
|
||||||
|
|||||||
+7
-1
@@ -10,7 +10,7 @@
|
|||||||
#include "esp_wifi_types.h"
|
#include "esp_wifi_types.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
|
||||||
/** @brief Start Wi-Fi (AP `led-XXXXXX` plus an optional client), and mDNS as led.local. Needs NVS. */
|
/** @brief Start Wi-Fi (AP `led-XXXXXX` plus an optional client), and mDNS. Needs NVS. */
|
||||||
esp_err_t net_start(void);
|
esp_err_t net_start(void);
|
||||||
|
|
||||||
/** @brief Fill buf with the AP SSID, e.g. "led-A1B2C3". */
|
/** @brief Fill buf with the AP SSID, e.g. "led-A1B2C3". */
|
||||||
@@ -22,5 +22,11 @@ bool net_sta_status(char *ip, size_t ip_len);
|
|||||||
/** @brief Save credentials, then connect the client with them. */
|
/** @brief Save credentials, then connect the client with them. */
|
||||||
esp_err_t net_sta_connect(const wifi_creds_t *c);
|
esp_err_t net_sta_connect(const wifi_creds_t *c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Save network settings and apply them: mDNS name, static or DHCP client address.
|
||||||
|
* A connected or configured client reconnects so the new address takes effect.
|
||||||
|
*/
|
||||||
|
esp_err_t net_save_network(const net_cfg_t *c);
|
||||||
|
|
||||||
/** @brief Scan for networks. On entry *count is the array size; on exit, the number found. */
|
/** @brief Scan for networks. On entry *count is the array size; on exit, the number found. */
|
||||||
esp_err_t net_scan(wifi_ap_record_t *recs, uint16_t *count);
|
esp_err_t net_scan(wifi_ap_record_t *recs, uint16_t *count);
|
||||||
+186
@@ -0,0 +1,186 @@
|
|||||||
|
#include "ota.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "esp_ota_ops.h"
|
||||||
|
#include "esp_app_format.h"
|
||||||
|
#include "esp_system.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include "esp_timer.h"
|
||||||
|
|
||||||
|
static const char *TAG = "ota";
|
||||||
|
|
||||||
|
#define CHUNK 4096
|
||||||
|
#define CONFIRM_AFTER_US (10 * 1000000LL)
|
||||||
|
|
||||||
|
static bool s_busy;
|
||||||
|
|
||||||
|
static void reboot_cb(void *arg)
|
||||||
|
{
|
||||||
|
esp_restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void confirm_cb(void *arg)
|
||||||
|
{
|
||||||
|
esp_err_t err = esp_ota_mark_app_valid_cancel_rollback();
|
||||||
|
if (err == ESP_OK) {
|
||||||
|
ESP_LOGI(TAG, "running image confirmed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ota_confirm_later(void)
|
||||||
|
{
|
||||||
|
esp_ota_img_states_t state;
|
||||||
|
const esp_partition_t *run = esp_ota_get_running_partition();
|
||||||
|
if (esp_ota_get_state_partition(run, &state) != ESP_OK || state != ESP_OTA_IMG_PENDING_VERIFY) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
static esp_timer_handle_t t;
|
||||||
|
const esp_timer_create_args_t args = { .callback = confirm_cb, .name = "ota_confirm" };
|
||||||
|
if (esp_timer_create(&args, &t) == ESP_OK) {
|
||||||
|
esp_timer_start_once(t, CONFIRM_AFTER_US);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t fail(httpd_req_t *req, const char *status, const char *msg)
|
||||||
|
{
|
||||||
|
ESP_LOGE(TAG, "%s", msg);
|
||||||
|
httpd_resp_set_status(req, status);
|
||||||
|
httpd_resp_set_type(req, "application/json");
|
||||||
|
char buf[160];
|
||||||
|
snprintf(buf, sizeof(buf), "{\"error\":\"%s\"}", msg);
|
||||||
|
return httpd_resp_sendstr(req, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t ota_post_handler(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
if (req->content_len == 0) {
|
||||||
|
return fail(req, "400 Bad Request", "empty body");
|
||||||
|
}
|
||||||
|
const esp_partition_t *part = esp_ota_get_next_update_partition(NULL);
|
||||||
|
if (!part) {
|
||||||
|
return fail(req, "500 Internal Server Error", "no update partition");
|
||||||
|
}
|
||||||
|
if (req->content_len > part->size) {
|
||||||
|
return fail(req, "413 Payload Too Large", "image larger than the app slot");
|
||||||
|
}
|
||||||
|
if (s_busy) {
|
||||||
|
return fail(req, "409 Conflict", "update already running");
|
||||||
|
}
|
||||||
|
s_busy = true;
|
||||||
|
|
||||||
|
char *buf = malloc(CHUNK);
|
||||||
|
if (!buf) {
|
||||||
|
s_busy = false;
|
||||||
|
return fail(req, "500 Internal Server Error", "out of memory");
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_ota_handle_t handle = 0;
|
||||||
|
esp_err_t err = esp_ota_begin(part, req->content_len, &handle);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
free(buf);
|
||||||
|
s_busy = false;
|
||||||
|
return fail(req, "500 Internal Server Error", "ota begin failed");
|
||||||
|
}
|
||||||
|
ESP_LOGI(TAG, "receiving %u bytes into %s", (unsigned)req->content_len, part->label);
|
||||||
|
|
||||||
|
size_t got = 0;
|
||||||
|
const char *bad = NULL;
|
||||||
|
while (got < req->content_len) {
|
||||||
|
int want = req->content_len - got;
|
||||||
|
int r = httpd_req_recv(req, buf, want > CHUNK ? CHUNK : want);
|
||||||
|
if (r == HTTPD_SOCK_ERR_TIMEOUT) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (r <= 0) {
|
||||||
|
bad = "receive failed";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (got == 0 && (r < (int)(sizeof(esp_image_header_t)) || (uint8_t)buf[0] != ESP_IMAGE_HEADER_MAGIC)) {
|
||||||
|
bad = "not an ESP32 app image";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (esp_ota_write(handle, buf, r) != ESP_OK) {
|
||||||
|
bad = "flash write failed";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
got += r;
|
||||||
|
}
|
||||||
|
free(buf);
|
||||||
|
if (bad) {
|
||||||
|
esp_ota_abort(handle);
|
||||||
|
s_busy = false;
|
||||||
|
return fail(req, "400 Bad Request", bad);
|
||||||
|
}
|
||||||
|
// esp_ota_end checks the image checksum and hash
|
||||||
|
if (esp_ota_end(handle) != ESP_OK) {
|
||||||
|
s_busy = false;
|
||||||
|
return fail(req, "400 Bad Request", "image failed validation");
|
||||||
|
}
|
||||||
|
if (esp_ota_set_boot_partition(part) != ESP_OK) {
|
||||||
|
s_busy = false;
|
||||||
|
return fail(req, "500 Internal Server Error", "set boot partition failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "update ok, rebooting");
|
||||||
|
httpd_resp_set_type(req, "application/json");
|
||||||
|
httpd_resp_sendstr(req, "{\"message\":\"update written, rebooting\"}");
|
||||||
|
|
||||||
|
static esp_timer_handle_t t;
|
||||||
|
const esp_timer_create_args_t args = { .callback = reboot_cb, .name = "ota_reboot" };
|
||||||
|
if (esp_timer_create(&args, &t) == ESP_OK) {
|
||||||
|
esp_timer_start_once(t, 500 * 1000);
|
||||||
|
} else {
|
||||||
|
esp_restart();
|
||||||
|
}
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t ota_get_handler(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
const esp_partition_t *run = esp_ota_get_running_partition();
|
||||||
|
const esp_app_desc_t *app = esp_app_get_description();
|
||||||
|
// The build date is only recompiled with its own file, so the ELF hash is what tells two builds apart
|
||||||
|
char sha[17];
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
snprintf(sha + 2 * i, 3, "%02x", app->app_elf_sha256[i]);
|
||||||
|
}
|
||||||
|
char buf[288];
|
||||||
|
snprintf(buf, sizeof(buf),
|
||||||
|
"{\"partition\":\"%s\",\"version\":\"%s\",\"built\":\"%s %s\",\"idf\":\"%s\",\"sha\":\"%s\"}",
|
||||||
|
run ? run->label : "?", app->version, app->date, app->time, app->idf_ver, sha);
|
||||||
|
httpd_resp_set_type(req, "application/json");
|
||||||
|
return httpd_resp_sendstr(req, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t reboot_post_handler(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "reboot requested");
|
||||||
|
httpd_resp_set_type(req, "application/json");
|
||||||
|
httpd_resp_sendstr(req, "{\"message\":\"rebooting\"}");
|
||||||
|
|
||||||
|
static esp_timer_handle_t t;
|
||||||
|
const esp_timer_create_args_t args = { .callback = reboot_cb, .name = "reboot" };
|
||||||
|
if (esp_timer_create(&args, &t) == ESP_OK) {
|
||||||
|
esp_timer_start_once(t, 500 * 1000);
|
||||||
|
} else {
|
||||||
|
esp_restart();
|
||||||
|
}
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t ota_register(httpd_handle_t server)
|
||||||
|
{
|
||||||
|
const httpd_uri_t post = { .uri = "/ota", .method = HTTP_POST, .handler = ota_post_handler };
|
||||||
|
const httpd_uri_t get = { .uri = "/ota", .method = HTTP_GET, .handler = ota_get_handler };
|
||||||
|
esp_err_t err = httpd_register_uri_handler(server, &post);
|
||||||
|
if (err == ESP_OK) {
|
||||||
|
err = httpd_register_uri_handler(server, &get);
|
||||||
|
}
|
||||||
|
const httpd_uri_t reboot = { .uri = "/reboot", .method = HTTP_POST, .handler = reboot_post_handler };
|
||||||
|
if (err == ESP_OK) {
|
||||||
|
err = httpd_register_uri_handler(server, &reboot);
|
||||||
|
}
|
||||||
|
return err;
|
||||||
|
}
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "esp_err.h"
|
||||||
|
#include "esp_http_server.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Register POST /ota (raw firmware .bin as the body) and GET /ota (running image info).
|
||||||
|
// The web UI is part of the image, so one upload updates both.
|
||||||
|
// POST /reboot restarts the board (replies first, reboots after 0.5 s).
|
||||||
|
// A good upload is written to the idle app slot, becomes the boot image and the board reboots.
|
||||||
|
esp_err_t ota_register(httpd_handle_t server);
|
||||||
|
|
||||||
|
// Call once at boot: confirms the running image after it has stayed up for a few seconds, so
|
||||||
|
// a new image that crashes on start rolls back to the previous one.
|
||||||
|
void ota_confirm_later(void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
+398
-122
@@ -1,188 +1,464 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Pattern engine: runs the selected pattern once per frame into a shadow frame, crossfades from the
|
||||||
|
* previous frame when a preset asks for it, and shows the result on the WS2812 strip.
|
||||||
*/
|
*/
|
||||||
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "driver/gpio.h"
|
||||||
|
#include "esp_heap_caps.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "esp_timer.h"
|
#include "esp_timer.h"
|
||||||
#include "driver/gpio.h"
|
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/semphr.h"
|
#include "freertos/semphr.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
|
|
||||||
|
#include "effects.h"
|
||||||
#include "patterns.h"
|
#include "patterns.h"
|
||||||
|
#include "settings.h"
|
||||||
#include "ws2812.h"
|
#include "ws2812.h"
|
||||||
|
|
||||||
#define RMT_LED_STRIP_GPIO_NUM GPIO_NUM_10
|
#define FRAME_MS 10
|
||||||
#define EXAMPLE_LED_NUMBERS 119
|
|
||||||
#define FRAME_MS 20
|
|
||||||
|
|
||||||
static const char *TAG = "patterns";
|
static const char *TAG = "patterns";
|
||||||
|
|
||||||
static const char *const s_names[PM_COUNT] = {
|
typedef bool (*fx_fn)(fx_t &, st_t &);
|
||||||
"solid", "rainbow", "chase", "breathe", "blink", "pulse", "transition", "circle",
|
struct pattern_def_t {
|
||||||
|
const char *name;
|
||||||
|
fx_fn fn;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Same names as the MicroPython led-driver patterns
|
||||||
|
static const pattern_def_t kPatterns[] = {
|
||||||
|
{ "off", fx_off },
|
||||||
|
{ "on", fx_on },
|
||||||
|
{ "blink", fx_blink },
|
||||||
|
{ "aurora", fx_aurora },
|
||||||
|
{ "bar_graph", fx_bar_graph },
|
||||||
|
{ "blizzard", fx_blizzard },
|
||||||
|
{ "candle_glow", fx_candle_glow },
|
||||||
|
{ "chase", fx_chase },
|
||||||
|
{ "circle", fx_circle },
|
||||||
|
{ "clock_sweep", fx_clock_sweep },
|
||||||
|
{ "colour_cycle", fx_colour_cycle },
|
||||||
|
{ "field_wave", fx_field_wave },
|
||||||
|
{ "flame", fx_flame },
|
||||||
|
{ "flicker", fx_flicker },
|
||||||
|
{ "gradient_wipe", fx_gradient_wipe },
|
||||||
|
{ "grow", fx_grow },
|
||||||
|
{ "hit", fx_hit },
|
||||||
|
{ "icicles", fx_icicles },
|
||||||
|
{ "keyframes", fx_keyframes },
|
||||||
|
{ "meteor", fx_meteor },
|
||||||
|
{ "orbit", fx_orbit },
|
||||||
|
{ "palette_morph", fx_palette_morph },
|
||||||
|
{ "particles", fx_particles },
|
||||||
|
{ "plasma", fx_plasma },
|
||||||
|
{ "pulse", fx_pulse },
|
||||||
|
{ "radiate", fx_radiate },
|
||||||
|
{ "rain_drops", fx_rain_drops },
|
||||||
|
{ "rime", fx_rime },
|
||||||
|
{ "ripple", fx_ripple },
|
||||||
|
{ "sparkle", fx_sparkle },
|
||||||
|
{ "spin", fx_spin },
|
||||||
|
{ "strobe_burst", fx_strobe_burst },
|
||||||
|
{ "sweep", fx_sweep },
|
||||||
|
{ "transition", fx_transition },
|
||||||
|
{ "twinkle", fx_twinkle },
|
||||||
|
};
|
||||||
|
#define PATTERN_COUNT ((int)(sizeof(kPatterns) / sizeof(kPatterns[0])))
|
||||||
|
|
||||||
static WS2812 *s_strip;
|
static WS2812 *s_strip;
|
||||||
static SemaphoreHandle_t s_lock;
|
static SemaphoreHandle_t s_lock;
|
||||||
static pattern_params_t s_p = { PM_CHASE, { 255, 0, 0 }, { 0, 0, 0 }, 1000, 16 };
|
static preset_t s_cur; // the preset requested (under s_lock)
|
||||||
|
static bool s_reset_pending; // a new pattern was selected (under s_lock)
|
||||||
|
|
||||||
const char *patterns_mode_name(pattern_mode_t m)
|
static preset_t s_run; // the preset the task is running
|
||||||
|
static fx_t s_fx; // shadow frame and timing for the running pattern
|
||||||
|
st_t g_st; // state of the running pattern
|
||||||
|
|
||||||
|
// Crossfade: the last frame shown, and the frame it fades from
|
||||||
|
static rgb *s_shown; // last frame shown (s_cap entries)
|
||||||
|
static rgb *s_from; // frame the crossfade starts from
|
||||||
|
static int s_cap; // LEDs the buffers are sized for
|
||||||
|
static bool s_fading;
|
||||||
|
|
||||||
|
// DDP: pixels sent by a controller. While they keep arriving they replace the pattern.
|
||||||
|
#define DDP_TIMEOUT_MS 2000
|
||||||
|
static uint8_t s_ddp[FX_LEDS_MAX * 3];
|
||||||
|
static uint32_t s_ddp_last_ms; // 0: never
|
||||||
|
static bool s_ddp_was;
|
||||||
|
static uint32_t s_ddp_packets;
|
||||||
|
static uint32_t s_ddp_frames; // frames completed (PUSH seen)
|
||||||
|
static uint32_t s_ddp_sum; // checksum of the last completed frame
|
||||||
|
static uint32_t s_ddp_log_ms; // last serial summary
|
||||||
|
static uint8_t s_max_brightness = 255;
|
||||||
|
static uint8_t s_global = 255;
|
||||||
|
static uint32_t s_fade_t0;
|
||||||
|
static uint32_t s_fade_ms;
|
||||||
|
|
||||||
|
int patterns_count(void)
|
||||||
{
|
{
|
||||||
return (m >= 0 && m < PM_COUNT) ? s_names[m] : "";
|
return PATTERN_COUNT;
|
||||||
}
|
}
|
||||||
|
|
||||||
pattern_mode_t patterns_mode_from_name(const char *name)
|
const char *patterns_name(int i)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < PM_COUNT; i++) {
|
return (i >= 0 && i < PATTERN_COUNT) ? kPatterns[i].name : "";
|
||||||
if (strcmp(name, s_names[i]) == 0) {
|
}
|
||||||
return (pattern_mode_t)i;
|
|
||||||
|
int patterns_index(const char *name)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < PATTERN_COUNT; i++) {
|
||||||
|
if (strcmp(name, kPatterns[i].name) == 0) {
|
||||||
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return PM_COUNT;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void patterns_set(const pattern_params_t &p)
|
void preset_defaults(preset_t *p, int pattern)
|
||||||
{
|
{
|
||||||
pattern_params_t q = p;
|
memset(p, 0, sizeof(*p));
|
||||||
if (q.speed_ms < 10) q.speed_ms = 10;
|
p->pattern = pattern;
|
||||||
if (q.speed_ms > 10000) q.speed_ms = 10000;
|
p->ncol = 0;
|
||||||
if (q.mode >= PM_COUNT) q.mode = PM_SOLID;
|
p->delay = 100;
|
||||||
|
p->brightness = 127;
|
||||||
|
p->autorun = true;
|
||||||
|
p->reverse = false;
|
||||||
|
p->fade = 0;
|
||||||
|
p->beat_time = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep a preset within the ranges the patterns expect
|
||||||
|
static void sanitize(preset_t *p)
|
||||||
|
{
|
||||||
|
if (p->pattern < 0 || p->pattern >= PATTERN_COUNT) {
|
||||||
|
p->pattern = 0;
|
||||||
|
}
|
||||||
|
if (p->delay < 10) p->delay = 10;
|
||||||
|
if (p->delay > 10000) p->delay = 10000;
|
||||||
|
if (p->ncol < 0) p->ncol = 0;
|
||||||
|
if (p->ncol > PATTERN_MAX_COLOURS) p->ncol = PATTERN_MAX_COLOURS;
|
||||||
|
if (p->fade > 10000) p->fade = 10000;
|
||||||
|
for (int i = 1; i <= 6; i++) {
|
||||||
|
if (p->n[i] < 0) p->n[i] = 0;
|
||||||
|
if (p->n[i] > 100000) p->n[i] = 100000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Live update: a change of pattern restarts it (with a crossfade if the preset asks); other changes
|
||||||
|
// take effect straight away, keeping the pattern's place
|
||||||
|
void patterns_set(const preset_t &p)
|
||||||
|
{
|
||||||
|
preset_t q = p;
|
||||||
|
sanitize(&q);
|
||||||
xSemaphoreTake(s_lock, portMAX_DELAY);
|
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||||
s_p = q;
|
bool changed = q.pattern != s_cur.pattern;
|
||||||
|
s_cur = q;
|
||||||
|
if (changed) {
|
||||||
|
s_reset_pending = true;
|
||||||
|
}
|
||||||
xSemaphoreGive(s_lock);
|
xSemaphoreGive(s_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
pattern_params_t patterns_get(void)
|
// Select a preset: always restarts the pattern, crossfading in from the frame on the strip
|
||||||
|
void patterns_select(const preset_t &p)
|
||||||
|
{
|
||||||
|
preset_t q = p;
|
||||||
|
sanitize(&q);
|
||||||
|
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||||
|
s_cur = q;
|
||||||
|
s_reset_pending = true;
|
||||||
|
xSemaphoreGive(s_lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void patterns_set_global(uint8_t level)
|
||||||
{
|
{
|
||||||
xSemaphoreTake(s_lock, portMAX_DELAY);
|
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||||
pattern_params_t p = s_p;
|
s_global = level;
|
||||||
xSemaphoreGive(s_lock);
|
xSemaphoreGive(s_lock);
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Linear blend from a (k=0) to b (k=255)
|
uint8_t patterns_get_global(void)
|
||||||
static void lerp(const uint8_t *a, const uint8_t *b, uint32_t k, uint8_t *out)
|
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 3; i++) {
|
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||||
out[i] = (uint8_t)(a[i] + ((int)b[i] - a[i]) * (int)k / 255);
|
uint8_t v = s_global;
|
||||||
}
|
xSemaphoreGive(s_lock);
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw one frame of the pattern at time t (ms since the start of the pattern)
|
void patterns_get(preset_t *out)
|
||||||
static void render(const pattern_params_t &p, uint32_t t, WS2812 &s)
|
|
||||||
{
|
{
|
||||||
const size_t n = s.size();
|
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||||
const uint32_t sp = p.speed_ms;
|
*out = s_cur;
|
||||||
uint8_t col[3];
|
xSemaphoreGive(s_lock);
|
||||||
|
}
|
||||||
|
|
||||||
switch (p.mode) {
|
static uint32_t now_ms(void)
|
||||||
case PM_SOLID:
|
{
|
||||||
s.fill(p.c1[0], p.c1[1], p.c1[2]);
|
return (uint32_t)(esp_timer_get_time() / 1000);
|
||||||
break;
|
}
|
||||||
|
|
||||||
case PM_RAINBOW: {
|
// Show the shadow frame, crossfading from the previous frame while a fade runs
|
||||||
uint32_t off = (t / sp) % 360;
|
static void output(uint32_t now)
|
||||||
for (size_t i = 0; i < n; i++) {
|
{
|
||||||
uint32_t r, g, b;
|
int s_scale = s_global * s_max_brightness / 255; // global brightness under the ceiling
|
||||||
WS2812::hsv2rgb((i * 360 / n + off) % 360, 100, 100, &r, &g, &b);
|
int k = -1;
|
||||||
s.set(i, r, g, b);
|
if (s_fading) {
|
||||||
}
|
uint32_t el = now - s_fade_t0;
|
||||||
break;
|
if (el >= s_fade_ms) {
|
||||||
}
|
s_fading = false;
|
||||||
|
|
||||||
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 {
|
} else {
|
||||||
s.fill(p.c2[0], p.c2[1], p.c2[2]);
|
k = (int)((el * 256) / s_fade_ms);
|
||||||
}
|
}
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
for (int i = 0; i < s_fx.n; i++) {
|
||||||
case PM_TRANSITION: {
|
rgb c = s_fx.px[i];
|
||||||
// c1 -> c2 over speed_ms, then back again
|
c.r = c.r * s_scale / 255;
|
||||||
uint32_t period = 2 * sp;
|
c.g = c.g * s_scale / 255;
|
||||||
uint32_t ph = t % period;
|
c.b = c.b * s_scale / 255;
|
||||||
uint32_t k = ph < sp ? ph * 255 / sp : (period - ph) * 255 / sp;
|
if (k >= 0) {
|
||||||
lerp(p.c1, p.c2, k, col);
|
rgb a = s_from[i];
|
||||||
s.fill(col[0], col[1], col[2]);
|
c.r = (a.r * (256 - k) + c.r * k) >> 8;
|
||||||
break;
|
c.g = (a.g * (256 - k) + c.g * k) >> 8;
|
||||||
}
|
c.b = (a.b * (256 - k) + c.b * k) >> 8;
|
||||||
|
|
||||||
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;
|
s_strip->set(i, c.r, c.g, c.b);
|
||||||
|
s_shown[i] = c;
|
||||||
}
|
}
|
||||||
|
esp_err_t err = s_strip->write();
|
||||||
default:
|
if (err != ESP_OK) {
|
||||||
break;
|
ESP_LOGW(TAG, "LED write failed: %s", esp_err_to_name(err));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- per-LED buffers --------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool frame_alloc(int n)
|
||||||
|
{
|
||||||
|
if (n < 1) n = 1;
|
||||||
|
if (n > FX_LEDS_MAX) n = FX_LEDS_MAX;
|
||||||
|
void *fresh[12] = {
|
||||||
|
calloc(n, sizeof(rgb)), calloc(n, sizeof(rgb)), calloc(n, sizeof(rgb)),
|
||||||
|
calloc(n, sizeof(int)), calloc(n, sizeof(float)), calloc(n, sizeof(int)),
|
||||||
|
calloc(n, 1), calloc(n, 1),
|
||||||
|
calloc(n, sizeof(int)), calloc(n, sizeof(int)),
|
||||||
|
calloc(n, sizeof(int)), calloc(n, sizeof(int)),
|
||||||
|
};
|
||||||
|
for (int k = 0; k < 12; k++) {
|
||||||
|
if (!fresh[k]) {
|
||||||
|
for (int j = 0; j < 12; j++) free(fresh[j]);
|
||||||
|
ESP_LOGE(TAG, "Out of memory for %d LEDs", n);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(s_fx.px);
|
||||||
|
free(s_shown);
|
||||||
|
free(s_from);
|
||||||
|
free(g_st.glow);
|
||||||
|
free(g_st.m);
|
||||||
|
free(g_st.shade);
|
||||||
|
free(g_st.on);
|
||||||
|
free(g_st.next_on);
|
||||||
|
free(g_st.col_i);
|
||||||
|
free(g_st.next_ci);
|
||||||
|
free(g_st.light);
|
||||||
|
free(g_st.dark);
|
||||||
|
s_fx.px = (rgb *)fresh[0];
|
||||||
|
s_shown = (rgb *)fresh[1];
|
||||||
|
s_from = (rgb *)fresh[2];
|
||||||
|
g_st.glow = (int *)fresh[3];
|
||||||
|
g_st.m = (float *)fresh[4];
|
||||||
|
g_st.shade = (int *)fresh[5];
|
||||||
|
g_st.on = (uint8_t *)fresh[6];
|
||||||
|
g_st.next_on = (uint8_t *)fresh[7];
|
||||||
|
g_st.col_i = (int *)fresh[8];
|
||||||
|
g_st.next_ci = (int *)fresh[9];
|
||||||
|
g_st.light = (int *)fresh[10];
|
||||||
|
g_st.dark = (int *)fresh[11];
|
||||||
|
s_cap = n;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void st_reset(void)
|
||||||
|
{
|
||||||
|
int *glow = g_st.glow, *shade = g_st.shade, *col_i = g_st.col_i, *next_ci = g_st.next_ci;
|
||||||
|
int *light = g_st.light, *dark = g_st.dark;
|
||||||
|
float *m = g_st.m;
|
||||||
|
uint8_t *on = g_st.on, *next_on = g_st.next_on;
|
||||||
|
memset(&g_st, 0, sizeof(g_st));
|
||||||
|
g_st.glow = glow;
|
||||||
|
g_st.shade = shade;
|
||||||
|
g_st.col_i = col_i;
|
||||||
|
g_st.next_ci = next_ci;
|
||||||
|
g_st.light = light;
|
||||||
|
g_st.dark = dark;
|
||||||
|
g_st.m = m;
|
||||||
|
g_st.on = on;
|
||||||
|
g_st.next_on = next_on;
|
||||||
|
memset(glow, 0, s_cap * sizeof(int));
|
||||||
|
memset(shade, 0, s_cap * sizeof(int));
|
||||||
|
memset(col_i, 0, s_cap * sizeof(int));
|
||||||
|
memset(next_ci, 0, s_cap * sizeof(int));
|
||||||
|
memset(light, 0, s_cap * sizeof(int));
|
||||||
|
memset(dark, 0, s_cap * sizeof(int));
|
||||||
|
memset(m, 0, s_cap * sizeof(float));
|
||||||
|
memset(on, 0, s_cap);
|
||||||
|
memset(next_on, 0, s_cap);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The whole frame (pattern and output) runs under s_lock, so the buffers can't be replaced mid-frame
|
||||||
static void patterns_task(void *arg)
|
static void patterns_task(void *arg)
|
||||||
{
|
{
|
||||||
(void)arg;
|
(void)arg;
|
||||||
const uint32_t t0 = (uint32_t)(esp_timer_get_time() / 1000);
|
s_fx.pr = &s_run;
|
||||||
while (true) {
|
while (true) {
|
||||||
pattern_params_t p = patterns_get();
|
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||||
uint32_t now = (uint32_t)(esp_timer_get_time() / 1000);
|
bool reset = s_reset_pending;
|
||||||
s_strip->setBrightness(p.brightness);
|
s_reset_pending = false;
|
||||||
render(p, now - t0, *s_strip);
|
s_run = s_cur;
|
||||||
esp_err_t err = s_strip->write();
|
uint32_t now = now_ms();
|
||||||
if (err != ESP_OK) {
|
bool ddp_on = s_ddp_last_ms != 0 && now - s_ddp_last_ms < DDP_TIMEOUT_MS;
|
||||||
ESP_LOGW(TAG, "LED write failed: %s", esp_err_to_name(err));
|
if (!ddp_on && s_ddp_was) {
|
||||||
|
reset = true; // the pattern starts again when the pixels stop
|
||||||
}
|
}
|
||||||
|
s_ddp_was = ddp_on;
|
||||||
|
if (reset) {
|
||||||
|
if (s_run.fade > 0) {
|
||||||
|
memcpy(s_from, s_shown, s_fx.n * sizeof(rgb));
|
||||||
|
s_fading = true;
|
||||||
|
s_fade_t0 = now;
|
||||||
|
s_fade_ms = s_run.fade;
|
||||||
|
} else {
|
||||||
|
s_fading = false;
|
||||||
|
}
|
||||||
|
st_reset();
|
||||||
|
g_st.t0 = now;
|
||||||
|
}
|
||||||
|
s_fx.now = now;
|
||||||
|
s_fx.t = now - g_st.t0;
|
||||||
|
if (ddp_on) {
|
||||||
|
for (int i = 0; i < s_fx.n; i++) {
|
||||||
|
s_fx.px[i] = { s_ddp[3 * i], s_ddp[3 * i + 1], s_ddp[3 * i + 2] };
|
||||||
|
}
|
||||||
|
} else if (!g_st.done) {
|
||||||
|
if (kPatterns[s_run.pattern].fn(s_fx, g_st)) {
|
||||||
|
g_st.done = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output(now);
|
||||||
|
xSemaphoreGive(s_lock);
|
||||||
vTaskDelay(pdMS_TO_TICKS(FRAME_MS));
|
vTaskDelay(pdMS_TO_TICKS(FRAME_MS));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void patterns_start(void)
|
static int clamp_leds(int n)
|
||||||
|
{
|
||||||
|
return n < 1 ? 1 : (n > FX_LEDS_MAX ? FX_LEDS_MAX : n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void patterns_start(const hw_cfg_t &hw)
|
||||||
{
|
{
|
||||||
s_lock = xSemaphoreCreateMutex();
|
s_lock = xSemaphoreCreateMutex();
|
||||||
s_strip = new WS2812(RMT_LED_STRIP_GPIO_NUM, EXAMPLE_LED_NUMBERS);
|
preset_defaults(&s_cur, 0);
|
||||||
xTaskCreate(patterns_task, "patterns", 4096, NULL, 5, NULL);
|
preset_defaults(&s_run, 0);
|
||||||
ESP_LOGI(TAG, "Started %d LEDs on GPIO %d", EXAMPLE_LED_NUMBERS, (int)RMT_LED_STRIP_GPIO_NUM);
|
s_cur.pattern = patterns_index("chase");
|
||||||
|
s_cur.delay = 1000;
|
||||||
|
s_cur.brightness = 16;
|
||||||
|
s_cur.col[0][0] = 255;
|
||||||
|
s_reset_pending = true;
|
||||||
|
|
||||||
|
int n = clamp_leds(hw.num_leds);
|
||||||
|
if (!frame_alloc(n)) {
|
||||||
|
ESP_ERROR_CHECK(ESP_ERR_NO_MEM);
|
||||||
|
}
|
||||||
|
s_fx.n = n;
|
||||||
|
s_max_brightness = hw.max_brightness;
|
||||||
|
s_global = hw.global_brightness;
|
||||||
|
s_strip = new WS2812((gpio_num_t)hw.pin, n);
|
||||||
|
s_strip->setOrder(hw.order);
|
||||||
|
s_strip->setBrightness(255); // brightness is applied per colour by the patterns
|
||||||
|
xTaskCreate(patterns_task, "patterns", 6144, NULL, 5, NULL);
|
||||||
|
ESP_LOGI(TAG, "Started %d LEDs on GPIO %d (%s), %d patterns", n, hw.pin, hw.order, PATTERN_COUNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Change the strip: count, byte order and brightness ceiling. The buffers and the driver are replaced
|
||||||
|
// under the lock the frame runs under; on out-of-memory the old strip is kept.
|
||||||
|
esp_err_t patterns_set_hardware(const hw_cfg_t &hw)
|
||||||
|
{
|
||||||
|
int n = clamp_leds(hw.num_leds);
|
||||||
|
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||||
|
if (!frame_alloc(n)) {
|
||||||
|
xSemaphoreGive(s_lock);
|
||||||
|
return ESP_ERR_NO_MEM;
|
||||||
|
}
|
||||||
|
s_fx.n = n;
|
||||||
|
delete s_strip;
|
||||||
|
s_strip = new WS2812((gpio_num_t)hw.pin, n);
|
||||||
|
s_strip->setOrder(hw.order);
|
||||||
|
s_strip->setBrightness(255);
|
||||||
|
s_max_brightness = hw.max_brightness;
|
||||||
|
s_fading = false;
|
||||||
|
s_reset_pending = true;
|
||||||
|
xSemaphoreGive(s_lock);
|
||||||
|
ESP_LOGI(TAG, "LED strip: %d LEDs on GPIO %d, %s, max brightness %u, free heap %u", n, hw.pin, hw.order,
|
||||||
|
(unsigned)hw.max_brightness, (unsigned)esp_get_free_heap_size());
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- DDP ---------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void patterns_ddp_write(uint32_t offset, const uint8_t *data, size_t len, bool push)
|
||||||
|
{
|
||||||
|
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||||
|
size_t cap = (size_t)s_fx.n * 3;
|
||||||
|
if (offset < cap) {
|
||||||
|
if (offset + len > cap) {
|
||||||
|
len = cap - offset;
|
||||||
|
}
|
||||||
|
memcpy(s_ddp + offset, data, len);
|
||||||
|
}
|
||||||
|
uint32_t now = now_ms();
|
||||||
|
s_ddp_last_ms = now | 1; // never 0
|
||||||
|
s_ddp_packets++;
|
||||||
|
if (push) {
|
||||||
|
s_ddp_frames++;
|
||||||
|
uint32_t sum = 0;
|
||||||
|
for (size_t i = 0; i < cap; i++) {
|
||||||
|
sum += s_ddp[i];
|
||||||
|
}
|
||||||
|
s_ddp_sum = sum;
|
||||||
|
// Summary on the serial log, at most twice a second, so the test can check what was shown
|
||||||
|
if (now - s_ddp_log_ms >= 500) {
|
||||||
|
s_ddp_log_ms = now;
|
||||||
|
ESP_LOGI(TAG, "DDP frames=%u packets=%u sum=%u led0=%u,%u,%u led_last=%u,%u,%u",
|
||||||
|
(unsigned)s_ddp_frames, (unsigned)s_ddp_packets, (unsigned)s_ddp_sum,
|
||||||
|
s_ddp[0], s_ddp[1], s_ddp[2],
|
||||||
|
s_ddp[cap - 3], s_ddp[cap - 2], s_ddp[cap - 1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xSemaphoreGive(s_lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void patterns_ddp_status(bool *active, uint32_t *packets)
|
||||||
|
{
|
||||||
|
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||||
|
*active = s_ddp_last_ms != 0 && now_ms() - s_ddp_last_ms < DDP_TIMEOUT_MS;
|
||||||
|
*packets = s_ddp_packets;
|
||||||
|
xSemaphoreGive(s_lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
void patterns_shown_pixel(int i, uint8_t out[3])
|
||||||
|
{
|
||||||
|
xSemaphoreTake(s_lock, portMAX_DELAY);
|
||||||
|
if (i >= 0 && i < s_fx.n) {
|
||||||
|
out[0] = (uint8_t)s_shown[i].r;
|
||||||
|
out[1] = (uint8_t)s_shown[i].g;
|
||||||
|
out[2] = (uint8_t)s_shown[i].b;
|
||||||
|
}
|
||||||
|
xSemaphoreGive(s_lock);
|
||||||
|
}
|
||||||
|
|
||||||
+54
-28
@@ -3,40 +3,66 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
// Pattern names (also the strings the web UI uses). Order matters: the index is the mode id.
|
#include "esp_err.h"
|
||||||
enum pattern_mode_t {
|
|
||||||
PM_SOLID,
|
#define PATTERN_MAX_COLOURS 8
|
||||||
PM_RAINBOW,
|
|
||||||
PM_CHASE,
|
// Preset fields follow the MicroPython presets: pattern, colours, background, delay (d), brightness (b),
|
||||||
PM_BREATHE,
|
// auto (a), reverse, fade, beat_time (tb) and the pattern-specific numbers n1..n6 (n6 is the style).
|
||||||
PM_BLINK,
|
struct preset_t {
|
||||||
PM_PULSE,
|
char name[32]; // "" for the live settings
|
||||||
PM_TRANSITION,
|
int pattern; // index into the pattern table
|
||||||
PM_CIRCLE,
|
int ncol; // 0 uses each pattern's own default colours
|
||||||
PM_COUNT,
|
uint8_t col[PATTERN_MAX_COLOURS][3];
|
||||||
|
uint8_t bg[3]; // background (d off colour)
|
||||||
|
uint32_t delay; // d, ms
|
||||||
|
uint8_t brightness; // b, 0-255
|
||||||
|
bool autorun; // a: keep running; false runs one cycle and holds
|
||||||
|
bool reverse; // run the other way along the strip
|
||||||
|
uint32_t fade; // crossfade in from the previous frame, ms
|
||||||
|
bool beat_time; // tb: durations are quarter beats (500 ms beat here)
|
||||||
|
int n[7]; // n[1]..n[6]
|
||||||
};
|
};
|
||||||
|
|
||||||
struct pattern_params_t {
|
/** @brief Start the LED strip and the pattern task, with the strip settings. */
|
||||||
pattern_mode_t mode;
|
void patterns_start(const struct hw_cfg_t &hw);
|
||||||
uint8_t c1[3]; // main colour (ignored by rainbow and chase)
|
|
||||||
uint8_t c2[3]; // background / second colour
|
|
||||||
uint32_t speed_ms; // one step of the pattern, 10-10000 ms
|
|
||||||
uint8_t brightness; // 0-255
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @brief Start the LED strip and the pattern task. */
|
/** @brief Apply new strip settings (count, colour order, brightness ceiling) while running. */
|
||||||
void patterns_start(void);
|
esp_err_t patterns_set_hardware(const struct hw_cfg_t &hw);
|
||||||
|
|
||||||
/** @brief Replace the running pattern. Values are clamped to valid ranges. */
|
/** @brief Live update of the preset on the strip. A new pattern restarts; other changes keep its place. */
|
||||||
void patterns_set(const pattern_params_t &p);
|
void patterns_set(const preset_t &p);
|
||||||
|
|
||||||
/** @brief Copy of the running pattern. */
|
/** @brief Select a preset: always restarts the pattern, crossfading from the frame on the strip. */
|
||||||
pattern_params_t patterns_get(void);
|
void patterns_select(const preset_t &p);
|
||||||
|
|
||||||
/** @brief Name of a mode, e.g. "rainbow". */
|
/** @brief DDP pixel data for the strip: byte offset into the RGB buffer, data, length. Replaces the pattern. */
|
||||||
const char *patterns_mode_name(pattern_mode_t m);
|
void patterns_ddp_write(uint32_t offset, const uint8_t *data, size_t len, bool push);
|
||||||
|
|
||||||
/** @brief Mode for a name, or PM_COUNT if unknown. */
|
/** @brief Whether DDP pixels are arriving, and how many packets have come in. */
|
||||||
pattern_mode_t patterns_mode_from_name(const char *name);
|
void patterns_ddp_status(bool *active, uint32_t *packets);
|
||||||
|
|
||||||
|
/** @brief The colour last shown on LED i, as RGB. */
|
||||||
|
void patterns_shown_pixel(int i, uint8_t out[3]);
|
||||||
|
|
||||||
|
/** @brief Live brightness for every pattern, 0-255 (applied under the max brightness ceiling). */
|
||||||
|
void patterns_set_global(uint8_t level);
|
||||||
|
uint8_t patterns_get_global(void);
|
||||||
|
|
||||||
|
/** @brief Copy of the preset being shown. */
|
||||||
|
void patterns_get(preset_t *out);
|
||||||
|
|
||||||
|
/** @brief Number of patterns. */
|
||||||
|
int patterns_count(void);
|
||||||
|
|
||||||
|
/** @brief Name of pattern i, e.g. "chase". */
|
||||||
|
const char *patterns_name(int i);
|
||||||
|
|
||||||
|
/** @brief Index for a pattern name, or -1 if unknown. */
|
||||||
|
int patterns_index(const char *name);
|
||||||
|
|
||||||
|
/** @brief Fill a preset with the defaults for a pattern: colours and timing as the MicroPython presets. */
|
||||||
|
void preset_defaults(preset_t *p, int pattern);
|
||||||
@@ -0,0 +1,391 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*
|
||||||
|
* Named presets, stored in NVS as one JSON list (as the MicroPython presets.json), with a startup preset.
|
||||||
|
*/
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "cJSON.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "nvs.h"
|
||||||
|
#include "presets.h"
|
||||||
|
|
||||||
|
static const char *TAG = "presets";
|
||||||
|
static const char *NS = "presets";
|
||||||
|
|
||||||
|
static preset_t s_list[PRESETS_MAX];
|
||||||
|
static size_t s_count;
|
||||||
|
static char s_startup[32];
|
||||||
|
static char s_selected[32];
|
||||||
|
|
||||||
|
static int find(const char *name)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < s_count; i++) {
|
||||||
|
if (strcmp(s_list[i].name, name) == 0) {
|
||||||
|
return (int)i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static cJSON *rgb_json(const uint8_t *c)
|
||||||
|
{
|
||||||
|
cJSON *a = cJSON_CreateArray();
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
cJSON_AddItemToArray(a, cJSON_CreateNumber(c[i]));
|
||||||
|
}
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool read_rgb(const cJSON *a, uint8_t *out)
|
||||||
|
{
|
||||||
|
if (!cJSON_IsArray(a) || cJSON_GetArraySize(a) != 3) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
cJSON *v = cJSON_GetArrayItem(a, i);
|
||||||
|
if (!cJSON_IsNumber(v)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int x = v->valueint;
|
||||||
|
out[i] = (uint8_t)(x < 0 ? 0 : (x > 255 ? 255 : x));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON *preset_to_json(const preset_t *p)
|
||||||
|
{
|
||||||
|
cJSON *o = cJSON_CreateObject();
|
||||||
|
cJSON_AddStringToObject(o, "name", p->name);
|
||||||
|
cJSON_AddStringToObject(o, "pattern", patterns_name(p->pattern));
|
||||||
|
cJSON *colors = cJSON_AddArrayToObject(o, "colors");
|
||||||
|
for (int i = 0; i < p->ncol; i++) {
|
||||||
|
cJSON_AddItemToArray(colors, rgb_json(p->col[i]));
|
||||||
|
}
|
||||||
|
cJSON_AddItemToObject(o, "background", rgb_json(p->bg));
|
||||||
|
cJSON_AddNumberToObject(o, "delay", p->delay);
|
||||||
|
cJSON_AddNumberToObject(o, "brightness", p->brightness);
|
||||||
|
cJSON_AddBoolToObject(o, "auto", p->autorun);
|
||||||
|
cJSON_AddBoolToObject(o, "reverse", p->reverse);
|
||||||
|
cJSON_AddNumberToObject(o, "fade", p->fade);
|
||||||
|
cJSON_AddBoolToObject(o, "beat_time", p->beat_time);
|
||||||
|
cJSON_AddNumberToObject(o, "n1", p->n[1]);
|
||||||
|
cJSON_AddNumberToObject(o, "n2", p->n[2]);
|
||||||
|
cJSON_AddNumberToObject(o, "n3", p->n[3]);
|
||||||
|
cJSON_AddNumberToObject(o, "n4", p->n[4]);
|
||||||
|
cJSON_AddNumberToObject(o, "mode", p->n[6]); // n6: the pattern's style
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool preset_from_json(const cJSON *o, preset_t *p)
|
||||||
|
{
|
||||||
|
if (!cJSON_IsObject(o)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
cJSON *v = cJSON_GetObjectItem(o, "name");
|
||||||
|
if (cJSON_IsString(v)) {
|
||||||
|
strncpy(p->name, v->valuestring, sizeof(p->name) - 1);
|
||||||
|
p->name[sizeof(p->name) - 1] = '\0';
|
||||||
|
}
|
||||||
|
v = cJSON_GetObjectItem(o, "pattern");
|
||||||
|
if (cJSON_IsString(v)) {
|
||||||
|
int idx = patterns_index(v->valuestring);
|
||||||
|
if (idx < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
p->pattern = idx;
|
||||||
|
}
|
||||||
|
v = cJSON_GetObjectItem(o, "colors");
|
||||||
|
if (cJSON_IsArray(v)) {
|
||||||
|
int n = cJSON_GetArraySize(v);
|
||||||
|
if (n > PATTERN_MAX_COLOURS) n = PATTERN_MAX_COLOURS;
|
||||||
|
p->ncol = 0;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (read_rgb(cJSON_GetArrayItem(v, i), p->col[p->ncol])) {
|
||||||
|
p->ncol++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
read_rgb(cJSON_GetObjectItem(o, "background"), p->bg);
|
||||||
|
v = cJSON_GetObjectItem(o, "delay");
|
||||||
|
if (cJSON_IsNumber(v)) p->delay = (uint32_t)(v->valueint < 0 ? 0 : v->valueint);
|
||||||
|
v = cJSON_GetObjectItem(o, "brightness");
|
||||||
|
if (cJSON_IsNumber(v)) p->brightness = (uint8_t)(v->valueint < 0 ? 0 : (v->valueint > 255 ? 255 : v->valueint));
|
||||||
|
v = cJSON_GetObjectItem(o, "auto");
|
||||||
|
if (cJSON_IsBool(v)) p->autorun = cJSON_IsTrue(v);
|
||||||
|
v = cJSON_GetObjectItem(o, "reverse");
|
||||||
|
if (cJSON_IsBool(v)) p->reverse = cJSON_IsTrue(v);
|
||||||
|
v = cJSON_GetObjectItem(o, "fade");
|
||||||
|
if (cJSON_IsNumber(v)) p->fade = (uint32_t)(v->valueint < 0 ? 0 : v->valueint);
|
||||||
|
v = cJSON_GetObjectItem(o, "beat_time");
|
||||||
|
if (cJSON_IsBool(v)) p->beat_time = cJSON_IsTrue(v);
|
||||||
|
static const char *keys[] = { "n1", "n2", "n3", "n4" };
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
v = cJSON_GetObjectItem(o, keys[i]);
|
||||||
|
if (cJSON_IsNumber(v)) p->n[i + 1] = v->valueint;
|
||||||
|
}
|
||||||
|
v = cJSON_GetObjectItem(o, "mode");
|
||||||
|
if (cJSON_IsNumber(v)) p->n[6] = v->valueint;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the whole list to NVS as one JSON blob
|
||||||
|
static esp_err_t save_list(void)
|
||||||
|
{
|
||||||
|
cJSON *arr = cJSON_CreateArray();
|
||||||
|
for (size_t i = 0; i < s_count; i++) {
|
||||||
|
cJSON_AddItemToArray(arr, preset_to_json(&s_list[i]));
|
||||||
|
}
|
||||||
|
char *s = cJSON_PrintUnformatted(arr);
|
||||||
|
cJSON_Delete(arr);
|
||||||
|
if (!s) {
|
||||||
|
return ESP_ERR_NO_MEM;
|
||||||
|
}
|
||||||
|
nvs_handle_t h;
|
||||||
|
esp_err_t err = nvs_open(NS, NVS_READWRITE, &h);
|
||||||
|
if (err == ESP_OK) {
|
||||||
|
err = nvs_set_blob(h, "list", s, strlen(s));
|
||||||
|
if (err == ESP_OK) err = nvs_set_str(h, "startup", s_startup);
|
||||||
|
if (err == ESP_OK) err = nvs_commit(h);
|
||||||
|
nvs_close(h);
|
||||||
|
}
|
||||||
|
cJSON_free(s);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The presets a new board starts with (added by presets_add_defaults, and on a board that has none saved)
|
||||||
|
static int build_defaults(preset_t *out, int max)
|
||||||
|
{
|
||||||
|
int n = 0;
|
||||||
|
auto add = [&](const char *name, const char *pattern) -> preset_t * {
|
||||||
|
if (n >= max) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
preset_t *p = &out[n++];
|
||||||
|
preset_defaults(p, patterns_index(pattern));
|
||||||
|
strncpy(p->name, name, sizeof(p->name) - 1);
|
||||||
|
p->name[sizeof(p->name) - 1] = '\0';
|
||||||
|
return p;
|
||||||
|
};
|
||||||
|
preset_t *p;
|
||||||
|
if ((p = add("Rainbow", "colour_cycle"))) {
|
||||||
|
p->n[6] = 1; // rainbow wheel
|
||||||
|
p->n[1] = 2;
|
||||||
|
p->delay = 60;
|
||||||
|
p->brightness = 160;
|
||||||
|
}
|
||||||
|
if ((p = add("Chase", "chase"))) {
|
||||||
|
p->ncol = 2;
|
||||||
|
p->col[0][0] = 255; p->col[0][1] = 80; p->col[0][2] = 0;
|
||||||
|
p->col[1][0] = 0; p->col[1][1] = 120; p->col[1][2] = 255;
|
||||||
|
p->n[1] = 4; p->n[2] = 4; p->n[3] = 1; p->n[4] = 1;
|
||||||
|
p->delay = 150;
|
||||||
|
p->brightness = 180;
|
||||||
|
}
|
||||||
|
if ((p = add("Aurora", "aurora"))) {
|
||||||
|
p->n[1] = 3;
|
||||||
|
p->n[2] = 40;
|
||||||
|
p->delay = 40;
|
||||||
|
p->brightness = 150;
|
||||||
|
}
|
||||||
|
if ((p = add("Candle", "candle_glow"))) {
|
||||||
|
p->ncol = 3;
|
||||||
|
p->col[0][0] = 255; p->col[0][1] = 140; p->col[0][2] = 40;
|
||||||
|
p->col[1][0] = 255; p->col[1][1] = 200; p->col[1][2] = 120;
|
||||||
|
p->col[2][0] = 255; p->col[2][1] = 90; p->col[2][2] = 20;
|
||||||
|
p->n[1] = 4; p->n[2] = 3; p->n[3] = 90;
|
||||||
|
p->delay = 120;
|
||||||
|
p->brightness = 200;
|
||||||
|
}
|
||||||
|
if ((p = add("Twinkle", "twinkle"))) {
|
||||||
|
p->ncol = 1;
|
||||||
|
p->col[0][0] = 200; p->col[0][1] = 220; p->col[0][2] = 255;
|
||||||
|
p->bg[2] = 20;
|
||||||
|
p->n[1] = 48; p->n[2] = 128;
|
||||||
|
p->delay = 100;
|
||||||
|
p->brightness = 200;
|
||||||
|
}
|
||||||
|
if ((p = add("Flame", "flame"))) {
|
||||||
|
p->delay = 40;
|
||||||
|
p->brightness = 255;
|
||||||
|
p->n[2] = 2500;
|
||||||
|
}
|
||||||
|
if ((p = add("Sweep", "sweep"))) {
|
||||||
|
p->ncol = 2;
|
||||||
|
p->col[0][0] = 255; p->col[0][1] = 0; p->col[0][2] = 120;
|
||||||
|
p->col[1][0] = 0; p->col[1][1] = 200; p->col[1][2] = 255;
|
||||||
|
p->n[1] = 90; p->n[2] = 200; p->n[3] = 200;
|
||||||
|
p->delay = 2000;
|
||||||
|
p->brightness = 200;
|
||||||
|
}
|
||||||
|
if ((p = add("Off", "off"))) {
|
||||||
|
p->brightness = 255;
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the default presets whose names aren't in the list yet. Returns how many were added.
|
||||||
|
int presets_add_defaults(void)
|
||||||
|
{
|
||||||
|
preset_t defs[16];
|
||||||
|
int n = build_defaults(defs, 16);
|
||||||
|
int added = 0;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
if (find(defs[i].name) < 0 && s_count < PRESETS_MAX) {
|
||||||
|
s_list[s_count++] = defs[i];
|
||||||
|
added++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (added > 0) {
|
||||||
|
save_list();
|
||||||
|
}
|
||||||
|
return added;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t presets_init(void)
|
||||||
|
{
|
||||||
|
s_count = 0;
|
||||||
|
s_startup[0] = '\0';
|
||||||
|
s_selected[0] = '\0';
|
||||||
|
|
||||||
|
nvs_handle_t h;
|
||||||
|
if (nvs_open(NS, NVS_READONLY, &h) != ESP_OK) {
|
||||||
|
presets_add_defaults(); // a new board
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
size_t len = 0;
|
||||||
|
bool have_list = nvs_get_blob(h, "list", NULL, &len) == ESP_OK;
|
||||||
|
if (have_list && len > 0) {
|
||||||
|
char *buf = (char *)malloc(len + 1);
|
||||||
|
if (buf && nvs_get_blob(h, "list", buf, &len) == ESP_OK) {
|
||||||
|
buf[len] = '\0';
|
||||||
|
cJSON *arr = cJSON_Parse(buf);
|
||||||
|
cJSON *o;
|
||||||
|
cJSON_ArrayForEach(o, arr) {
|
||||||
|
if (s_count >= PRESETS_MAX) break;
|
||||||
|
preset_t p;
|
||||||
|
preset_defaults(&p, 0);
|
||||||
|
if (preset_from_json(o, &p) && p.name[0]) {
|
||||||
|
s_list[s_count++] = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cJSON_Delete(arr);
|
||||||
|
}
|
||||||
|
free(buf);
|
||||||
|
}
|
||||||
|
len = sizeof(s_startup);
|
||||||
|
if (nvs_get_str(h, "startup", s_startup, &len) != ESP_OK) {
|
||||||
|
s_startup[0] = '\0';
|
||||||
|
}
|
||||||
|
nvs_close(h);
|
||||||
|
if (!have_list) {
|
||||||
|
presets_add_defaults(); // nothing saved yet: start with the defaults
|
||||||
|
}
|
||||||
|
ESP_LOGI(TAG, "%u presets loaded", (unsigned)s_count);
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t presets_count(void)
|
||||||
|
{
|
||||||
|
return s_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
const preset_t *presets_at(size_t i)
|
||||||
|
{
|
||||||
|
return i < s_count ? &s_list[i] : NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t presets_save(const preset_t *preset)
|
||||||
|
{
|
||||||
|
int idx = find(preset->name);
|
||||||
|
if (idx < 0) {
|
||||||
|
if (s_count >= PRESETS_MAX) {
|
||||||
|
return ESP_ERR_NO_MEM;
|
||||||
|
}
|
||||||
|
idx = (int)s_count++;
|
||||||
|
}
|
||||||
|
s_list[idx] = *preset;
|
||||||
|
return save_list();
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t presets_set_order(const char *const *names, size_t n)
|
||||||
|
{
|
||||||
|
// Static: this is too big for the HTTP server's task stack, and only that task calls it
|
||||||
|
static preset_t fresh[PRESETS_MAX];
|
||||||
|
bool used[PRESETS_MAX] = { false };
|
||||||
|
size_t m = 0;
|
||||||
|
for (size_t k = 0; k < n && m < s_count; k++) {
|
||||||
|
int idx = find(names[k]);
|
||||||
|
if (idx >= 0 && !used[idx]) {
|
||||||
|
used[idx] = true;
|
||||||
|
fresh[m++] = s_list[idx];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < s_count; i++) {
|
||||||
|
if (!used[i]) {
|
||||||
|
fresh[m++] = s_list[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
memcpy(s_list, fresh, m * sizeof(preset_t));
|
||||||
|
s_count = m;
|
||||||
|
return save_list();
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t presets_delete(const char *name)
|
||||||
|
{
|
||||||
|
int idx = find(name);
|
||||||
|
if (idx < 0) {
|
||||||
|
return ESP_ERR_NOT_FOUND;
|
||||||
|
}
|
||||||
|
for (size_t i = (size_t)idx; i + 1 < s_count; i++) {
|
||||||
|
s_list[i] = s_list[i + 1];
|
||||||
|
}
|
||||||
|
s_count--;
|
||||||
|
if (strcmp(s_startup, name) == 0) {
|
||||||
|
s_startup[0] = '\0';
|
||||||
|
}
|
||||||
|
if (strcmp(s_selected, name) == 0) {
|
||||||
|
s_selected[0] = '\0';
|
||||||
|
}
|
||||||
|
return save_list();
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t presets_select(const char *name)
|
||||||
|
{
|
||||||
|
int idx = find(name);
|
||||||
|
if (idx < 0) {
|
||||||
|
return ESP_ERR_NOT_FOUND;
|
||||||
|
}
|
||||||
|
patterns_select(s_list[idx]);
|
||||||
|
strncpy(s_selected, name, sizeof(s_selected) - 1);
|
||||||
|
s_selected[sizeof(s_selected) - 1] = '\0';
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *presets_selected(void)
|
||||||
|
{
|
||||||
|
return s_selected;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t presets_set_startup(const char *name)
|
||||||
|
{
|
||||||
|
if (name[0] && find(name) < 0) {
|
||||||
|
return ESP_ERR_NOT_FOUND;
|
||||||
|
}
|
||||||
|
strncpy(s_startup, name, sizeof(s_startup) - 1);
|
||||||
|
s_startup[sizeof(s_startup) - 1] = '\0';
|
||||||
|
return save_list();
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *presets_startup(void)
|
||||||
|
{
|
||||||
|
return s_startup;
|
||||||
|
}
|
||||||
|
|
||||||
|
void presets_apply_startup(void)
|
||||||
|
{
|
||||||
|
if (s_startup[0] && presets_select(s_startup) == ESP_OK) {
|
||||||
|
ESP_LOGI(TAG, "Startup preset: %s", s_startup);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
#include "cJSON.h"
|
||||||
|
#include "esp_err.h"
|
||||||
|
#include "patterns.h"
|
||||||
|
|
||||||
|
#define PRESETS_MAX 32
|
||||||
|
|
||||||
|
/** @brief Load the saved presets and the startup name from NVS. Call after NVS is initialised. */
|
||||||
|
esp_err_t presets_init(void);
|
||||||
|
|
||||||
|
/** @brief Add the default presets whose names are not in the list yet. Returns how many were added. */
|
||||||
|
int presets_add_defaults(void);
|
||||||
|
|
||||||
|
/** @brief Number of presets. */
|
||||||
|
size_t presets_count(void);
|
||||||
|
|
||||||
|
/** @brief Preset at index i (0 <= i < presets_count()). */
|
||||||
|
const preset_t *presets_at(size_t i);
|
||||||
|
|
||||||
|
/** @brief Create a preset, or update the one with the same name. ESP_ERR_NO_MEM when full. */
|
||||||
|
esp_err_t presets_save(const preset_t *preset);
|
||||||
|
|
||||||
|
/** @brief Put the presets in this order. Names not given keep their place at the end. */
|
||||||
|
esp_err_t presets_set_order(const char *const *names, size_t n);
|
||||||
|
|
||||||
|
/** @brief Delete a preset by name. Clears the startup name if it was that preset. */
|
||||||
|
esp_err_t presets_delete(const char *name);
|
||||||
|
|
||||||
|
/** @brief Select a preset: crossfades in and restarts its pattern. ESP_ERR_NOT_FOUND if no such name. */
|
||||||
|
esp_err_t presets_select(const char *name);
|
||||||
|
|
||||||
|
/** @brief Name of the last preset selected since boot, or "". */
|
||||||
|
const char *presets_selected(void);
|
||||||
|
|
||||||
|
/** @brief Set the preset applied at boot. An empty name clears it. */
|
||||||
|
esp_err_t presets_set_startup(const char *name);
|
||||||
|
|
||||||
|
/** @brief Name of the startup preset, or "". */
|
||||||
|
const char *presets_startup(void);
|
||||||
|
|
||||||
|
/** @brief Apply the startup preset, if one is set and still exists. */
|
||||||
|
void presets_apply_startup(void);
|
||||||
|
|
||||||
|
/** @brief A preset as JSON, with the MicroPython field names. The caller frees it with cJSON_Delete. */
|
||||||
|
cJSON *preset_to_json(const preset_t *p);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Apply the fields present in JSON onto p; missing fields keep their value.
|
||||||
|
* @return false if a field is malformed (an unknown pattern name, for example).
|
||||||
|
*/
|
||||||
|
bool preset_from_json(const cJSON *o, preset_t *p);
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
#include "nvs.h"
|
#include "nvs.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
|
||||||
@@ -40,3 +42,106 @@ esp_err_t settings_save_wifi(const wifi_creds_t *c)
|
|||||||
nvs_close(h);
|
nvs_close(h);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
esp_err_t settings_load_hw(hw_cfg_t *c)
|
||||||
|
{
|
||||||
|
strncpy(c->order, "grb", sizeof(c->order));
|
||||||
|
c->max_brightness = 255;
|
||||||
|
c->num_leds = 119;
|
||||||
|
c->pin = 10;
|
||||||
|
c->global_brightness = 255;
|
||||||
|
nvs_handle_t h;
|
||||||
|
if (nvs_open(NS, NVS_READONLY, &h) != ESP_OK) {
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
size_t len = sizeof(c->order);
|
||||||
|
if (nvs_get_str(h, "rgb_order", c->order, &len) != ESP_OK) {
|
||||||
|
strncpy(c->order, "grb", sizeof(c->order));
|
||||||
|
}
|
||||||
|
uint8_t mb;
|
||||||
|
if (nvs_get_u8(h, "max_bright", &mb) == ESP_OK) {
|
||||||
|
c->max_brightness = mb;
|
||||||
|
}
|
||||||
|
int32_t n;
|
||||||
|
if (nvs_get_i32(h, "num_leds", &n) == ESP_OK) {
|
||||||
|
c->num_leds = (int)n;
|
||||||
|
}
|
||||||
|
if (nvs_get_i32(h, "pin", &n) == ESP_OK) {
|
||||||
|
c->pin = (int)n;
|
||||||
|
}
|
||||||
|
if (nvs_get_u8(h, "global_bright", &mb) == ESP_OK) {
|
||||||
|
c->global_brightness = mb;
|
||||||
|
}
|
||||||
|
nvs_close(h);
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t settings_save_hw(const hw_cfg_t *c)
|
||||||
|
{
|
||||||
|
nvs_handle_t h;
|
||||||
|
esp_err_t err = nvs_open(NS, NVS_READWRITE, &h);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
err = nvs_set_str(h, "rgb_order", c->order);
|
||||||
|
if (err == ESP_OK) err = nvs_set_u8(h, "max_bright", c->max_brightness);
|
||||||
|
if (err == ESP_OK) err = nvs_set_i32(h, "num_leds", c->num_leds);
|
||||||
|
if (err == ESP_OK) err = nvs_set_i32(h, "pin", c->pin);
|
||||||
|
if (err == ESP_OK) err = nvs_set_u8(h, "global_bright", c->global_brightness);
|
||||||
|
if (err == ESP_OK) err = nvs_commit(h);
|
||||||
|
nvs_close(h);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read a string key; use def when it is missing or unreadable
|
||||||
|
static void get_str(nvs_handle_t h, const char *key, char *buf, size_t len, const char *def)
|
||||||
|
{
|
||||||
|
size_t n = len;
|
||||||
|
if (nvs_get_str(h, key, buf, &n) != ESP_OK) {
|
||||||
|
strncpy(buf, def, len - 1);
|
||||||
|
buf[len - 1] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t settings_load_net(net_cfg_t *c)
|
||||||
|
{
|
||||||
|
memset(c, 0, sizeof(*c));
|
||||||
|
nvs_handle_t h;
|
||||||
|
esp_err_t err = nvs_open(NS, NVS_READONLY, &h);
|
||||||
|
if (err == ESP_ERR_NVS_NOT_FOUND) {
|
||||||
|
// Nothing saved yet: defaults
|
||||||
|
strncpy(c->hostname, "led", sizeof(c->hostname) - 1);
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
get_str(h, "hostname", c->hostname, sizeof(c->hostname), "led");
|
||||||
|
char mode[8];
|
||||||
|
get_str(h, "ip_mode", mode, sizeof(mode), "dhcp");
|
||||||
|
c->use_static = strcmp(mode, "static") == 0;
|
||||||
|
get_str(h, "ip", c->ip, sizeof(c->ip), "");
|
||||||
|
get_str(h, "netmask", c->netmask, sizeof(c->netmask), "");
|
||||||
|
get_str(h, "gateway", c->gateway, sizeof(c->gateway), "");
|
||||||
|
get_str(h, "dns", c->dns, sizeof(c->dns), "");
|
||||||
|
nvs_close(h);
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t settings_save_net(const net_cfg_t *c)
|
||||||
|
{
|
||||||
|
nvs_handle_t h;
|
||||||
|
esp_err_t err = nvs_open(NS, NVS_READWRITE, &h);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
err = nvs_set_str(h, "hostname", c->hostname);
|
||||||
|
if (err == ESP_OK) err = nvs_set_str(h, "ip_mode", c->use_static ? "static" : "dhcp");
|
||||||
|
if (err == ESP_OK) err = nvs_set_str(h, "ip", c->ip);
|
||||||
|
if (err == ESP_OK) err = nvs_set_str(h, "netmask", c->netmask);
|
||||||
|
if (err == ESP_OK) err = nvs_set_str(h, "gateway", c->gateway);
|
||||||
|
if (err == ESP_OK) err = nvs_set_str(h, "dns", c->dns);
|
||||||
|
if (err == ESP_OK) err = nvs_commit(h);
|
||||||
|
nvs_close(h);
|
||||||
|
return err;
|
||||||
|
}
|
||||||
@@ -3,6 +3,8 @@
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
|
|
||||||
struct wifi_creds_t {
|
struct wifi_creds_t {
|
||||||
@@ -10,8 +12,37 @@ struct wifi_creds_t {
|
|||||||
char pass[65];
|
char pass[65];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct net_cfg_t {
|
||||||
|
char hostname[64]; // mDNS name, without ".local"
|
||||||
|
bool use_static; // false: DHCP on the client interface
|
||||||
|
char ip[16]; // static client address, dotted quad
|
||||||
|
char netmask[16];
|
||||||
|
char gateway[16];
|
||||||
|
char dns[16]; // optional; empty uses the gateway
|
||||||
|
};
|
||||||
|
|
||||||
/** @brief Read saved Wi-Fi client credentials. ESP_ERR_NVS_NOT_FOUND if none are saved. */
|
/** @brief Read saved Wi-Fi client credentials. ESP_ERR_NVS_NOT_FOUND if none are saved. */
|
||||||
esp_err_t settings_load_wifi(wifi_creds_t *c);
|
esp_err_t settings_load_wifi(wifi_creds_t *c);
|
||||||
|
|
||||||
/** @brief Save Wi-Fi client credentials to NVS. */
|
/** @brief Save Wi-Fi client credentials to NVS. */
|
||||||
esp_err_t settings_save_wifi(const wifi_creds_t *c);
|
esp_err_t settings_save_wifi(const wifi_creds_t *c);
|
||||||
|
|
||||||
|
struct hw_cfg_t {
|
||||||
|
char order[4]; // colour order of the strip: rgb, rbg, grb, gbr, brg or bgr
|
||||||
|
uint8_t max_brightness; // ceiling for every output colour, 0-255
|
||||||
|
int num_leds; // 1-512
|
||||||
|
int pin; // GPIO for the data line
|
||||||
|
uint8_t global_brightness; // live brightness for all patterns, 0-255 (under max_brightness)
|
||||||
|
};
|
||||||
|
|
||||||
|
/** @brief Read the LED strip settings. Missing keys get their defaults (grb, 255, 119). */
|
||||||
|
esp_err_t settings_load_hw(hw_cfg_t *c);
|
||||||
|
|
||||||
|
/** @brief Save the LED strip settings. */
|
||||||
|
esp_err_t settings_save_hw(const hw_cfg_t *c);
|
||||||
|
|
||||||
|
/** @brief Read network settings. Missing keys get their defaults (hostname "led", DHCP). */
|
||||||
|
esp_err_t settings_load_net(net_cfg_t *c);
|
||||||
|
|
||||||
|
/** @brief Save network settings to NVS. */
|
||||||
|
esp_err_t settings_save_net(const net_cfg_t *c);
|
||||||
+449
-58
@@ -1,14 +1,20 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
#include "cJSON.h"
|
#include "cJSON.h"
|
||||||
#include "esp_http_server.h"
|
#include "esp_http_server.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "esp_netif.h"
|
||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
#include "ota.h"
|
||||||
|
#include "ddp.h"
|
||||||
#include "patterns.h"
|
#include "patterns.h"
|
||||||
|
#include "effects.h"
|
||||||
|
#include "presets.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
#include "web.h"
|
#include "web.h"
|
||||||
|
|
||||||
@@ -24,14 +30,19 @@ extern const char style_css_end[] asm("_binary_style_css_end");
|
|||||||
|
|
||||||
static httpd_handle_t s_server;
|
static httpd_handle_t s_server;
|
||||||
|
|
||||||
#define MAX_BODY 512
|
#define MAX_BODY 1024
|
||||||
#define MAX_SCAN 20
|
#define MAX_SCAN 20
|
||||||
|
|
||||||
static esp_err_t send_embedded(httpd_req_t *req, const char *type, const char *start, const char *end)
|
static esp_err_t send_embedded(httpd_req_t *req, const char *type, const char *start, const char *end)
|
||||||
{
|
{
|
||||||
httpd_resp_set_type(req, type);
|
httpd_resp_set_type(req, type);
|
||||||
httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
|
httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
|
||||||
return httpd_resp_send(req, start, end - start);
|
// EMBED_TXTFILES includes the file's terminating NUL; don't send it
|
||||||
|
size_t len = end - start;
|
||||||
|
if (len > 0 && start[len - 1] == '\0') {
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
return httpd_resp_send(req, start, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t send_json(httpd_req_t *req, cJSON *obj)
|
static esp_err_t send_json(httpd_req_t *req, cJSON *obj)
|
||||||
@@ -74,26 +85,14 @@ static bool read_body(httpd_req_t *req, char *buf, size_t cap)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read_rgb(const cJSON *arr, uint8_t *out)
|
// Read a JSON body; NULL (no reply sent) if missing, too big or not JSON. Caller frees with cJSON_Delete.
|
||||||
|
static cJSON *read_json(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
if (!cJSON_IsArray(arr) || cJSON_GetArraySize(arr) != 3) {
|
char body[MAX_BODY];
|
||||||
return;
|
if (!read_body(req, body, sizeof(body))) {
|
||||||
}
|
return NULL;
|
||||||
for (int i = 0; i < 3; i++) {
|
|
||||||
cJSON *v = cJSON_GetArrayItem(arr, i);
|
|
||||||
if (cJSON_IsNumber(v)) {
|
|
||||||
int x = v->valueint;
|
|
||||||
out[i] = (uint8_t)(x < 0 ? 0 : x > 255 ? 255 : x);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void put_rgb(cJSON *obj, const char *key, const uint8_t *c)
|
|
||||||
{
|
|
||||||
cJSON *a = cJSON_AddArrayToObject(obj, key);
|
|
||||||
for (int i = 0; i < 3; i++) {
|
|
||||||
cJSON_AddItemToArray(a, cJSON_CreateNumber(c[i]));
|
|
||||||
}
|
}
|
||||||
|
return cJSON_Parse(body);
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET / , /app.js , /style.css
|
// GET / , /app.js , /style.css
|
||||||
@@ -112,61 +111,45 @@ static esp_err_t style_css_get(httpd_req_t *req)
|
|||||||
return send_embedded(req, "text/css", style_css_start, style_css_end);
|
return send_embedded(req, "text/css", style_css_start, style_css_end);
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /api/status
|
// GET /api/status : the running preset (same fields as a preset), plus device and network state
|
||||||
static esp_err_t status_get(httpd_req_t *req)
|
static esp_err_t status_get(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
pattern_params_t p = patterns_get();
|
preset_t p;
|
||||||
|
patterns_get(&p);
|
||||||
char ap[16], ip[16];
|
char ap[16], ip[16];
|
||||||
net_ap_ssid(ap, sizeof(ap));
|
net_ap_ssid(ap, sizeof(ap));
|
||||||
bool up = net_sta_status(ip, sizeof(ip));
|
bool up = net_sta_status(ip, sizeof(ip));
|
||||||
|
|
||||||
cJSON *o = cJSON_CreateObject();
|
net_cfg_t n;
|
||||||
|
settings_load_net(&n);
|
||||||
|
cJSON *o = preset_to_json(&p);
|
||||||
cJSON_AddStringToObject(o, "ap_ssid", ap);
|
cJSON_AddStringToObject(o, "ap_ssid", ap);
|
||||||
|
cJSON_AddStringToObject(o, "hostname", n.hostname);
|
||||||
cJSON_AddBoolToObject(o, "sta_connected", up);
|
cJSON_AddBoolToObject(o, "sta_connected", up);
|
||||||
cJSON_AddStringToObject(o, "sta_ip", ip);
|
cJSON_AddStringToObject(o, "sta_ip", ip);
|
||||||
cJSON_AddStringToObject(o, "mode", patterns_mode_name(p.mode));
|
cJSON_AddStringToObject(o, "selected", presets_selected());
|
||||||
cJSON *modes = cJSON_AddArrayToObject(o, "modes");
|
cJSON_AddNumberToObject(o, "global_brightness", patterns_get_global());
|
||||||
for (int i = 0; i < PM_COUNT; i++) {
|
cJSON *modes = cJSON_AddArrayToObject(o, "patterns");
|
||||||
cJSON_AddItemToArray(modes, cJSON_CreateString(patterns_mode_name((pattern_mode_t)i)));
|
for (int i = 0; i < patterns_count(); i++) {
|
||||||
|
cJSON_AddItemToArray(modes, cJSON_CreateString(patterns_name(i)));
|
||||||
}
|
}
|
||||||
put_rgb(o, "c1", p.c1);
|
|
||||||
put_rgb(o, "c2", p.c2);
|
|
||||||
cJSON_AddNumberToObject(o, "speed_ms", p.speed_ms);
|
|
||||||
cJSON_AddNumberToObject(o, "brightness", p.brightness);
|
|
||||||
return send_json(req, o);
|
return send_json(req, o);
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST /api/pattern {"mode","c1":[r,g,b],"c2":[r,g,b],"speed_ms","brightness"}
|
// POST /api/pattern : change the running settings. Fields missing from the body keep their value.
|
||||||
static esp_err_t pattern_post(httpd_req_t *req)
|
static esp_err_t pattern_post(httpd_req_t *req)
|
||||||
{
|
{
|
||||||
char body[MAX_BODY];
|
cJSON *in = read_json(req);
|
||||||
if (!read_body(req, body, sizeof(body))) {
|
|
||||||
return send_error(req, "400 Bad Request", "empty or too large body");
|
|
||||||
}
|
|
||||||
cJSON *in = cJSON_Parse(body);
|
|
||||||
if (!in) {
|
if (!in) {
|
||||||
return send_error(req, "400 Bad Request", "invalid JSON");
|
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||||
}
|
|
||||||
pattern_params_t p = patterns_get();
|
|
||||||
cJSON *mode = cJSON_GetObjectItem(in, "mode");
|
|
||||||
if (cJSON_IsString(mode)) {
|
|
||||||
pattern_mode_t m = patterns_mode_from_name(mode->valuestring);
|
|
||||||
if (m == PM_COUNT) {
|
|
||||||
cJSON_Delete(in);
|
|
||||||
return send_error(req, "400 Bad Request", "unknown mode");
|
|
||||||
}
|
|
||||||
p.mode = m;
|
|
||||||
}
|
|
||||||
read_rgb(cJSON_GetObjectItem(in, "c1"), p.c1);
|
|
||||||
read_rgb(cJSON_GetObjectItem(in, "c2"), p.c2);
|
|
||||||
cJSON *speed = cJSON_GetObjectItem(in, "speed_ms");
|
|
||||||
if (cJSON_IsNumber(speed)) p.speed_ms = (uint32_t)speed->valueint;
|
|
||||||
cJSON *bright = cJSON_GetObjectItem(in, "brightness");
|
|
||||||
if (cJSON_IsNumber(bright)) {
|
|
||||||
int b = bright->valueint;
|
|
||||||
p.brightness = (uint8_t)(b < 0 ? 0 : b > 255 ? 255 : b);
|
|
||||||
}
|
}
|
||||||
|
preset_t p;
|
||||||
|
patterns_get(&p);
|
||||||
|
bool ok = preset_from_json(in, &p);
|
||||||
cJSON_Delete(in);
|
cJSON_Delete(in);
|
||||||
|
if (!ok) {
|
||||||
|
return send_error(req, "400 Bad Request", "unknown pattern");
|
||||||
|
}
|
||||||
patterns_set(p);
|
patterns_set(p);
|
||||||
return send_json(req, cJSON_CreateObject());
|
return send_json(req, cJSON_CreateObject());
|
||||||
}
|
}
|
||||||
@@ -245,6 +228,399 @@ static esp_err_t wifi_scan_get(httpd_req_t *req)
|
|||||||
return send_json(req, root);
|
return send_json(req, root);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A DNS label: 1-63 letters, digits or hyphens, not starting or ending with a hyphen
|
||||||
|
static bool valid_hostname(const char *s)
|
||||||
|
{
|
||||||
|
size_t n = strlen(s);
|
||||||
|
if (n == 0 || n > 63 || s[0] == '-' || s[n - 1] == '-') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < n; i++) {
|
||||||
|
char c = s[i];
|
||||||
|
bool ok = (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-';
|
||||||
|
if (!ok) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool valid_ipv4(const char *s)
|
||||||
|
{
|
||||||
|
esp_ip4_addr_t a;
|
||||||
|
return s && s[0] && esp_netif_str_to_ip4(s, &a) == ESP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /api/network : mDNS name and client address settings, with the current address
|
||||||
|
static esp_err_t network_get(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
net_cfg_t n;
|
||||||
|
settings_load_net(&n);
|
||||||
|
char ip[16];
|
||||||
|
bool up = net_sta_status(ip, sizeof(ip));
|
||||||
|
cJSON *o = cJSON_CreateObject();
|
||||||
|
cJSON_AddStringToObject(o, "hostname", n.hostname);
|
||||||
|
cJSON_AddStringToObject(o, "mode", n.use_static ? "static" : "dhcp");
|
||||||
|
cJSON_AddStringToObject(o, "ip", n.ip);
|
||||||
|
cJSON_AddStringToObject(o, "netmask", n.netmask);
|
||||||
|
cJSON_AddStringToObject(o, "gateway", n.gateway);
|
||||||
|
cJSON_AddStringToObject(o, "dns", n.dns);
|
||||||
|
cJSON_AddBoolToObject(o, "connected", up);
|
||||||
|
cJSON_AddStringToObject(o, "current_ip", ip);
|
||||||
|
return send_json(req, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /api/network {"hostname","mode":"dhcp"|"static","ip","netmask","gateway","dns"}
|
||||||
|
// Missing fields keep their current value. Saves, applies and reconnects the client.
|
||||||
|
static esp_err_t network_post(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
cJSON *in = read_json(req);
|
||||||
|
if (!in) {
|
||||||
|
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||||
|
}
|
||||||
|
net_cfg_t n;
|
||||||
|
settings_load_net(&n);
|
||||||
|
|
||||||
|
cJSON *h = cJSON_GetObjectItem(in, "hostname");
|
||||||
|
if (cJSON_IsString(h)) {
|
||||||
|
char name[64];
|
||||||
|
strncpy(name, h->valuestring, sizeof(name) - 1);
|
||||||
|
name[sizeof(name) - 1] = '\0';
|
||||||
|
for (char *p = name; *p; p++) {
|
||||||
|
if (*p >= 'A' && *p <= 'Z') *p = *p - 'A' + 'a';
|
||||||
|
}
|
||||||
|
if (!valid_hostname(name)) {
|
||||||
|
cJSON_Delete(in);
|
||||||
|
return send_error(req, "400 Bad Request", "hostname: 1-63 letters, digits or hyphens");
|
||||||
|
}
|
||||||
|
strncpy(n.hostname, name, sizeof(n.hostname) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON *mode = cJSON_GetObjectItem(in, "mode");
|
||||||
|
if (cJSON_IsString(mode)) {
|
||||||
|
if (strcmp(mode->valuestring, "static") == 0) {
|
||||||
|
n.use_static = true;
|
||||||
|
} else if (strcmp(mode->valuestring, "dhcp") == 0) {
|
||||||
|
n.use_static = false;
|
||||||
|
} else {
|
||||||
|
cJSON_Delete(in);
|
||||||
|
return send_error(req, "400 Bad Request", "mode must be dhcp or static");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct { const char *key; char *dst; size_t len; } fields[] = {
|
||||||
|
{ "ip", n.ip, sizeof(n.ip) },
|
||||||
|
{ "netmask", n.netmask, sizeof(n.netmask) },
|
||||||
|
{ "gateway", n.gateway, sizeof(n.gateway) },
|
||||||
|
{ "dns", n.dns, sizeof(n.dns) },
|
||||||
|
};
|
||||||
|
for (auto &f : fields) {
|
||||||
|
cJSON *v = cJSON_GetObjectItem(in, f.key);
|
||||||
|
if (cJSON_IsString(v)) {
|
||||||
|
strncpy(f.dst, v->valuestring, f.len - 1);
|
||||||
|
f.dst[f.len - 1] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cJSON_Delete(in);
|
||||||
|
|
||||||
|
if (n.use_static) {
|
||||||
|
if (!valid_ipv4(n.ip) || !valid_ipv4(n.netmask) || !valid_ipv4(n.gateway)) {
|
||||||
|
return send_error(req, "400 Bad Request", "static mode needs ip, netmask and gateway as IPv4 addresses");
|
||||||
|
}
|
||||||
|
if (n.dns[0] && !valid_ipv4(n.dns)) {
|
||||||
|
return send_error(req, "400 Bad Request", "dns must be an IPv4 address or empty");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t err = net_save_network(&n);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
ESP_LOGW(TAG, "Network apply failed: %s", esp_err_to_name(err));
|
||||||
|
return send_error(req, "500 Internal Server Error", esp_err_to_name(err));
|
||||||
|
}
|
||||||
|
// With a static address the page's current address changes, so say where to go next
|
||||||
|
char addr[80];
|
||||||
|
if (n.use_static) {
|
||||||
|
snprintf(addr, sizeof(addr), "%s", n.ip);
|
||||||
|
} else {
|
||||||
|
snprintf(addr, sizeof(addr), "%s.local", n.hostname);
|
||||||
|
}
|
||||||
|
cJSON *o = cJSON_CreateObject();
|
||||||
|
cJSON_AddStringToObject(o, "message", "saved; reconnecting");
|
||||||
|
cJSON_AddStringToObject(o, "address", addr);
|
||||||
|
return send_json(req, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- presets -----------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// ---- LED strip hardware ------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static bool valid_order(const char *s)
|
||||||
|
{
|
||||||
|
if (strlen(s) != 3) return false;
|
||||||
|
bool seen[3] = { false, false, false };
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
int k = s[i] == 'r' ? 0 : (s[i] == 'g' ? 1 : (s[i] == 'b' ? 2 : -1));
|
||||||
|
if (k < 0 || seen[k]) return false;
|
||||||
|
seen[k] = true;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// GPIOs that can drive the strip on the ESP32-C3: not the flash (11-17) or the USB (18, 19)
|
||||||
|
static bool valid_pin(int pin)
|
||||||
|
{
|
||||||
|
return (pin >= 0 && pin <= 10) || pin == 20 || pin == 21;
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /api/hardware : LED strip settings
|
||||||
|
static esp_err_t hardware_get(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
hw_cfg_t hw;
|
||||||
|
settings_load_hw(&hw);
|
||||||
|
cJSON *o = cJSON_CreateObject();
|
||||||
|
cJSON_AddStringToObject(o, "rgb_order", hw.order);
|
||||||
|
cJSON_AddNumberToObject(o, "max_brightness", hw.max_brightness);
|
||||||
|
cJSON_AddNumberToObject(o, "num_leds", hw.num_leds);
|
||||||
|
cJSON_AddNumberToObject(o, "pin", hw.pin);
|
||||||
|
return send_json(req, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /api/hardware {"rgb_order","max_brightness":0-255,"num_leds":1-512}. Missing fields keep their value.
|
||||||
|
static esp_err_t hardware_post(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
cJSON *in = read_json(req);
|
||||||
|
if (!in) {
|
||||||
|
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||||
|
}
|
||||||
|
hw_cfg_t hw;
|
||||||
|
settings_load_hw(&hw);
|
||||||
|
cJSON *v = cJSON_GetObjectItem(in, "rgb_order");
|
||||||
|
if (cJSON_IsString(v)) {
|
||||||
|
char order[4];
|
||||||
|
for (int i = 0; i < 3; i++) order[i] = (char)(v->valuestring[i] >= 'A' && v->valuestring[i] <= 'Z' ? v->valuestring[i] + 32 : v->valuestring[i]);
|
||||||
|
order[3] = '\0';
|
||||||
|
if (!valid_order(order)) {
|
||||||
|
cJSON_Delete(in);
|
||||||
|
return send_error(req, "400 Bad Request", "rgb_order must be rgb, rbg, grb, gbr, brg or bgr");
|
||||||
|
}
|
||||||
|
strncpy(hw.order, order, sizeof(hw.order));
|
||||||
|
}
|
||||||
|
v = cJSON_GetObjectItem(in, "max_brightness");
|
||||||
|
if (cJSON_IsNumber(v)) {
|
||||||
|
if (v->valueint < 0 || v->valueint > 255) {
|
||||||
|
cJSON_Delete(in);
|
||||||
|
return send_error(req, "400 Bad Request", "max_brightness must be 0-255");
|
||||||
|
}
|
||||||
|
hw.max_brightness = (uint8_t)v->valueint;
|
||||||
|
}
|
||||||
|
v = cJSON_GetObjectItem(in, "num_leds");
|
||||||
|
if (cJSON_IsNumber(v)) {
|
||||||
|
if (v->valueint < 1 || v->valueint > FX_LEDS_MAX) {
|
||||||
|
cJSON_Delete(in);
|
||||||
|
return send_error(req, "400 Bad Request", "num_leds must be 1-512");
|
||||||
|
}
|
||||||
|
hw.num_leds = v->valueint;
|
||||||
|
}
|
||||||
|
v = cJSON_GetObjectItem(in, "pin");
|
||||||
|
if (cJSON_IsNumber(v)) {
|
||||||
|
if (!valid_pin(v->valueint)) {
|
||||||
|
cJSON_Delete(in);
|
||||||
|
return send_error(req, "400 Bad Request", "pin must be 0-10 or 20-21 (not 11-19, which the flash and USB use)");
|
||||||
|
}
|
||||||
|
hw.pin = v->valueint;
|
||||||
|
}
|
||||||
|
cJSON_Delete(in);
|
||||||
|
|
||||||
|
esp_err_t err = patterns_set_hardware(hw);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
return send_error(req, "500 Internal Server Error", esp_err_to_name(err));
|
||||||
|
}
|
||||||
|
err = settings_save_hw(&hw);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
return send_error(req, "500 Internal Server Error", esp_err_to_name(err));
|
||||||
|
}
|
||||||
|
cJSON *o = cJSON_CreateObject();
|
||||||
|
cJSON_AddStringToObject(o, "message", "saved");
|
||||||
|
return send_json(req, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /api/ddp[?led=N] : DDP receiver state, and the colour shown on LED N (for checks)
|
||||||
|
static esp_err_t ddp_get(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
bool active;
|
||||||
|
uint32_t packets;
|
||||||
|
patterns_ddp_status(&active, &packets);
|
||||||
|
int led = 0;
|
||||||
|
char q[32];
|
||||||
|
if (httpd_req_get_url_query_str(req, q, sizeof(q)) == ESP_OK) {
|
||||||
|
char v[8];
|
||||||
|
if (httpd_query_key_value(q, "led", v, sizeof(v)) == ESP_OK) {
|
||||||
|
led = atoi(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
uint8_t px[3];
|
||||||
|
patterns_shown_pixel(led, px);
|
||||||
|
cJSON *o = cJSON_CreateObject();
|
||||||
|
cJSON_AddBoolToObject(o, "active", active);
|
||||||
|
cJSON_AddNumberToObject(o, "packets", packets);
|
||||||
|
cJSON_AddNumberToObject(o, "port", DDP_PORT);
|
||||||
|
cJSON_AddNumberToObject(o, "led", led);
|
||||||
|
cJSON *px_arr = cJSON_AddArrayToObject(o, "rgb");
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
cJSON_AddItemToArray(px_arr, cJSON_CreateNumber(px[i]));
|
||||||
|
}
|
||||||
|
return send_json(req, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /api/presets/order {"names":[...]} : the presets' order on the front page
|
||||||
|
static esp_err_t presets_order_post(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
cJSON *in = read_json(req);
|
||||||
|
if (!in) {
|
||||||
|
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||||
|
}
|
||||||
|
cJSON *arr = cJSON_GetObjectItem(in, "names");
|
||||||
|
if (!cJSON_IsArray(arr)) {
|
||||||
|
cJSON_Delete(in);
|
||||||
|
return send_error(req, "400 Bad Request", "names must be a list");
|
||||||
|
}
|
||||||
|
const char *names[PRESETS_MAX];
|
||||||
|
size_t n = 0;
|
||||||
|
cJSON *item;
|
||||||
|
cJSON_ArrayForEach(item, arr) {
|
||||||
|
if (n < PRESETS_MAX && cJSON_IsString(item)) {
|
||||||
|
names[n++] = item->valuestring;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
presets_set_order(names, n);
|
||||||
|
cJSON_Delete(in);
|
||||||
|
cJSON *o = cJSON_CreateObject();
|
||||||
|
cJSON_AddStringToObject(o, "message", "saved");
|
||||||
|
return send_json(req, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /api/presets/defaults : add the default presets that are missing
|
||||||
|
static esp_err_t presets_defaults_post(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
cJSON *o = cJSON_CreateObject();
|
||||||
|
cJSON_AddNumberToObject(o, "added", presets_add_defaults());
|
||||||
|
return send_json(req, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
// GET /api/presets
|
||||||
|
static esp_err_t presets_get(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
cJSON *o = cJSON_CreateObject();
|
||||||
|
cJSON *arr = cJSON_AddArrayToObject(o, "presets");
|
||||||
|
for (size_t i = 0; i < presets_count(); i++) {
|
||||||
|
cJSON_AddItemToArray(arr, preset_to_json(presets_at(i)));
|
||||||
|
}
|
||||||
|
cJSON_AddStringToObject(o, "selected", presets_selected());
|
||||||
|
cJSON_AddStringToObject(o, "startup", presets_startup());
|
||||||
|
cJSON_AddNumberToObject(o, "max", PRESETS_MAX);
|
||||||
|
return send_json(req, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /api/brightness {"brightness":0-255} : live brightness for every pattern, saved
|
||||||
|
static esp_err_t brightness_post(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
cJSON *in = read_json(req);
|
||||||
|
if (!in) {
|
||||||
|
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||||
|
}
|
||||||
|
cJSON *v = cJSON_GetObjectItem(in, "brightness");
|
||||||
|
if (!cJSON_IsNumber(v) || v->valueint < 0 || v->valueint > 255) {
|
||||||
|
cJSON_Delete(in);
|
||||||
|
return send_error(req, "400 Bad Request", "brightness must be 0-255");
|
||||||
|
}
|
||||||
|
uint8_t level = (uint8_t)v->valueint;
|
||||||
|
cJSON_Delete(in);
|
||||||
|
patterns_set_global(level);
|
||||||
|
hw_cfg_t hw;
|
||||||
|
settings_load_hw(&hw);
|
||||||
|
hw.global_brightness = level;
|
||||||
|
settings_save_hw(&hw);
|
||||||
|
cJSON *o = cJSON_CreateObject();
|
||||||
|
cJSON_AddStringToObject(o, "message", "saved");
|
||||||
|
return send_json(req, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
// POST /api/presets : create or update by name. Missing fields take the running settings.
|
||||||
|
static esp_err_t presets_post(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
cJSON *in = read_json(req);
|
||||||
|
if (!in) {
|
||||||
|
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||||
|
}
|
||||||
|
cJSON *name = cJSON_GetObjectItem(in, "name");
|
||||||
|
if (!cJSON_IsString(name) || !name->valuestring[0] || strlen(name->valuestring) >= 32) {
|
||||||
|
cJSON_Delete(in);
|
||||||
|
return send_error(req, "400 Bad Request", "name must be 1-31 characters");
|
||||||
|
}
|
||||||
|
preset_t pr;
|
||||||
|
patterns_get(&pr);
|
||||||
|
bool ok = preset_from_json(in, &pr);
|
||||||
|
cJSON_Delete(in);
|
||||||
|
if (!ok) {
|
||||||
|
return send_error(req, "400 Bad Request", "unknown pattern");
|
||||||
|
}
|
||||||
|
|
||||||
|
esp_err_t err = presets_save(&pr);
|
||||||
|
if (err == ESP_ERR_NO_MEM) {
|
||||||
|
return send_error(req, "400 Bad Request", "preset list is full");
|
||||||
|
}
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
return send_error(req, "500 Internal Server Error", esp_err_to_name(err));
|
||||||
|
}
|
||||||
|
cJSON *o = cJSON_CreateObject();
|
||||||
|
cJSON_AddStringToObject(o, "message", "saved");
|
||||||
|
return send_json(req, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shared body for select, delete and startup: {"name"}
|
||||||
|
static esp_err_t preset_name_action(httpd_req_t *req, esp_err_t (*action)(const char *), const char *ok_msg)
|
||||||
|
{
|
||||||
|
cJSON *in = read_json(req);
|
||||||
|
if (!in) {
|
||||||
|
return send_error(req, "400 Bad Request", "empty, too large or invalid JSON");
|
||||||
|
}
|
||||||
|
cJSON *name = cJSON_GetObjectItem(in, "name");
|
||||||
|
if (!cJSON_IsString(name)) {
|
||||||
|
cJSON_Delete(in);
|
||||||
|
return send_error(req, "400 Bad Request", "name is required");
|
||||||
|
}
|
||||||
|
char buf[32];
|
||||||
|
strncpy(buf, name->valuestring, sizeof(buf) - 1);
|
||||||
|
buf[sizeof(buf) - 1] = '\0';
|
||||||
|
cJSON_Delete(in);
|
||||||
|
|
||||||
|
esp_err_t err = action(buf);
|
||||||
|
if (err == ESP_ERR_NOT_FOUND) {
|
||||||
|
return send_error(req, "404 Not Found", "no such preset");
|
||||||
|
}
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
return send_error(req, "500 Internal Server Error", esp_err_to_name(err));
|
||||||
|
}
|
||||||
|
cJSON *o = cJSON_CreateObject();
|
||||||
|
cJSON_AddStringToObject(o, "message", ok_msg);
|
||||||
|
return send_json(req, o);
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t select_post(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
return preset_name_action(req, presets_select, "selected");
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t delete_post(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
return preset_name_action(req, presets_delete, "deleted");
|
||||||
|
}
|
||||||
|
|
||||||
|
static esp_err_t startup_post(httpd_req_t *req)
|
||||||
|
{
|
||||||
|
return preset_name_action(req, presets_set_startup, "startup set");
|
||||||
|
}
|
||||||
|
|
||||||
static void add_uri(httpd_handle_t srv, const char *uri, httpd_method_t method, esp_err_t (*handler)(httpd_req_t *))
|
static void add_uri(httpd_handle_t srv, const char *uri, httpd_method_t method, esp_err_t (*handler)(httpd_req_t *))
|
||||||
{
|
{
|
||||||
httpd_uri_t u = { .uri = uri, .method = method, .handler = handler, .user_ctx = NULL };
|
httpd_uri_t u = { .uri = uri, .method = method, .handler = handler, .user_ctx = NULL };
|
||||||
@@ -254,7 +630,8 @@ static void add_uri(httpd_handle_t srv, const char *uri, httpd_method_t method,
|
|||||||
esp_err_t web_start(void)
|
esp_err_t web_start(void)
|
||||||
{
|
{
|
||||||
httpd_config_t cfg = HTTPD_DEFAULT_CONFIG();
|
httpd_config_t cfg = HTTPD_DEFAULT_CONFIG();
|
||||||
cfg.max_uri_handlers = 12;
|
cfg.max_uri_handlers = 30;
|
||||||
|
cfg.stack_size = 8192;
|
||||||
cfg.lru_purge_enable = true;
|
cfg.lru_purge_enable = true;
|
||||||
cfg.max_open_sockets = 7;
|
cfg.max_open_sockets = 7;
|
||||||
cfg.backlog_conn = 10;
|
cfg.backlog_conn = 10;
|
||||||
@@ -271,6 +648,20 @@ esp_err_t web_start(void)
|
|||||||
add_uri(s_server, "/api/wifi", HTTP_GET, wifi_get);
|
add_uri(s_server, "/api/wifi", HTTP_GET, wifi_get);
|
||||||
add_uri(s_server, "/api/wifi", HTTP_POST, wifi_post);
|
add_uri(s_server, "/api/wifi", HTTP_POST, wifi_post);
|
||||||
add_uri(s_server, "/api/wifi/scan", HTTP_GET, wifi_scan_get);
|
add_uri(s_server, "/api/wifi/scan", HTTP_GET, wifi_scan_get);
|
||||||
|
add_uri(s_server, "/api/network", HTTP_GET, network_get);
|
||||||
|
add_uri(s_server, "/api/network", HTTP_POST, network_post);
|
||||||
|
add_uri(s_server, "/api/ddp", HTTP_GET, ddp_get);
|
||||||
|
add_uri(s_server, "/api/hardware", HTTP_GET, hardware_get);
|
||||||
|
add_uri(s_server, "/api/hardware", HTTP_POST, hardware_post);
|
||||||
|
add_uri(s_server, "/api/presets", HTTP_GET, presets_get);
|
||||||
|
add_uri(s_server, "/api/presets", HTTP_POST, presets_post);
|
||||||
|
add_uri(s_server, "/api/presets/select", HTTP_POST, select_post);
|
||||||
|
add_uri(s_server, "/api/presets/delete", HTTP_POST, delete_post);
|
||||||
|
add_uri(s_server, "/api/presets/startup", HTTP_POST, startup_post);
|
||||||
|
add_uri(s_server, "/api/presets/defaults", HTTP_POST, presets_defaults_post);
|
||||||
|
add_uri(s_server, "/api/presets/order", HTTP_POST, presets_order_post);
|
||||||
|
add_uri(s_server, "/api/brightness", HTTP_POST, brightness_post);
|
||||||
|
ota_register(s_server);
|
||||||
ESP_LOGI(TAG, "Web UI on port 80");
|
ESP_LOGI(TAG, "Web UI on port 80");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
+24
-3
@@ -166,9 +166,30 @@ void WS2812::set(size_t index, uint8_t r, uint8_t g, uint8_t b)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uint8_t *p = &pixels_[index * 3];
|
uint8_t *p = &pixels_[index * 3];
|
||||||
p[0] = g;
|
uint8_t c[3] = { r, g, b };
|
||||||
p[1] = r;
|
p[0] = c[order_[0]];
|
||||||
p[2] = b;
|
p[1] = c[order_[1]];
|
||||||
|
p[2] = c[order_[2]];
|
||||||
|
}
|
||||||
|
|
||||||
|
void WS2812::setOrder(const char *order)
|
||||||
|
{
|
||||||
|
if (!order || strlen(order) != 3) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uint8_t idx[3];
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
switch (order[i]) {
|
||||||
|
case 'r': idx[i] = 0; break;
|
||||||
|
case 'g': idx[i] = 1; break;
|
||||||
|
case 'b': idx[i] = 2; break;
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (idx[0] == idx[1] || idx[1] == idx[2] || idx[0] == idx[2]) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
memcpy(order_, idx, sizeof(order_));
|
||||||
}
|
}
|
||||||
|
|
||||||
void WS2812::fill(uint8_t r, uint8_t g, uint8_t b)
|
void WS2812::fill(uint8_t r, uint8_t g, uint8_t b)
|
||||||
|
|||||||
+9
-1
@@ -63,9 +63,17 @@ public:
|
|||||||
*/
|
*/
|
||||||
static void hsv2rgb(uint32_t h, uint32_t s, uint32_t v, uint32_t *r, uint32_t *g, uint32_t *b);
|
static void hsv2rgb(uint32_t h, uint32_t s, uint32_t v, uint32_t *r, uint32_t *g, uint32_t *b);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Byte order of the strip, as the letters of the colours in the order they are sent.
|
||||||
|
* "grb" (the default) is the WS2812 norm; "rgb", "rbg", "gbr", "brg" and "bgr" cover the rest.
|
||||||
|
* Unknown names keep the current order.
|
||||||
|
*/
|
||||||
|
void setOrder(const char *order);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t num_leds_;
|
size_t num_leds_;
|
||||||
std::vector<uint8_t> pixels_; // GRB byte order, 3 bytes per LED, as WS2812 expects
|
uint8_t order_[3] = { 1, 0, 2 }; // indices into (r, g, b) for each byte sent: GRB
|
||||||
|
std::vector<uint8_t> pixels_; // 3 bytes per LED, in the strip's byte order
|
||||||
std::vector<uint8_t> tx_pixels_; // pixels_ scaled by brightness_, sent by write()
|
std::vector<uint8_t> tx_pixels_; // pixels_ scaled by brightness_, sent by write()
|
||||||
uint8_t brightness_ = 255;
|
uint8_t brightness_ = 255;
|
||||||
rmt_channel_handle_t channel_ = nullptr;
|
rmt_channel_handle_t channel_ = nullptr;
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
# Name, Type, SubType, Offset, Size
|
||||||
|
nvs, data, nvs, 0x9000, 0x6000
|
||||||
|
otadata, data, ota, 0xf000, 0x2000
|
||||||
|
phy_init, data, phy, 0x11000, 0x1000
|
||||||
|
ota_0, app, ota_0, 0x20000, 0x1E0000
|
||||||
|
ota_1, app, ota_1, 0x200000, 0x1E0000
|
||||||
|
@@ -1,3 +1,11 @@
|
|||||||
CONFIG_IDF_TARGET="esp32c3"
|
CONFIG_IDF_TARGET="esp32c3"
|
||||||
# The mDNS console CLI needs the console component; the app does not use it.
|
# The mDNS console CLI needs the console component; the app does not use it.
|
||||||
CONFIG_MDNS_ENABLE_CONSOLE_CLI=n
|
CONFIG_MDNS_ENABLE_CONSOLE_CLI=n
|
||||||
|
# The chip has 4 MB: two app slots for over-the-air updates (see partitions.csv)
|
||||||
|
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
|
||||||
|
# A new OTA image must confirm itself (ota_confirm_later) or the bootloader goes back to the old one
|
||||||
|
CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y
|
||||||
|
CONFIG_PARTITION_TABLE_CUSTOM=y
|
||||||
|
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
|
||||||
|
# mDNS opens sockets on both the AP and client interfaces, plus HTTP and DDP. The default 10 runs out and httpd stops accepting.
|
||||||
|
CONFIG_LWIP_MAX_SOCKETS=16
|
||||||
@@ -0,0 +1,160 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Test the LED driver's DDP receiver (UDP port 4048).
|
||||||
|
|
||||||
|
Sends frames of RGB pixels the way a DDP controller does: a version 1 header, the pixel data split
|
||||||
|
across two packets, and the PUSH flag on the last packet of a frame. Then checks through the driver's
|
||||||
|
HTTP API that the pixels were shown, and that the pattern comes back once the pixels stop.
|
||||||
|
|
||||||
|
python3 tools/ddp_test.py 192.168.4.1
|
||||||
|
"""
|
||||||
|
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
import socket
|
||||||
|
import struct
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
PORT = 4048
|
||||||
|
LEDS = 119
|
||||||
|
PUSH = 0x01
|
||||||
|
VER1 = 0x40
|
||||||
|
RGB = 0x01
|
||||||
|
|
||||||
|
|
||||||
|
def packet(seq, offset, data, push):
|
||||||
|
flags = VER1 | (PUSH if push else 0)
|
||||||
|
header = struct.pack(">BBBBIH", flags, seq & 0x0F, RGB, 1, offset, len(data))
|
||||||
|
return header + data
|
||||||
|
|
||||||
|
|
||||||
|
def send_frame(sock, host, pixels, seq):
|
||||||
|
"""Send pixels (a list of (r, g, b)) as two packets, PUSH on the second."""
|
||||||
|
raw = bytes(c for px in pixels for c in px)
|
||||||
|
split = (len(pixels) // 2) * 3
|
||||||
|
sock.sendto(packet(seq, 0, raw[:split], False), (host, PORT))
|
||||||
|
sock.sendto(packet(seq, split, raw[split:], True), (host, PORT))
|
||||||
|
|
||||||
|
|
||||||
|
def frame_sum(pixels):
|
||||||
|
"""The driver's checksum of a frame: the sum of its bytes, as an unsigned 32-bit value."""
|
||||||
|
return sum(c for px in pixels for c in px) & 0xFFFFFFFF
|
||||||
|
|
||||||
|
|
||||||
|
def read_serial(port, seconds, lines):
|
||||||
|
"""Collect the driver's log lines from the USB serial port for a few seconds."""
|
||||||
|
import serial
|
||||||
|
ser = serial.Serial(port, 115200, timeout=0.2)
|
||||||
|
end = time.time() + seconds
|
||||||
|
buf = b""
|
||||||
|
while time.time() < end:
|
||||||
|
buf += ser.read(4096)
|
||||||
|
ser.close()
|
||||||
|
lines.extend(buf.decode("utf-8", "replace").splitlines())
|
||||||
|
|
||||||
|
|
||||||
|
def get(host, path):
|
||||||
|
with urllib.request.urlopen(f"http://{host}{path}", timeout=5) as r:
|
||||||
|
return json.loads(r.read().decode())
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = [a for a in sys.argv[1:] if not a.startswith("--")]
|
||||||
|
serial_port = next((a.split("=", 1)[1] for a in sys.argv[1:] if a.startswith("--serial=")), None)
|
||||||
|
host = args[0] if args else "192.168.4.1"
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
failures = 0
|
||||||
|
|
||||||
|
def check(name, ok, detail=""):
|
||||||
|
nonlocal failures
|
||||||
|
print(("PASS " if ok else "FAIL ") + name + (f" {detail}" if detail else ""))
|
||||||
|
if not ok:
|
||||||
|
failures += 1
|
||||||
|
|
||||||
|
before = get(host, "/api/ddp")
|
||||||
|
print(f"before: active={before['active']} packets={before['packets']}")
|
||||||
|
|
||||||
|
# A frame: LED i is a colour that depends on i, so every LED is distinguishable
|
||||||
|
def frame(k):
|
||||||
|
return [((i * 7 + k) % 256, (i * 13) % 256, (255 - i * 3) % 256) for i in range(LEDS)]
|
||||||
|
|
||||||
|
serial_lines = []
|
||||||
|
reader = None
|
||||||
|
if serial_port:
|
||||||
|
reader = threading.Thread(target=read_serial, args=(serial_port, 3.0, serial_lines), daemon=True)
|
||||||
|
reader.start()
|
||||||
|
sent_sums = {}
|
||||||
|
last = None
|
||||||
|
for k in range(60): # about 1.5 s at 40 fps
|
||||||
|
last = frame(k)
|
||||||
|
sent_sums[frame_sum(last)] = k
|
||||||
|
send_frame(sock, host, last, k)
|
||||||
|
time.sleep(0.025)
|
||||||
|
time.sleep(0.2)
|
||||||
|
|
||||||
|
mid = get(host, "/api/ddp")
|
||||||
|
check("receiving while sending", mid["active"], f"packets={mid['packets']}")
|
||||||
|
check("packets counted", mid["packets"] - before["packets"] >= 120,
|
||||||
|
f"+{mid['packets'] - before['packets']}")
|
||||||
|
|
||||||
|
# Keep sending the last frame while reading back: the readback takes longer than the DDP timeout
|
||||||
|
stop = threading.Event()
|
||||||
|
|
||||||
|
def keep_sending():
|
||||||
|
seq = 100
|
||||||
|
while not stop.is_set():
|
||||||
|
send_frame(sock, host, last, seq)
|
||||||
|
seq += 1
|
||||||
|
time.sleep(0.025)
|
||||||
|
|
||||||
|
sender = threading.Thread(target=keep_sending, daemon=True)
|
||||||
|
sender.start()
|
||||||
|
bad = []
|
||||||
|
for i in range(LEDS):
|
||||||
|
got = get(host, f"/api/ddp?led={i}")["rgb"]
|
||||||
|
want = list(last[i])
|
||||||
|
if got != want:
|
||||||
|
bad.append((i, got, want))
|
||||||
|
stop.set()
|
||||||
|
sender.join()
|
||||||
|
check(f"all {LEDS} LEDs show the frame", not bad,
|
||||||
|
"" if not bad else f"first mismatch LED {bad[0][0]}: got {bad[0][1]} want {bad[0][2]}")
|
||||||
|
|
||||||
|
if reader is not None:
|
||||||
|
reader.join()
|
||||||
|
pat = re.compile(r"DDP frames=\d+ packets=\d+ sum=(\d+) led0=(\d+),(\d+),(\d+) led_last=(\d+),(\d+),(\d+)")
|
||||||
|
summaries = [m for m in (pat.search(line) for line in serial_lines) if m]
|
||||||
|
check("serial log has DDP summaries", len(summaries) > 0, f"{len(summaries)} lines")
|
||||||
|
matched = [m for m in summaries if int(m.group(1)) in sent_sums]
|
||||||
|
check("logged checksums are of sent frames", len(matched) == len(summaries),
|
||||||
|
f"{len(matched)}/{len(summaries)} match")
|
||||||
|
for m in summaries[-1:]:
|
||||||
|
k = sent_sums.get(int(m.group(1)))
|
||||||
|
if k is not None:
|
||||||
|
sent = frame(k)
|
||||||
|
check("serial led0 and last LED match that frame",
|
||||||
|
[int(m.group(i)) for i in (2, 3, 4)] == list(sent[0]) and
|
||||||
|
[int(m.group(i)) for i in (5, 6, 7)] == list(sent[-1]),
|
||||||
|
f"led0 {m.group(2, 3, 4)} led_last {m.group(5, 6, 7)}")
|
||||||
|
|
||||||
|
# Pixels past the strip end are ignored and don't break the rest
|
||||||
|
sock.sendto(packet(0, LEDS * 3 + 30, b"\xff" * 30, True), (host, PORT))
|
||||||
|
time.sleep(0.1)
|
||||||
|
check("out-of-range packet ignored", get(host, "/api/ddp?led=0")["rgb"] == list(last[0]))
|
||||||
|
|
||||||
|
# Stop sending: the pattern should come back after the timeout
|
||||||
|
time.sleep(3.0)
|
||||||
|
after = get(host, "/api/ddp")
|
||||||
|
check("inactive after pixels stop", not after["active"])
|
||||||
|
shown = get(host, "/api/ddp?led=0")["rgb"]
|
||||||
|
check("pattern is shown again", shown != list(last[0]), f"LED 0 now {shown}")
|
||||||
|
|
||||||
|
print("\nRESULT:", "all passed" if not failures else f"{failures} failed")
|
||||||
|
sys.exit(1 if failures else 0)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
"""
|
||||||
|
Over-the-air firmware update for the LED driver (ESP32-C3, Wi-Fi, plain HTTP).
|
||||||
|
|
||||||
|
The driver takes the raw app image on POST /ota, writes it to the idle slot and reboots.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import http.client
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
IMAGE_NAME = "led_driver.bin"
|
||||||
|
# Searched in order when no image is given (relative to where the tool is run, since the
|
||||||
|
# PyInstaller binary has no useful location of its own)
|
||||||
|
DEFAULT_IMAGES = [
|
||||||
|
os.path.join("build", IMAGE_NAME),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def _default_image() -> Optional[str]:
|
||||||
|
return next((p for p in DEFAULT_IMAGES if os.path.isfile(p)), None)
|
||||||
|
|
||||||
|
|
||||||
|
def _info(host: str, port: int) -> Optional[dict]:
|
||||||
|
try:
|
||||||
|
conn = http.client.HTTPConnection(host, port, timeout=5)
|
||||||
|
conn.request("GET", "/ota")
|
||||||
|
r = conn.getresponse()
|
||||||
|
return json.loads(r.read().decode()) if r.status == 200 else None
|
||||||
|
except (OSError, ValueError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def ota_update(host: str, image: Optional[str] = None, port: int = 80) -> None:
|
||||||
|
"""Upload image to host and wait for it to come back. Raises RuntimeError on failure."""
|
||||||
|
image = image or _default_image()
|
||||||
|
if not image:
|
||||||
|
raise RuntimeError(f"no image given and {IMAGE_NAME} not found under ./build")
|
||||||
|
if not os.path.isfile(image):
|
||||||
|
raise RuntimeError(f"image not found: {image}")
|
||||||
|
size = os.path.getsize(image)
|
||||||
|
|
||||||
|
before = _info(host, port)
|
||||||
|
if before is None:
|
||||||
|
raise RuntimeError(f"no OTA endpoint at http://{host}:{port}/ota (is this the LED driver?)")
|
||||||
|
print(f"Running {before['partition']} (built {before['built']})")
|
||||||
|
|
||||||
|
print(f"Uploading {image} ({size} bytes)...")
|
||||||
|
conn = http.client.HTTPConnection(host, port, timeout=60)
|
||||||
|
with open(image, "rb") as f:
|
||||||
|
conn.request("POST", "/ota", body=f, headers={
|
||||||
|
"Content-Length": str(size),
|
||||||
|
"Content-Type": "application/octet-stream",
|
||||||
|
})
|
||||||
|
r = conn.getresponse()
|
||||||
|
body = r.read().decode()
|
||||||
|
if r.status != 200:
|
||||||
|
try:
|
||||||
|
body = json.loads(body).get("error", body)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
raise RuntimeError(f"update rejected ({r.status}): {body}")
|
||||||
|
|
||||||
|
print("Written, waiting for reboot...")
|
||||||
|
time.sleep(3)
|
||||||
|
for _ in range(30):
|
||||||
|
after = _info(host, port)
|
||||||
|
if after:
|
||||||
|
print(f"Back up on {after['partition']} (built {after['built']})")
|
||||||
|
if after["partition"] == before["partition"]:
|
||||||
|
raise RuntimeError("driver came back on the same slot: new image was rejected or rolled back")
|
||||||
|
return
|
||||||
|
time.sleep(1)
|
||||||
|
raise RuntimeError("driver did not come back within 30 s")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
|
||||||
|
ap = argparse.ArgumentParser(description="Update the LED driver firmware over Wi-Fi.")
|
||||||
|
ap.add_argument("host", help="address or name of the driver, e.g. 192.168.4.1 or led.local")
|
||||||
|
ap.add_argument("image", nargs="?", help=f"firmware image (default: build/{IMAGE_NAME})")
|
||||||
|
ap.add_argument("--port", type=int, default=80)
|
||||||
|
args = ap.parse_args()
|
||||||
|
try:
|
||||||
|
ota_update(args.host, args.image, args.port)
|
||||||
|
except RuntimeError as e:
|
||||||
|
print(f"error: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
+829
-46
@@ -1,6 +1,7 @@
|
|||||||
// LED strip web UI. Talks to the JSON API in main/web.cpp.
|
// LED strip web UI. Talks to the JSON API in main/web.cpp.
|
||||||
(() => {
|
(() => {
|
||||||
const $ = (id) => document.getElementById(id);
|
const $ = (id) => document.getElementById(id);
|
||||||
|
const MAX_COLOURS = 8;
|
||||||
|
|
||||||
async function api(path, opts) {
|
async function api(path, opts) {
|
||||||
const r = await fetch(path, opts);
|
const r = await fetch(path, opts);
|
||||||
@@ -18,52 +19,760 @@
|
|||||||
const toHex = (rgb) => '#' + rgb.map((v) => v.toString(16).padStart(2, '0')).join('');
|
const toHex = (rgb) => '#' + rgb.map((v) => v.toString(16).padStart(2, '0')).join('');
|
||||||
const fromHex = (h) => [1, 3, 5].map((i) => parseInt(h.slice(i, i + 2), 16));
|
const fromHex = (h) => [1, 3, 5].map((i) => parseInt(h.slice(i, i + 2), 16));
|
||||||
|
|
||||||
function updateOutputs() {
|
let colours = ['#ff0000'];
|
||||||
$('speedOut').textContent = $('speed').value + ' ms';
|
let presetState = { presets: [], selected: '', startup: '' };
|
||||||
$('brightOut').textContent = $('bright').value;
|
let loadedName = ''; // the preset open in the editor ('' for a new one)
|
||||||
|
|
||||||
|
// ---- the editor (a child window) ------------------------------------------------------------
|
||||||
|
|
||||||
|
function renderColours() {
|
||||||
|
const box = $('colours');
|
||||||
|
box.textContent = '';
|
||||||
|
colours.forEach((hex, i) => {
|
||||||
|
const input = document.createElement('input');
|
||||||
|
input.type = 'color';
|
||||||
|
input.value = hex;
|
||||||
|
input.title = 'Colour ' + (i + 1);
|
||||||
|
input.addEventListener('input', () => { colours[i] = input.value; });
|
||||||
|
box.appendChild(input);
|
||||||
|
});
|
||||||
|
$('addColour').disabled = colours.length >= MAX_COLOURS;
|
||||||
|
$('removeColour').disabled = colours.length <= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Coalesce slider drags into one request
|
function updateOutputs() {
|
||||||
let sendTimer;
|
$('delayOut').textContent = $('delay').value + ' ms';
|
||||||
function sendPattern() {
|
$('brightOut').textContent = $('bright').value;
|
||||||
updateOutputs();
|
$('fadeOut').textContent = $('fade').value + ' ms';
|
||||||
clearTimeout(sendTimer);
|
|
||||||
sendTimer = setTimeout(async () => {
|
|
||||||
try {
|
|
||||||
await post('/api/pattern', {
|
|
||||||
mode: $('mode').value,
|
|
||||||
c1: fromHex($('c1').value),
|
|
||||||
c2: fromHex($('c2').value),
|
|
||||||
speed_ms: Number($('speed').value),
|
|
||||||
brightness: Number($('bright').value),
|
|
||||||
});
|
|
||||||
$('status').textContent = 'Applied';
|
|
||||||
} catch (e) {
|
|
||||||
$('status').textContent = e.message;
|
|
||||||
}
|
|
||||||
}, 150);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ---- the pattern-specific numbers -------------------------------------------------------------
|
||||||
|
// Which of n1-n4 each pattern reads, and what they mean (defaults are used when a number is 0).
|
||||||
|
// Patterns with styles read n6 (the style) to choose which set applies.
|
||||||
|
const F = (n, label, extra) => Object.assign({ n, label }, extra || {});
|
||||||
|
const DIR = [[0, 'Left to right'], [1, 'Right to left']];
|
||||||
|
const SPEC = {
|
||||||
|
bar_graph: [F('n1', 'Level (%)', { min: 0, max: 100, hint: 'lit share of the strip' })],
|
||||||
|
blizzard: [
|
||||||
|
F('n1', 'Density', { hint: 'flakes spawned per frame, 0-255 (default 90)' }),
|
||||||
|
F('n2', 'Speed', { hint: 'LEDs per frame (default 2)' }),
|
||||||
|
F('n3', 'Wind', { hint: '128 is no wind; below drifts one way, above the other' }),
|
||||||
|
],
|
||||||
|
candle_glow: [
|
||||||
|
F('n1', 'Candles', { hint: 'number of flames (default 4)' }),
|
||||||
|
F('n2', 'Width', { hint: 'LEDs each flame covers (default 3)' }),
|
||||||
|
F('n3', 'Flicker', { hint: 'how much each flame varies, 1-255 (default 90)' }),
|
||||||
|
],
|
||||||
|
circle: [
|
||||||
|
F('n1', 'Head speed', { hint: 'LEDs per second' }),
|
||||||
|
F('n2', 'Max length', { hint: 'LEDs before the tail starts to move' }),
|
||||||
|
F('n3', 'Tail speed', { hint: 'LEDs per second' }),
|
||||||
|
F('n4', 'Min length', { hint: '0 turns the ring off between loops' }),
|
||||||
|
],
|
||||||
|
clock_sweep: [
|
||||||
|
F('n1', 'Width', { hint: 'LEDs in the hand (default 1)' }),
|
||||||
|
F('n2', 'Marker every', { hint: 'LEDs between marks; 0 for none' }),
|
||||||
|
],
|
||||||
|
colour_cycle: [F('n1', 'Step', { hint: 'how far it moves each step (default 1)' })],
|
||||||
|
field_wave: [
|
||||||
|
F('n1', 'Direction', { hint: 'degrees (0 is left to right)' }),
|
||||||
|
F('n2', 'Wavelength', { hint: 'per mille of the strip (default 1000)' }),
|
||||||
|
F('n3', 'Shape', { options: [[0, 'Linear'], [1, 'Rings'], [2, 'Swirl'], [3, 'Plasma']] }),
|
||||||
|
],
|
||||||
|
flame: [
|
||||||
|
F('n1', 'Lowest level', { hint: 'dimmest the flame gets, 0-255' }),
|
||||||
|
F('n2', 'Breath period', { hint: 'ms per breath (default 2500)' }),
|
||||||
|
F('n3', 'Spark gap (min)', { hint: 'ms; 0 with the max for the default' }),
|
||||||
|
F('n4', 'Spark gap (max)', { hint: 'ms' }),
|
||||||
|
],
|
||||||
|
flicker: [F('n1', 'Lowest brightness', { hint: '0-255; the top is the brightness' })],
|
||||||
|
gradient_wipe: [
|
||||||
|
F('n1', 'First LED', { hint: 'where the wipe starts' }),
|
||||||
|
F('n2', 'Last LED', { hint: 'where it ends' }),
|
||||||
|
],
|
||||||
|
grow: [
|
||||||
|
F('n1', 'Grows from', { options: [[0, 'Centre out'], [1, 'Edges in'], [2, 'Round the centre'], [3, 'Across']] }),
|
||||||
|
F('n2', 'Then', { options: [[0, 'Stays lit'], [1, 'Empties back'], [2, 'Empties from the start'], [3, 'Goes off']] }),
|
||||||
|
F('n3', 'Angle', { hint: 'degrees, used when growing across' }),
|
||||||
|
],
|
||||||
|
hit: [
|
||||||
|
F('n1', 'Rise', { hint: 'time to full, ms (or quarter beats)' }),
|
||||||
|
F('n2', 'Hold', { hint: 'time at full' }),
|
||||||
|
F('n3', 'Decay', { hint: 'time to fade out' }),
|
||||||
|
F('n4', 'After', { options: [[0, 'Background'], [1, 'Stays on the colour']] }),
|
||||||
|
],
|
||||||
|
icicles: [
|
||||||
|
F('n1', 'Spacing', { hint: 'LEDs between icicles (default 12)' }),
|
||||||
|
F('n2', 'Max length', { hint: 'LEDs (default 14)' }),
|
||||||
|
F('n3', 'Phase step', { hint: 'how fast they grow and shrink (default 1)' }),
|
||||||
|
],
|
||||||
|
keyframes: [
|
||||||
|
F('n1', 'Easing', { options: [[0, 'Linear'], [1, 'Ease in and out'], [2, 'Step'], [3, 'Ease in'], [4, 'Ease out']] }),
|
||||||
|
F('n2', 'When done', { options: [[0, 'Last colour'], [1, 'Background'], [2, 'Off']] }),
|
||||||
|
],
|
||||||
|
orbit: [
|
||||||
|
F('n1', 'Dots', { hint: 'number of orbiting lights (default 3)' }),
|
||||||
|
F('n2', 'Speed', { hint: 'default 1' }),
|
||||||
|
],
|
||||||
|
palette_morph: [
|
||||||
|
F('n1', 'Morph time', { hint: 'ms from one colour to the next (default 1200)' }),
|
||||||
|
F('n2', 'Warp rate', { hint: 'default 3' }),
|
||||||
|
F('n3', 'Turbulence', { hint: 'how much the colours ripple (default 24)' }),
|
||||||
|
],
|
||||||
|
plasma: [
|
||||||
|
F('n1', 'Scale', { hint: 'default 6' }),
|
||||||
|
F('n2', 'Speed', { hint: 'default 2' }),
|
||||||
|
F('n3', 'Contrast', { hint: 'default 2' }),
|
||||||
|
],
|
||||||
|
pulse: [
|
||||||
|
F('n1', 'Attack', { hint: 'ms' }),
|
||||||
|
F('n2', 'Hold', { hint: 'ms' }),
|
||||||
|
F('n3', 'Decay', { hint: 'ms' }),
|
||||||
|
],
|
||||||
|
radiate: [
|
||||||
|
F('n1', 'Node spacing', { hint: 'LEDs between nodes (default 1)' }),
|
||||||
|
F('n2', 'Outward', { hint: 'ms for the ring to travel out' }),
|
||||||
|
F('n3', 'Return', { hint: 'ms for it to come back' }),
|
||||||
|
],
|
||||||
|
rain_drops: [
|
||||||
|
F('n1', 'Rate', { hint: 'drops per frame, 0-255 (default 32)' }),
|
||||||
|
F('n2', 'Width', { hint: 'LEDs each drop covers (default 3)' }),
|
||||||
|
],
|
||||||
|
rime: [
|
||||||
|
F('n1', 'Chill', { hint: 'how often frost forms, 1-255 (default 36)' }),
|
||||||
|
F('n2', 'Melt', { hint: 'how fast it fades (default 12)' }),
|
||||||
|
F('n3', 'Sparkles', { hint: 'new spots per frame (default 3)' }),
|
||||||
|
],
|
||||||
|
ripple: [
|
||||||
|
F('n1', 'Ring width', { hint: 'per mille (default 150)' }),
|
||||||
|
F('n2', 'Rings', { hint: 'rings at once (default 1)' }),
|
||||||
|
F('n3', 'Direction', { options: [[0, 'Outward'], [1, 'Inward from the edge']] }),
|
||||||
|
F('n4', 'Fade', { hint: 'per mille of distance; 0 for none' }),
|
||||||
|
],
|
||||||
|
spin: [
|
||||||
|
F('n1', 'Arms', { hint: 'default 1' }),
|
||||||
|
F('n2', 'Arm width', { hint: 'degrees (default 20)' }),
|
||||||
|
F('n3', 'Tail', { hint: 'degrees (default 90)' }),
|
||||||
|
F('n4', 'Turn', { options: [[0, 'Anticlockwise'], [1, 'Clockwise']] }),
|
||||||
|
],
|
||||||
|
strobe_burst: [
|
||||||
|
F('n1', 'Flashes', { hint: 'per burst (default 3)' }),
|
||||||
|
F('n2', 'Gap', { hint: 'ms between flashes (default 60)' }),
|
||||||
|
F('n3', 'Cooldown', { hint: 'ms between bursts (default 400)' }),
|
||||||
|
],
|
||||||
|
sweep: [
|
||||||
|
F('n1', 'Direction', { hint: 'degrees (0 is left to right)' }),
|
||||||
|
F('n2', 'Band width', { hint: 'per mille of the strip (default 200)' }),
|
||||||
|
F('n3', 'Trail', { hint: 'per mille behind the band' }),
|
||||||
|
F('n4', 'After', { options: [[0, 'Band moves on'], [1, 'Leave lit (wipe)']] }),
|
||||||
|
],
|
||||||
|
twinkle: [
|
||||||
|
F('n1', 'Activity', { hint: '1-255 (default 48)' }),
|
||||||
|
F('n2', 'Density', { hint: '0-255 (default 128)' }),
|
||||||
|
F('n3', 'Smallest cluster', { hint: 'LEDs (default 1)' }),
|
||||||
|
F('n4', 'Largest cluster', { hint: 'LEDs (default 4)' }),
|
||||||
|
],
|
||||||
|
// Patterns with styles: n6 picks the set of fields
|
||||||
|
aurora: {
|
||||||
|
styles: ['Bands', 'Northern wave'],
|
||||||
|
fields: [
|
||||||
|
[F('n1', 'Bands', { hint: 'default 3' }), F('n2', 'Shimmer', { hint: '0-255 (default 40)' })],
|
||||||
|
[F('n1', 'Wave length', { hint: 'LEDs (default 20)' }), F('n2', 'Contrast', { hint: '1-255 (default 200)' }),
|
||||||
|
F('n3', 'Drift', { hint: 'default 2' })],
|
||||||
|
],
|
||||||
|
},
|
||||||
|
chase: {
|
||||||
|
styles: ['Chase', 'Marquee'],
|
||||||
|
fields: [
|
||||||
|
[F('n1', 'Lit LEDs', { hint: 'default 1' }), F('n2', 'Gap LEDs', { hint: 'default 1' }),
|
||||||
|
F('n3', 'Step (even beats)'), F('n4', 'Step (odd beats)')],
|
||||||
|
[F('n1', 'Lit LEDs', { hint: 'default 3' }), F('n2', 'Gap LEDs', { hint: 'default 2' }),
|
||||||
|
F('n3', 'Step', { hint: 'default 1' })],
|
||||||
|
],
|
||||||
|
},
|
||||||
|
meteor: {
|
||||||
|
styles: ['Meteor', 'Comet', 'Scanner'],
|
||||||
|
fields: [
|
||||||
|
[F('n1', 'Tail', { hint: 'LEDs (default 8)' }), F('n2', 'Speed', { hint: 'default 1' }),
|
||||||
|
F('n3', 'Fade', { hint: '1-255 (default 192)' })],
|
||||||
|
[F('n1', 'Tail', { hint: 'LEDs (default 6)' }), F('n2', 'Speed', { hint: 'default 1' }),
|
||||||
|
F('n3', 'Gap', { hint: 'LEDs between the two (default 0)' })],
|
||||||
|
[F('n1', 'Width', { hint: 'LEDs (default 4)' }), F('n2', 'End pause', { hint: 'frames at each end' })],
|
||||||
|
],
|
||||||
|
},
|
||||||
|
particles: {
|
||||||
|
styles: ['Snow', 'Starfall'],
|
||||||
|
fields: [
|
||||||
|
[F('n1', 'Density', { hint: '0-255 (default 20)' }), F('n2', 'Speed', { hint: 'default 1' })],
|
||||||
|
[F('n1', 'Spawn rate', { hint: '1-255 (default 14)' }), F('n2', 'Speed', { hint: 'default 2' }),
|
||||||
|
F('n3', 'Tail', { hint: 'LEDs (default 10)' })],
|
||||||
|
],
|
||||||
|
},
|
||||||
|
sparkle: {
|
||||||
|
styles: ['Trail', 'Ice', 'Fireflies'],
|
||||||
|
fields: [
|
||||||
|
[F('n1', 'Density', { hint: '0-255 (default 24)' }), F('n2', 'Decay', { hint: '1-255 (default 210)' })],
|
||||||
|
[F('n1', 'Rate', { hint: '1-255 (default 55)' }), F('n2', 'Decay', { hint: '1-255 (default 140)' }),
|
||||||
|
F('n3', 'Halo', { hint: '0-3 LEDs' })],
|
||||||
|
[F('n1', 'Fireflies', { hint: 'default 6' }), F('n2', 'Speed', { hint: 'default 8' })],
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
// colour_cycle has a style too: gradient (0) or rainbow (1), both using the step
|
||||||
|
SPEC.colour_cycle = {
|
||||||
|
styles: ['Gradient', 'Rainbow'],
|
||||||
|
fields: [[F('n1', 'Step', { hint: 'default 1' })], [F('n1', 'Step', { hint: 'default 1' })]],
|
||||||
|
};
|
||||||
|
|
||||||
|
// The fields a pattern uses for a style: [] when it has none
|
||||||
|
function fieldsFor(name, style) {
|
||||||
|
const spec = SPEC[name];
|
||||||
|
if (!spec) return [];
|
||||||
|
if (Array.isArray(spec)) return spec;
|
||||||
|
return spec.fields[style] || spec.fields[0] || [];
|
||||||
|
}
|
||||||
|
|
||||||
|
function stylesFor(name) {
|
||||||
|
const spec = SPEC[name];
|
||||||
|
return spec && !Array.isArray(spec) ? spec.styles : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The current numbers, by key (n1..n4) and the style (n6)
|
||||||
|
let nums = { n1: 0, n2: 0, n3: 0, n4: 0 };
|
||||||
|
|
||||||
|
function renderPatternFields() {
|
||||||
|
const name = $('pattern').value;
|
||||||
|
const styles = stylesFor(name);
|
||||||
|
const styleSel = $('style');
|
||||||
|
if (styles) {
|
||||||
|
if (styleSel.options.length !== styles.length) {
|
||||||
|
styleSel.textContent = '';
|
||||||
|
styles.forEach((label, i) => styleSel.add(new Option(label, i)));
|
||||||
|
}
|
||||||
|
$('styleRow').hidden = false;
|
||||||
|
} else {
|
||||||
|
styleSel.textContent = '';
|
||||||
|
styleSel.add(new Option('—', 0));
|
||||||
|
$('styleRow').hidden = true;
|
||||||
|
}
|
||||||
|
const style = styles ? Number(styleSel.value) || 0 : 0;
|
||||||
|
const box = $('numbers');
|
||||||
|
box.textContent = '';
|
||||||
|
fieldsFor(name, style).forEach((f) => {
|
||||||
|
const label = document.createElement('label');
|
||||||
|
label.textContent = f.label;
|
||||||
|
let input;
|
||||||
|
if (f.options) {
|
||||||
|
input = document.createElement('select');
|
||||||
|
f.options.forEach(([v, text]) => input.add(new Option(text, v)));
|
||||||
|
input.value = nums[f.n] ?? 0;
|
||||||
|
if (!f.options.some(([v]) => v === Number(input.value))) input.value = f.options[0][0];
|
||||||
|
} else {
|
||||||
|
input = document.createElement('input');
|
||||||
|
input.type = 'number';
|
||||||
|
input.min = f.min ?? 0;
|
||||||
|
if (f.max !== undefined) input.max = f.max;
|
||||||
|
input.step = 1;
|
||||||
|
input.value = nums[f.n] ?? 0;
|
||||||
|
}
|
||||||
|
input.addEventListener('input', () => { nums[f.n] = Number(input.value) || 0; });
|
||||||
|
input.addEventListener('change', () => { nums[f.n] = Number(input.value) || 0; });
|
||||||
|
label.appendChild(input);
|
||||||
|
if (f.hint) {
|
||||||
|
const hint = document.createElement('span');
|
||||||
|
hint.className = 'muted';
|
||||||
|
hint.textContent = f.hint;
|
||||||
|
label.appendChild(hint);
|
||||||
|
}
|
||||||
|
box.appendChild(label);
|
||||||
|
});
|
||||||
|
if (!fieldsFor(name, style).length) {
|
||||||
|
const none = document.createElement('p');
|
||||||
|
none.className = 'muted';
|
||||||
|
none.textContent = 'This pattern has no extra settings.';
|
||||||
|
box.appendChild(none);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load the numbers and style of a preset into the editor
|
||||||
|
function setNumbers(p) {
|
||||||
|
nums = { n1: p.n1 || 0, n2: p.n2 || 0, n3: p.n3 || 0, n4: p.n4 || 0 };
|
||||||
|
const styles = stylesFor(p.pattern);
|
||||||
|
$('style').textContent = '';
|
||||||
|
if (styles) {
|
||||||
|
styles.forEach((label, i) => $('style').add(new Option(label, i)));
|
||||||
|
$('style').value = p.mode || 0;
|
||||||
|
}
|
||||||
|
renderPatternFields();
|
||||||
|
}
|
||||||
|
|
||||||
|
// The editor's fields as a preset
|
||||||
|
function gather() {
|
||||||
|
return {
|
||||||
|
pattern: $('pattern').value,
|
||||||
|
colors: colours.map(fromHex),
|
||||||
|
background: fromHex($('bg').value),
|
||||||
|
delay: Number($('delay').value),
|
||||||
|
brightness: Number($('bright').value),
|
||||||
|
fade: Number($('fade').value),
|
||||||
|
auto: $('auto').checked,
|
||||||
|
reverse: $('reverse').checked,
|
||||||
|
beat_time: $('beatTime').checked,
|
||||||
|
n1: nums.n1,
|
||||||
|
n2: nums.n2,
|
||||||
|
n3: nums.n3,
|
||||||
|
n4: nums.n4,
|
||||||
|
mode: stylesFor($('pattern').value) ? Number($('style').value) || 0 : 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fill the editor from a preset
|
||||||
|
function fillEditor(p) {
|
||||||
|
if (p.pattern !== undefined) $('pattern').value = p.pattern;
|
||||||
|
colours = (p.colors && p.colors.length ? p.colors : [[255, 255, 255]]).map(toHex);
|
||||||
|
renderColours();
|
||||||
|
$('bg').value = toHex(p.background || [0, 0, 0]);
|
||||||
|
$('delay').value = p.delay;
|
||||||
|
$('bright').value = p.brightness;
|
||||||
|
$('fade').value = p.fade || 0;
|
||||||
|
$('auto').checked = !!p.auto;
|
||||||
|
$('reverse').checked = !!p.reverse;
|
||||||
|
$('beatTime').checked = !!p.beat_time;
|
||||||
|
setNumbers(p);
|
||||||
|
updateOutputs();
|
||||||
|
}
|
||||||
|
|
||||||
|
function editorMsg(text) {
|
||||||
|
$('presetMsg').textContent = text;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open the child window: an existing preset, or a new one (null)
|
||||||
|
function openEditor(p) {
|
||||||
|
if (p) {
|
||||||
|
fillEditor(p);
|
||||||
|
$('presetName').value = p.name;
|
||||||
|
loadedName = p.name;
|
||||||
|
$('editor-title').textContent = 'Edit ' + p.name;
|
||||||
|
} else {
|
||||||
|
fillEditor({
|
||||||
|
pattern: 'chase', colors: [[255, 0, 0]], background: [0, 0, 0],
|
||||||
|
delay: 1000, brightness: 16, auto: true,
|
||||||
|
});
|
||||||
|
$('presetName').value = '';
|
||||||
|
loadedName = '';
|
||||||
|
$('editor-title').textContent = 'New preset';
|
||||||
|
}
|
||||||
|
refreshEditorButtons();
|
||||||
|
editorMsg('');
|
||||||
|
$('editor-modal').hidden = false;
|
||||||
|
$('presetName').focus();
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeEditor() {
|
||||||
|
$('editor-modal').hidden = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function refreshEditorButtons() {
|
||||||
|
const exists = presetState.presets.some((p) => p.name === loadedName);
|
||||||
|
$('presetStartup').disabled = !exists;
|
||||||
|
$('presetDelete').disabled = !exists;
|
||||||
|
$('presetStartup').textContent = exists && presetState.startup === loadedName
|
||||||
|
? 'Clear startup' : 'Use at startup';
|
||||||
|
}
|
||||||
|
|
||||||
|
$('editorClose').addEventListener('click', closeEditor);
|
||||||
|
$('editor-modal').addEventListener('click', (ev) => {
|
||||||
|
if (ev.target === $('editor-modal')) closeEditor();
|
||||||
|
});
|
||||||
|
document.addEventListener('keydown', (ev) => {
|
||||||
|
if (ev.key === 'Escape' && !$('editor-modal').hidden) closeEditor();
|
||||||
|
});
|
||||||
|
|
||||||
|
$('apply').addEventListener('click', async () => {
|
||||||
|
try {
|
||||||
|
await post('/api/pattern', gather());
|
||||||
|
editorMsg('Applied to the strip (not saved)');
|
||||||
|
await loadStatus();
|
||||||
|
} catch (e) {
|
||||||
|
editorMsg(e.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$('presetSave').addEventListener('click', async () => {
|
||||||
|
const name = $('presetName').value.trim();
|
||||||
|
if (!name) {
|
||||||
|
editorMsg('Give the preset a name');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await post('/api/presets', Object.assign(gather(), { name }));
|
||||||
|
await post('/api/presets/select', { name });
|
||||||
|
loadedName = name;
|
||||||
|
await loadPresets();
|
||||||
|
await loadStatus();
|
||||||
|
closeEditor();
|
||||||
|
} catch (e) {
|
||||||
|
editorMsg(e.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$('presetStartup').addEventListener('click', async () => {
|
||||||
|
const name = presetState.startup === loadedName ? '' : loadedName;
|
||||||
|
try {
|
||||||
|
await post('/api/presets/startup', { name });
|
||||||
|
await loadPresets();
|
||||||
|
refreshEditorButtons();
|
||||||
|
editorMsg(name ? name + ' will load at startup' : 'Startup preset cleared');
|
||||||
|
} catch (e) {
|
||||||
|
editorMsg(e.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$('presetDelete').addEventListener('click', async () => {
|
||||||
|
const name = loadedName;
|
||||||
|
if (!name || !confirm(`Delete preset "${name}"?`)) return;
|
||||||
|
try {
|
||||||
|
await post('/api/presets/delete', { name });
|
||||||
|
await loadPresets();
|
||||||
|
closeEditor();
|
||||||
|
} catch (e) {
|
||||||
|
editorMsg(e.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$('addColour').addEventListener('click', () => {
|
||||||
|
if (colours.length < MAX_COLOURS) {
|
||||||
|
colours.push(colours[colours.length - 1]);
|
||||||
|
renderColours();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
$('removeColour').addEventListener('click', () => {
|
||||||
|
if (colours.length > 1) {
|
||||||
|
colours.pop();
|
||||||
|
renderColours();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
['delay', 'bright', 'fade'].forEach((id) => $(id).addEventListener('input', updateOutputs));
|
||||||
|
$('pattern').addEventListener('change', () => { renderPatternFields(); });
|
||||||
|
$('style').addEventListener('change', () => { renderPatternFields(); });
|
||||||
|
|
||||||
|
// ---- the running settings ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
async function loadPatterns(status) {
|
||||||
|
if ($('pattern').options.length) return;
|
||||||
|
status.patterns.forEach((name) => $('pattern').add(new Option(name, name)));
|
||||||
|
}
|
||||||
|
|
||||||
|
let globalTimer;
|
||||||
|
$('globalBright').addEventListener('input', () => {
|
||||||
|
$('globalOut').textContent = $('globalBright').value;
|
||||||
|
clearTimeout(globalTimer);
|
||||||
|
globalTimer = setTimeout(() => {
|
||||||
|
post('/api/brightness', { brightness: Number($('globalBright').value) }).catch((e) => {
|
||||||
|
$('status').textContent = e.message;
|
||||||
|
});
|
||||||
|
}, 150);
|
||||||
|
});
|
||||||
|
|
||||||
async function loadStatus() {
|
async function loadStatus() {
|
||||||
const s = await api('/api/status');
|
const s = await api('/api/status');
|
||||||
|
if (document.activeElement !== $('globalBright')) {
|
||||||
|
$('globalBright').value = s.global_brightness;
|
||||||
|
$('globalOut').textContent = s.global_brightness;
|
||||||
|
}
|
||||||
$('apName').textContent = s.ap_ssid;
|
$('apName').textContent = s.ap_ssid;
|
||||||
$('staState').textContent = s.sta_connected ? 'connected (' + s.sta_ip + ')' : 'not connected';
|
$('staState').textContent = s.sta_connected ? 'connected (' + s.sta_ip + ')' : 'not connected';
|
||||||
if (!$('mode').options.length) {
|
await loadPatterns(s);
|
||||||
s.modes.forEach((m) => $('mode').add(new Option(m, m)));
|
|
||||||
}
|
|
||||||
$('mode').value = s.mode;
|
|
||||||
$('c1').value = toHex(s.c1);
|
|
||||||
$('c2').value = toHex(s.c2);
|
|
||||||
$('speed').value = s.speed_ms;
|
|
||||||
$('bright').value = s.brightness;
|
|
||||||
updateOutputs();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ---- preset buttons on the front --------------------------------------------------------------
|
||||||
|
|
||||||
|
// Drag to reorder. Mouse: move 8 px to start. Touch: hold a moment, so scrolling still works.
|
||||||
|
let dragging = false; // a drag is in progress
|
||||||
|
let dragRow = null;
|
||||||
|
let dragId = null;
|
||||||
|
let dragX = 0;
|
||||||
|
let dragY = 0;
|
||||||
|
let holdTimer = null;
|
||||||
|
let justDragged = false;
|
||||||
|
|
||||||
|
function startDrag(row) {
|
||||||
|
dragging = true;
|
||||||
|
dragRow = row;
|
||||||
|
row.classList.add('dragging');
|
||||||
|
}
|
||||||
|
|
||||||
|
function attachDrag(row) {
|
||||||
|
row.addEventListener('pointerdown', (ev) => {
|
||||||
|
if (!isEditMode()) return;
|
||||||
|
if (ev.pointerType === 'mouse' && ev.button !== 0) return;
|
||||||
|
dragId = ev.pointerId;
|
||||||
|
dragX = ev.clientX;
|
||||||
|
dragY = ev.clientY;
|
||||||
|
if (ev.pointerType !== 'mouse') {
|
||||||
|
clearTimeout(holdTimer);
|
||||||
|
holdTimer = setTimeout(() => startDrag(row), 300);
|
||||||
|
}
|
||||||
|
row.dataset.pending = '1';
|
||||||
|
});
|
||||||
|
row.addEventListener('pointerup', () => { row.dataset.pending = ''; });
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener('pointermove', (ev) => {
|
||||||
|
if (ev.pointerId !== dragId) return;
|
||||||
|
if (!dragRow) {
|
||||||
|
const moved = Math.hypot(ev.clientX - dragX, ev.clientY - dragY);
|
||||||
|
const pending = [...$('presetList').querySelectorAll('.preset-tile-row')].find((r) => r.dataset.pending === '1');
|
||||||
|
if (pending && moved > 8 && ev.pointerType === 'mouse') {
|
||||||
|
startDrag(pending);
|
||||||
|
} else if (pending && moved > 8) {
|
||||||
|
clearTimeout(holdTimer); // moved before the hold: a scroll
|
||||||
|
}
|
||||||
|
if (!dragRow) return;
|
||||||
|
}
|
||||||
|
ev.preventDefault();
|
||||||
|
const list = $('presetList');
|
||||||
|
const over = document.elementFromPoint(ev.clientX, ev.clientY);
|
||||||
|
const target = over && over.closest('.preset-tile-row');
|
||||||
|
if (target && target !== dragRow && target.parentNode === list) {
|
||||||
|
const r = target.getBoundingClientRect();
|
||||||
|
const before = ev.clientY < r.top + r.height / 2 || (Math.abs(ev.clientY - (r.top + r.height / 2)) < r.height / 4 && ev.clientX < r.left + r.width / 2);
|
||||||
|
list.insertBefore(dragRow, before ? target : target.nextSibling);
|
||||||
|
}
|
||||||
|
}, { passive: false });
|
||||||
|
|
||||||
|
async function endDrag(ev) {
|
||||||
|
if (ev.pointerId !== dragId) return;
|
||||||
|
clearTimeout(holdTimer);
|
||||||
|
dragId = null;
|
||||||
|
if (!dragRow) return;
|
||||||
|
const row = dragRow;
|
||||||
|
dragRow = null;
|
||||||
|
row.classList.remove('dragging');
|
||||||
|
row.dataset.pending = '';
|
||||||
|
justDragged = true; // swallow the click that follows
|
||||||
|
setTimeout(() => { justDragged = false; }, 0);
|
||||||
|
const names = [...$('presetList').querySelectorAll('.preset-tile-row')].map((r) => r.dataset.name);
|
||||||
|
dragging = false;
|
||||||
|
try {
|
||||||
|
await post('/api/presets/order', { names });
|
||||||
|
await loadPresets();
|
||||||
|
} catch (e) {
|
||||||
|
presetMsg(e.message);
|
||||||
|
await loadPresets();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
document.addEventListener('pointerup', endDrag);
|
||||||
|
document.addEventListener('pointercancel', (ev) => {
|
||||||
|
clearTimeout(holdTimer);
|
||||||
|
if (ev.pointerId === dragId && dragRow) endDrag(ev);
|
||||||
|
else dragId = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
$('presetList').addEventListener('click', (ev) => {
|
||||||
|
if (justDragged) {
|
||||||
|
ev.preventDefault();
|
||||||
|
ev.stopPropagation();
|
||||||
|
}
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
function renderPresets() {
|
||||||
|
const list = $('presetList');
|
||||||
|
list.textContent = '';
|
||||||
|
if (!presetState.presets.length) {
|
||||||
|
list.innerHTML = isEditMode()
|
||||||
|
? '<span class="muted">No presets yet. Use + New preset.</span>'
|
||||||
|
: '<span class="muted">No presets yet. Switch to Edit mode to add some.</span>';
|
||||||
|
}
|
||||||
|
presetState.presets.forEach((p) => {
|
||||||
|
const row = document.createElement('div');
|
||||||
|
row.className = 'preset-tile-row';
|
||||||
|
row.dataset.name = p.name;
|
||||||
|
attachDrag(row);
|
||||||
|
|
||||||
|
const btn = document.createElement('button');
|
||||||
|
btn.type = 'button';
|
||||||
|
btn.className = 'preset-tile' + (p.name === presetState.selected ? ' active' : '');
|
||||||
|
const cols = (p.colors && p.colors.length ? p.colors : [[255, 255, 255]]).map(toHex);
|
||||||
|
const stops = cols.length === 1 ? [cols[0], cols[0]] : cols;
|
||||||
|
btn.style.background = `linear-gradient(90deg, ${stops.join(', ')})`;
|
||||||
|
const name = document.createElement('span');
|
||||||
|
name.className = 'preset-tile-name';
|
||||||
|
name.textContent = (p.name === presetState.startup ? '★ ' : '') + p.name;
|
||||||
|
const pat = document.createElement('span');
|
||||||
|
pat.className = 'preset-tile-pattern';
|
||||||
|
pat.textContent = p.pattern;
|
||||||
|
btn.appendChild(name);
|
||||||
|
btn.appendChild(pat);
|
||||||
|
btn.title = p.name + ' · ' + p.pattern;
|
||||||
|
btn.addEventListener('click', () => playPreset(p.name));
|
||||||
|
|
||||||
|
const edit = document.createElement('button');
|
||||||
|
edit.type = 'button';
|
||||||
|
edit.className = 'preset-edit';
|
||||||
|
edit.textContent = 'Edit';
|
||||||
|
edit.addEventListener('click', () => openEditor(p));
|
||||||
|
|
||||||
|
row.appendChild(btn);
|
||||||
|
if (isEditMode()) row.appendChild(edit); // Edit and drag are for Edit mode only
|
||||||
|
list.appendChild(row);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadPresets() {
|
||||||
|
presetState = await api('/api/presets');
|
||||||
|
if (!dragging) renderPresets();
|
||||||
|
if (!$('editor-modal').hidden) refreshEditorButtons();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tapping a preset plays it on the strip
|
||||||
|
async function playPreset(name) {
|
||||||
|
try {
|
||||||
|
await post('/api/presets/select', { name });
|
||||||
|
await loadPresets();
|
||||||
|
await loadStatus();
|
||||||
|
$('status').textContent = 'Playing ' + name;
|
||||||
|
} catch (e) {
|
||||||
|
$('status').textContent = e.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$('newPreset').addEventListener('click', () => openEditor(null));
|
||||||
|
|
||||||
|
// ---- firmware (over the air) ------------------------------------------------------------------
|
||||||
|
|
||||||
|
let fwPartition = null;
|
||||||
|
|
||||||
|
async function loadFirmware() {
|
||||||
|
const f = await api('/ota');
|
||||||
|
fwPartition = f.partition;
|
||||||
|
$('fwInfo').textContent = `${f.version} (built ${f.built}, slot ${f.partition})`;
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
||||||
|
|
||||||
|
// After an update the board reboots onto the other slot: wait until it answers again
|
||||||
|
async function waitForReboot(before) {
|
||||||
|
await sleep(3000);
|
||||||
|
for (let i = 0; i < 40; i++) {
|
||||||
|
try {
|
||||||
|
const f = await api('/ota');
|
||||||
|
return f.partition !== before ? f : null;
|
||||||
|
} catch (e) {
|
||||||
|
await sleep(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$('fwForm').addEventListener('submit', async (ev) => {
|
||||||
|
ev.preventDefault();
|
||||||
|
const file = $('fwFile').files[0];
|
||||||
|
if (!file) {
|
||||||
|
$('fwMsg').textContent = 'Choose a .bin file first';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!confirm(`Update to ${file.name} (${file.size} bytes)? The driver reboots afterwards.`)) return;
|
||||||
|
$('fwUpload').disabled = true;
|
||||||
|
$('fwMsg').textContent = 'Uploading…';
|
||||||
|
const before = fwPartition;
|
||||||
|
try {
|
||||||
|
const r = await fetch('/ota', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/octet-stream' },
|
||||||
|
body: file,
|
||||||
|
});
|
||||||
|
const j = await r.json().catch(() => ({}));
|
||||||
|
if (!r.ok) throw new Error(j.error || r.statusText);
|
||||||
|
$('fwMsg').textContent = 'Written. Rebooting…';
|
||||||
|
const now = await waitForReboot(before);
|
||||||
|
$('fwMsg').textContent = now
|
||||||
|
? `Updated: now running ${now.version} on ${now.partition}.`
|
||||||
|
: 'The driver is taking a while to come back. Reload the page in a minute.';
|
||||||
|
if (now) await loadFirmware();
|
||||||
|
} catch (e) {
|
||||||
|
$('fwMsg').textContent = e.message;
|
||||||
|
} finally {
|
||||||
|
$('fwUpload').disabled = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$('fwReboot').addEventListener('click', async () => {
|
||||||
|
if (!confirm('Reboot the driver now? The page is unavailable for a few seconds.')) return;
|
||||||
|
try {
|
||||||
|
await post('/reboot', {});
|
||||||
|
$('fwMsg').textContent = 'Rebooting…';
|
||||||
|
setTimeout(() => location.reload(), 8000);
|
||||||
|
} catch (e) {
|
||||||
|
$('fwMsg').textContent = e.message;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$('addDefaults').addEventListener('click', async () => {
|
||||||
|
try {
|
||||||
|
const j = await post('/api/presets/defaults', {});
|
||||||
|
$('defaultsMsg').textContent = j.added ? `Added ${j.added} preset${j.added === 1 ? '' : 's'}.` : 'All default presets are already there.';
|
||||||
|
await loadPresets();
|
||||||
|
} catch (e) {
|
||||||
|
$('defaultsMsg').textContent = e.message;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---- LED strip settings -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
async function loadHardware() {
|
||||||
|
const h = await api('/api/hardware');
|
||||||
|
$('hwLeds').value = h.num_leds;
|
||||||
|
$('hwPin').value = h.pin;
|
||||||
|
$('hwOrder').value = h.rgb_order;
|
||||||
|
$('hwMax').value = h.max_brightness;
|
||||||
|
$('hwMaxOut').textContent = h.max_brightness;
|
||||||
|
}
|
||||||
|
|
||||||
|
$('hwMax').addEventListener('input', () => { $('hwMaxOut').textContent = $('hwMax').value; });
|
||||||
|
|
||||||
|
$('hwForm').addEventListener('submit', async (ev) => {
|
||||||
|
ev.preventDefault();
|
||||||
|
$('hwMsg').textContent = 'Saving…';
|
||||||
|
try {
|
||||||
|
await post('/api/hardware', {
|
||||||
|
num_leds: Number($('hwLeds').value),
|
||||||
|
pin: Number($('hwPin').value),
|
||||||
|
rgb_order: $('hwOrder').value,
|
||||||
|
max_brightness: Number($('hwMax').value),
|
||||||
|
});
|
||||||
|
$('hwMsg').textContent = 'Saved. The strip has been reset with the new settings.';
|
||||||
|
} catch (e) {
|
||||||
|
$('hwMsg').textContent = e.message;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---- Wi-Fi and network ------------------------------------------------------------------------
|
||||||
|
|
||||||
async function loadWifi() {
|
async function loadWifi() {
|
||||||
const w = await api('/api/wifi');
|
const w = await api('/api/wifi');
|
||||||
if (w.ssid && !$('ssid').value) $('ssid').value = w.ssid;
|
if (w.ssid && !$('ssid').value) $('ssid').value = w.ssid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateStaticFields() {
|
||||||
|
$('staticFields').hidden = $('ipMode').value !== 'static';
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadNetwork() {
|
||||||
|
const n = await api('/api/network');
|
||||||
|
$('hostname').value = n.hostname;
|
||||||
|
$('mdnsName').textContent = n.hostname;
|
||||||
|
$('ipMode').value = n.mode;
|
||||||
|
$('sIp').value = n.ip;
|
||||||
|
$('sMask').value = n.netmask;
|
||||||
|
$('sGw').value = n.gateway;
|
||||||
|
$('sDns').value = n.dns;
|
||||||
|
updateStaticFields();
|
||||||
|
}
|
||||||
|
|
||||||
async function scan() {
|
async function scan() {
|
||||||
const list = $('networks');
|
const list = $('networks');
|
||||||
list.textContent = 'Scanning…';
|
list.textContent = 'Scanning…';
|
||||||
@@ -86,20 +795,6 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tabs
|
|
||||||
document.querySelectorAll('.tab').forEach((btn) => {
|
|
||||||
btn.addEventListener('click', () => {
|
|
||||||
document.querySelectorAll('.tab').forEach((b) => b.classList.toggle('active', b === btn));
|
|
||||||
$('patterns').hidden = btn.dataset.tab !== 'patterns';
|
|
||||||
$('settings').hidden = btn.dataset.tab !== 'settings';
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
['mode', 'c1', 'c2', 'speed', 'bright'].forEach((id) => {
|
|
||||||
$(id).addEventListener('input', sendPattern);
|
|
||||||
$(id).addEventListener('change', sendPattern);
|
|
||||||
});
|
|
||||||
|
|
||||||
$('scan').addEventListener('click', scan);
|
$('scan').addEventListener('click', scan);
|
||||||
|
|
||||||
$('wifiForm').addEventListener('submit', async (ev) => {
|
$('wifiForm').addEventListener('submit', async (ev) => {
|
||||||
@@ -115,7 +810,95 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$('ipMode').addEventListener('change', updateStaticFields);
|
||||||
|
|
||||||
|
$('netForm').addEventListener('submit', async (ev) => {
|
||||||
|
ev.preventDefault();
|
||||||
|
const body = {
|
||||||
|
hostname: $('hostname').value.trim().toLowerCase(),
|
||||||
|
mode: $('ipMode').value,
|
||||||
|
ip: $('sIp').value.trim(),
|
||||||
|
netmask: $('sMask').value.trim(),
|
||||||
|
gateway: $('sGw').value.trim(),
|
||||||
|
dns: $('sDns').value.trim(),
|
||||||
|
};
|
||||||
|
$('netMsg').textContent = 'Saving…';
|
||||||
|
try {
|
||||||
|
const j = await post('/api/network', body);
|
||||||
|
$('netMsg').textContent = `Saved. The client reconnects; open http://${j.address}/ in a few seconds.`;
|
||||||
|
$('mdnsName').textContent = body.hostname;
|
||||||
|
} catch (e) {
|
||||||
|
$('netMsg').textContent = e.message;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// ---- tabs and start-up ------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// On phones the header menu is collapsed behind the ☰ button
|
||||||
|
const header = document.querySelector('header');
|
||||||
|
const menuToggle = $('menuToggle');
|
||||||
|
const setMenu = (open) => {
|
||||||
|
header.classList.toggle('menu-open', open);
|
||||||
|
menuToggle.setAttribute('aria-expanded', String(open));
|
||||||
|
};
|
||||||
|
menuToggle.addEventListener('click', () => setMenu(!header.classList.contains('menu-open')));
|
||||||
|
|
||||||
|
// Run mode: tap presets to play them. Edit mode: also Edit, drag to reorder, New preset and Settings.
|
||||||
|
const MODE_KEY = 'led-driver-ui-mode';
|
||||||
|
let uiMode = 'run';
|
||||||
|
try {
|
||||||
|
uiMode = localStorage.getItem(MODE_KEY) === 'edit' ? 'edit' : 'run';
|
||||||
|
} catch (_) { /* private mode: keep Run */ }
|
||||||
|
function isEditMode() { return uiMode === 'edit'; }
|
||||||
|
|
||||||
|
function applyMode() {
|
||||||
|
const edit = isEditMode();
|
||||||
|
document.body.classList.toggle('mode-edit', edit);
|
||||||
|
document.body.classList.toggle('mode-run', !edit);
|
||||||
|
$('modeToggle').textContent = edit ? 'Run mode' : 'Edit mode'; // label is the mode you switch to
|
||||||
|
$('modeToggle').setAttribute('aria-pressed', String(edit));
|
||||||
|
$('newPreset').hidden = !edit;
|
||||||
|
document.querySelector('.tab[data-tab="settings"]').hidden = !edit;
|
||||||
|
if (!edit) {
|
||||||
|
document.querySelector('.tab[data-tab="settings"]').classList.remove('active');
|
||||||
|
showTab('presets');
|
||||||
|
if (!$('editor-modal').hidden) closeEditor();
|
||||||
|
}
|
||||||
|
renderPresets();
|
||||||
|
}
|
||||||
|
|
||||||
|
function setMode(mode) {
|
||||||
|
uiMode = mode === 'edit' ? 'edit' : 'run';
|
||||||
|
try { localStorage.setItem(MODE_KEY, uiMode); } catch (_) { /* ignore */ }
|
||||||
|
applyMode();
|
||||||
|
setMenu(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
function showTab(id) {
|
||||||
|
document.querySelectorAll('.tab').forEach((b) => b.classList.toggle('active', b.dataset.tab === id));
|
||||||
|
['presets', 'settings'].forEach((s) => { $(s).hidden = s !== id; });
|
||||||
|
}
|
||||||
|
|
||||||
|
document.querySelectorAll('.tab').forEach((btn) => {
|
||||||
|
btn.addEventListener('click', () => {
|
||||||
|
showTab(btn.dataset.tab);
|
||||||
|
setMenu(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
$('modeToggle').addEventListener('click', () => setMode(isEditMode() ? 'run' : 'edit'));
|
||||||
|
$('newPreset').addEventListener('click', () => setMenu(false));
|
||||||
|
applyMode();
|
||||||
|
|
||||||
|
renderColours();
|
||||||
|
updateOutputs();
|
||||||
loadStatus().catch((e) => { $('status').textContent = e.message; });
|
loadStatus().catch((e) => { $('status').textContent = e.message; });
|
||||||
|
loadPresets().catch((e) => { $('status').textContent = e.message; });
|
||||||
loadWifi().catch(() => {});
|
loadWifi().catch(() => {});
|
||||||
setInterval(() => loadStatus().catch(() => {}), 5000);
|
loadNetwork().catch(() => {});
|
||||||
|
loadHardware().catch(() => {});
|
||||||
|
loadFirmware().catch((e) => { $('fwInfo').textContent = e.message; });
|
||||||
|
setInterval(() => {
|
||||||
|
loadStatus().catch(() => {});
|
||||||
|
loadPresets().catch(() => {});
|
||||||
|
}, 5000);
|
||||||
})();
|
})();
|
||||||
+149
-20
@@ -9,35 +9,73 @@
|
|||||||
<body>
|
<body>
|
||||||
<header>
|
<header>
|
||||||
<h1>LED strip</h1>
|
<h1>LED strip</h1>
|
||||||
<nav>
|
<button id="menuToggle" type="button" class="menu-toggle" aria-controls="menu" aria-expanded="false" aria-label="Menu">
|
||||||
<button class="tab active" data-tab="patterns">Patterns</button>
|
<span class="menu-bars" aria-hidden="true"></span>
|
||||||
|
</button>
|
||||||
|
<nav id="menu">
|
||||||
|
<button class="tab active" data-tab="presets">Presets</button>
|
||||||
<button class="tab" data-tab="settings">Settings</button>
|
<button class="tab" data-tab="settings">Settings</button>
|
||||||
|
<button id="modeToggle" type="button" class="mode-toggle" aria-pressed="false">Edit mode</button>
|
||||||
|
<button id="newPreset" type="button" class="tab-action" hidden>+ New preset</button>
|
||||||
|
<label class="header-bright">Brightness
|
||||||
|
<input type="range" id="globalBright" min="0" max="255" step="1">
|
||||||
|
<output id="globalOut"></output>
|
||||||
|
</label>
|
||||||
</nav>
|
</nav>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
<section id="patterns">
|
<!-- Front: the preset buttons, each with an Edit button -->
|
||||||
<label>Pattern
|
<section id="presets">
|
||||||
<select id="mode"></select>
|
<div id="presetList" class="preset-grid"></div>
|
||||||
</label>
|
|
||||||
<div class="row">
|
|
||||||
<label>Colour
|
|
||||||
<input type="color" id="c1" value="#ff0000">
|
|
||||||
</label>
|
|
||||||
<label>Colour 2
|
|
||||||
<input type="color" id="c2" value="#000000">
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
<label>Speed <output id="speedOut"></output>
|
|
||||||
<input type="range" id="speed" min="10" max="3000" step="10">
|
|
||||||
</label>
|
|
||||||
<label>Brightness <output id="brightOut"></output>
|
|
||||||
<input type="range" id="bright" min="0" max="255" step="1">
|
|
||||||
</label>
|
|
||||||
<p id="status" class="muted" role="status"></p>
|
<p id="status" class="muted" role="status"></p>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section id="settings" hidden>
|
<section id="settings" hidden>
|
||||||
|
<h2>Presets</h2>
|
||||||
|
<p class="muted">Adds the default presets (Rainbow, Chase, Aurora, Candle, Twinkle, Flame, Sweep, Off) that are not in the list yet. Your own presets are kept.</p>
|
||||||
|
<button id="addDefaults" type="button">Add default presets</button>
|
||||||
|
<p id="defaultsMsg" class="muted" role="status"></p>
|
||||||
|
|
||||||
|
<h2>LED strip</h2>
|
||||||
|
<form id="hwForm">
|
||||||
|
<label>Number of LEDs
|
||||||
|
<input type="number" id="hwLeds" min="1" max="512" step="1" required>
|
||||||
|
</label>
|
||||||
|
<label>Data pin (GPIO)
|
||||||
|
<input type="number" id="hwPin" min="0" max="21" step="1" required>
|
||||||
|
</label>
|
||||||
|
<p class="muted">0-10 or 20-21. Pins 11-19 are used by the flash and USB.</p>
|
||||||
|
<label>Colour order
|
||||||
|
<select id="hwOrder">
|
||||||
|
<option value="rgb">RGB</option>
|
||||||
|
<option value="rbg">RBG</option>
|
||||||
|
<option value="grb">GRB (WS2812 default)</option>
|
||||||
|
<option value="gbr">GBR</option>
|
||||||
|
<option value="brg">BRG</option>
|
||||||
|
<option value="bgr">BGR</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<label>Max brightness <output id="hwMaxOut"></output>
|
||||||
|
<input type="range" id="hwMax" min="0" max="255" step="1">
|
||||||
|
</label>
|
||||||
|
<button type="submit">Save strip settings</button>
|
||||||
|
</form>
|
||||||
|
<p id="hwMsg" class="muted" role="status"></p>
|
||||||
|
|
||||||
|
<h2>Firmware</h2>
|
||||||
|
<p>Running <span id="fwInfo">…</span></p>
|
||||||
|
<form id="fwForm">
|
||||||
|
<label>Firmware file (.bin)
|
||||||
|
<input type="file" id="fwFile" accept=".bin,application/octet-stream">
|
||||||
|
</label>
|
||||||
|
<button type="submit" id="fwUpload">Update firmware</button>
|
||||||
|
</form>
|
||||||
|
<div class="row">
|
||||||
|
<button id="fwReboot" type="button" class="btn-secondary">Reboot</button>
|
||||||
|
</div>
|
||||||
|
<p id="fwMsg" class="muted" role="status"></p>
|
||||||
|
|
||||||
<h2>Access point</h2>
|
<h2>Access point</h2>
|
||||||
<p>Network <strong id="apName">…</strong> (open). Address <strong>192.168.4.1</strong>.</p>
|
<p>Network <strong id="apName">…</strong> (open). Address <strong>192.168.4.1</strong>.</p>
|
||||||
|
|
||||||
@@ -56,9 +94,100 @@
|
|||||||
<button type="submit">Save and connect</button>
|
<button type="submit">Save and connect</button>
|
||||||
</form>
|
</form>
|
||||||
<p id="wifiMsg" class="muted" role="status"></p>
|
<p id="wifiMsg" class="muted" role="status"></p>
|
||||||
|
|
||||||
|
<h2>Device name and address</h2>
|
||||||
|
<form id="netForm">
|
||||||
|
<label>Name (<span id="mdnsName">led</span>.local)
|
||||||
|
<input id="hostname" required maxlength="63" pattern="[-A-Za-z0-9]+" autocomplete="off">
|
||||||
|
</label>
|
||||||
|
<label>Client address
|
||||||
|
<select id="ipMode">
|
||||||
|
<option value="dhcp">Automatic (DHCP)</option>
|
||||||
|
<option value="static">Static</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<div id="staticFields" hidden>
|
||||||
|
<label>IP address
|
||||||
|
<input id="sIp" inputmode="decimal" placeholder="192.168.1.50" autocomplete="off">
|
||||||
|
</label>
|
||||||
|
<label>Netmask
|
||||||
|
<input id="sMask" inputmode="decimal" placeholder="255.255.255.0" autocomplete="off">
|
||||||
|
</label>
|
||||||
|
<label>Gateway
|
||||||
|
<input id="sGw" inputmode="decimal" placeholder="192.168.1.1" autocomplete="off">
|
||||||
|
</label>
|
||||||
|
<label>DNS <span class="muted">(optional, blank uses the gateway)</span>
|
||||||
|
<input id="sDns" inputmode="decimal" autocomplete="off">
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<button type="submit">Save network settings</button>
|
||||||
|
</form>
|
||||||
|
<p id="netMsg" class="muted" role="status"></p>
|
||||||
|
<p class="muted">The access point always stays at 192.168.4.1, so you can get back in if a setting breaks the client connection.</p>
|
||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
<!-- Preset editor: a child window opened from Edit or New preset -->
|
||||||
|
<div id="editor-modal" class="modal" hidden>
|
||||||
|
<div class="modal-content" role="dialog" aria-modal="true" aria-labelledby="editor-title">
|
||||||
|
<div class="modal-head">
|
||||||
|
<h2 id="editor-title">Preset</h2>
|
||||||
|
<button id="editorClose" type="button" class="modal-close" aria-label="Close">×</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<label>Preset name
|
||||||
|
<input id="presetName" maxlength="31" autocomplete="off" placeholder="e.g. warm chase">
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label>Pattern
|
||||||
|
<select id="pattern"></select>
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<div class="field">
|
||||||
|
<span class="label">Colours</span>
|
||||||
|
<div id="colours" class="swatches"></div>
|
||||||
|
<div class="row">
|
||||||
|
<button id="addColour" type="button">Add colour</button>
|
||||||
|
<button id="removeColour" type="button">Remove colour</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<label>Background
|
||||||
|
<input type="color" id="bg" value="#000000">
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label>Delay <output id="delayOut"></output>
|
||||||
|
<input type="range" id="delay" min="10" max="3000" step="10">
|
||||||
|
</label>
|
||||||
|
<label>Brightness <output id="brightOut"></output>
|
||||||
|
<input type="range" id="bright" min="0" max="255" step="1">
|
||||||
|
</label>
|
||||||
|
<label>Fade in <output id="fadeOut"></output>
|
||||||
|
<input type="range" id="fade" min="0" max="5000" step="50">
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<div class="checks">
|
||||||
|
<label class="check"><input type="checkbox" id="auto"> Keep running (auto)</label>
|
||||||
|
<label class="check"><input type="checkbox" id="reverse"> Reverse direction</label>
|
||||||
|
<label class="check"><input type="checkbox" id="beatTime"> Beat timing (quarter beats)</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<label id="styleRow" hidden>Style
|
||||||
|
<select id="style"></select>
|
||||||
|
</label>
|
||||||
|
<div id="numbers" class="numbers"></div>
|
||||||
|
|
||||||
|
<p id="presetMsg" class="muted" role="status"></p>
|
||||||
|
|
||||||
|
<div class="modal-actions">
|
||||||
|
<button id="apply" type="button" class="btn-secondary">Apply to strip</button>
|
||||||
|
<button id="presetStartup" type="button" class="btn-secondary" disabled>Use at startup</button>
|
||||||
|
<button id="presetDelete" type="button" class="btn-danger" disabled>Delete</button>
|
||||||
|
<button id="presetSave" type="button">Save preset</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<script src="/app.js"></script>
|
<script src="/app.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
+594
-112
@@ -1,121 +1,603 @@
|
|||||||
:root {
|
/* Styled to match led-controller: dark grey panels, a darker header, grey buttons, purple selection */
|
||||||
--bg: #f6f6f7;
|
|
||||||
--fg: #1d1d1f;
|
|
||||||
--muted: #6e6e73;
|
|
||||||
--card: #ffffff;
|
|
||||||
--accent: #2563eb;
|
|
||||||
--border: #d2d2d7;
|
|
||||||
}
|
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
* {
|
||||||
:root:not([data-theme="light"]) {
|
margin: 0;
|
||||||
--bg: #111114;
|
padding: 0;
|
||||||
--fg: #f2f2f5;
|
box-sizing: border-box;
|
||||||
--muted: #9a9aa3;
|
|
||||||
--card: #1c1c21;
|
|
||||||
--accent: #5b8cff;
|
|
||||||
--border: #34343c;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
:root[data-theme="dark"] {
|
|
||||||
--bg: #111114;
|
|
||||||
--fg: #f2f2f5;
|
|
||||||
--muted: #9a9aa3;
|
|
||||||
--card: #1c1c21;
|
|
||||||
--accent: #5b8cff;
|
|
||||||
--border: #34343c;
|
|
||||||
}
|
|
||||||
|
|
||||||
* { box-sizing: border-box; }
|
|
||||||
|
|
||||||
body {
|
body {
|
||||||
margin: 0;
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
|
||||||
padding: 0 16px 32px;
|
background-color: #2e2e2e;
|
||||||
background: var(--bg);
|
color: white;
|
||||||
color: var(--fg);
|
min-height: 100vh;
|
||||||
font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
header {
|
|
||||||
max-width: 560px;
|
|
||||||
margin: 0 auto;
|
|
||||||
padding: 16px 0 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
h1 { margin: 0 0 12px; font-size: 1.5rem; }
|
|
||||||
h2 { font-size: 1.1rem; margin: 20px 0 8px; }
|
|
||||||
|
|
||||||
nav { display: flex; gap: 8px; }
|
|
||||||
|
|
||||||
main {
|
|
||||||
max-width: 560px;
|
|
||||||
margin: 0 auto;
|
|
||||||
background: var(--card);
|
|
||||||
border: 1px solid var(--border);
|
|
||||||
border-radius: 12px;
|
|
||||||
padding: 16px;
|
|
||||||
}
|
|
||||||
|
|
||||||
section > * + * { margin-top: 12px; }
|
|
||||||
|
|
||||||
label {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
gap: 6px;
|
|
||||||
font-weight: 500;
|
|
||||||
}
|
|
||||||
|
|
||||||
output { font-weight: 400; color: var(--muted); }
|
|
||||||
|
|
||||||
.row { display: flex; gap: 16px; flex-wrap: wrap; }
|
|
||||||
.row > label { flex: 1 1 140px; }
|
|
||||||
|
|
||||||
select, input[type="text"], input:not([type]), input[type="password"] {
|
|
||||||
font: inherit;
|
|
||||||
padding: 8px 10px;
|
|
||||||
border: 1px solid var(--border);
|
|
||||||
border-radius: 8px;
|
|
||||||
background: var(--bg);
|
|
||||||
color: var(--fg);
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
input[type="color"] {
|
|
||||||
width: 100%;
|
|
||||||
height: 44px;
|
|
||||||
padding: 2px;
|
|
||||||
border: 1px solid var(--border);
|
|
||||||
border-radius: 8px;
|
|
||||||
background: var(--bg);
|
|
||||||
}
|
|
||||||
|
|
||||||
input[type="range"] { width: 100%; accent-color: var(--accent); }
|
|
||||||
|
|
||||||
button {
|
|
||||||
font: inherit;
|
|
||||||
padding: 8px 14px;
|
|
||||||
border: 1px solid var(--border);
|
|
||||||
border-radius: 8px;
|
|
||||||
background: var(--bg);
|
|
||||||
color: var(--fg);
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
button.tab.active,
|
|
||||||
button[type="submit"] {
|
|
||||||
background: var(--accent);
|
|
||||||
border-color: var(--accent);
|
|
||||||
color: #fff;
|
|
||||||
}
|
|
||||||
|
|
||||||
.list { list-style: none; padding: 0; margin: 0; }
|
|
||||||
.list li + li { margin-top: 6px; }
|
|
||||||
button.net { width: 100%; text-align: left; }
|
|
||||||
|
|
||||||
.muted { color: var(--muted); font-size: 0.9rem; }
|
|
||||||
|
|
||||||
[hidden] { display: none !important; }
|
[hidden] { display: none !important; }
|
||||||
|
|
||||||
form { display: flex; flex-direction: column; gap: 12px; }
|
/* Header: title, tabs */
|
||||||
|
header {
|
||||||
|
background-color: #1a1a1a;
|
||||||
|
padding: 0.75rem 1rem;
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
border-bottom: 2px solid #4a4a4a;
|
||||||
|
gap: 0.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
header h1 {
|
||||||
|
font-size: 1.35rem;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
nav {
|
||||||
|
display: flex;
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.tab {
|
||||||
|
padding: 0.45rem 0.9rem;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
background-color: #3a3a3a;
|
||||||
|
color: white;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
font-weight: 500;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.tab:hover { background-color: #4a4a4a; }
|
||||||
|
|
||||||
|
button.tab.active {
|
||||||
|
background-color: #6a5acd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Run/Edit mode switch: the label is the mode it switches to */
|
||||||
|
button.mode-toggle {
|
||||||
|
padding: 0.45rem 0.9rem;
|
||||||
|
border: 1px solid #6a5acd;
|
||||||
|
border-radius: 4px;
|
||||||
|
background-color: transparent;
|
||||||
|
color: white;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
font-weight: 500;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.mode-toggle:hover { background-color: #3a3a3a; }
|
||||||
|
|
||||||
|
body.mode-edit button.mode-toggle {
|
||||||
|
background-color: #6a5acd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Run mode: the Edit button and drag handles are not shown */
|
||||||
|
body.mode-run button.preset-edit {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.mode-run .preset-tile-row {
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Main panel */
|
||||||
|
main {
|
||||||
|
padding: 1rem;
|
||||||
|
max-width: 760px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
section > * + * { margin-top: 0.85rem; }
|
||||||
|
|
||||||
|
h2 {
|
||||||
|
font-size: 1.1rem;
|
||||||
|
font-weight: 600;
|
||||||
|
margin-top: 1.25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Form controls */
|
||||||
|
label {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.35rem;
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
output {
|
||||||
|
font-weight: 400;
|
||||||
|
color: #bdbdbd;
|
||||||
|
}
|
||||||
|
|
||||||
|
select,
|
||||||
|
input[type="text"],
|
||||||
|
input:not([type]),
|
||||||
|
input[type="password"],
|
||||||
|
input[type="number"] {
|
||||||
|
font: inherit;
|
||||||
|
padding: 0.4rem 0.5rem;
|
||||||
|
background-color: #2e2e2e;
|
||||||
|
border: 1px solid #4a4a4a;
|
||||||
|
border-radius: 4px;
|
||||||
|
color: white;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
input[type="color"] {
|
||||||
|
width: 100%;
|
||||||
|
height: 2.5rem;
|
||||||
|
padding: 0.15rem;
|
||||||
|
background-color: #2e2e2e;
|
||||||
|
border: 1px solid #4a4a4a;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
input[type="range"] {
|
||||||
|
width: 100%;
|
||||||
|
accent-color: #6a5acd;
|
||||||
|
}
|
||||||
|
|
||||||
|
input[type="checkbox"] {
|
||||||
|
accent-color: #6a5acd;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
font: inherit;
|
||||||
|
padding: 0.45rem 0.9rem;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
background-color: #4a4a4a;
|
||||||
|
color: white;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
font-weight: 500;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background-color 0.2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:hover { background-color: #5a5a5a; }
|
||||||
|
|
||||||
|
button:disabled {
|
||||||
|
opacity: 0.45;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
|
||||||
|
button[type="submit"] {
|
||||||
|
background-color: #6a5acd;
|
||||||
|
}
|
||||||
|
|
||||||
|
button[type="submit"]:hover { background-color: #7b6bdb; }
|
||||||
|
|
||||||
|
form {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
form button { align-self: flex-start; }
|
form button { align-self: flex-start; }
|
||||||
|
|
||||||
|
.row {
|
||||||
|
display: flex;
|
||||||
|
gap: 0.75rem;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
align-items: flex-end;
|
||||||
|
}
|
||||||
|
|
||||||
|
.row > label { flex: 1 1 140px; }
|
||||||
|
|
||||||
|
.field {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.35rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.swatches {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.swatches input[type="color"] { width: 3.5rem; }
|
||||||
|
|
||||||
|
.checks {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
label.check {
|
||||||
|
flex-direction: row;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
font-weight: 400;
|
||||||
|
}
|
||||||
|
|
||||||
|
label.check input { width: auto; }
|
||||||
|
|
||||||
|
details {
|
||||||
|
background-color: #3a3a3a;
|
||||||
|
border-radius: 4px;
|
||||||
|
padding: 0.6rem 0.8rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
details summary {
|
||||||
|
cursor: pointer;
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
|
||||||
|
details > * + * { margin-top: 0.6rem; }
|
||||||
|
|
||||||
|
.muted {
|
||||||
|
color: #bdbdbd;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.list {
|
||||||
|
list-style: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.list li + li { margin-top: 0.35rem; }
|
||||||
|
|
||||||
|
button.net {
|
||||||
|
width: 100%;
|
||||||
|
text-align: left;
|
||||||
|
background-color: #3a3a3a;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.net:hover { background-color: #4a4a4a; }
|
||||||
|
|
||||||
|
/* Preset tiles: as led-controller's pattern buttons, filled with the preset colours */
|
||||||
|
.preset-grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr));
|
||||||
|
gap: 0.6rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.preset-tile {
|
||||||
|
height: 5rem;
|
||||||
|
padding: 0 0.5rem;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.12rem;
|
||||||
|
position: relative;
|
||||||
|
background-color: #3a3a3a;
|
||||||
|
border: 3px solid #000;
|
||||||
|
border-radius: 4px;
|
||||||
|
color: white;
|
||||||
|
text-align: center;
|
||||||
|
text-shadow: 0 0 2px rgba(0, 0, 0, 0.8), 0 1px 2px rgba(0, 0, 0, 0.6);
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.preset-tile:hover { filter: brightness(1.12); }
|
||||||
|
|
||||||
|
button.preset-tile.active {
|
||||||
|
border-color: #ffffff;
|
||||||
|
outline: 3px solid #ffffff;
|
||||||
|
outline-offset: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header-bright {
|
||||||
|
flex-direction: row;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
font-weight: 500;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header-bright input[type="range"] { width: 7rem; }
|
||||||
|
|
||||||
|
.header-bright output { min-width: 2.5rem; color: #bdbdbd; font-weight: 400; }
|
||||||
|
|
||||||
|
button.tab-action {
|
||||||
|
padding: 0.45rem 0.9rem;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
background-color: #6a5acd;
|
||||||
|
color: white;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
font-weight: 500;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.tab-action:hover { background-color: #7b6bdb; }
|
||||||
|
|
||||||
|
.preset-tile-name {
|
||||||
|
font-weight: 600;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
max-width: 100%;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preset-tile-pattern {
|
||||||
|
font-size: 0.75rem;
|
||||||
|
opacity: 0.85;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preset-tile-row {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.3rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preset-tile-row.dragging {
|
||||||
|
opacity: 0.6;
|
||||||
|
outline: 2px dashed #bdbdbd;
|
||||||
|
outline-offset: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preset-tile-row { touch-action: pan-y; }
|
||||||
|
|
||||||
|
button.preset-edit {
|
||||||
|
padding: 0.2rem 0.5rem;
|
||||||
|
font-size: 0.8rem;
|
||||||
|
background-color: #3a3a3a;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.preset-edit:hover { background-color: #4a4a4a; }
|
||||||
|
|
||||||
|
/* Child window (the preset editor) */
|
||||||
|
.modal {
|
||||||
|
position: fixed;
|
||||||
|
inset: 0;
|
||||||
|
background-color: rgba(0, 0, 0, 0.6);
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-start;
|
||||||
|
justify-content: center;
|
||||||
|
padding: 1rem;
|
||||||
|
overflow-y: auto;
|
||||||
|
z-index: 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-content {
|
||||||
|
background-color: #2e2e2e;
|
||||||
|
padding: 1.25rem;
|
||||||
|
border-radius: 8px;
|
||||||
|
width: 100%;
|
||||||
|
max-width: 480px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.85rem;
|
||||||
|
margin: auto 0;
|
||||||
|
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-head {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-head h2 {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 1.2rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.modal-close {
|
||||||
|
background: none;
|
||||||
|
font-size: 1.5rem;
|
||||||
|
line-height: 1;
|
||||||
|
padding: 0 0.4rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.modal-close:hover { background: #3a3a3a; }
|
||||||
|
|
||||||
|
.modal-actions {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.5rem;
|
||||||
|
justify-content: flex-end;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.btn-secondary { background-color: #3a3a3a; }
|
||||||
|
button.btn-secondary:hover { background-color: #4a4a4a; }
|
||||||
|
button.btn-danger { background-color: #d32f2f; }
|
||||||
|
button.btn-danger:hover { background-color: #c62828; }
|
||||||
|
|
||||||
|
/* Phones and small tablets */
|
||||||
|
html {
|
||||||
|
-webkit-text-size-adjust: 100%;
|
||||||
|
text-size-adjust: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
overflow-x: hidden;
|
||||||
|
overflow-wrap: anywhere;
|
||||||
|
}
|
||||||
|
|
||||||
|
main {
|
||||||
|
width: 100%;
|
||||||
|
padding-left: max(1rem, env(safe-area-inset-left));
|
||||||
|
padding-right: max(1rem, env(safe-area-inset-right));
|
||||||
|
padding-bottom: max(1rem, env(safe-area-inset-bottom));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The menu toggle only shows on phones */
|
||||||
|
button.menu-toggle {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 600px) {
|
||||||
|
header {
|
||||||
|
padding: 0.6rem max(0.75rem, env(safe-area-inset-left));
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
header h1 {
|
||||||
|
flex: 1;
|
||||||
|
font-size: 1.2rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ☰ button: three bars, pushed to the right of the title */
|
||||||
|
button.menu-toggle {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
width: 2.75rem;
|
||||||
|
height: 2.75rem;
|
||||||
|
min-height: 2.75rem;
|
||||||
|
padding: 0;
|
||||||
|
background-color: #3a3a3a;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.menu-toggle:hover { background-color: #4a4a4a; }
|
||||||
|
|
||||||
|
button.menu-toggle[aria-expanded="true"] {
|
||||||
|
background-color: #6a5acd;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-bars {
|
||||||
|
position: relative;
|
||||||
|
display: block;
|
||||||
|
width: 1.25rem;
|
||||||
|
height: 2px;
|
||||||
|
background-color: white;
|
||||||
|
border-radius: 1px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-bars::before,
|
||||||
|
.menu-bars::after {
|
||||||
|
content: "";
|
||||||
|
position: absolute;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 2px;
|
||||||
|
background-color: white;
|
||||||
|
border-radius: 1px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.menu-bars::before { top: -6px; }
|
||||||
|
.menu-bars::after { top: 6px; }
|
||||||
|
|
||||||
|
/* The menu is collapsed until the toggle opens it */
|
||||||
|
nav {
|
||||||
|
display: none;
|
||||||
|
width: 100%;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.4rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
header.menu-open nav {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
nav > button.tab,
|
||||||
|
nav > button.mode-toggle,
|
||||||
|
nav > button.tab-action {
|
||||||
|
flex: 1 1 auto;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header-bright {
|
||||||
|
width: 100%;
|
||||||
|
justify-content: space-between;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header-bright input[type="range"] {
|
||||||
|
flex: 1;
|
||||||
|
width: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 16px inputs stop iOS from zooming into the page when focused */
|
||||||
|
select,
|
||||||
|
input[type="text"],
|
||||||
|
input:not([type]),
|
||||||
|
input[type="password"],
|
||||||
|
input[type="number"],
|
||||||
|
input[type="color"],
|
||||||
|
input[type="file"] {
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Bigger touch targets */
|
||||||
|
button,
|
||||||
|
button.tab,
|
||||||
|
button.tab-action,
|
||||||
|
button.preset-edit {
|
||||||
|
min-height: 2.75rem;
|
||||||
|
padding: 0.5rem 0.9rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.preset-edit {
|
||||||
|
min-height: 2.25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
input[type="range"] {
|
||||||
|
height: 1.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preset-grid {
|
||||||
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.preset-tile {
|
||||||
|
height: 5.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.row > label {
|
||||||
|
flex: 1 1 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.row > button {
|
||||||
|
flex: 1 1 auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The preset editor fills the screen; its buttons stack */
|
||||||
|
.modal {
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-content {
|
||||||
|
min-height: 100%;
|
||||||
|
max-width: none;
|
||||||
|
margin: 0;
|
||||||
|
border-radius: 0;
|
||||||
|
padding: max(1rem, env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right))
|
||||||
|
max(1.25rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
|
||||||
|
overflow-y: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-actions {
|
||||||
|
flex-direction: column-reverse;
|
||||||
|
justify-content: stretch;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-actions > button {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal-head h2 {
|
||||||
|
font-size: 1.1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.modal-close {
|
||||||
|
min-height: 2.75rem;
|
||||||
|
min-width: 2.75rem;
|
||||||
|
font-size: 1.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Settings forms: the buttons span the width */
|
||||||
|
form button {
|
||||||
|
align-self: stretch;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.net {
|
||||||
|
min-height: 2.75rem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (hover: none) {
|
||||||
|
/* No hover on touch screens: keep the pressed state visible instead */
|
||||||
|
button.preset-tile:hover {
|
||||||
|
filter: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
button.preset-tile:active {
|
||||||
|
filter: brightness(1.15);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user