#include <ECU_Check/ECU_Check.h>
#include "can.h"
#include "Global_Variable.h"

/*
 * count = 10  → 100ms
 * count = 50  → 500ms
 * count = 100 → 1s
 */
#define count	50

void ACU_Arc_check(void)
{
	if(GV_prev_ACU_ECU_Arc == GV_ACU_VCU_counter)
	{
		GV_ACU_ECU_Arc_ErrCnt++;
		if(GV_ACU_ECU_Arc_ErrCnt >= count)	GV_ACU_ECU_Arc_ErrCnt = count;
		if(GV_ACU_ECU_Arc_ErrCnt >= count)	GV_ACU_ECU_Fault_Flag = 1;
		else								GV_ACU_ECU_Fault_Flag = 0;
	}
	else
	{
		GV_ACU_ECU_Arc_ErrCnt = 0;
		GV_ACU_ECU_Fault_Flag = 0;
	}

	GV_prev_ACU_ECU_Arc = GV_ACU_VCU_counter;
}

void ACU_Arc_check1(void)
{
	if(GV_prev_ACU_ECU_Arc1 == GV_ACU_VCU_counter1)
	{
		GV_ACU_ECU_Arc_ErrCnt1++;
		if(GV_ACU_ECU_Arc_ErrCnt1 >= count)	GV_ACU_ECU_Arc_ErrCnt1 = count;
		if(GV_ACU_ECU_Arc_ErrCnt1 >= count)	GV_ACU_ECU_Fault_Flag1 = 1;
		else								GV_ACU_ECU_Fault_Flag1 = 0;
	}
	else
	{
		GV_ACU_ECU_Arc_ErrCnt1 = 0;
		GV_ACU_ECU_Fault_Flag1 = 0;
	}

	GV_prev_ACU_ECU_Arc1 = GV_ACU_VCU_counter1;
}

void ACU_Arc_check2(void)
{
	if(GV_prev_ACU_ECU_Arc2 == GV_ACU_VCU_counter2)
	{
		GV_ACU_ECU_Arc_ErrCnt2++;
		if(GV_ACU_ECU_Arc_ErrCnt2 >= count)	GV_ACU_ECU_Arc_ErrCnt2 = count;
		if(GV_ACU_ECU_Arc_ErrCnt2 >= count)	GV_ACU_ECU_Fault_Flag2 = 1;
		else								GV_ACU_ECU_Fault_Flag2 = 0;
	}
	else
	{
		GV_ACU_ECU_Arc_ErrCnt2 = 0;
		GV_ACU_ECU_Fault_Flag2 = 0;
	}

	GV_prev_ACU_ECU_Arc2 = GV_ACU_VCU_counter2;
}

void ACU_Arc_check3(void)
{
	if(GV_prev_ACU_ECU_Arc3 == GV_ACU_VCU_counter3)
	{
		GV_ACU_ECU_Arc_ErrCnt3++;
		if(GV_ACU_ECU_Arc_ErrCnt3 >= count)	GV_ACU_ECU_Arc_ErrCnt3 = count;
		if(GV_ACU_ECU_Arc_ErrCnt3 >= count)	GV_ACU_ECU_Fault_Flag3 = 1;
		else								GV_ACU_ECU_Fault_Flag3 = 0;
	}
	else
	{
		GV_ACU_ECU_Arc_ErrCnt3 = 0;
		GV_ACU_ECU_Fault_Flag3 = 0;
	}

	GV_prev_ACU_ECU_Arc3 = GV_ACU_VCU_counter3;
}

void IDB_Arc_check(void)	// Ch2 0x092 5ms
{
	if(GV_prev_IDB_ECU_Arc == GV_IDB_Arc)
	{
		GV_IDB_ECU_Arc_ErrCnt++;
		if(GV_IDB_ECU_Arc_ErrCnt >= count)	GV_IDB_ECU_Arc_ErrCnt = count;
		if(GV_IDB_ECU_Arc_ErrCnt >= count)	GV_VCU_RC_IDB_ECU_Fault_Flag = 1;
		else								GV_VCU_RC_IDB_ECU_Fault_Flag = 0;
	}
	else
	{
		GV_IDB_ECU_Arc_ErrCnt = 0;
		GV_VCU_RC_IDB_ECU_Fault_Flag = 0;
	}

	GV_prev_IDB_ECU_Arc = GV_IDB_Arc;
}

void RCU_Arc_check(void)	// Ch3 0x093 5ms
{
	if(GV_prev_RCU_ECU_Arc == GV_RCU_Arc)
	{
		GV_RCU_ECU_Arc_ErrCnt++;
		if(GV_RCU_ECU_Arc_ErrCnt >= count)	GV_RCU_ECU_Arc_ErrCnt = count;
		if(GV_RCU_ECU_Arc_ErrCnt >= count)	GV_VCU_RC_RCU_ECU_Fault_Flag = 1;
		else								GV_VCU_RC_RCU_ECU_Fault_Flag = 0;
	}
	else
	{
		GV_RCU_ECU_Arc_ErrCnt = 0;
		GV_VCU_RC_RCU_ECU_Fault_Flag = 0;
	}

	GV_prev_RCU_ECU_Arc = GV_RCU_Arc;
}

void RWA1_Arc_check(void)	// Ch2 0x072 5ms
{
	if(GV_prev_RWA1_ECU_Arc == GV_RWA1_Arc)
	{
		GV_RWA1_ECU_Arc_ErrCnt++;
		if(GV_RWA1_ECU_Arc_ErrCnt >= count)	GV_RWA1_ECU_Arc_ErrCnt = count;
		if(GV_RWA1_ECU_Arc_ErrCnt >= count)	GV_VCU_RC_RWA1_ECU_Fault_Flag = 1;
		else								GV_VCU_RC_RWA1_ECU_Fault_Flag = 0;
	}
	else
	{
		GV_RWA1_ECU_Arc_ErrCnt = 0;
		GV_VCU_RC_RWA1_ECU_Fault_Flag = 0;
	}

	GV_prev_RWA1_ECU_Arc = GV_RWA1_Arc;
}

void RWA2_Arc_check(void)	// Ch3 0x072 5ms
{
	if(GV_prev_RWA2_ECU_Arc == GV_RWA2_Arc)
	{
		GV_RWA2_ECU_Arc_ErrCnt++;
		if(GV_RWA2_ECU_Arc_ErrCnt >= count)	GV_RWA2_ECU_Arc_ErrCnt = count;
		if(GV_RWA2_ECU_Arc_ErrCnt >= count)	GV_VCU_RC_RWA2_ECU_Fault_Flag = 1;
		else								GV_VCU_RC_RWA2_ECU_Fault_Flag = 0;
	}
	else
	{
		GV_RWA2_ECU_Arc_ErrCnt = 0;
		GV_VCU_RC_RWA2_ECU_Fault_Flag = 0;
	}

	GV_prev_RWA2_ECU_Arc = GV_RWA2_Arc;
}

void ECU_check(void)
{
	if(GV_RC_VCU_ACU_Fault_Flag == 1 && GV_Act_Fault_Exist)	GV_VCU_RC_ACU_Fault_Flag = 1;
	else													GV_VCU_RC_ACU_Fault_Flag = 0;

	if(GV_RC_VCU_MCU_Actuator_Fault_Flag == 1 && GV_Act_Fault_Exist)	GV_VCU_RC_MCU_Actuator_Fault_Flag = 1;
	else																GV_VCU_RC_MCU_Actuator_Fault_Flag = 0;

	if(GV_RC_VCU_RWA_Actuator_Fault_Flag == 1 && GV_Act_Fault_Exist)	GV_VCU_RC_RWA_Actuator_Fault_Flag = 1;
	else																GV_VCU_RC_RWA_Actuator_Fault_Flag = 0;

	if(GV_ACU_ECU_Fault_Flag == 1 || GV_ACU_ECU_Fault_Flag1 == 1 || GV_ACU_ECU_Fault_Flag2 == 1 || GV_ACU_ECU_Fault_Flag3 == 1)
		GV_ACU_ECU_Fault_Flag_Master = 1;
	else
		GV_ACU_ECU_Fault_Flag_Master = 0;
}




void No_signal_RC(void){
	// GV_RC_VCU_counter1_CHECK = 260 으로 초기에 GV_RC_VCU_counter1와 같은 숫자 회피
	if(GV_VCU_Drive_Mode == 1 ){
		if(GV_RC_VCU_counter1_CHECK != GV_RC_VCU_ARC){
			GV_RC_VCU_counter1_CHECK = GV_RC_VCU_ARC;
		}else{				// 200ms가 지낫는데도 둘다 같은 값이면 -> 신호가 안들어오고 있음 -> 이머전시 스탑 (기존 정상치라면 20 증가 해야함)
//			GV_Operation_Mode = 2 ;
			GV_RC_ALV_FLAG= 1;
		}
	}else if(GV_VCU_Drive_Mode == 2){
		// 이머전시 모드 푸는 법 - rc 모듈 이머전시 토글
		// 얼라이브 카운트로 이머전시 발동 시 - 이머전시 버튼 두번 누르면 됨
		if(GV_RC_Emergency_Stop == 1){
			GV_RC_ALV_FLAG= 0;
		}
	}
}
