Skip to content

Instantly share code, notes, and snippets.

@sutaburosu
Last active June 17, 2020 11:30
Show Gist options
  • Save sutaburosu/790860575a4421b0005b54aef9e50227 to your computer and use it in GitHub Desktop.
Save sutaburosu/790860575a4421b0005b54aef9e50227 to your computer and use it in GitHub Desktop.
A more flexible XY() mapping function for FastLED
#define kMatrixWidth 16
#define kMatrixHeight 16
#define kMatrixSerpentine true
#define kMatrixRowMajor true
#define kMatrixFlipMajor true
#define kMatrixFlipMinor false
#define NUM_LEDS ((kMatrixWidth) * (kMatrixHeight))
CRGB leds[NUM_LEDS + 1]; // 1 extra for XY() to use when out-of-bounds
uint16_t XY(uint8_t x, uint8_t y) {
uint8_t major, minor, sz_major, sz_minor;
if (x >= kMatrixWidth || y >= kMatrixHeight) return NUM_LEDS;
if (kMatrixRowMajor)
major = x, minor = y, sz_major = kMatrixWidth, sz_minor = kMatrixHeight;
else
major = y, minor = x, sz_major = kMatrixHeight, sz_minor = kMatrixWidth;
if (kMatrixFlipMajor ^ (minor & 1 && kMatrixSerpentine))
major = sz_major - 1 - major;
if (kMatrixFlipMinor)
minor = sz_minor - 1 - minor;
return (uint16_t) minor * sz_major + major;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment