 /***
 Arduino controller script for a interactive, light up table.
-
 Author: Bereket Abraham
 ***/
-
 #include "FastSPI_LED2.h"
 
+// col 13, row 8
+#define NUM_LEDS 104
+#define COL_SIZE 13
+#define ROW_SIZE 8
+#define DATA_PIN 6
 #define STATUS_PIN 3
-#define NUM_LEDS 50
-#define DATA_PIN 6
-CRGB leds[NUM_LEDS];
-
-int index1, index2, red, green, blue;
+#define BUFFER_SIZE 5
 #define DEBUG false
 
+CRGB leds[NUM_LEDS];
+int serialBuffer[] = {0,0,0,0,0};
+int currIndex = 0;
 
 void setColor(unsigned int index, unsigned long hexcolor) {
     // Accepts the LED index (int) and the 3 byte color # (unsigned long)
-
-    //leds[index] = CRGB::White;
     leds[index] = hexcolor;
     FastLED.show();
 


void setup() {
    Serial.begin(9600);
    index1 = 0;
    index2 = 0;
    red = 0;
    green = 0;
    blue = 0;

    // Set the LED controller
    delay(1000);
    FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
    blackout();
    if(DEBUG) { Serial.println("Lights engaged"); }

    // Set the status light
    pinMode(STATUS_PIN, OUTPUT);

}

void loop() {
    // 2 bytes for the index, 3 for the color
    if ( Serial.available() >= 5 )  {
        index1 = Serial.read();
        index2 = Serial.read();
        red = Serial.read();
        green = Serial.read();
        blue = Serial.read();

        // combine index bytes
        unsigned int index = ((unsigned int)index1 << 8) + (unsigned int)index2;
        // RGB, 0xFFAACC, 3 bytes
        unsigned long hexcolor = ((unsigned long)red << 16) + ((unsigned long)green << 8) + (unsigned long)blue;

        setColor(index, hexcolor);
    }

    //FastLED.show();
}



    }
}

unsigned int mapIndex(unsigned int index) {
    // map the row,col indexing to the layout of
    // the actual LED string
    // assuming a zigzag orientation
    int row = index / ROW_SIZE;
    int col = index % COL_SIZE;
    if((row % 2) == 0) {
      // invert the row if even 
      index = index - ((col*2) - COL_SIZE + 1);
    }
    return index;
}

void blackout() {
    for(int i = 0; i < NUM_LEDS; i++)
    { leds[i] = CRGB::Black; }
    
    FastLED.show();
    delay(2000);
}
void loop_lights() {
  // Move a single white led
    for(unsigned int ind = 0; ind < NUM_LEDS; ind++) {
        unsigned int whiteLed = mapIndex(ind);
        leds[whiteLed] = CRGB::White;
        // Show the leds (only one of which is set to white, from above)
        FastLED.show();
        delay(200);
        leds[whiteLed] = CRGB::Black;
    }
