/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial

#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WidgetRTC.h>
#include <TimeLib.h>
#include "ESP12f.h"

#define VALUE_BLANC 0x60
#define VALUE_CHAUD 0x61
#define VALUE_BLEU	0x62
#define VALUE_ROUGE 0x63
#define VALUE_UV	0x64
#define VALUE_VERT  0x65

#define STOP 		0x70
#define RETABLIR	0x80
#define SYNCRTC		0x90

#define	DEMMARAGE_BLANC	0x01
#define	DEMMARAGE_CHAUD	0x02
#define	DEMMARAGE_BLEU	0x03
#define	DEMMARAGE_ROUGE	0x04
#define	DEMMARAGE_UV	0x05
#define	DEMMARAGE_VERT	0x06
#define	ARRET_BLANC		0x08
#define	ARRET_CHAUD		0x09
#define	ARRET_BLEU		0x0A
#define	ARRET_ROUGE		0X0B
#define	ARRET_UV		0x0C
#define	ARRET_VERT		0x0D
#define	MAX_BLANC		0x11
#define	MAX_CHAUD		0x12
#define	MAX_BLEU		0x13
#define	MAX_ROUGE		0x14
#define	MAX_UV			0x15
#define	MAX_VERT		0x16
#define	PENTE_BLANC		0x18
#define	PENTE_CHAUD		0x19
#define	PENTE_BLEU		0x1A
#define	PENTE_ROUGE		0X0B
#define	PENTE_UV		0x1C
#define	PENTE_VERT		0x1D

/* Global Variable */

// Token identification
char auth[] = "";

// Your WiFi credentials.
char ssid[] = "Livebox-7C2C";
char pass[] = "566C61F61F9C925C9F3D7D79E6";

WidgetRTC rtc;

/* Fonction */
void setup(){
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  
  timer.setInterval(600000L, requestTime);//On appel la fonction de mise a jour de la RTC toute les 10 min
}

void requestTime() {
  Blynk.sendInternal("rtc", "sync");//Appel a la mise a jour de time
}

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

BLYNK_WRITE(InternalPinRTC) {
  long t = param.asLong();//renvoi le temps unix
  
}

BLYNK_WRITE(InternalPinRTC) {
	unsigned long t = param.asLong();
	uint8_t mes[8];
	mes[0]=SYNCRTC;
	mes[1]= year(t);
	mes[2]=month(t);
	mes[3]=day(t);
	mes[4]=weekday(t);
	mes[5]=hour(t);
	mes[6]=minute(t);
	mes[7]=second(t);
	SPI_send(val,8);
}

BLYNK_WRITE(V0){ //Slider Blanc
	uint8_t val[2]:
	val[0] = VALUE_BLANC;
	val[1] = param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V1){ //Slider Chaud
	uint8_t val[2]:
	val[0] = VALUE_CHAUD;
	val[1] = param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V2){ //Slider Bleu
	uint8_t val[2]:
	val[0] = VALUE_BLEU;
	val[1] = param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V3){ //Slider Rouge
	uint8_t val[2]:
	val[0] = VALUE_ROUGE;
	val[1] = param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V4){ //Slider UV
	uint8_t val[2]:
	val[0] = VALUE_UV;
	val[1] = param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V5){ //Slider Vert
	uint8_t val[2]:
	val[0] = VALUE_VERT;
	val[1] = param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V6){ //Bouton STOP
	uint8_t val[1]:
	val[0] = STOP;
	SPI_send(val,1);
}

BLYNK_WRITE(V7){ //BOUTON RETABLIR
	uint8_t val[1]:
	val[0] = RETABLIR;
	SPI_send(val,1);
}

BLYNK_WRITE(V8){ //MAX blanc
	uint8_t val[2];
	val[0]= MAX_BLANC;
	val[1]= param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V12){ //MAX chaud
	uint8_t val[2];
	val[0]= MAX_CHAUD;
	val[1]= param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V16){ //MAX bleu
	uint8_t val[2];
	val[0]= MAX_BLEU;
	val[1]= param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V20){ //MAX rouge
	uint8_t val[2];
	val[0]= MAX_ROUGE;
	val[1]= param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V24){ //MAX UV
	uint8_t val[2];
	val[0]= MAX_UV;
	val[1]= param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V28){ //MAX Vert
	uint8_t val[2];
	val[0]= MAX_VERT;
	val[1]= param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V9){ //PENTE blanc
	uint8_t val[2];
	val[0]= PENTE_BLANC;
	val[1]= param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V13){ //PENTE chaud
	uint8_t val[2];
	val[0]= PENTE_CHAUD;
	val[1]= param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V17){ //PENTE bleu
	uint8_t val[2];
	val[0]= PENTE_BLEU;
	val[1]= param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V21){ //PENTE rouge
	uint8_t val[2];
	val[0]= PENTE_ROUGE;
	val[1]= param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V25){ //PENTE UV
	uint8_t val[2];
	val[0]= PENTE_UV;
	val[1]= param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V29){ //PENTE Vert
	uint8_t val[2];
	val[0]= PENTE_VERT;
	val[1]= param.asInt();
	SPI_send(val,2);
}

BLYNK_WRITE(V10){ //DEMARAGE blanc
	long h = param[0].asLong();
	uint8_t val[3];
	val[0]= DEMMARAGE_BLANC;
	val[1]= h/3600;
	val[2]= (h-(val[1]*3600))/60;
	SPI_send(val,3);
}

BLYNK_WRITE(V14){ //DEMARAGE chaud
	long h = param[0].asLong();
	uint8_t val[3];
	val[0]= DEMMARAGE_CHAUD;
	val[1]= h/3600;
	val[2]= (h-(val[1]*3600))/60;
	SPI_send(val,3);
}

BLYNK_WRITE(V18){ //DEMARAGE bleu
	long h = param[0].asLong();
	uint8_t val[3];
	val[0]= DEMMARAGE_BLEU;
	val[1]= h/3600;
	val[2]= (h-(val[1]*3600))/60;
	SPI_send(val,3);
}

BLYNK_WRITE(V22){ //DEMARAGE rouge
	long h = param[0].asLong();
	uint8_t val[3];
	val[0]= DEMMARAGE_ROUGE;
	val[1]= h/3600;
	val[2]= (h-(val[1]*3600))/60;
	SPI_send(val,3);
}

BLYNK_WRITE(V26){ //DEMARAGE UV
	long h = param[0].asLong();
	uint8_t val[3];
	val[0]= DEMMARAGE_UV;
	val[1]= h/3600;
	val[2]= (h-(val[1]*3600))/60;
	SPI_send(val,3);
}

BLYNK_WRITE(V30){ //DEMARAGE Vert
	long h = param[0].asLong();
	uint8_t val[3];
	val[0]= DEMMARAGE_VERT;
	val[1]= h/3600;
	val[2]= (h-(val[1]*3600))/60;
	SPI_send(val,3);
}

BLYNK_WRITE(V11){ //ARRET blanc
	long h = param[0].asLong();
	uint8_t val[3];
	val[0]= ARRET_BLANC;
	val[1]= h/3600;
	val[2]= (h-(val[1]*3600))/60;
	SPI_send(val,3);
}

BLYNK_WRITE(V13){ //ARRET chaud
	long h = param[0].asLong();
	uint8_t val[3];
	val[0]= ARRET_CHAUD;
	val[1]= h/3600;
	val[2]= (h-(val[1]*3600))/60;
	SPI_send(val,3);
}

BLYNK_WRITE(V19){ //ARRET bleu
	long h = param[0].asLong();
	uint8_t val[3];
	val[0]= ARRET_BLEU;
	val[1]= h/3600;
	val[2]= (h-(val[1]*3600))/60;
	SPI_send(val,3);
}

BLYNK_WRITE(V23){ //ARRET rouge
	long h = param[0].asLong();
	uint8_t val[3];
	val[0]= ARRET_ROUGE;
	val[1]= h/3600;
	val[2]= (h-(val[1]*3600))/60;
	SPI_send(val,3);
}

BLYNK_WRITE(V27){ //ARRET UV
	long h = param[0].asLong();
	uint8_t val[3];
	val[0]= ARRET_UV;
	val[1]= h/3600;
	val[2]= (h-(val[1]*3600))/60;
	SPI_send(val,3);
}

BLYNK_WRITE(V31){ //ARRET Vert
	long h = param[0].asLong();
	uint8_t val[3];
	val[0]= ARRET_VERT;
	val[1]= h/3600;
	val[2]= (h-(val[1]*3600))/60;
	SPI_send(val,3);
}