Files
led-driver/main/effects_rig.cpp
T
jimmyandClaude Haiku 5.5 9a04d28ecf 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>
2026-11-11 01:54:32 +13:00

296 lines
9.2 KiB
C++

/*
* 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;
}