// https://www.reddit.com/r/FastLED/comments/16bum9z/ways_to_reduce_memory_footprint_of_led_array/
#include <FastLED.h>
#define kMatrixWidth 16
#define kMatrixHeight 16
#define NUM_LEDS ((kMatrixWidth) * (kMatrixHeight))
// define a `leds` pointer, to keep compatibility with many FastLED sketches
CRGB* leds;
uint16_t XY(uint8_t x, uint8_t y) {
if (x >= kMatrixWidth || y >= kMatrixHeight || x < 0 || y < 0)
return -1;
if (y & 1)
x = kMatrixWidth - 1 - x;
return x + (y * kMatrixWidth);
}
void setup() {
// attach FastLED to the right pin, but with no LEDs
FastLED.addLeds<WS2812B, 3, GRB>(0, 0);
}
void loop() {
// allocate space on the stack to render the LEDs into
CRGB tempLeds[NUM_LEDS];
// point `leds` at the temporary storage (because wu_pixel writes to `leds`)
leds = tempLeds;
// tell FastLED where the temp storage is, and how large it is
FastLED[0].setLeds(tempLeds, NUM_LEDS);
// optionally zero out the tempLeds array, if your effect depends on this
FastLED.clear();
static uint16_t startHue = 0;
static uint16_t xPhase = 0;
static uint16_t yPhase = 64 * 256;
uint8_t pixelHue = startHue;
for (uint16_t i = 0; i < 128; i++) {
uint16_t x = 32767 + cos16(xPhase + i * 2 * 256);
uint16_t y = 32767 + sin16(yPhase + i * 2 * 256);
x /= 256 / (kMatrixWidth - 1);
y /= 256 / (kMatrixHeight - 1);
CRGB col = ColorFromPalette(RainbowColors_p, pixelHue, 255, LINEARBLEND);
wu_pixel(x, y, col);
pixelHue += 2;
}
startHue += 3;
xPhase += 512;
FastLED.show();
}
void wu_pixel(uint16_t x, uint16_t y, CRGB &col) {
// extract the fractional parts and derive their inverses
uint8_t xx = x & 0xff, yy = y & 0xff, ix = 255 - xx, iy = 255 - yy;
// calculate the intensities for each affected pixel
#define WU_WEIGHT(a, b) ((uint8_t)(((a) * (b) + (a) + (b)) >> 8))
uint8_t wu[4] = {WU_WEIGHT(ix, iy), WU_WEIGHT(xx, iy),
WU_WEIGHT(ix, yy), WU_WEIGHT(xx, yy)
};
#undef WU_WEIGHT
// multiply the intensities by the colour, and saturating-add them to the pixels
for (uint8_t i = 0; i < 4; i++) {
uint8_t local_x = (x >> 8) + (i & 1);
uint8_t local_y = (y >> 8) + ((i >> 1) & 1);
uint16_t xy = XY(local_x, local_y);
if (xy >= NUM_LEDS) continue;
leds[xy] += col % wu[i];
}
}