#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include <ds3231.h>

#define PIN 2
 int encoderPosCount = 0;
 int pinALast;
 int aVal;
 boolean bCW;
//TIMER
struct ts t; //déclaration variable t
 //-----BOUTON QUI TOURNE-----
  #define CLK 3
 #define DATA 4
 #define BUTTON_PIN   5
 int Rotation = 0;
 int EtatCLK;
 int AncienEtatCLK;
 int Clique;
 int LectureBouton;
//Variable Menu :
int ruee=0;
int conquet=0;
int demo=0;
//Bouton
int PinBleu = 12;
int PinRouge = 10;
int PinVert = 11;
int EtatBleu;
int EtatRouge;
int EtatVert;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(32, PIN, NEO_GRB + NEO_KHZ800);
LiquidCrystal_I2C lcd(0x3F,20,4);  // set the LCD address to 0x27 for a 16 chars and 2 line display



void setup()
{
  Serial.begin (9600);
  Wire.begin();
  DS3231_init(DS3231_INTCN);

  pinMode (CLK,INPUT);
  pinMode (DATA,INPUT);
  AncienEtatCLK = digitalRead(CLK);



  lcd.init();                      // initialize the lcd
  lcd.init();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.setCursor(3,0);
  lcd.print("-TECHflag-");
  lcd.setCursor(12,0);
  lcd.setCursor(3,1);
  lcd.print("Conquet");
   lcd.setCursor(0,2);
  lcd.print("--->    DEMO    <---");
   lcd.setCursor(11,3);
  lcd.print("Ruee");
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  strip.begin();
  strip.setBrightness(30); //adjust brightness here
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
//-----------------------------------------------------------------------------
//-----------------------ROTATION----------------------------------------------
//-----------------------------------------------------------------------------
  EtatCLK = digitalRead(CLK);
  if (EtatCLK != AncienEtatCLK){
    if (digitalRead(DATA) != EtatCLK && Rotation < 1) {
      Rotation++;
    } else {if(Rotation > -1){
      Rotation++;
    }}
    if(Rotation > 1){
      Rotation = -1;
    }
    if(Rotation < -1){
      Rotation = 1;
    }
    Serial.print("Rotation: ");
    Serial.println(Rotation);
     AncienEtatCLK = EtatCLK;
  }
//-----------------------------------------------------------------------------
//-----------------------ACCEUIL-----------------------------------------------
//-----------------------------------------------------------------------------
 // Print a message to the LCD.
  LectureBouton = digitalRead(BUTTON_PIN);
  if (LectureBouton == HIGH){
    Clique = 1;
  }
  lcd.backlight();
  lcd.setCursor(5,0);
  lcd.print("-TECHflag-");
  if(Rotation == -1){
       lcd.setCursor(5,0);
       lcd.print("-TECHflag-");
       lcd.setCursor(3,1);
       lcd.print("Ruee");
       lcd.setCursor(0,2);
       lcd.print("--->   Conquet   <---");
       lcd.setCursor(11,3);
       lcd.print("DEMO");
       if(Clique == 1){
        conquet = 1;
       }
  }
  if(Rotation == 0){
      lcd.setCursor(3,1);
      lcd.print("Conquet");
      lcd.setCursor(0,2);
      lcd.print("--->    DEMO    <---");
      lcd.setCursor(11,3);
      lcd.print("Ruee");
      if(Clique == 1){
        demo = 1;
       }
  }
  if(Rotation == 1){
      lcd.setCursor(3,1);
      lcd.print("DEMO");
      lcd.setCursor(0,2);
      lcd.print("--->    Ruee    <---");
      lcd.setCursor(11,3);
      lcd.print("Conquet");
      if(Clique == 1){
        ruee = 1;
       }
  }

//-----------------------------------------------------------------------------
//-----------------------DEMO--------------------------------------------------
//-----------------------------------------------------------------------------
if(demo == 1){
     Serial.print ("Rotated: ");
     if (bCW){
       Serial.println ("clockwise");
        colorWipe(strip.Color(0, 255, 0), 50); // Green
     }else{
       Serial.println("counterclockwise");
       colorWipe(strip.Color(255, 0, 0), 50); // Red
     }
     Serial.print("Encoder Position: ");
     Serial.println(encoderPosCount);

   }
   pinALast = aVal;
//-----------------------------------------------------------------------------
//-----------------------Ruee--------------------------------------------------
//-----------------------------------------------------------------------------
if(ruee == 1){

     Serial.print ("Rotated: ");
     if (bCW){
       Serial.println ("clockwise");
        colorWipe(strip.Color(0, 255, 0), 50); // Green
     }else{
       Serial.println("counterclockwise");
       colorWipe(strip.Color(255, 0, 0), 50); // Red
     }
     Serial.print("Encoder Position: ");
     Serial.println(encoderPosCount);

   }
   pinALast = aVal;
//-----------------------------------------------------------------------------
}
void rueee(){
  btn();
  if(EtatBleu == HIGH){
    for(uint16_t i=0; i<strip.numPixels() && EtatBleu == HIGH ; i++) {
      strip.setPixelColor(i, 0,0,2500);
      strip.show();
      Serial.println(i);
      if(i == strip.numPixels()){

      }
      delay(1000);
  }

  }
  }
void btn(){
  EtatVert = digitalRead(PinVert);
  EtatBleu =  digitalRead(PinBleu);
  EtatRouge = digitalRead(PinRouge);

}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }


}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}
