
#include "Drive_Info.h"
//#include "Menu_Process.h"

uint8_t TripPlay;
uint8_t ReverseGear_temp;
//uint8_t	BG_display_flag = 0;    //�˵���ʼ�����λ
uint8_t DriveInfoCurrent;
/*****************�ܼ�******************************/
DriveInfoODOExchangeStruct    DriveInfoODO;
DriveInfoODOClearStruct       DriveInfoODOClear;
DriveInfoODOClearStruct       DriveInfoTPMSSet;
/****************�������****************************/
DriveInfoSpeedPlusConverterStruct  DriveInfoSpeedPlusConverter;
/****************ȼ����ֵƵ�����****************************/
DriveInfoSpeedPlusConverterStruct  DriveInfoFuelPlusConverter;
uint8_t Time_Synchronize_Flag = 0;

void Drive_Info_Power_On_Init ( void )
{
	DriveInfoCurrent            = DRIVE_INFO_TPMS; //��ʼ��Ĭ�Ͽ�����̥ѹ״̬
	DriveInfoODO.Output         = 0x000FFFFF;
	DriveInfoODO.BackupComplete = 0;
	DriveInfoODO.BackupEnable   = 0;
	DriveInfoODOClear.TripMask  = 0;
	DriveInfoODOClear.Enable    = 0;
	DriveInfoODOClear.IGNOn     = 0;
	DriveInfoODOClear.Timer     = 0;
	
	DriveInfoTPMSSet.TripMask = 0;
	DriveInfoTPMSSet.Enable   = 0;
	DriveInfoTPMSSet.IGNOn    = 0;
	DriveInfoTPMSSet.Timer    = 0;
	DriveInfoTPMSSet.TimerTenS = 0;
	DriveInfoTPMSSet.TimerEighteenS = 0;
	DriveInfoTPMSSet.TpmsStatus = 0;
	DriveInfoTPMSSet.TpmsStatustens = 0;
	
	DriveInfoSpeedPlusConverter.Enable    = 0;
	DriveInfoSpeedPlusConverter.Level     = 1;
	DriveInfoSpeedPlusConverter.Timer     = 0xFFFE;
	DriveInfoSpeedPlusConverter.Period    = 0xFFFF;
	DriveInfoSpeedPlusConverter.ActPeriod = 0xFFFF;

	DriveInfoFuelPlusConverter.Enable    = 0;
	DriveInfoFuelPlusConverter.Level     = 1;
	DriveInfoFuelPlusConverter.Timer     = 0xFFFE;
	DriveInfoFuelPlusConverter.Period    = 0xFFFF;
	DriveInfoFuelPlusConverter.ActPeriod = 0xFFFF;
}
 
void Drive_Info_Display_Update_Service(void)
{
  /******************������Ϣ��ʾ*********************************/
  if (IODispMode == IO_DISP_MODE_IGN_ON)
  {  
    if (IO_BG_INFO_DISPLAY_ENABLE)
    {
      //----hyq--20190304 15���¼��һ��״̬   ��ʾ������Ϣ��ÿ����15������һ��
      /* if (BG_display_flag == 0)
      {
        //��ʼ����Ļ�˵�����ֵλ��
        BG_display_flag = 1;
        MenuInit();
      } 
      */

      //����ѹ
		  GUI_BG_GAS1_Display ( GaugesCtrl.GAS1Seg ); 
		  //����ѹ
		  GUI_BG_GAS2_Display ( GaugesCtrl.GAS2Seg ); 
		  //����
      GUI_BG_Vehicle_Display (GaugesCtrl.VSpeedSeg);
      SEG_LCD_Vehicle_Speed_Num_Display(DATA_VEHICLE_SPEED_DISPLAYING); 
      //ת��
      GUI_BG_Engine_Display ( GaugesCtrl.ESpeedSeg );        
      SEG_LCD_Engine_Speed_Num_Display ( DATA_ENGINE_SPEED_DISPLAYING );
      
      //��ͼ
      //TFT_LCD_Display_Sprite(LAYER_BEIJING, SN_heisequyu_png,  0, 0, 64); 
		  
		  //TRIP
		  GUI_BG_TRIP_A_Display(DATA_TRIP_VALID, DATA_TRIPA, GUI_DISP_MODE_NORMAL);
		  
		  //ODO
		  GUI_BG_ODO_Display ( GUI_DISP_MODE_NORMAL, NVM_ODO_VALUE );
		  
		  //ʱ����ʾ
		  if (Time_Synchronize_Flag == 0)
		  {
    		if (IO_DISP_TEST_OFF_TIME <= 150)         //20200908
  		  {
    		  if (!(GPS_SYSTime_STATUS&CAN_MSG_LOST))
          {
            if ((GPS_SYSTime_Min < 60) && (GPS_SYSTime_Hour < 24))
            {
              RTC_MINUTE = GPS_SYSTime_Min;
              RTC_HOUR   = GPS_SYSTime_Hour;
              Time_Synchronize_Flag = 1;
            }
          }
        }
        else
          Time_Synchronize_Flag = 1;
      }
    #if 0  
      if (Time_Synchronize_Flag == 1)
		    GUI_BG_Clock_Display(GUI_DISP_MODE_NORMAL);
      else
        TFT_LCD_Clear_Multi_Layer(LAYER_Time_Hour1, LAYER_Time_Minute2);
		  
		  GUI_BG_TEMP_Display(GUI_DISP_MODE_NORMAL);
		#endif
		  //��λ
      
		  if (NVM_CONFIG_TBOX == 0) //�ֶ�
		  {
        if (!(EGSM_1_STATUS & CAN_MSG_LOST)) //����λ������   20200530fhl
        {
          if ((ActualGearPosition == 0x09) || (ActualGearPosition == 0x0A))
            ReverseGear_temp = 1;
          else
            ReverseGear_temp = 0;
          GUI_BG_Gear_Alarm_Display(GUI_DISP_MODE_NORMAL);
        }
        else //������λ������
        {
          TFT_LCD_Clear_Multi_Layer(LAYER_Gear, LAYER_Gear2); //20190618
    		  
    		  if (LINE_IN_GEAR_SIGNAL) 
    		  {
    		    ReverseGear_temp = 1;
            TFT_LCD_Display_Sprite(LAYER_Gear, SN_GEAR_R_png, 367, 428, 64);
    		  }
          else 
          {
            ReverseGear_temp = 0;
            TFT_LCD_Clear_Layer(LAYER_Gear);
          }
        }
      }
      else //�Զ�
      {
        if (Gear_Display == 0x30)
          ReverseGear_temp = 1;
        else
          ReverseGear_temp = 0;
        GUI_BG_Gear_Display(GUI_DISP_MODE_NORMAL);
      }   

      //TFT_LCD_Clear_Multi_Layer(LAYER_MENU01, LAYER_MENU14); 
      //������ʾ����
      if ((IO_DRIVE_INFO_DISPLAY_ENABLE) && (~IO_POPUP_DISPLAY_ENABLE)) 
      { 
        //MenuManage();
        GUI_ADAS_Display(); 
      }
      Menu_Processing_Service(); //�˵��������� 
      Telltale_LCD_Display();  
    }
  } 
  else if (SYS_IGN_OFF_DISP_MODE)
  {
    TFT_LCD_Clear_Multi_Layer(0, YVC_LYR_NUM_CPU-3);
    if (PepslcuAlarm == 11)
    {
    }
    else if (((PepslcuAlarm > 0) && (PepslcuAlarm < 11)) || (PepslcuAlarm == 12) || (PepslcuAlarm == 14))
    {
      TFT_LCD_Clear_Layer(LAYER_MENU14);
    }
    else
      TFT_LCD_Clear_Multi_Layer(YVC_LYR_NUM_CPU - 2, YVC_LYR_NUM_CPU - 1);
    
    //�ſ�������     KL30     ��������15
  	if (TelltaleLCDFlag.Sig.LCD06 == 1)
  	  TFT_LCD_Display_Sprite(LAYER_LED06, SN_zhishideng_06_png, 265, 9, 64);
  	else 
  	  TFT_LCD_Clear_Layer(LAYER_LED06);
  	
  	//С��     KL15��KL30        48
  	if (TelltaleLCDFlag.Sig.LCD11 == 1) 
  	  TFT_LCD_Display_Sprite(LAYER_LED11, SN_zhishideng_11_png, 490, 9, 64);
  	else 
  	  TFT_LCD_Clear_Layer(LAYER_LED11);
    //Telltale_LCD_Display(); 
  }       
}

/***************************************�����ⷢ**************************************/
void Drive_Info_Export_Init(void)
{
  //Drive_Info_IC1_STATUS_Tx();
	ICM_Sleep_Indicator = 1;           //20200306��ʼ��ֻ�����籨��
	//ASP_STATUS     |= CAN_TX_EN;       //���� 

  NWM_ICM_STATUS |= CAN_TX_EN;       //���� 
	TCO1_STATUS  	 |= CAN_TX_EN;       //����  
	VDHR_STATUS	   |= CAN_TX_EN;       //����  
	AIR1_STATUS	   |= CAN_TX_EN;       //����  
  //PGNRQ_STATUS	 |= CAN_TX_EN;       //���� 
	ETCS_STATUS  	 |= CAN_TX_EN;       //����  
	ICM_1_STATUS 	 |= CAN_TX_EN;       //���� 
	ICM_2_STATUS	 |= CAN_TX_EN;       //���� 
	ICM_3_STATUS   |= CAN_TX_EN;       //����
	ICM_4_STATUS   |= CAN_TX_EN;       //����  

}
/***************************************�ⷢ����**************************************/
static void 	Drive_Info_IC1_STATUS_Tx(void)
{
   if(SYS_OPR_STAT_IGN_ON)           //20200306ֻ��ON���ŷ�1
   {
     ICM_Sleep_Indicator = 1;        //20200119
   }
   Tachograph_Vehicle_SpeedL = FA_SpeedL;                                                               
   Tachograph_Vehicle_SpeedH = FA_SpeedH;
   
   High_Resolution_Total_Vehicle_Distance_LL = (NVM_ODO_VALUE  * 20) & 0xFF; //ϵ��0.05
   High_Resolution_Total_Vehicle_Distance_LH = ((NVM_ODO_VALUE * 20) >> 8) &0xFF;
   High_Resolution_Total_Vehicle_Distance_HL = ((NVM_ODO_VALUE * 20) >> 16) &0xFF;
   High_Resolution_Total_Vehicle_Distance_HH = ((NVM_ODO_VALUE * 20) >> 24) &0xFF;
 	 
 	 FrontBarometricPress = FrontPress;
 	 RearBarometricPress  = RearPress;
 	 ReverseGear          = ReverseGear_temp;
 	 
 	 //AverageFuelConsumption_LL = AverageFuel_LL;
 	 //AverageFuelConsumption_LH = AverageFuel_LH;
 	 //AverageFuelConsumption_HL = AverageFuel_HL;
 	 //AverageFuelConsumption_HH = AverageFuel_HH;
 	
 	 LowBrakeLuidLevel  = LowBrakeLuidLevel_temp;
 	 SafetyBeltSt       = SafetyBeltSt_temp;
 	 TankLevel          = TankLevel_temp;
 	 MaintainSignal     = MaintainSignal_temp;
 	 ParkingBrakeSwitch = ParkingBrakeSwitch_temp;
 	 CabLockSt          = CabLockSt_temp;
}
/***************************************�������**************************************/
void Drive_Info_Speed_Plus_Conversion_ISR ( void )
{
	if ( DriveInfoSpeedPlusConverter.Enable )
	{
		DriveInfoSpeedPlusConverter.Timer++;

		if ( DriveInfoSpeedPlusConverter.ActPeriod >= ( 500000 / API_INT_CYCLE ) ) //1Hz���µ�Ƶ����������
			DriveInfoSpeedPlusConverter.ActPeriod = DriveInfoSpeedPlusConverter.Period;

		if ( DriveInfoSpeedPlusConverter.Timer >= DriveInfoSpeedPlusConverter.ActPeriod )
		{
			DriveInfoSpeedPlusConverter.Timer = 0;

			if ( DriveInfoSpeedPlusConverter.Level )
			{
				DriveInfoSpeedPlusConverter.Level = 0;
				DriveInfoSpeedPlusConverter.ActPeriod = DriveInfoSpeedPlusConverter.Period; //�����ؼ�������
			}
			else
				DriveInfoSpeedPlusConverter.Level = 1;
		}

		//LINE_SPEED_OUT = DriveInfoSpeedPlusConverter.Level;
	}
}
void Drive_Info_Speed_Plus_Conversion ( void )
{
	uint32_t Speed;

	if ( ( DATA_VEHICLE_SPEED_VALID ) && ( DATA_VEHICLE_SPEED_ACTUAL ) )
	{
		Speed = ( uint32_t ) DATA_VEHICLE_SPEED_ACTUAL;
		//18000000000 / API_INT_CYCLE = 281250000;
		DriveInfoSpeedPlusConverter.Period = ( uint32_t ) ( 36000000 / DRIVE_INFO_SPEED_PLUS_PPK ) / Speed;
		//DriveInfoSpeedPlusConverter.Period = ( uint32_t ) ( 281250000 / DRIVE_INFO_SPEED_PLUS_PPK ) / Speed;
		DriveInfoSpeedPlusConverter.Enable = 1;
	}
	else
	{
		if ( DriveInfoSpeedPlusConverter.Enable )
		{
			DriveInfoSpeedPlusConverter.Enable    = 0;
			DriveInfoSpeedPlusConverter.Level     = 0;
			DriveInfoSpeedPlusConverter.Timer     = 0;
			DriveInfoSpeedPlusConverter.Period    = 0xFFFFFFFF;
			DriveInfoSpeedPlusConverter.ActPeriod = 0xFFFFFFFF;
		}

		//LINE_SPEED_OUT = 1;
	}
}

void Drive_Info_Import_Export_Service ( void )
{
  Drive_Info_IC1_STATUS_Tx();            //�����ⷢ
	//Drive_Info_ODO_Clear_Ctrl();         //����������
	//Drive_Info_Speed_Plus_Conversion();  //�������
	//Drive_Info_Fuel_Plus_Conversion();   //ȼ����ֵ���
	//Drive_Info_Tpms_Learning_Ctrl();     //̥ѹѧϰģʽ
}
