#include <FastLED.h>
#define pWIDTH 64
#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) {
uint16_t idx = getPixelNumber(x, y);
if (idx >= 0 && idx < NUM_LEDS) return idx;
}
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.show();
}
const uint8_t regime = 1;
uint8_t CodeR(uint8_t x, uint8_t y, uint32_t t) {
switch (regime) {
case 0:
return sin8(x * x) + sin8(t);
break;
case 1:
return sin8((x - (pWIDTH/2.)) * cos8((y + 20) * 4) / 4 + t);
break;
case 2:
return inoise8(x * 20, y * 20, t);
break;
case 3:
return sin8(x*x*8+y/2+t);
break;
case 4:
//return ((y-(pHEIGHT-1)/2.-t/25)/4.+(sin(x/2.))*sin(t/75))*128;
return 0;//(1-fmod(((t/210+x+sin(t/210+x)/1.5)-y/12),1.0))*255;
break;
}
}
uint8_t CodeG(uint8_t x, uint8_t y, uint32_t t) {
switch (regime) {
case 0:
return sin8(((x - (pWIDTH/2.)) * (x - (pWIDTH/2.)) + y * y) - t);
break;
case 1:
return (sin8(x * 16 + t / 3) + cos8(y * 8 + t / 2)) / 2; // cos8(sin8(x * 16 + cos8(t / 3)) + cos8(y * 8 + sin8(t / 5)));
break;
case 2:
return inoise8(x * 20, y * 20 + t);
break;
case 3:
return sin8(cos8(y*32)-t);
break;
case 4:
return sin(t/10-sqrt(pow((x-(pWIDTH-1)/2.),2)+pow((y-(pHEIGHT-1)/2.),2)))*255;
//return (sin(hypot(x-=((pWIDTH-1)/2.),y-=((pHEIGHT-1)/2.))-t/50+atan2(y,x)))*255;
break;
}
}
uint8_t CodeB(uint8_t x, uint8_t y, uint32_t t) {
switch (regime) {
case 0:
return sin8(x * 5 + sin8(y * 5 + t) - cos8(y));
break;
case 1:
return sin8(cos8(x * 8 + t / 3) + sin8(y * 8 + t / 4) + t);
break;
case 2:
return inoise8(x * 20 + t, y * 20);
break;
case 3:
return sin8(cos8(y*32)+t);
break;
case 4:
return 0;//-(sin(hypot(x-=(pWIDTH-1)/2.,y-=(pHEIGHT-1)/2.)-t/50+atan2(y,x)))*255;
break;
}
}
void draw() {
uint32_t t = millis() / 10.;
//fadeToBlackBy(leds, NUM_LEDS, 10);
FastLED.clear();
for (uint8_t x = 0; x < pWIDTH; x++) {
for (uint8_t y = 0; y < pHEIGHT; y++) {
leds[XY(x, y)] += CHSV (0, 255, CodeR(x, y, t));
leds[XY(x, y)] += CHSV (96, 255, CodeG(x, y, t));
leds[XY(x, y)] += CHSV (160, 255, CodeB(x, y, t));
}
}
blur2d(leds, pWIDTH, pHEIGHT, 128);
}