#include "CD4051BPWR.h"
#include "RTE_GPIO.h"

#define LINE_IN_IGNON 1u
#define LINE_IN_IGNOFF 0u
#define LINE_IN_IGNONOFF 2u

#define LINE_IN_HIGH_LEVEL 1U
#define LINE_IN_LOW_LEVEL 0U
/*硬线输入服务函数调用周期*/
#define LINE_IN_CYCLE 10U

uint8_t NLHV4051Step; //�л�����
NLHV4051Type_st_t g_stNLHV4051Chip1;
NLHV4051Type_st_t g_stNLHV4051Chip2;
NLHV4051Type_st_t g_stNLHV4051Chip3;
NLHV4051Type_st_t g_stNLHV4051Chip4;
NLHV4051Type_st_t g_stNLHV4051Chip5;

/*属性表与硬线索引号一一对应*/
const LineInAttribute_st_t g_stLineInAttribute[LINE_IN_MAX] =
{
	/*部分硬线电源状态未明确，后续确定后需修改*/
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //低有效3_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //低有效2_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //低有效1_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //上装举升报警_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //ECAS提升轴指示_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //灯具故障_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //燃气泄漏报警_N
  {LINE_IN_LOW_LEVEL,  500u, 500u, LINE_IN_IGNON,},     //驻车制动_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //蓄电池充电指示_N
  {LINE_IN_HIGH_LEVEL, 150u, 150u, LINE_IN_IGNON,},     //刹车蹄片磨损报警_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //油水分离警报_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //ASR指示_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //轴间差速锁指示_N
  {LINE_IN_HIGH_LEVEL, 150u, 150u, LINE_IN_IGNON,},     //挂车刹车蹄片磨损报警_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //ABS挂车故障警报_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //VCU故障警报_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //发动机预热指示_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //发动机故障警报_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //OBD排放故障_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //安全带未系警报_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //轮间差速锁指示_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //取力器工作指示_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //空气滤清器堵塞警报_N
  {LINE_IN_LOW_LEVEL,  150u, 150u, LINE_IN_IGNON,},     //驾驶室翻转警报_N
  {LINE_IN_HIGH_LEVEL, 150u, 150u, LINE_IN_IGNON,},     //高有效2_P
  {LINE_IN_HIGH_LEVEL, 150u, 150u, LINE_IN_IGNON,},     //发动机故障警报_P
  {LINE_IN_HIGH_LEVEL, 150u, 150u, LINE_IN_IGNON,},     //高有效1_P
  {LINE_IN_HIGH_LEVEL, 150u, 150u, LINE_IN_IGNON,},     //燃气泄漏报警_P
  {LINE_IN_HIGH_LEVEL, 150u, 150u, LINE_IN_IGNON,},     //高有效3_P
  {LINE_IN_HIGH_LEVEL, 150u, 150u, LINE_IN_IGNON,},     //变速箱故障警报_P
  {LINE_IN_HIGH_LEVEL, 150u, 150u, LINE_IN_IGNON,},     //高有效4_P
  {LINE_IN_HIGH_LEVEL, 40u, 40u, LINE_IN_IGNONOFF,},  //左转向指示_P
  {LINE_IN_HIGH_LEVEL, 40u, 40u, LINE_IN_IGNONOFF,},  //挂车右转向指示_P
  {LINE_IN_HIGH_LEVEL, 40u, 40u, LINE_IN_IGNONOFF,},  //挂车左转向指示_P
  {LINE_IN_HIGH_LEVEL, 40u, 40u, LINE_IN_IGNONOFF,},  //右转向指示_P
  {LINE_IN_HIGH_LEVEL, 60u, 60u, LINE_IN_IGNONOFF,},  //远光灯指示_P
  {LINE_IN_HIGH_LEVEL, 150u, 150u, LINE_IN_IGNONOFF,},  //仪表照明信号_P       

};


LineInControl g_stLineInControl[LINE_IN_MAX];

uint8_t g_u8LineInPowerStatus;

/*4051оƬ�л�����������
�������2ms�е���*/
void CD4051_Check_Service(void)
{

	switch (NLHV4051Step)
	{
	case 0u:
	{
		g_stNLHV4051Chip1.PIN_X7 = CD4051B_COM1_IN;
		g_stNLHV4051Chip2.PIN_X7 = CD4051B_COM2_IN;
		g_stNLHV4051Chip3.PIN_X7 = CD4051B_COM3_IN;
		g_stNLHV4051Chip4.PIN_X7 = CD4051B_COM4_IN;
		g_stNLHV4051Chip5.PIN_X7 = CD4051B_COM5_IN;
		CD4051B_A_OUT = 0;
		CD4051B_B_OUT = 0;
		CD4051B_C_OUT = 0;
		NLHV4051Step++;
		break;
	}
	case 1:
	{
		g_stNLHV4051Chip1.PIN_X0 = CD4051B_COM1_IN;
		g_stNLHV4051Chip2.PIN_X0 = CD4051B_COM2_IN;
		g_stNLHV4051Chip3.PIN_X0 = CD4051B_COM3_IN;
		g_stNLHV4051Chip4.PIN_X0 = CD4051B_COM4_IN;
		g_stNLHV4051Chip5.PIN_X0 = CD4051B_COM5_IN;
		CD4051B_A_OUT = 1u;
		CD4051B_B_OUT = 0u;
		CD4051B_C_OUT = 0u;
		NLHV4051Step++;
		break;
	}
	case 2u:
	{
		g_stNLHV4051Chip1.PIN_X1 = CD4051B_COM1_IN;
		g_stNLHV4051Chip2.PIN_X1 = CD4051B_COM2_IN;
		g_stNLHV4051Chip3.PIN_X1 = CD4051B_COM3_IN;
		g_stNLHV4051Chip4.PIN_X1 = CD4051B_COM4_IN;
		g_stNLHV4051Chip5.PIN_X1 = CD4051B_COM5_IN;
		CD4051B_A_OUT = 0u;
		CD4051B_B_OUT = 1u;
		CD4051B_C_OUT = 0u;
		NLHV4051Step++;
		break;
	}
	case 3u:
	{
		g_stNLHV4051Chip1.PIN_X2 = CD4051B_COM1_IN;
		g_stNLHV4051Chip2.PIN_X2 = CD4051B_COM2_IN;
		g_stNLHV4051Chip3.PIN_X2 = CD4051B_COM3_IN;
		g_stNLHV4051Chip4.PIN_X2 = CD4051B_COM4_IN;
		g_stNLHV4051Chip5.PIN_X2 = CD4051B_COM5_IN;
		CD4051B_A_OUT = 1u;
		CD4051B_B_OUT = 1u;
		CD4051B_C_OUT = 0u;
		NLHV4051Step++;
		break;
	}
	case 4u:
	{
		g_stNLHV4051Chip1.PIN_X3 = CD4051B_COM1_IN;
		g_stNLHV4051Chip2.PIN_X3 = CD4051B_COM2_IN;
		g_stNLHV4051Chip3.PIN_X3 = CD4051B_COM3_IN;
		g_stNLHV4051Chip4.PIN_X3 = CD4051B_COM4_IN;
		g_stNLHV4051Chip5.PIN_X3 = CD4051B_COM5_IN;
		CD4051B_A_OUT = 0u;
		CD4051B_B_OUT = 0u;
		CD4051B_C_OUT = 1u;
		NLHV4051Step++;
		break;
	}
	case 5u:
	{
		g_stNLHV4051Chip1.PIN_X4 = CD4051B_COM1_IN;
		g_stNLHV4051Chip2.PIN_X4 = CD4051B_COM2_IN;
		g_stNLHV4051Chip3.PIN_X4 = CD4051B_COM3_IN;
		g_stNLHV4051Chip4.PIN_X4 = CD4051B_COM4_IN;
		g_stNLHV4051Chip5.PIN_X4 = CD4051B_COM5_IN;
		CD4051B_A_OUT = 1u;
		CD4051B_B_OUT = 0u;
		CD4051B_C_OUT = 1u;
		NLHV4051Step++;
		break;
	}
	case 6u:
	{
		g_stNLHV4051Chip1.PIN_X5 = CD4051B_COM1_IN;
		g_stNLHV4051Chip2.PIN_X5 = CD4051B_COM2_IN;
		g_stNLHV4051Chip3.PIN_X5 = CD4051B_COM3_IN;
		g_stNLHV4051Chip4.PIN_X5 = CD4051B_COM4_IN;
		g_stNLHV4051Chip5.PIN_X5 = CD4051B_COM5_IN;
		CD4051B_A_OUT = 0u;
		CD4051B_B_OUT = 1u;
		CD4051B_C_OUT = 1u;
		NLHV4051Step++;
		break;
	}
	case 7u:
	{
		g_stNLHV4051Chip1.PIN_X6 = CD4051B_COM1_IN;
		g_stNLHV4051Chip2.PIN_X6 = CD4051B_COM2_IN;
		g_stNLHV4051Chip3.PIN_X6 = CD4051B_COM3_IN;
		g_stNLHV4051Chip4.PIN_X6 = CD4051B_COM4_IN;
		g_stNLHV4051Chip5.PIN_X6 = CD4051B_COM5_IN;
		CD4051B_A_OUT = 1u;
		CD4051B_B_OUT = 1u;
		CD4051B_C_OUT = 1u;
		NLHV4051Step = 0u;
		break;
	}

	default:
	{
		NLHV4051Step = 0u;
		CD4051B_A_OUT = 1u;
		CD4051B_B_OUT = 1u;
		CD4051B_C_OUT = 1u;
		break;
	}
	}
}

void CD4051_KL30_Init(void)
{

	CD4051B_A_OUT = 0u;
	CD4051B_B_OUT = 0u;
	CD4051B_C_OUT = 0u;
	NLHV4051Step = 1u;
}
void CD4051_KL15_ON_Init(void)
{
}
void CD4051_KL15_OFF_Init(void)
{
}
void CD4051_Wakeup_Init(void)
{
}
void CD4051_Sleep_Init(void)
{
	NLHV4051Step = 1u;
	CD4051B_A_OUT = 0u;
	CD4051B_B_OUT = 0u;
	CD4051B_C_OUT = 0u;
}

void Line_In_Real_Time_Input(void)
{
  g_stLineInControl[Low_significant_3_N].u8PinInput        = g_stNLHV4051Chip1.PIN_X0;  //低有效3_N
  g_stLineInControl[Low_significant_2_N].u8PinInput        = g_stNLHV4051Chip1.PIN_X1;  //低有效2_N
  g_stLineInControl[Low_valid_1_N].u8PinInput              = g_stNLHV4051Chip1.PIN_X2;  //低有效1_N
  g_stLineInControl[Install_lift_N].u8PinInput             = g_stNLHV4051Chip1.PIN_X3;  //上装举升报警_N
  g_stLineInControl[ECAS_lifting_shaft_N].u8PinInput       = g_stNLHV4051Chip1.PIN_X4;  //ECAS提升轴指示_N
  g_stLineInControl[The_lamp_faulty_N].u8PinInput          = g_stNLHV4051Chip1.PIN_X5;  //灯具故障_N
  g_stLineInControl[Gas_leak_N].u8PinInput                 = g_stNLHV4051Chip1.PIN_X6;  //燃气泄漏报警_N
  g_stLineInControl[Parking_brake_N].u8PinInput            = g_stNLHV4051Chip1.PIN_X7;  //驻车制动_N
 
  g_stLineInControl[Battery_charging_N].u8PinInput         = g_stNLHV4051Chip2.PIN_X0;  //蓄电池充电指示_N
  g_stLineInControl[Brake_shoe_wear_N].u8PinInput          = g_stNLHV4051Chip2.PIN_X1;  //刹车蹄片磨损报警_N
  g_stLineInControl[Oil_water_separation_N].u8PinInput     = g_stNLHV4051Chip2.PIN_X2;  //油水分离警报_N
  g_stLineInControl[ASR_indicates_N].u8PinInput            = g_stNLHV4051Chip2.PIN_X3;  //ASR指示_N
  g_stLineInControl[Shaft_differential_lock_N].u8PinInput  = g_stNLHV4051Chip2.PIN_X4;  //轴间差速锁指示_N
  g_stLineInControl[Trailer_Brake_shoe_wear_N].u8PinInput  = g_stNLHV4051Chip2.PIN_X5;  //挂车刹车蹄片磨损报警_N
  g_stLineInControl[ABS_trailer_fault_N].u8PinInput        = g_stNLHV4051Chip2.PIN_X6;  //ABS挂车故障警报_N
  g_stLineInControl[VCU_fault_alert_N].u8PinInput          = g_stNLHV4051Chip2.PIN_X7;  //VCU故障警报_N
  
  g_stLineInControl[Engine_warm_up_N].u8PinInput           = g_stNLHV4051Chip3.PIN_X0;  //发动机预热指示_N
  g_stLineInControl[Engine_failure].u8PinInput             = g_stNLHV4051Chip3.PIN_X1;  //发动机故障警报_N
  g_stLineInControl[OBD_emission_failure_N].u8PinInput     = g_stNLHV4051Chip3.PIN_X2;  //OBD排放故障_N
  g_stLineInControl[Seat_belt_fastened].u8PinInput         = g_stNLHV4051Chip3.PIN_X3;  //安全带未系警报_N
  g_stLineInControl[Wheel_differential_lock_N].u8PinInput  = g_stNLHV4051Chip3.PIN_X4;  //轮间差速锁指示_N
  g_stLineInControl[Force_retractor_working_N].u8PinInput  = g_stNLHV4051Chip3.PIN_X5;  //取力器工作指示_N
  g_stLineInControl[Air_filter_blocked_N].u8PinInput       = g_stNLHV4051Chip3.PIN_X6;  //空气滤清器堵塞警报_N
  g_stLineInControl[Cab_rollover_N].u8PinInput             = g_stNLHV4051Chip3.PIN_X7;  //驾驶室翻转警报_N
  
  g_stLineInControl[Highly_significant_2_P].u8PinInput     = g_stNLHV4051Chip4.PIN_X0;  //高有效2_P
  g_stLineInControl[Engine_failure_P].u8PinInput           = g_stNLHV4051Chip4.PIN_X2;  //发动机故障警报_P
  g_stLineInControl[High_effective_1_P].u8PinInput         = g_stNLHV4051Chip4.PIN_X3;  //高有效1_P
  g_stLineInControl[Gas_leak_P].u8PinInput                 = g_stNLHV4051Chip4.PIN_X4;  //燃气泄漏报警_P
  g_stLineInControl[High_effective_3_P].u8PinInput         = g_stNLHV4051Chip4.PIN_X5;  //高有效3_P
  g_stLineInControl[Transmission_failure_P].u8PinInput     = g_stNLHV4051Chip4.PIN_X6;  //变速箱故障警报_P
  g_stLineInControl[Highly_significant_4_P].u8PinInput     = g_stNLHV4051Chip4.PIN_X7;  //高有效4_P

  g_stLineInControl[Turn_left_P].u8PinInput                = g_stNLHV4051Chip5.PIN_X0;  //左转向指示_P
  g_stLineInControl[Trailer_right_turn_P].u8PinInput       = g_stNLHV4051Chip5.PIN_X1;  //挂车右转向指示_P
  g_stLineInControl[Trailer_left_turn_P].u8PinInput        = g_stNLHV4051Chip5.PIN_X2;  //挂车左转向指示_P
  g_stLineInControl[Turn_right_P].u8PinInput               = g_stNLHV4051Chip5.PIN_X3;  //右转向指示_P
  g_stLineInControl[The_high_beam_P].u8PinInput            = g_stNLHV4051Chip5.PIN_X5;  //远光灯指示_P
  g_stLineInControl[Instrument_lighting_P].u8PinInput      = g_stNLHV4051Chip5.PIN_X7;  //仪表照明信号_P
}  

void Line_In_Debounce_Handle(LineInChannel Channel)
{
	if (g_stLineInControl[Channel].u8PinInputLast != g_stLineInControl[Channel].u8PinInput)
	{
		g_stLineInControl[Channel].u16TimeCount = 0u;
		g_stLineInControl[Channel].u8PinInputLast = g_stLineInControl[Channel].u8PinInput;
	}
	else
	{
		/*硬线输入等于有效电平*/
		if (g_stLineInAttribute[Channel].u8ValidLevel == g_stLineInControl[Channel].u8PinInput)
		{
			g_stLineInControl[Channel].u16TimeCount += LINE_IN_CYCLE;
			if (g_stLineInControl[Channel].u16TimeCount >= g_stLineInAttribute[Channel].u16ValidTime)
			{
				g_stLineInControl[Channel].u16TimeCount = 0u;
				g_stLineInControl[Channel].u8Status = 1u;
			}
		}
		else
		{
			g_stLineInControl[Channel].u16TimeCount += LINE_IN_CYCLE;
			if (g_stLineInControl[Channel].u16TimeCount >= g_stLineInAttribute[Channel].u16InvalidTime)
			{
				g_stLineInControl[Channel].u16TimeCount = 0u;
				g_stLineInControl[Channel].u8Status = 0u;
			}
		}
	}
}
//推荐值10ms
void CD4051_In_Debounce_Service(void)
{
	LineInChannel i = 0;
	Line_In_Real_Time_Input();
	for (i = 0; i < LINE_IN_MAX; i++)
	{
		if (LINE_IN_IGNONOFF == g_stLineInAttribute[i].u8PowerStatus)
		{
			Line_In_Debounce_Handle(i);
		}
		else if (LINE_IN_IGNON == g_stLineInAttribute[i].u8PowerStatus)
		{
			if (LINE_IN_IGNON == g_u8LineInPowerStatus)
			{
				Line_In_Debounce_Handle(i);
			}
			else /*切换电源状态时已经操作过了*/
			{
			}
		}
		else //if(LINE_IN_IGNOFF==g_stLineInAttribute[i].u8PowerStatus)
		{
			if (LINE_IN_IGNOFF == g_u8LineInPowerStatus)
			{
				Line_In_Debounce_Handle(i);
			}
			else /*切换电源状态时已经操作过了*/
			{
			}
		}
	}
}
/*1表示硬线有效，0硬线无效*/
uint8_t Line_In_Get_Status(LineInChannel Channel)
{
	return g_stLineInControl[Channel].u8Status;
}
void Line_In_KL30_Init(void)
{
	uint8_t i = 0;
	g_u8LineInPowerStatus = 0u;
	for (i = 0; i < LINE_IN_MAX; i++)
	{
		g_stLineInControl[i].u16TimeCount = 0;
		g_stLineInControl[i].u8PinInputLast = 0;
		g_stLineInControl[i].u8Status = 0;
	}
}
void Line_In_KL15_ON_Init(void)
{
	uint8_t i = 0;
	g_u8LineInPowerStatus = LINE_IN_IGNON;
	for (i = 0; i < LINE_IN_MAX; i++)
	{ /*硬线在当前电源状态下不工作*/
		if (LINE_IN_IGNOFF == g_stLineInAttribute[i].u8PowerStatus)

		{
			g_stLineInControl[i].u8Status = 0;
			g_stLineInControl[i].u16TimeCount = 0;
		}
	}
}
void Line_In_KL15_OFF_Init(void)
{
	uint8_t i = 0;
	g_u8LineInPowerStatus = LINE_IN_IGNOFF;
	for (i = 0; i < LINE_IN_MAX; i++)
	{ /*硬线在当前电源状态下不工作*/
		if (LINE_IN_IGNON == g_stLineInAttribute[i].u8PowerStatus)

		{
			g_stLineInControl[i].u8Status = 0;
			g_stLineInControl[i].u16TimeCount = 0;
		}
	}
}
