#include "main.h"
#include "relay_task.h"

#define RELAY1_ON() 		LL_TIM_OC_SetCompareCH1(TIM2,255)
#define RELAY1_OFF() 		LL_TIM_OC_SetCompareCH1(TIM2,0)
#define RELAY1_HOLD() 	LL_TIM_OC_SetCompareCH1(TIM2,200)

#define RELAY2_ON() 		LL_TIM_OC_SetCompareCH1(TIM12,255)
#define RELAY2_OFF() 		LL_TIM_OC_SetCompareCH1(TIM12,0)
#define RELAY2_HOLD() 	LL_TIM_OC_SetCompareCH1(TIM12,200)

typedef struct
{
	unsigned char fault;
	unsigned char alarm;   
	unsigned char dump0;  
	unsigned char dump1; 
}TSys_Relay_State_Struct;

TSys_Relay_State_Struct relay_state = {0,0};

void set_Relay_alarm_state(unsigned char aSta)
{
	relay_state.alarm = aSta;
}

void set_Relay_fault_state(unsigned char aSta)
{
	relay_state.fault = aSta;
}

void alarm_relay_output_handle(void)
{
	static unsigned int relay_tick = 0;
	
	if(relay_state.alarm){
		if(relay_tick < 200)relay_tick++;
	}else{
		relay_tick = 0;
	}
	
	if(0 == relay_tick)RELAY1_OFF();
	else if(relay_tick < 100)RELAY1_ON();
	else RELAY1_HOLD();
}

void fault_relay_output_handle(void)
{
	static unsigned int relay_tick = 0;
	
	if(relay_state.fault){
		if(relay_tick < 200)relay_tick++;
	}else{
		relay_tick = 0;
	}
	
	if(0 == relay_tick)RELAY2_OFF();
	else if(relay_tick < 100)RELAY2_ON();
	else RELAY2_HOLD();
}

void relay_output_handle(void)//100ms
{
	alarm_relay_output_handle();
	fault_relay_output_handle();
}
