// #define SEEED_XIAO_M0
#include <FastLED.h>
#include <colorutils.h>
#include <Toggle.h>
#include "branes_codeanim.h"
#include "branes_gradients.h"
// #define DATA_PIN_A (3)
#define DATA_PIN_A (4)
#define TOUCH_PIN (5)
Toggle touchButton(TOUCH_PIN);
uint8_t white_brightness = 80;
uint8_t turnOn = 0;
uint8_t firstTouch = 0;
// Define the array of leds
CRGB leds[NUM_LEDS];
CRGB *ledShow = (CRGB*)&leds[0];
uint8_t currState = 1;
#define NUM_STATES 3
bool inFrame = true;
uint32_t stateStartTime = 0;
uint32_t transitionStartTime = 0;
CRGB ledStates[NUM_STATES][NUM_LEDS];
uint32_t stateDurations[NUM_STATES] = {5000,5000,5000};
uint32_t transitionDurations[NUM_STATES] = {1000,1000,1000};
uint32_t staticRainbowOffset = 300;
void static_branes(CRGB* templeds);
void lateral_rainbow(CRGB* templeds);
void lateral_rainbow_shift_start(CRGB* ledsA);
void lateral_rainbow_shift();
void initializeStates();
CRGB lerp_RGB(uint16_t currAnimTime, uint16_t startTime, uint16_t transitionDuration,
CRGB startColor, CRGB endColor);
// void (*states[NUM_STATES])() = {&static_branes, &lateral_rainbow};
void setup(){
FastLED.addLeds<NEOPIXEL, DATA_PIN_A>(ledShow, NUM_LEDS).setCorrection(TypicalSMD5050); // GRB ordering is assumed
#ifdef ESPSIM
FastLED.setBrightness(200);
#else
FastLED.setBrightness(50);
#endif
// touchButton.begin(TOUCH_PIN);
initializeStates();
memcpy(leds,ledStates[currState],NUM_LEDS * sizeof(CRGB));
FastLED.show();
Serial.begin(9600);
Serial.println("poopy");
}
void static_branes(CRGB* ledsA){
for(int led = 0; led < NUM_LEDS; led++){
ledsA[led] = ColorFromPalette(branes_static_pal,map(LEDA_X[led],0,IMAGE_WIDTH,0,255));
Serial.println(ledsA[led].r);
}
}
void lateral_rainbow(CRGB* ledsA){
for(int led = 0; led < NUM_LEDS; led++){
ledsA[led] = ColorFromPalette(RainbowColors_p,map((LEDA_X[led]+staticRainbowOffset)%IMAGE_WIDTH,0,IMAGE_WIDTH,255,0));
}
}
void lateral_rainbow_shift(CRGB* ledsA){
for(int led = 0; led < NUM_LEDS; led++){
ledsA[led] = ColorFromPalette(RainbowColors_p,map(LEDA_X[led],0,IMAGE_WIDTH,255,0));
}
}
void lateral_rainbow_shift(){
static uint16_t hueShift = 0;
hueShift = hueShift - 10;
for(int led = 0; led < NUM_LEDS; led++){
ledStates[1][led] = ColorFromPalette(RainbowColors_p,map((LEDA_X[led]+hueShift)%IMAGE_WIDTH,0,IMAGE_WIDTH,255,0));
}
Serial.println(hueShift);
}
void initializeStates(){
static_branes(ledStates[0]);
lateral_rainbow_shift(ledStates[1]);
lateral_rainbow(ledStates[2]);
}
CRGB lerp_RGB(uint16_t currAnimTime, uint16_t startTime, uint16_t transitionDuration,
CRGB startColor, CRGB endColor){
uint8_t red = map(currAnimTime, startTime, transitionDuration, startColor.red, endColor.red);
uint8_t green = map(currAnimTime, startTime, transitionDuration, startColor.green, endColor.green);
uint8_t blue = map(currAnimTime, startTime, transitionDuration, startColor.blue, endColor.blue);
return CRGB(red,green,blue);
}
void loop(){
EVERY_N_MILLISECONDS(42){
if(!inFrame){
// end of transition
if((millis() - transitionStartTime) > transitionDurations[currState]){
stateStartTime = millis();
currState = (currState + 1 ) % NUM_STATES;
// ledShow = (CRGB*)&(ledStates[currState][0]);
// CLEDController::m_pHead->m_Data = (CRGB*)&(ledStates[currState][0]);
memcpy(leds,ledStates[currState],NUM_LEDS * sizeof(CRGB));
inFrame = true;
}
else{
// in middle of transition
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = lerp_RGB(millis() - transitionStartTime, 0,
transitionDurations[currState],
ledStates[currState][i],
ledStates[(currState+1)%NUM_STATES][i]);
}
}
}
else if(inFrame && (millis() - stateStartTime) > stateDurations[currState]){
// end of static state
transitionStartTime = millis();
inFrame = false;
// ledShow = (CRGB*)&leds[0];
#ifdef USBXIAO
Serial.println("endOfState");
#endif
}
else{
if(currState == 1){
memcpy(leds,ledStates[currState],NUM_LEDS * sizeof(CRGB));
}
}
// if(currState == 1){
lateral_rainbow_shift();
Serial.println("boo");
// }
FastLED.show();
}
// EVERY_N_MILLIS(10){
// if(currState == 1){
// lateral_rainbow_shift();
// FastLED.show();
// }
// }
}