- 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>
185 lines
4.0 KiB
C++
185 lines
4.0 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 "beat_sync.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 &, int v)
|
|
{
|
|
return v; // reverse mirrors the strip only (led_at), it does not change direction
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
// A preset's timing is always milliseconds. Beats belong to sequences and animations, which the
|
|
// controller plays, so a preset never follows the tempo.
|
|
uint32_t dur(const fx_t &, int v)
|
|
{
|
|
return v < 0 ? 0 : (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;
|
|
}
|