/* Includes ------------------------------------------------------------------*/
#include <sys_conf.h>
#include <MC_UserInterface_layer.h>   
#include <MC_Led.h>


/** @addtogroup FT68xx
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
u8  gUserLEDCunter;           //LED闪烁次数  FAULT
u16 gUserLEDmsCunter;        //LED闪烁次数
u16 gUserNoOperationDelaynms; //No operation time delay

/* Private define ------------------------------------------------------------*/
#define LEDFAULTDIS_EN     (1)     //故障指示
#define LEDONOFFDIS_EN     (0)     //ONOFF指示
#define LEDCWCCWDIS_EN     (1)     //正反转显示
#define LEDCELLDIS_EN      (0)     //电量显示
#define LEDSHIFTDIS_EN     (0)     //档位显示

/* Private macro -------------------------------------------------------------*/

void LED_show(void);
void Fault_Led(Error_TypeDef value); 
/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : Fault_Led   P15 P17 P20
* Description    : 
* Input          : Ctl.SysError故障代码
* Output         : None
* Return         : None
*******************************************************************************/
void Fault_Led(Error_TypeDef value)
{
  #if(LEDFAULTDIS_EN)
  u16 temp = 500;
  if(value != 0)   
  {
    if(gUserLEDmsCunter > temp)
    {
      if(gUserLEDCunter <= ((value<<1) - 1))  //一元操作符不加空格 3.6.6
      {
        if(gUserLEDmsCunter > (temp + 500))
        {
          gUserLEDmsCunter = temp;
          gUserLEDCunter++;
          FAULT_ONOFF;
        }       
      }
      else
      {
        #if(0)
        FAULT_ONOFF;                     //故障指示完成，输出固定信号
        gUserLEDmsCunter = 0;
        #else                              
        FAULT_OFF;
        if(gUserLEDmsCunter>temp+3000)  //故障指示完成，3秒后再次闪烁
        {
          gUserLEDmsCunter = 0;
          gUserLEDCunter = 0;
        } 
        #endif                   
      }             
    }
  }
  else
  {
    gUserLEDmsCunter = 0;
  }
  #endif
}

/*******************************************************************************
* Function Name  : LED_show
* Description    : 
* Input          : 
* Output         : None
* Return         : None
*******************************************************************************/
void LED_show(void)
{
  //照明指示
  #if(LAMP_EN) //扳手螺丝批，延时掉电程序
  if(Ctl.Tim.NoOperationDelaynms >= NOOPDELAYNMS)
  {
    Ctl.Tim.NoOperationDelaynms = NOOPDELAYNMS;
    Rom.ReadValue = Flash_GetAddress();     //读
    Flash_ErasePageRom();                  //擦
    Flash_KeyWriteValue((Rom.WriteValue));  //写
    PDELAY_OFF;
  } 
  else
  {
    PDELAY_ON;
  }
  #endif   
                          
  #if(LEDONOFFDIS_EN)
  if(Ctl.gStart == TRUE)
  {
    LED1_ON;
  }
  else
  {
    LED1_OFF;
  }
  #endif
//正反转显示
  
  #if(LEDCWCCWDIS_EN)
  if (Ctl.SysError == NONE)
  {
    if(Ui.flg.FR == CW)
    {
      LED2_OFF;
    }
    else if(Ui.flg.FR == CCW)
    {
      LED2_ON;
    }
  }
  #endif
//电量显示
  #if(LEDCELLDIS_EN)
  if (Ctl.SysError == NONE)
  {
    if(Drv.AdcMeas.Vdc > 18)
    {
      LED1_ON;
      LED2_ON;
      LED3_ON;
    }
    else if(Drv.AdcMeas.Vdc > 16.5)
    {
      LED1_ON;
      LED2_ON;
      LED3_OFF;
    }
    else if(Drv.AdcMeas.Vdc > 15)
    {
      LED1_ON;
      LED2_OFF;
      LED3_OFF;
    }
    else
    {
      LED1_OFF;
      LED2_OFF;
      LED3_OFF;
    }
  }
  #endif 
//档位显示

  #if(LEDSHIFTDIS_EN)
  switch(Ctl.spd.SpeedShift)
  {
    case 1:
    {
      LED1_ON;
      LED2_OFF;
      LED3_OFF;
    }
      break;
    case 2:
    {
      LED1_OFF;
      LED2_ON;
      LED3_OFF;
    }
      break;
    case 3:
    {
      LED1_OFF;
      LED2_OFF;
      LED3_ON;
    }      
      break;
    
    default:
      break;
  }
  #endif
}