#pragma once
#include <FastLED.h>
#include "fl/stl/fixed_point/s16x16.h" // from FastLED (was fl/fixed_point/s16x16.h in older versions)
using coord = fl::s16x16;
// convert integer pixel coordinates to buffer index (row-major, y down)
static inline int pixelIndex(int x, int y) {
if (kMatrixSerpentine && (y & 1)) {
// odd rows are reversed
return y * kMatrixWidth + (kMatrixWidth - 1 - x);
} else {
return y * kMatrixWidth + x;
}
}
// map fixed-point coordinate to integer pixel by truncation
static inline bool coordToPixel(coord x, coord y, int &outX, int &outY) {
// convert to integer, rounding toward zero (same as >> FRAC_BITS)
outX = x.to_int();
outY = y.to_int();
// unsigned cast: combines < 0 and >= width into a single comparison per axis
if ((unsigned)outX >= kMatrixWidth || (unsigned)outY >= kMatrixHeight) {
return false;
}
return true;
}
// Write a pixel's color using integer coords (overwrites existing value).
// Avoids fixed-point round-trip in hot loops where coordinates are already integers.
static inline bool setPixelXY(int x, int y, const CRGB &color) {
if ((unsigned)x >= kMatrixWidth || (unsigned)y >= kMatrixHeight) {
return false;
}
leds[pixelIndex(x, y)] = color;
return true;
}
// Additively composite a color onto a pixel using integer coords (dst += src).
// Used by AA drawing routines where multiple primitives or sub-pixel contributions
// accumulate on a freshly-cleared frame buffer.
static inline bool addPixelXY(int x, int y, const CRGB &color) {
if ((unsigned)x >= kMatrixWidth || (unsigned)y >= kMatrixHeight) {
return false;
}
leds[pixelIndex(x, y)] += color;
return true;
}
// Write a pixel's color using fixed-point coords (overwrites existing value).
static inline bool setPixel(coord x, coord y, const CRGB &color) {
int ix = x.to_int(), iy = y.to_int();
if ((unsigned)ix >= kMatrixWidth || (unsigned)iy >= kMatrixHeight) {
return false;
}
leds[pixelIndex(ix, iy)] = color;
return true;
}
// Additively composite a color onto a pixel using fixed-point coords (dst += src).
static inline bool addPixel(coord x, coord y, const CRGB &color) {
int ix = x.to_int(), iy = y.to_int();
if ((unsigned)ix >= kMatrixWidth || (unsigned)iy >= kMatrixHeight) {
return false;
}
leds[pixelIndex(ix, iy)] += color;
return true;
}
FPS: 0
Power: 0.00W
Power: 0.00W