
#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();     //胎压学习模式
}
