#include <FastLED.h>
const uint8_t kMatrixWidth = 5;
const uint8_t kMatrixHeight = 30;
const bool kMatrixSerpentineLayout = false;
uint16_t XY( uint8_t x, uint8_t y)
{
uint16_t i;
if( kMatrixSerpentineLayout == false) {
i = (y * kMatrixWidth) + x;
}
if( kMatrixSerpentineLayout == true) {
if( y & 0x01) {
// Odd rows run backwards
uint8_t reverseX = (kMatrixWidth - 1) - x;
i = (y * kMatrixWidth) + reverseX;
} else {
// Even rows run forwards
i = (y * kMatrixWidth) + x;
}
}
return i;
}
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
CRGB* const leds( leds_plus_safety_pixel + 1);
uint16_t XYsafe( uint8_t x, uint8_t y)
{
if( x >= kMatrixWidth) return -1;
if( y >= kMatrixHeight) return -1;
return XY(x,y);
}
int16_t XYsafe_signed( uint8_t x, uint8_t y)
{
if( x >= kMatrixWidth) return -1;
if( y >= kMatrixHeight) return -1;
return XY(x,y);
}
void loop() {
delay(1000);
}
void print_rgb(int32_t ledno) {
Serial.print("leds_plus_safety_pixel[");
Serial.print(ledno);
Serial.print("] = ");
Serial.print(leds_plus_safety_pixel[ledno].r);
Serial.print(", ");
Serial.print(leds_plus_safety_pixel[ledno].g);
Serial.print(", ");
Serial.println(leds_plus_safety_pixel[ledno].b);
}
void setup() {
Serial.begin(9600);
print_rgb(0);
Serial.println("Setting leds[XYsafe(255, 255)] to white.");
leds[XYsafe(255, 255)] = CRGB::White;
print_rgb(0);
Serial.println("Setting leds[XYsafe_signed(255, 255)] to white.");
leds[XYsafe_signed(255, 255)] = CRGB::White;
print_rgb(0);
}