/*
  Blink

  Turns an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino
  model, check the Technical Specs of your board at:
  https://www.arduino.cc/en/Main/Products

  modified 8 May 2014
  by Scott Fitzgerald
  modified 2 Sep 2016
  by Arturo Guadalupi
  modified 8 Sep 2016
  by Colby Newman

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Blink
*/
#include <Adafruit_NeoPixel.h>

#define MAX_CAMERA 2
// Which pin on the Arduino is connected to the NeoPixels?
#define PIN 2
 
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 32
#define SEPARATOR '\n' //'|' 

Adafruit_NeoPixel *strip[MAX_CAMERA];

typedef enum { Nothing, Preview, Live } enumMode;

void Blink(int idelay = 200) {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(idelay);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(idelay);                       // wait for a second

}

void setStrip(int istrip, enumMode mode) {
    for(int i=0;i<NUMPIXELS;i++){

      // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
      switch(mode)
      {
        case Preview:
          strip[istrip]->setPixelColor(i, strip[istrip]->Color( 255, 0, 0));
          break;
        case Live:
          strip[istrip]->setPixelColor(i, strip[istrip]->Color( 0, 255, 0));
          break;
        default:
          strip[istrip]->setPixelColor(i, strip[istrip]->Color( 0, 0, 0));
      }
      strip[istrip]->show(); // This sends the updated pixel color to the hardware. 
    }
    /*
    switch(mode)
    {
      case Preview:
        Blink();
        break;
      case Live:
        Blink();
        Blink();
        break;
    }
    */
}


// the setup function runs once when you press reset or power the board
void setup() {
  Serial.begin(115200);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  for(int i=0; i<MAX_CAMERA; i++){
    strip[i]=new Adafruit_NeoPixel(NUMPIXELS, PIN+i, NEO_GRB + NEO_KHZ800);
    strip[i]->begin();// This initializes the NeoPixel library.
    strip[i]->setBrightness(32);
    strip[i]->show();// This sends the updated pixel color to the hardware.
  }
  delay(1000);
  Serial.println("Ready!");
  
}

bool bBlink = false;

String buffer;

// the loop function runs over and over again forever
void loop() {
  int iCam = 0;
  //Serial.print(".");
  if (Serial.available()){
    buffer = buffer + Serial.readString();
  }

  if (buffer.length()>1) {
    int pos = buffer.indexOf(SEPARATOR);
    //Serial.print(pos);
    if (pos>0) {
      String line = buffer.substring(0, pos);
      Serial.println(line);
      // Mise à jour du buffer
      buffer = buffer.substring(pos+1, buffer.length());
      //Serial.print(buffer);
  
      // on recherche le caractère "="
      int pos2 = line.indexOf('=');
      String status = line.substring(pos2+1,line.length());
      iCam = atoi(line.substring(0, pos2).c_str());
      Serial.print(iCam, status.c_str());
      
      if (status == "off"){
        Serial.println("status OFF");
        setStrip(iCam-1, enumMode::Nothing);
      } else
      if (status == "preview"){
        Serial.println("status PREVIEW");
        setStrip(iCam-1, enumMode::Preview);
      } else
      if (status == "live"){
        Serial.println("status LIVE");
        setStrip(iCam-1, enumMode::Live);
      } 
    }
  }

// #ifdef 0
/*
  if (iCam>0) {
    for(int i=0; i<iCam; i++){
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(200);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(200);                       // wait for a second
    }
    delay(1000);
  }
  */
// #endif
 delay(500);
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
ring1:GND
ring1:VCC
ring1:DIN
ring1:DOUT
ring2:GND
ring2:VCC
ring2:DIN
ring2:DOUT
ring3:GND
ring3:VCC
ring3:DIN
ring3:DOUT
ring4:GND
ring4:VCC
ring4:DIN
ring4:DOUT