#include <FastLED.h>
#define pWIDTH 5
#define pHEIGHT 8
#define NUM_LEDS ((pWIDTH) * (pHEIGHT))
extern CRGBPalette16 gCurrentGradientPalette;
extern CRGBPalette16 gTargetGradientPalette;
CRGB leds[NUM_LEDS];
bool loadingFlag = true;
void drawPixelXY(int8_t x, int8_t y, CRGB color) {
if (leds == nullptr) return;
if (x < 0 || x > pWIDTH - 1 || y < 0 || y > pHEIGHT - 1) return;
int16_t thisPixel = getPixelNumber(x, y);
if (thisPixel >= 0 && thisPixel < NUM_LEDS) leds[thisPixel] = color;
}
uint16_t getPixelNumber(uint8_t x, uint8_t y) {
if (x >= pWIDTH || y >= pHEIGHT)
return NUM_LEDS-1;
if (y & 1)
x = pWIDTH - 1 - x;
return x + (y * pWIDTH);
}
uint32_t getPixColor(int16_t thisPixel) {
if (leds == nullptr) return 0;
if (thisPixel < 0 || thisPixel > NUM_LEDS - 1) return 0;
return (((uint32_t)leds[thisPixel].r << 16) | ((uint32_t)leds[thisPixel].g << 8 ) | (uint32_t)leds[thisPixel].b);
}
uint32_t getPixColorXY(int8_t x, int8_t y) {
return getPixColor(getPixelNumber(x, y));
}
uint16_t XY(uint8_t x, uint8_t y) {
int16_t idx = getPixelNumber(x, y);
if (idx >= 0 && idx < NUM_LEDS) return idx;
}
void shiftDown() {
for (uint8_t x = 0; x < pWIDTH; x++) {
for (uint8_t y = 0; y < pHEIGHT - 1; y++) {
drawPixelXY(x, y, getPixColorXY(x, y - (y>0)?1:0));
}
}
}
void shiftUp() {
for (uint8_t x = 0; x < pWIDTH; x++) {
for (uint8_t y = pHEIGHT - 1; y > 0; y--) {
drawPixelXY(x, y, getPixColorXY(x, y - 1));
}
}
}
void setup() {
FastLED.addLeds<WS2812B, 3, GRB>(leds, NUM_LEDS);
}
void loop() {
if (loadingFlag) {
loadingFlag = false;
FastLED.clear();
}
draw();
FastLED.delay(120);
}
void draw() {
static uint8_t step=0, row=0, col=0;
static bool dir=true;
//fadeToBlackBy(leds, NUM_LEDS, 16);
shiftUp();
if (step==0){
for(uint8_t x=0; x<pWIDTH; x++){
if(x==col) drawPixelXY(x, 0, CHSV(48, 255, 255));
else drawPixelXY(x, 0, CHSV(0, 0, 0));
// if (row>0) drawPixelXY(x, row-1, CHSV(0, 0, 0));
// if (row==0) drawPixelXY(x, pHEIGHT-1, CHSV(0, 0, 0));
}
}
if (++step>1) {
step=0;
if (++row>pHEIGHT) row=0;
if (col<pWIDTH-1&&dir) {
col++;
if (col==pWIDTH-1) dir = false;
}
else {
col--;
if (col==0) dir = true;
}
}
// EVERY_N_MILLISECONDS(20) {hue++;}
// EVERY_N_SECONDS(10) {chooseNextPalette();}
// EVERY_N_MILLISECONDS(12) nblendPaletteTowardPalette( gCurrentGradientPalette, gTargetGradientPalette, 12);
}