#include "ESP12F.h"
#include <WidgetRTC.h>



/* **************************************************************
  Fonction Perso
 ***************************************************************/
uint8_t transmit(uint8_t *in, uint8_t nbTr, uint8_t *out) {
  int i;
  for (i = 0; i < nbTr; i++)
    Serial.write(in[i]);
  i = 0;
  return i;
}

void SyncRTC() {
  rtc.begin();

  uint8_t nbTr = 8;
  uint8_t in[nbTr];
  uint8_t out[nbTr];

  in[0] = 0x90;
  in[1] = year() - 2000;
  in[2] = month();
  in[3] = day();
  uint8_t weekd = weekday() - 1;
  if (weekday() == 1) weekd = 0;
  in[4] = weekd;
  in[5] = hour();
  in[6] = minute();
  in[7] = second();

  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}

/* **************************************************************
  Fonction Blynk
 ***************************************************************/

BLYNK_CONNECTED() {
  rtc.begin();
}

//MAX
BLYNK_WRITE(V8) {
  Blanc.Max = param.asInt();
  uint8_t in[2] = {0x10, Blanc.Max};
  uint8_t nbTr = 2;
  uint8_t out[2];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }

}

BLYNK_WRITE(V12) {
  Chaud.Max = param.asInt();
  uint8_t in[2] = {0x11, Chaud.Max};
  uint8_t nbTr = 2;
  uint8_t out[2];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}

BLYNK_WRITE(V16)
{
  Bleu.Max = param.asInt();
  uint8_t in[2] = {0x12, Bleu.Max};
  uint8_t nbTr = 2;
  uint8_t out[2];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}

BLYNK_WRITE(V20)
{
  Uv.Max = param.asInt();
  uint8_t in[2] = {0x13, Uv.Max};
  uint8_t nbTr = 2;
  uint8_t out[2];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}

//Pente
BLYNK_WRITE(V9)
{
  Blanc.Pente = param.asInt();
  uint8_t in[2] = {0x18, Blanc.Pente};
  uint8_t nbTr = 2;
  uint8_t out[2];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}

BLYNK_WRITE(V13)
{
  Chaud.Pente = param.asInt();
  uint8_t in[2] = {0x19, Chaud.Pente};
  uint8_t nbTr = 2;
  uint8_t out[2];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}

BLYNK_WRITE(V17)
{
  Bleu.Pente = param.asInt();
  uint8_t in[2] = {0x1A, Bleu.Pente};
  uint8_t nbTr = 2;
  uint8_t out[2];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}

BLYNK_WRITE(V21)
{
  Uv.Pente = param.asInt();
  uint8_t in[2] = {0x1B, Uv.Pente};
  uint8_t nbTr = 2;
  uint8_t out[2];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}

//HeureDem
BLYNK_WRITE(V10)
{
  Blanc.HeureDem = param.asLong() / 60;
  uint8_t in[3] = {0x00, Blanc.HeureDem / 60, Blanc.HeureDem % 60};
  uint8_t nbTr = 3;
  uint8_t out[3];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}

BLYNK_WRITE(V14)
{
  Chaud.HeureDem = param.asLong() / 60;
  uint8_t in[3] = {0x01, Chaud.HeureDem / 60, Chaud.HeureDem % 60};
  uint8_t nbTr = 3;
  uint8_t out[3];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}
BLYNK_WRITE(V18)
{
  Bleu.HeureDem = param.asLong() / 60;
  uint8_t in[3] = {0x02, Bleu.HeureDem / 60, Bleu.HeureDem % 60};
  uint8_t nbTr = 3;
  uint8_t out[3];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}
BLYNK_WRITE(V22)
{
  Uv.HeureDem = param.asLong() / 60;
  uint8_t in[3] = {0x03, Uv.HeureDem / 60, Uv.HeureDem % 60};
  uint8_t nbTr = 3;
  uint8_t out[3];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}

//HeureArret
BLYNK_WRITE(V11)
{
  Blanc.HeureArret = param.asLong() / 60;
  uint8_t in[3] = {0x08, Blanc.HeureArret / 60, Blanc.HeureArret % 60};
  uint8_t nbTr = 3;
  uint8_t out[3];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}
BLYNK_WRITE(V15)
{
  Chaud.HeureArret = param.asLong() / 60;
  uint8_t in[3] = {0x09, Chaud.HeureArret / 60, Chaud.HeureArret % 60};
  uint8_t nbTr = 3;
  uint8_t out[3];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}
BLYNK_WRITE(V19)
{
  Bleu.HeureArret = param.asLong() / 60;
  uint8_t in[3] = {0x0A, Bleu.HeureArret / 60, Bleu.HeureArret % 60};
  uint8_t nbTr = 3;
  uint8_t out[3];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}
BLYNK_WRITE(V23)
{
  Uv.HeureArret = param.asLong() / 60;
  uint8_t in[3] = {0x0B, Uv.HeureArret / 60, Uv.HeureArret % 60};
  uint8_t nbTr = 3;
  uint8_t out[3];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}

BLYNK_WRITE(V1) {
  Blanc.Essai = param.asInt();
  uint8_t in[2] = {0x60, Blanc.Essai};
  uint8_t nbTr = 2;
  uint8_t out[2];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}
BLYNK_WRITE(V2) {
  Chaud.Essai = param.asInt();
  uint8_t in[2] = {0x61, Blanc.Essai};
  uint8_t nbTr = 2;
  uint8_t out[2];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}
BLYNK_WRITE(V3) {
  Bleu.Essai = param.asInt();
  uint8_t in[2] = {0x62, Blanc.Essai};
  uint8_t nbTr = 2;
  uint8_t out[2];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}
BLYNK_WRITE(V4) {
  Uv.Essai = param.asInt();
  uint8_t in[2] = {0x63, Blanc.Essai};
  uint8_t nbTr = 2;
  uint8_t out[2];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}



BLYNK_WRITE(V6) {
  uint8_t in[1];
  if (param.asInt() == 0) in[0] = 0x71;
  else in[0] = 0x70;
  uint8_t nbTr = 1;
  uint8_t out[1];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}


BLYNK_WRITE(V7) {
  uint8_t in[1];
  if (param.asInt() == 0) in[0] = 0x80;
  else in[0] = 0x81;
  uint8_t nbTr = 1;
  uint8_t out[1];
  if (transmit(in, nbTr, out) == 0) {
    //erreur transmit
  }
}



/* **************************************************************
    Fonction Arduino
 ***************************************************************/

void setup() {
  Blynk.begin(auth, ssid, pass, IPAddress(192, 168, 1, 18), 8081);
  timer.setInterval(1000L * 3600, SyncRTC); // une synchro de la RTC par heure
  Serial.begin(115200);
}

void loop() {
  Blynk.run();
  timer.run();

  if (Serial.available() > 0) {
    int out = Serial.read();
    if (out = 0xF0) {
      Blynk.syncAll();
      SyncRTC();
    }
  }



  delay(500);

}
