#include "FastLED.h"

#define NUM_LEDS 124 //Type in the number of LED's that you will use 
#define DATA_PIN 6 //Type in the PWM IO Pin of the Arduino the data pin is connected to 
#define COLOR_ORDER RGB
#define SerialRate 500000 //Baudrate: higher = faster refresh rate
CRGB leds[NUM_LEDS];

uint8_t prefix[] = {'A', 'd', 'a'}, hi, lo, chk, i; //Magic Word

void setup() {
        FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
        LEDS.showColor(CRGB(255, 0, 0)); //testing
        delay(500);
        LEDS.showColor(CRGB(0, 255, 0));
        delay(500);
        LEDS.showColor(CRGB(0, 0, 255));
        delay(500);
        LEDS.showColor(CRGB(0, 0, 0));
       
        Serial.begin(SerialRate);
        Serial.print("Ada\n"); // Send Magic Word string to host
}
 
void loop() {

  int switchPin = digitalRead(12);
  if (switchPin == 0){
    for(i = 0; i < sizeof prefix; ++i) {
    waitLoop: while (!Serial.available()) ;;
    if(prefix[i] == Serial.read()) continue;
    i = 0;
    goto waitLoop;
  }
 
  while (!Serial.available()) ;;
  hi=Serial.read();
  while (!Serial.available()) ;;
  lo=Serial.read();
  while (!Serial.available()) ;;
  chk=Serial.read();

  if (chk != (hi ^ lo ^ 0x55))
  {
    i=0;
    goto waitLoop;
  }

    memset(leds, 0, NUM_LEDS * sizeof(struct CRGB));
    for (uint8_t i = 0; i < NUM_LEDS; i++) {
      byte r, g, b;    
      while(!Serial.available());
      r = Serial.read();
      while(!Serial.available());
      g = Serial.read();
      while(!Serial.available());
      b = Serial.read();
      leds[i].r = r;
      leds[i].g = g;
      leds[i].b = b;
     }
     
     FastLED.show(); //Update with new values   
  }
  
  else{
    LEDS.showColor(CRGB(0, 0, 0));
    FastLED.show();  
      }
}
