Files
led-driver/main/effects.h
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

150 lines
5.6 KiB
C++

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