/*
  Horloge à LED, réglages et choix des animations par Bluetooth
*/
#define DEBUG 0 // Mettre à 1 pour les affichages console

#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX

#include <MD_DS3231.h>
#include <Wire.h>

#include "FastLED.h"
#include <Streaming.h>

// Fastled constants
#define DATA_PIN    12        // data pin : D12 (pas 4 grace au define ligne 1)
#define COLOR_ORDER GRB
#define NUM_LEDS    60       // number of LEDs in the strip
#define LED_TYPE    WS2812B
#define BRIGHTNESS  64
// this creates an LED array to hold the values for each led in your strip
CRGB leds[NUM_LEDS];
CRGBPalette16 currentPalette( CRGB::Black);
TBlendType    currentBlending;

// Palettes
DEFINE_GRADIENT_PALETTE( pal_BJRJB ) {  // Palette blanc - jaune - rouge
  0,   255, 255, 255,  //full white
  46,   255, 255,   0,  //bright yellow
  92,   255,   0,   0,  //red
  137,  255, 255,   0,  //bright yellow
  184,  255, 255, 255,  //full white
  255,  255, 255, 255
}; //full white
CRGBPalette16 Pal2_p = pal_BJRJB;

DEFINE_GRADIENT_PALETTE( pal_BJVJB ) {  // Palette bleu - jaune - vert
  0,     0,   0, 255,  //blue
  46,   255, 255,   0,  //bright yellow
  92,     0, 255,   0,  //green
  137,  255, 255,   0,  //bright yellow
  184,    0,   0, 255,  //green
  255,    0, 255,   0
}; //green
CRGBPalette16 Pal3_p = pal_BJVJB;

DEFINE_GRADIENT_PALETTE( pal_VVVVV ) {  // Palette vert clair - foncé - clair
  0,   153, 255, 153,  //vert clair
  46,    80, 255,  80,  //vert brillant
  92,     0,  77,   0,  //vert foncé
  137,    80, 255,  80,  //vert brillant
  184,   153, 255, 153,  //vert clair
  255,   153, 255, 153
}; //vert clair
CRGBPalette16 Pal4_p = pal_VVVVV;

// paramètres horloge
int fuseau = 1;		  // Fuseau horaire de Paris
int dst = 1;		    // Daylight saving time (1 = heure été, 0 = hiver)
// dst inutile car mise à l'heure par commande BT
bool matin = true;
bool change = false;
bool change_heure = false;
byte minutes5 = 0;  // Affichage des LEDS aux 5 minutes
byte fond = 0;      // Affichage d'un fond lumineux entre les aiguilles
int dir_sat = 1;
byte sat = 30;
byte offset = 0;    // Décalage numéro de la première LED
byte heures = 7;
byte minutes = 12;
byte secondes = 43;
long TlastHorloge;
int  periode_horloge  = 1000; // Mise à jour de l'horloge : 1 seconde
int numero_pal = 1;
byte num_secondes = 0;
byte aff_heures = 0;
bool alarme = false;
byte heure_alarme = 8;
byte minute_alarme = 0;
bool sens_horaire = false;
// Paramètres animations
enum ANIMS {NO_ANIM, COLOR_PULSE, MOVING_RAINBOW, COLOR_BAND, \
            COLOR_BAND_AR, RANDOM_PAL, GLOWING_SEC, BBR, ALEA
           }; // 0, 1, 2, 3, 4, 5, 6, 7, 8
byte alea = 0;          // drapeau animation aléatoire
byte change_anim = 0;
byte anim = 0;          // numéro de l'animation, 0=aucune
byte initialHue = 0;    // couleur initiale
byte fondHue = 0;       // couleur du fond
byte changeInHue = 255 / NUM_LEDS; // delta couleur pour rainbow
int  periode_anim = 15; //50; // ms
long TlastAnim = 0;
long TlastFond = 0;
int  pos_bande = 1;     // Position bande blanche animation COLOR_BAND
int  dir_bande = 1;     // Direction de déplacement de la bande anim C_B_AR
byte numero_hue = 170;  // Choix de la couleur (hue)
int dir_anim = 1;
CRGB couleur;
CRGB couleur1;
CRGB couleur_fond;
CRGB couleurA ;
CRGB couleurB ;
CRGB couleurC ;

// Paramètres BT
byte receivedNumber = 0;
char command;

#include "HorlogeBT.h"
#include "Horloge.h"
#include "Animations.h"

void setup() {
  BTserial.begin(9600);//115200);
  if (DEBUG) Serial.begin(115200);
  if (DEBUG) Serial << "Demarrage" << endl ;
  // Déclaration du ruban de leds
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setCorrection( TypicalLEDStrip );
  FastLED.setBrightness( BRIGHTNESS );
  currentPalette = RainbowColors_p;
  currentBlending = LINEARBLEND;
  FastLED.clear();
//  FastLED.show();
  MaJ_heure();
  MaJ_horloge ();
  // Déclaration & extinction LED bleue
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  TlastHorloge = millis();
  TlastAnim = millis();
  randomSeed(analogRead(0));
}

void loop() {
  //Process any info coming from the bluetooth serial link
  // if serial data available, process it
  while (BTserial.available () > 0)
    processIncomingByte (BTserial.read ());

  PaletteSelect ();

  // On vérifie si mise à jour animation
  if (millis() - TlastAnim >= periode_anim) {
    GestionAnimations ();
  }

  // On vérifie si la seconde est passée
  if (millis() - TlastHorloge >= periode_horloge) {
    GestionHeure ();
  }

  if (change) { // Mise à jour affichage si quelque chose a changé
    if (anim == NO_ANIM) {
      FastLED.clear ();
      if (fond) fill_fond();
    }
    MaJ_horloge();
    change = false;
    FastLED.show();
  }
}

