#include "Comunication.h"
#include "confOrdi.h"
#include "Actuator.h"
#include <stdint.h>
#include "gpio.h"
#include "spi.h"
#include "tim.h"
#include "RTC.h"
#include "fan.h"

uint8_t Com_dispactch(uint8_t* mes){
	//TODO (ORDINATEUR important) Tester le SPI it sur la rampe a led avant de l'implemanté ici
	switch(mes[0]|MASKMES){
	case (STATUS):
		Com_Status(mes);
		return 0;
	case (WRITEACTUA):
		Com_WriteActuator(mes);
		return 0;
	case (WRITEVENTIL):
		Com_WriteVentil(mes);
		return 0;
	case (ACKERREUR):
		Com_ACKErreur(mes);
		return 0;
	case (RESERVE2):
		Com_2Osmolateur(mes);
		return 0;
	case (DELAYBRASS):
		Com_delayBrassage(mes);
		return 0;
	case (SYNCRTC):
		Com_SyncRTC(mes);
		return 0;
	default:
		return -1;//todo (ORDINATEUR AMELIORATION) error
	}
}

void Com_WriteActuator(uint8_t* mes){
	if((mes[0]&MASKOSMOLATEUR1) == 1)
		Actuator_osmo_start(OSMOLATEUR1);
	else
		Actuator_stop(OSMOLATEUR1);

	if((mes[0]&MASKOSMOLATEUR2) == 1)
		Actuator_osmo_start(OSMOLATEUR2);
	else
		Actuator_stop(OSMOLATEUR2);

	if((mes[0]&MASKSUP12V1) == 1)
		Actuator_start(SUP12_1);
	else
		Actuator_stop(SUP12_1);

	if((mes[0]&MASKSUP12V2) == 1)
		Actuator_start(SUP12_2);
	else
		Actuator_stop(SUP12_2);

	if((mes[1]&MASKBRASSAGE1) == 1)
		Actuator_start(BRASSAGE1);
	else
		Actuator_stop(BRASSAGE1);

	if((mes[1]&MASKBRASSAGE2) == 1)
		Actuator_start(BRASSAGE2);
	else
		Actuator_stop(BRASSAGE2);

	if((mes[1]&MASKCHAUFFAGE) == 1)
		Actuator_start(CHAUFFAGE);
	else
		Actuator_stop(CHAUFFAGE);

	if((mes[1]&MASKECUMEUR) == 1)
		Actuator_start(ECUMEUR);
	else
		Actuator_stop(ECUMEUR);

	if((mes[1]&MASKRAH) == 1)
		Actuator_start(RAH);
	else
		Actuator_stop(RAH);

	if((mes[1]&MASKREMONTER) == 1)
		Actuator_start(REMONTER);
	else
		Actuator_stop(REMONTER);

	if((mes[1]&MASK230V1) == 1)
		Actuator_start(SUP230_1);
	else
		Actuator_stop(SUP230_1);

	if((mes[1]&MASK230V2) == 1)
		Actuator_start(SUP230_2);
	else
		Actuator_stop(SUP230_2);
}

void Com_WriteVentil(uint8_t* mes){
	Fan_Pow(FAN1,mes[0]&MASKVENT1);
	Fan_Pow(FAN2,(mes[1]&MASKVENT2)>>4);
	Fan_Pow(FAN3,mes[1]&MASKVENT3);
}

void Com_Status(uint8_t* mes){
	uint8_t data[6];
	data[0]=temp(adcBuffer[1]);
	data[1]=temp(adcBuffer[0]);
	data[2]=ph(adcBuffer[2]);
	data[3] = data[3] | HAL_GPIO_ReadPin(TechniqueBas_GPIO_Port,TechniqueBas_Pin)<<7;
	data[3] = data[3] | HAL_GPIO_ReadPin(TechniqueHaut_GPIO_Port,TechniqueHaut_Pin)<<6;
	data[3] = data[3] | HAL_GPIO_ReadPin(CuveHaut_GPIO_Port,CuveHaut_Pin)<<5;
	data[3] = data[3] | HAL_GPIO_ReadPin(ReserveBas_GPIO_Port,ReserveBas_Pin)<<4;
	data[3] = data[3] | HAL_GPIO_ReadPin(CapteurAutres1_GPIO_Port,CapteurAutres1_Pin)<<3;
	data[3] = data[3] | HAL_GPIO_ReadPin(CapteurAutres2_GPIO_Port,CapteurAutres2_Pin)<<2;
	data[3] = data[3] | gl_osmo1;
	data[3] = data[3] | gl_osmo2;
	data[4] = 0x00; //TODO (ORDINATEUR AMELIORATION) gestionnaire ERROR
	data[5] = 0x00; //TODO (ORDINATEUR AMELIORATION) gestionnaire ERROR

	gl_osmo1 = 0; //On remet a 0 les variables global d'osmolation
	gl_osmo2 = 0; //On remet a 0 les variables global d'osmolation

	HAL_SPI_Transmit(&hspi1,data,7,1000);
}

void Com_ACKErreur(uint8_t* mes){
	//TODO (ORDINATEUR AMELIORATION) ACK gestionnaire ERROR
}

void Com_2Osmolateur(uint8_t* mes){
	Actuator_t[2].active =(mes[0]&MASKOSMO2TIMER)>>1;
	if((mes[0]&MASKOSMO2ACTI)==0)
		Actuator_stop(OSMOLATEUR2);
	else
		Actuator_start(OSMOLATEUR2);
}

void Com_delayBrassage(uint8_t* mes){
	g_DBrassage =mes[0]&MASKDELAYBRASS;
	//TODO (ORDINATEUR important) Mettre dans l'EEPROM pour le redemarage
}

void Com_SyncRTC(uint8_t* mes){
	Date d;
	d.annee=mes[1];
	d.mois=mes[2];
	d.jour=mes[3];
	d.joursemaine=mes[4];
	d.heure=mes[5];
	d.minute=mes[6];
	d.second=mes[7];
	writeDate(&d);
}
