Files
jimmyandClaude Opus 5.5 24dd5ffb15 feat: ESP-NOW link, beat sync and control messages in the C++ driver
- espnow_link, ctrl_msg and beat_sync: the reliable link, envelopes and
  beat-synced timing.
- ctrl_msg: accept an envelope with the trailing save byte.
- patterns_frame() and espnow_link_poll() split out of their tasks, so the
  WebAssembly simulator can drive one frame and one link pass from a timer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 19:32:36 +13:00

949 lines
29 KiB
C++

/*
* 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 = f.pr->n[3] == 0 ? 1 : f.pr->n[3]; // negative steps walk the other way
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 + 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);
// Steps may be negative: a negative n3/n4 walks the dot the other way. 0 (unset) means 1.
int n3 = f.pr->n[3] == 0 ? 1 : f.pr->n[3];
int n4 = f.pr->n[4] == 0 ? 1 : 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 * max_pos); // the dot walks the whole cycle; even modulus keeps the parity
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 * max_pos); // the dot walks the whole cycle; even modulus keeps the parity
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;
}
// Rainbow: the spectrum wheel scrolling at n1 steps per frame (the controller's "rainbow" pattern)
bool fx_rainbow(fx_t &f, st_t &s)
{
int step_amount = nz(f.pr->n[1], 1);
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;
}
// ---- 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 --------------------------------------------------------------------------------
// Wipe: the next colour sweeps in from the first LED to the last over `delay` ms, then becomes the
// current colour and the one after it starts wiping in.
bool fx_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;
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;
elapsed = 0;
}
rgb cur = c[s.ci % nc];
rgb nxt = c[(s.ci + 1) % nc];
int front = (elapsed * f.n) / duration; // LEDs already showing the next colour
for (int i = 0; i < f.n; i++) {
putl(f, i, ab(i < front ? nxt : cur, b));
}
return false;
}
// Transition: the whole bar shows one colour, which fades from each colour to the next over `delay` ms.
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;
elapsed = 0;
}
rgb cur = c[s.ci % nc];
rgb nxt = c[(s.ci + 1) % nc];
int k = (elapsed * 255) / duration; // 0 = all current colour, 255 = all next colour
fill(f, ab(mix(cur, nxt, k), 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;
}