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

186 lines
3.9 KiB
C++

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