/******************************************************************************
背光驱动
*****************************************************************************/
#include "BackLight.h"

BlPWMCaptureStruct  BlPWMCapture;
BlPWMFilterStruct   BlPWMFilter;
BlOFFCtrlStruct     BlOFFCtrl;
uint8_t             BlTurnOnDelay;
uint8_t             BlTurnOffDelay;
/******************************************************************************
函数名： BL_Init
功  能： 背光上电初始化
参  数： dutyCycle用于调节背光
返回值： 无
*******************************************************************************/
void BL_Init(void)
{
  BlOFFCtrl.DoorStatusBackup = 0;
  BlOFFCtrl.Timer            = 0;
  BlTurnOnDelay              = 10;
  BlTurnOffDelay             = 10;
}
/******************************************************************************
函数名： Backlight_Plate_Needle_Set
功  能： 表盘及表针背光设定
参  数： duty用于调节的背光占空比
返回值： 无
*******************************************************************************/
void BackLight_Plate_Needle_Set(uint8_t duty)
{
  if(duty < 7)
    duty = 7;
  if(duty > 95)
    duty = 95;
  PWM_Channel_Set_Duty_Cycle(2, duty);
}
/******************************************************************************
函数名： BackLight_Plate_Needle_Close
功  能： 表盘及表针背光关闭
参  数： 无
返回值： 无
*******************************************************************************/
void BackLight_Plate_Needle_Close(void)
{
  PWM_Channel_Set_Duty_Cycle(2, 0);
}
/******************************************************************************
函数名： Backlight_Init
功  能： TFT屏的背光设置
参  数： duty用于设置的占空比
返回值： 无
*******************************************************************************/
void BackLight_TFT_Set(uint8_t duty)
{
  if((duty < 7))
    duty = 7;
  if(duty > 95)
    duty = 95;
  PWM_Channel_Set_Duty_Cycle(7, duty);
}
/******************************************************************************
函数名： Backlight_Init
功  能： TFT屏背光关闭
参  数： 无
返回值： 无
*******************************************************************************/
void BackLight_TFT_Close(void)
{
  PWM_Channel_Set_Duty_Cycle(7, 0);
}
/*******************************************************************************
函数名：背光服务函数
参  数：背光调节步数KEY_Step
********************************************************************************/
void BL_Management_Service(void)
{
  //uint8_t  Msg;
  //uint8_t  DutyCycle;

  //DutyCycle = BL_KEY_Step;
  //if(SYS_OPR_STAT_IGN_ON)
  //  {}
  if (SYS_IGN_OFF_DISP_MODE)
  {
    BlOFFCtrl.Timer = 0;
    if (BlTurnOffDelay)
      BlTurnOffDelay--;
    else
    {
      if ((DriveDoorSts == 1) || (PsngrDoorSts == 1) || (PositionLightSts == 1) || (HazardLightSts == 1) || ((PepslcuAlarm > 0) && (PepslcuAlarm < 13)) || (PepslcuAlarm == 14) || ((RHTurnSignalSts == 1) && (LHTurnSignalSts == 1)))    //20200501 fhl
      {
			  if (PositionLightSts == 1) 
        { 
          BackLight_TFT_Set((BcakLightPOS + 1) * 15);             
          if ((DriveDoorSts == 1) || (PsngrDoorSts == 1) || (HazardLightSts == 1) || ((RHTurnSignalSts == 1) && (LHTurnSignalSts == 1))) 
          {  
            // BackLight_Plate_Needle_Set((BcakLightPOS+1)*15);
            PWM_Channel_Set_Duty_Cycle(5, ((BcakLightPOS + 1) * 15));
          }                     
          else
          {
            //BackLight_Plate_Needle_Close(); //20200306
            PWM_Channel_Set_Duty_Cycle(5, 0); 
          }    
        }
        else   
        {                   
          if ((DriveDoorSts == 1) || (PsngrDoorSts == 1) || (HazardLightSts == 1) || ((RHTurnSignalSts == 1) && (LHTurnSignalSts == 1))) 
          {  
            //BackLight_Plate_Needle_Set((BcakLightPOS + 1) * 25);
            PWM_Channel_Set_Duty_Cycle(5, ((BcakLightPOS + 1) * 25));
          }                    
          if ((DriveDoorSts == 1) || (PsngrDoorSts == 1))/* || ((PepslcuAlarm > 0) && (PepslcuAlarm < 13)) || (PepslcuAlarm == 14)) */
          {
            BackLight_TFT_Set((BcakLightPOS + 1) * 25);
          } 
          else if (((PepslcuAlarm > 0) && (PepslcuAlarm < 13)) || (PepslcuAlarm == 14)) 
          {
            BackLight_TFT_Set((BcakLightPOS + 1) * 25);
            PWM_Channel_Set_Duty_Cycle(5, 0); //2020 01 06 WH 修改off下报警大C问题
          }    
          else
            BackLight_TFT_Close(); //屏    
        }       
      }
      else
      {
        BackLight_TFT_Close();     //屏
        PWM_Channel_Set_Duty_Cycle(5, 0);
      }
      
      BackLight_Plate_Needle_Close(); //20200306 
    }
  } 
  else 
  {
    BlOFFCtrl.Timer = 0;
    if(BlTurnOnDelay)
      BlTurnOnDelay--;
    else
    {
      if (PositionLightSts == 1)
      { 	 			
        BackLight_TFT_Set((BcakLightPOS + 1) * 15);
        BackLight_Plate_Needle_Set((BcakLightPOS + 1) * 15);
        PWM_Channel_Set_Duty_Cycle(5, ((BcakLightPOS + 1) * 15));
      }
      else
      {
        BackLight_TFT_Set((BcakLightPOS + 1) * 25);
        BackLight_Plate_Needle_Set((BcakLightPOS + 1) * 25);
        PWM_Channel_Set_Duty_Cycle(5, ((BcakLightPOS + 1) * 25));
      }
    }
  }
}

void PWM_INIT(void)
{
  PWM_Channel_Init(7, PWM_CLOCK_SB, PWM_POL_POS, PWM_LEFT_ALIGN, PWM7_PM3); //屏背光初始化
  PWM_Channel_Set_Freq(7, 20000);
  PWM_Channel_Set_Duty_Cycle(7, 0); //初始化为背光关闭,由后续应用程序开启
  PWM_Channel_Start(7);        

  PWM_Channel_Init(2, PWM_CLOCK_SB, PWM_POL_POS, PWM_LEFT_ALIGN, PWM2_PP2); //指示灯背光
  PWM_Channel_Set_Freq(2, 20000);
  PWM_Channel_Set_Duty_Cycle(2, 0); //初始化为背光关闭,由后续应用程序开启
  PWM_Channel_Start(2);
  
  PWM_Channel_Init(5, PWM_CLOCK_SA, PWM_POL_POS, PWM_LEFT_ALIGN, PWM5_PP5); //大C背光
  PWM_Channel_Set_Freq(5, 100);
  PWM_Channel_Set_Duty_Cycle(5, 0); //初始化为背光关闭,由后续应用程序开启
  PWM_Channel_Start(5);
}
