/******************************************************************************
�� �� ����Fuel.c
����������ȼ��
��    �ߣ�����
��    ����V1.0
��    �ڣ�2016.11.3
******************************************************************************/
#include "Include.h"

volatile uint16_t AnalogFuelSensorRes;       //ȼ�ʹ�������ֵ,��λ��0.1 Ohm
unsigned char sFuelInitOver ;
	
void Fuel_KL30_Read_Config_Init(void)	
{
	FuelInit_Data.DisplayZoneBoundary[0] = Val_EOL_F102_EFuelSensor();	    	    
	FuelInit_Data.DisplayZoneBoundary[1] = Val_EOL_F102_EFuelSensor();	  ;	 //ָE��   10
	FuelInit_Data.DisplayZoneBoundary[2] = Val_EOL_F102_EighthFuelSensor();	 //ָ1/8   20
	FuelInit_Data.DisplayZoneBoundary[3] = Val_EOL_F102_QuarterFuelSensor();	 //ָ2/8   30
	FuelInit_Data.DisplayZoneBoundary[4] = Val_EOL_F102_Three8FuelSensor();	 //ָ3/8   40
	FuelInit_Data.DisplayZoneBoundary[5] = Val_EOL_F102_AHalfFuelSensor();	 //ָ4/8   50
	FuelInit_Data.DisplayZoneBoundary[6] = Val_EOL_F102_Five8FuelSensor();	 //5/8     60
	FuelInit_Data.DisplayZoneBoundary[7] = Val_EOL_F102_Three3FuelSensor();	 //6/8     70
	FuelInit_Data.DisplayZoneBoundary[8] = Val_EOL_F102_Seven8FuelSensor();	 //7/8     80
	FuelInit_Data.DisplayZoneBoundary[9] = Val_EOL_F102_FFuelSensor();	 //8/8     90
	FuelInit_Data.DisplayZoneBoundary[10] = Val_EOL_F102_FFuelSensor();	 //ƫ��  
	

	FuelInit_Data.FuelCapacityBoundary[0] = 0;
	FuelInit_Data.FuelCapacityBoundary[1] = Val_EOL_F102_EFuelLevel();      //E
	FuelInit_Data.FuelCapacityBoundary[2] = Val_EOL_F102_EighthFuelLevel();    //1
	FuelInit_Data.FuelCapacityBoundary[3] = Val_EOL_F102_QuarterFuelLevel();    //2
	FuelInit_Data.FuelCapacityBoundary[4] = Val_EOL_F102_Three8FuelLevel();    //3
	FuelInit_Data.FuelCapacityBoundary[5] = Val_EOL_F102_AHalfFuelLevel();    //4
	FuelInit_Data.FuelCapacityBoundary[6] = Val_EOL_F102_Five8FuelLevel();    //5
	FuelInit_Data.FuelCapacityBoundary[7] = Val_EOL_F102_Three4FuelLevel();    //6
	FuelInit_Data.FuelCapacityBoundary[8] = Val_EOL_F102_Seven8FuelLevel();    //7
	FuelInit_Data.FuelCapacityBoundary[9] = Val_EOL_F102_FFuelLevel();    //8 F
	FuelInit_Data.FuelCapacityBoundary[10] = Val_EOL_F102_FFuelLevel();//
	
	FuelInit_DataBackup.DisplayZoneBoundary[0] = FuelInit_Data.DisplayZoneBoundary[0]; 
	FuelInit_DataBackup.DisplayZoneBoundary[1] = FuelInit_Data.DisplayZoneBoundary[1]; 
	FuelInit_DataBackup.DisplayZoneBoundary[2] = FuelInit_Data.DisplayZoneBoundary[2]; 
	FuelInit_DataBackup.DisplayZoneBoundary[3] = FuelInit_Data.DisplayZoneBoundary[3]; 
	FuelInit_DataBackup.DisplayZoneBoundary[4] = FuelInit_Data.DisplayZoneBoundary[4]; 
	FuelInit_DataBackup.DisplayZoneBoundary[5] = FuelInit_Data.DisplayZoneBoundary[5]; 
	FuelInit_DataBackup.DisplayZoneBoundary[6] = FuelInit_Data.DisplayZoneBoundary[6];
	FuelInit_DataBackup.DisplayZoneBoundary[7] = FuelInit_Data.DisplayZoneBoundary[7]; 
	FuelInit_DataBackup.DisplayZoneBoundary[8] = FuelInit_Data.DisplayZoneBoundary[8]; 
	FuelInit_DataBackup.DisplayZoneBoundary[9] = FuelInit_Data.DisplayZoneBoundary[9]; 
	FuelInit_DataBackup.DisplayZoneBoundary[10] = FuelInit_Data.DisplayZoneBoundary[10]  ;

	FuelInit_DataBackup.FuelCapacityBoundary[0] = FuelInit_Data.FuelCapacityBoundary[0]  ;
	FuelInit_DataBackup.FuelCapacityBoundary[1] = FuelInit_Data.FuelCapacityBoundary[1]  ;
	FuelInit_DataBackup.FuelCapacityBoundary[2] = FuelInit_Data.FuelCapacityBoundary[2]  ;
	FuelInit_DataBackup.FuelCapacityBoundary[3] = FuelInit_Data.FuelCapacityBoundary[3]  ;
	FuelInit_DataBackup.FuelCapacityBoundary[4] = FuelInit_Data.FuelCapacityBoundary[4]  ;
	FuelInit_DataBackup.FuelCapacityBoundary[5] = FuelInit_Data.FuelCapacityBoundary[5]  ;
	FuelInit_DataBackup.FuelCapacityBoundary[6] = FuelInit_Data.FuelCapacityBoundary[6] ;
	FuelInit_DataBackup.FuelCapacityBoundary[7] = FuelInit_Data.FuelCapacityBoundary[7]  ;
	FuelInit_DataBackup.FuelCapacityBoundary[8] = FuelInit_Data.FuelCapacityBoundary[8]  ;
	FuelInit_DataBackup.FuelCapacityBoundary[9] = FuelInit_Data.FuelCapacityBoundary[9]  ;
	FuelInit_DataBackup.FuelCapacityBoundary[10] = FuelInit_Data.FuelCapacityBoundary[10] ;
	
}

void Fuel_Change_Config(void)	
{
	FuelInit_Data.DisplayZoneBoundary[0] = Val_EOL_F102_EFuelSensor();	    	    
	FuelInit_Data.DisplayZoneBoundary[1] = Val_EOL_F102_EFuelSensor();	  ;	 //ָE��   10
	FuelInit_Data.DisplayZoneBoundary[2] = Val_EOL_F102_EighthFuelSensor();	 //ָ1/8   20
	FuelInit_Data.DisplayZoneBoundary[3] = Val_EOL_F102_QuarterFuelSensor();	 //ָ2/8   30
	FuelInit_Data.DisplayZoneBoundary[4] = Val_EOL_F102_Three8FuelSensor();	 //ָ3/8   40
	FuelInit_Data.DisplayZoneBoundary[5] = Val_EOL_F102_AHalfFuelSensor();	 //ָ4/8   50
	FuelInit_Data.DisplayZoneBoundary[6] = Val_EOL_F102_Five8FuelSensor();	 //5/8     60
	FuelInit_Data.DisplayZoneBoundary[7] = Val_EOL_F102_Three3FuelSensor();	 //6/8     70
	FuelInit_Data.DisplayZoneBoundary[8] = Val_EOL_F102_Seven8FuelSensor();	 //7/8     80
	FuelInit_Data.DisplayZoneBoundary[9] = Val_EOL_F102_FFuelSensor();	 //8/8     90
	FuelInit_Data.DisplayZoneBoundary[10] = Val_EOL_F102_FFuelSensor();	 //ƫ��  
	

	FuelInit_Data.FuelCapacityBoundary[1] = Val_EOL_F102_EFuelLevel();      //E
	FuelInit_Data.FuelCapacityBoundary[2] = Val_EOL_F102_EighthFuelLevel();    //1
	FuelInit_Data.FuelCapacityBoundary[3] = Val_EOL_F102_QuarterFuelLevel();    //2
	FuelInit_Data.FuelCapacityBoundary[4] = Val_EOL_F102_Three8FuelLevel();    //3
	FuelInit_Data.FuelCapacityBoundary[5] = Val_EOL_F102_AHalfFuelLevel();    //4
	FuelInit_Data.FuelCapacityBoundary[6] = Val_EOL_F102_Five8FuelLevel();    //5
	FuelInit_Data.FuelCapacityBoundary[7] = Val_EOL_F102_Three4FuelLevel();    //6
	FuelInit_Data.FuelCapacityBoundary[8] = Val_EOL_F102_Seven8FuelLevel();    //7
	FuelInit_Data.FuelCapacityBoundary[9] = Val_EOL_F102_FFuelLevel();    //8 F
	FuelInit_Data.FuelCapacityBoundary[10] = Val_EOL_F102_FFuelLevel();//	
	
}

void Fuel_KL15_Change_Config_Init(void)	
{
	if( (FuelInit_DataBackup.DisplayZoneBoundary[0] != FuelInit_Data.DisplayZoneBoundary[0]) ||
	(FuelInit_DataBackup.DisplayZoneBoundary[1] != FuelInit_Data.DisplayZoneBoundary[1] )||
	(FuelInit_DataBackup.DisplayZoneBoundary[2] != FuelInit_Data.DisplayZoneBoundary[2] )||
	(FuelInit_DataBackup.DisplayZoneBoundary[3] != FuelInit_Data.DisplayZoneBoundary[3] )||
	(FuelInit_DataBackup.DisplayZoneBoundary[4] != FuelInit_Data.DisplayZoneBoundary[4] )|| 
	(FuelInit_DataBackup.DisplayZoneBoundary[5] != FuelInit_Data.DisplayZoneBoundary[5])||
	(FuelInit_DataBackup.DisplayZoneBoundary[6] != FuelInit_Data.DisplayZoneBoundary[6])||
	(FuelInit_DataBackup.DisplayZoneBoundary[7] != FuelInit_Data.DisplayZoneBoundary[7])|| 
	(FuelInit_DataBackup.DisplayZoneBoundary[8] != FuelInit_Data.DisplayZoneBoundary[8])|| 
	(FuelInit_DataBackup.DisplayZoneBoundary[9] != FuelInit_Data.DisplayZoneBoundary[9])|| 
	(FuelInit_DataBackup.DisplayZoneBoundary[10] != FuelInit_Data.DisplayZoneBoundary[10] )|| 
	(FuelInit_DataBackup.FuelCapacityBoundary[2] != FuelInit_Data.FuelCapacityBoundary[2]  )|| 
	(FuelInit_DataBackup.FuelCapacityBoundary[3] != FuelInit_Data.FuelCapacityBoundary[3]  )|| 
	(FuelInit_DataBackup.FuelCapacityBoundary[4] != FuelInit_Data.FuelCapacityBoundary[4]  )|| 
	(FuelInit_DataBackup.FuelCapacityBoundary[5] != FuelInit_Data.FuelCapacityBoundary[5]  )|| 
	(FuelInit_DataBackup.FuelCapacityBoundary[6] != FuelInit_Data.FuelCapacityBoundary[6] )||
	(FuelInit_DataBackup.FuelCapacityBoundary[7] != FuelInit_Data.FuelCapacityBoundary[7]  )|| 
	(FuelInit_DataBackup.FuelCapacityBoundary[8] != FuelInit_Data.FuelCapacityBoundary[8]  )|| 
	(FuelInit_DataBackup.FuelCapacityBoundary[9] != FuelInit_Data.FuelCapacityBoundary[9]  )|| 
	(FuelInit_DataBackup.FuelCapacityBoundary[10] !=FuelInit_Data.FuelCapacityBoundary[10] ) )
	{
		
		FuelInit_DataBackup.DisplayZoneBoundary[0] = FuelInit_Data.DisplayZoneBoundary[0]; 
		FuelInit_DataBackup.DisplayZoneBoundary[1] = FuelInit_Data.DisplayZoneBoundary[1]; 
		FuelInit_DataBackup.DisplayZoneBoundary[2] = FuelInit_Data.DisplayZoneBoundary[2]; 
		FuelInit_DataBackup.DisplayZoneBoundary[3] = FuelInit_Data.DisplayZoneBoundary[3]; 
		FuelInit_DataBackup.DisplayZoneBoundary[4] = FuelInit_Data.DisplayZoneBoundary[4]; 
		FuelInit_DataBackup.DisplayZoneBoundary[5] = FuelInit_Data.DisplayZoneBoundary[5]; 
		FuelInit_DataBackup.DisplayZoneBoundary[6] = FuelInit_Data.DisplayZoneBoundary[6];
		FuelInit_DataBackup.DisplayZoneBoundary[7] = FuelInit_Data.DisplayZoneBoundary[7]; 
		FuelInit_DataBackup.DisplayZoneBoundary[8] = FuelInit_Data.DisplayZoneBoundary[8]; 
		FuelInit_DataBackup.DisplayZoneBoundary[9] = FuelInit_Data.DisplayZoneBoundary[9]; 
		FuelInit_DataBackup.DisplayZoneBoundary[10] = FuelInit_Data.DisplayZoneBoundary[10];

		FuelInit_DataBackup.FuelCapacityBoundary[1] = FuelInit_Data.FuelCapacityBoundary[1]  ;
		FuelInit_DataBackup.FuelCapacityBoundary[2] = FuelInit_Data.FuelCapacityBoundary[2]  ;
		FuelInit_DataBackup.FuelCapacityBoundary[3] = FuelInit_Data.FuelCapacityBoundary[3]  ;
		FuelInit_DataBackup.FuelCapacityBoundary[4] = FuelInit_Data.FuelCapacityBoundary[4]  ;
		FuelInit_DataBackup.FuelCapacityBoundary[5] = FuelInit_Data.FuelCapacityBoundary[5]  ;
		FuelInit_DataBackup.FuelCapacityBoundary[6] = FuelInit_Data.FuelCapacityBoundary[6] ;
		FuelInit_DataBackup.FuelCapacityBoundary[7] = FuelInit_Data.FuelCapacityBoundary[7]  ;
		FuelInit_DataBackup.FuelCapacityBoundary[8] = FuelInit_Data.FuelCapacityBoundary[8]  ;
		FuelInit_DataBackup.FuelCapacityBoundary[9] = FuelInit_Data.FuelCapacityBoundary[9]  ;
		FuelInit_DataBackup.FuelCapacityBoundary[10] = FuelInit_Data.FuelCapacityBoundary[10] ;
		
		
		SET_YB_InitData();	
	}
	
}

 
void PowerDN_Time_Add(void)
{                              
	uint8_t i;

	if (SYS_OPR_STAT_IGN_ON)
	{
	}
	else
	{
		SetPowerSt(0);

		for (i = 0; i < 10; i++)
		{
			AFCTimerCall();
		}
		FuelFSM();
	}
}

	       

//INT16U FUE_REST = 760;;
void GET_Data_For_YB(void) //10ms����
{
	if(SYS_OPR_STAT_IGN_ON)
	{
		SetPowerSt(1); 
		//FuelPARA.RPM = DataESpeedDisp;//���ת��// DataEspeedDisp  1
		FuelPARA.RPM = 1;//���ת��// DataEspeedDisp  1
		FuelPARA.Speed = DataVSpeedDisp/10 ;//����                1
		FuelPARA.CurODO = NVM_ODO_VALUE * 100; //��λΪ��  1m
		FuelPARA.FuleConsume =	  0;
		SetAFCPara((AFC_PARA *)&FuelPARA); 
		AFCTimerCall();
		SaveSampleVal(AnalogFuelSensorRes);
		FuelFSM();

	}
}

void SET_YB_InitData(void)
{
 //#if(IC_Current == LD01 )
 	FuelData.Flag  = 0;
	FuelInit.FuelOpenCircuitR = Fuel_Open_Res;
	FuelInit.FuelShortCircuitR = Fuel_Short_Cricuit_Res;
	

	FuelInit.DisplayZoneBoundary[0] =  (FuelInit_DataBackup.DisplayZoneBoundary[0] +55)*10;
	FuelInit.DisplayZoneBoundary[1] =  FuelInit_DataBackup.DisplayZoneBoundary[1] *10;
	FuelInit.DisplayZoneBoundary[2] =  FuelInit_DataBackup.DisplayZoneBoundary[2] *10; 
	FuelInit.DisplayZoneBoundary[3] =  FuelInit_DataBackup.DisplayZoneBoundary[3]  *10;
	FuelInit.DisplayZoneBoundary[4] =  FuelInit_DataBackup.DisplayZoneBoundary[4]  *10;
	FuelInit.DisplayZoneBoundary[5] =  FuelInit_DataBackup.DisplayZoneBoundary[5]  *10;    
	FuelInit.DisplayZoneBoundary[6] =  FuelInit_DataBackup.DisplayZoneBoundary[6]  *10;
	FuelInit.DisplayZoneBoundary[7] =  FuelInit_DataBackup.DisplayZoneBoundary[7]  *10;
	FuelInit.DisplayZoneBoundary[8] =  FuelInit_DataBackup.DisplayZoneBoundary[8]  *10; 
	FuelInit.DisplayZoneBoundary[9] =  FuelInit_DataBackup.DisplayZoneBoundary[9]  *10;
	FuelInit.DisplayZoneBoundary[10] = (FuelInit_DataBackup.DisplayZoneBoundary[10]-8)*10;
	
	FuelInit.FuelCapacityBoundary[0] = 0;
	FuelInit.FuelCapacityBoundary[1] = (FuelInit_DataBackup.FuelCapacityBoundary[1]-FuelInit_DataBackup.FuelCapacityBoundary[1]) *10;
	FuelInit.FuelCapacityBoundary[2] = (FuelInit_DataBackup.FuelCapacityBoundary[2]-12) *10;
	FuelInit.FuelCapacityBoundary[3] = (FuelInit_DataBackup.FuelCapacityBoundary[3]-12) *10; 
	FuelInit.FuelCapacityBoundary[4] = (FuelInit_DataBackup.FuelCapacityBoundary[4]-12) *10;
	FuelInit.FuelCapacityBoundary[5] = (FuelInit_DataBackup.FuelCapacityBoundary[5]-12) *10;
	FuelInit.FuelCapacityBoundary[6] = (FuelInit_DataBackup.FuelCapacityBoundary[6]-12) *10;    
	FuelInit.FuelCapacityBoundary[7] = (FuelInit_DataBackup.FuelCapacityBoundary[7] -12) *10;
	FuelInit.FuelCapacityBoundary[8] = (FuelInit_DataBackup.FuelCapacityBoundary[8] -12)*10;
	FuelInit.FuelCapacityBoundary[9] = (FuelInit_DataBackup.FuelCapacityBoundary[9] -12)*10; 
	FuelInit.FuelCapacityBoundary[10] = (FuelInit_DataBackup.FuelCapacityBoundary[10]-12+ 8)*10;
															
	InitFuelParamter(&FuelInit);

	FuelAFCInit.CalcAFCMaxNum = 100;
	FuelAFCInit.AFCMin = 30;
	FuelAFCInit.AFCMax = 199;
	FuelAFCInit.AFCPreinstall = 60;
	FuelAFCInit.CurODO = NVM_ODO_VALUE * 100;
	FuelAFCInit.MaxDRMileage = 100000;
	
	InitAFC((AFC_INIT *)&FuelAFCInit);
	
	SetFuelSensorRecoverGroupCnt(40);
	SetFuelSensorOpenCircuitGroupCnt(40);
	SetFuelSensorShortCircuitGroupCnt(40);
}

void GET_Data_To_DSP(void) //100ms����
{
       uint32_t m32 = 0;
	if((SYS_OPR_STAT_IGN_ON)&&(g_u8SelfLampTimes >= 60))
	{
		Fuel_Change_Config();

		FuelData.Res_Get = GetFuelResistance();	
		FuelData.Sensor_State = FuelSensorState(FuelData.Res_Get);
		
		if(FuelData.Sensor_State == FuelSensorNormal)
		{	         
		         FuelData.Res_Valid  = 1;
			if((FuelData.Res_Get >= Fuel_Short_Cricuit_Res)&& (FuelData.Res_Get < Fuel_Open_Res))
			{				
				FuelData.Flag = 1;
				FuelData.Step_Get = GetFuelStep();
				FuelData.Capacity_Get = GetFuelCapacityVal();				
				//�ⷢʣ�������ٷֱ�
				FuelData.Capacity_Val = GetFuelCapacityVal();
				FuelData.Res_Dis = FuelData.Res_Get ;		
				if(FuelData.Res_Get <= FuelInit.DisplayZoneBoundary[9])
				{
					FuelData.Precent_Val = 100;	
				}
				else if(FuelData.Res_Get >= FuelInit.DisplayZoneBoundary[1])
				{
					FuelData.Precent_Val  = 0;	
				}
				else
				{
					m32 = FuelData.Capacity_Val ;
					m32 *= 1000 ;
					m32 /= (FuelInit.FuelCapacityBoundary[9]);
					m32 += 5;
					m32 /= 10;
					FuelData.Precent_Val = (uint8_t)m32;
				}
			}
			else //ʵ�ʿ�·�����Ǵ�����״̬û�з����ı�
			{
				FuelData.Flag = 0;	
			}
		}
		else if(FuelData.Sensor_State == FuelSensorShortCircuit)
		{
			FuelData.Step_Get = 0;	
			FuelData.Res_Dis = 0 ;
			FuelData.Res_Valid  = 1;
			FuelData.Flag = 0;	
			FuelData.Precent_Val = 0;
			FuelData.Capacity_Get = 0;
			
		}
		else  if(FuelData.Sensor_State == FuelSensorOpenCircuit)
		{
			FuelData.Step_Get = 0;
			FuelData.Res_Dis = 5000 ;
			FuelData.Res_Valid  = 1;
			FuelData.Flag = 0;
			FuelData.Precent_Val = 0;
			FuelData.Capacity_Get = 0;
		}
		else
		{
			FuelData.Step_Get = 0;
			FuelData.Res_Valid  = 0;
			FuelData.Flag = 0;
			FuelData.Precent_Val = 0;
			FuelData.Capacity_Get = 0;
		}
	}
	else
	{
		FuelData.Flag = 0;
		FuelData.Step_Get = 0;
		FuelData.Res_Valid  = 0;
	}
}

unsigned char FuelSensorState(unsigned int vFuelR)
{
	unsigned char sFuelSensor;
	sFuelSensor = GetFuelSensorState();
//	sFuelSensor_1 = GetFuelSensorState();
	if(sFuelInitOver == 0)
	{	                
		if((sFuelSensor == FuelSensorNormal) && (vFuelR <= Fuel_Open_Res) && (vFuelR >= Fuel_Short_Cricuit_Res))
		{
			sFuelInitOver = 1;
		}
		else
		{
			if((sFuelSensor == FuelSensorShortCircuit) || (sFuelSensor == FuelSensorOpenCircuit))
			{
				sFuelInitOver = 1;
			}
			else
			{
				sFuelSensor = FuelSensorPreFault;
			}
		}
	}

	return sFuelSensor;
}  



	
	


