#include "Comunication.h"
#include "Tab.h"
#include "PWM.h"
#include "usart.h"
#include <Heure.h>
#include "rtc.h"

extern Sortie tabSortie[10];
extern uint8_t gl_Stop;
extern uint8_t gl_Essai;
extern uint8_t erreur;

void parse(uint8_t* mes){
	switch(mes[0] & MASKMES){
	case WRITETABHEU:
		Com_WriteTabHeure(mes);
		break;
	case READTABHEU:
		Com_ReadTabHeure(mes);
		break;
	case WRITETABVAL:
		Com_WriteTabVal(mes);
		break;
	case READTABVAL:
		Com_ReadTabVal(mes);
		break;
	case READVENT:
		Com_ReadVent(mes);
		break;
	case WRITVENT:
		Com_WriteVent(mes);
		break;
	case WRITEVALESS:
		Com_WriteValEsai(mes);
		break;
	case STOP:
		Com_STOP(mes);
		break;
	case ESSAI:
		Com_ESSAI(mes);
		break;
	case SYNCRTC:
		Com_SyncRTC(mes);
		break;
	case STATUS:
		Com_Status();
		break;
	default:
		HAL_NVIC_SystemReset();
		break;
	}
}

void Com_WriteTabHeure(uint8_t* mes){

	if(mes[1]>>7){
		tabSortie[mes[1]&0x0F].Extinxtion=mes[2]*60+mes[3];
		writeEeprom((mes[1]&0x0F)*5, &mes[2], 2);
	}
	else{
		tabSortie[mes[1]&0x0F].Demmarage=mes[2]*60+mes[3];
		writeEeprom((mes[1]&0x0F)*5+2, &mes[2], 2);

	}
	uint8_t mesRet[8];
	mesRet[0] =  WRITETABHEU;
	mesRet[1]=55;
	HAL_UART_Transmit(&huart6, mesRet, 8, 500);
}

void Com_ReadTabVal(uint8_t* mes){
//TODO a faire
}

void Com_WriteTabVal(uint8_t* mes){
	HAL_StatusTypeDef res;
	if(mes[1]>>7){
		tabSortie[mes[1]&0x0F].Pente=mes[2];
		writeEeprom((mes[1]&0x0F)*5+4, &mes[2], 1);
	}
	else{
		tabSortie[mes[1]&0x0F].PuissanceMax=mes[2];
		writeEeprom((mes[1]&0x0F)*5+3, &mes[2], 1);
	}
	uint8_t mesRet[8];
	mesRet[0] =  WRITETABVAL;
	HAL_UART_Transmit(&huart6, mesRet, 8, 500);

}

void Com_ReadTabHeure(uint8_t* mes){
//TODO a faire
}

void Com_ReadVent(uint8_t* mes){
//TODO a faire
}

void Com_WriteVent(uint8_t* mes){
	//TODO a faire
}

void Com_WriteValEsai(uint8_t* mes){
	tabSortie[mes[1]&0x0F].PuissanceEssai=mes[2];
	uint8_t mesRet[8];
	mesRet[0] =  WRITEVALESS;
	HAL_UART_Transmit(&huart6, mesRet, 8, 500);
	if (gl_Essai)
		Essai();
}

void Com_STOP(uint8_t* mes){
	if(mes[1]==1){
		gl_Stop=1;
		Stop();
	}
	else{
		gl_Stop=0;
		uint16_t Hmin = ReadHeureMin();
		GerePWM(Hmin);
	}
	uint8_t mesRet[8];
	mesRet[0] = STOP ;
	HAL_UART_Transmit(&huart6, mesRet, 8, 500);
}

void Com_ESSAI(uint8_t* mes){
	if(mes[1]==1){
		gl_Essai=1;
		Essai();
		}
	else{
		gl_Essai=0;
		uint16_t Hmin = ReadHeureMin();
		GerePWM(Hmin);
	}
	uint8_t mesRet[8];
	mesRet[0] =  ESSAI;
	HAL_UART_Transmit(&huart6, mesRet, 8, 500);
}

void Com_SyncRTC(uint8_t* mes){
	RTC_TimeTypeDef sTime;
	RTC_DateTypeDef sDate;
	sDate.Year=mes[1];
	sDate.Month=mes[2];
	sDate.Date=mes[3];
	sDate.WeekDay=mes[4];
	sTime.Hours=mes[5];
	sTime.Minutes=mes[6];
	sTime.Seconds=mes[7];
	sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
	sTime.StoreOperation = RTC_STOREOPERATION_RESET;
	if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
		Error_Handler; //TODO
	if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
		Error_Handler;//TODO
	uint8_t mesRet[8];
 	mesRet[0] =  SYNCRTC;
	HAL_UART_Transmit(&huart6, mesRet, 8, 500);
}

void Com_Status(){
	uint8_t mesRet[8];
 	mesRet[0] =  STATUS;
 	mesRet[1] = erreur;
	HAL_UART_Transmit(&huart6, mesRet, 8, 500);
	erreur = 0;
}

