#include <FastLED.h>
#define pWIDTH 128
#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;
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) {
return getPixelNumber(x, y);
}
void setup() {
FastLED.addLeds<WS2812B, 3, GRB>(leds, NUM_LEDS);
}
void loop() {
if (loadingFlag) {
loadingFlag = false;
FastLED.clear();
}
EVERY_N_SECONDS(15) {chooseNextPalette();}
EVERY_N_MILLISECONDS(12) nblendPaletteTowardPalette( gCurrentGradientPalette, gTargetGradientPalette, 12);
draw();
FastLED.delay(5);
}
bool setupm = 1;
const uint8_t C_X = pWIDTH / 2;
const uint8_t C_Y = pHEIGHT / 2;
const uint8_t mapp = 255 / (pWIDTH / 2);
struct {
uint8_t angle;
uint8_t radius;
}
rMap[pWIDTH][pHEIGHT];
static uint8_t divider = 2;
void draw() {
//EVERY_N_SECONDS(10) { divider = random8(2,10); setupm = 1;} // !!! slider?
FastLED.clear();
if (setupm) {
setupm = 0;
for (int8_t x = -C_X; x < C_X + (pWIDTH % 2); x++) {
for (int8_t y = -C_Y; y < C_Y + (pHEIGHT % 2); y++) {
rMap[x + C_X][y + C_Y].angle = 255*(atan2(y, x) / PI)*divider; //atan2(x, y) - clockwise
rMap[x + C_X][y + C_Y].radius = hypot(x, y)*mapp; //hypot(y, x) - clockwise
}
}
}
static byte speed = 2;
static uint32_t t;
t += speed;
for (uint8_t x = 0; x < pWIDTH; x++) {
for (uint8_t y = 0; y < pHEIGHT; y++) {
byte angle = rMap[x][y].angle;
byte radius = rMap[x][y].radius;
leds[XY(x, y)] = CHSV(t + radius, 255, sin8(t * divider + sin8(t * divider - radius) + angle * 3));
//leds[XY(x, y)] = CHSV(sin8(t - radius), 255, sin8(t * divider + sin8(radius * divider) + angle * divider - 1));
//leds[XY(x, y)] = CHSV(t - rMap[x][y].radius, 255, sin8(t * divider - rMap[x][y].radius - rMap[x][y].angle * divider));
}
}
}