
/************************ (C) COPYRIGHT 2015 FT *******************************
* File Name          : 
* Author             : Application Team  Tom.wang 
* Version            : V2.0.0
* Date               : 06/15/2015
* Description        : 用户接口层
********************************************************************************
/*******************************************************************************
* All Rights Reserved
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include <FU6812_MCU.h>
#include <sys_conf.h>
#include <fu681x_Device.h>
#include <interrupt.h>
#include <Ftech_BLDC.h>
#include <MC_Led.h>
#include <MC_key.h>
#include <MC_PwmIn.h>
#include <MC_Beep.h>
#include <MC_init.h>
#include <MC_UserInterface_layer.h>
#include <MC_MotorCotrol_layer.h>  
#include <MC_MotorFunction_layer.h> 
#include <MC_MotorDriver_layer.h>   

/** @addtogroup FT68xx
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
Ui_TypeDef xdata Ui;                  //变量定义
float xdata tSpeedShiftcoefficient;   //速度系数
s16 xdata tChipIDLEConter;            //低功耗
/* Private function prototypes -----------------------------------------------*/
void Ui_Function(void);            //全局函数  声明

/* Private functions ---------------------------------------------------------*/

/*******************************************************************************
* Function Name  : UI
* Description    : a、故障指示、温度计算、ADC转换、速度实际值计算 
                   b、正反转命令给定、速度命令给定
                   c、命令执行
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void Ui_Function(void)        //函数定义
{  
  u16 xdata trefTar;
//-----------------------------------------
//故障指示
  Fault_Led(Ctl.SysError);
  
//-----------------------------------------
//档位指示，电量显示
  LED_show();
  
//-----------------------------------------
//ADC实际值转换
  ADCGetConversionValue();

//-----------------------------------------  
//实际转速 rpm 
#if (SPEED_CALC_EN == 2)
  MDUFlag.CalcSpeedRpm = 1;    
#endif
//-----------------------------------------
//过载判断
#if(OVERLOAD_EN) 
  Ctl.OL.Value = MCL_OverLoadCalc(Drv.AdcMeas.ImeasBus);
#endif

//-----------------------------------------
//温度计算  
#if(OH1_EN)
  Drv.AdcMeas.Therm1 = Drv_PowerThermCalc(Drv.AdcMeas.Vth1);
#endif

#if(OH2_EN)
  Drv.AdcMeas.Therm2 = Drv_PowerThermCalc(Drv.AdcMeas.Vth2); //温度采样
#endif

#if(ESC_PPMSREFEN)
  MDUFlag.TargetSpeed = 1;  
  if(Ctl.State >= MOTOR_STOP)
  {
    ESC_PPMSwitch();
  }
  BeepResponse();
#endif
//=====================================================
//开关机 命令  速度给定 正反转给定
//-----------------------------------------------------
//ON/OFF 测试模式
#if(0)
  if((Ctl.Tim.OnOffnms < 3000))   
  {
    Ui.flg.START = FALSE;
    
    Ctl.SysError = NONE;  //清除故障
    DRV_OE_ON;
    Ctl.State = MOTOR_STOP; 
  }
  else if (Ctl.Tim.OnOffnms < 6000)
  {
    if((Ctl.SysError == NONE)&&(Ui.flg.START == FALSE))
    {
      Ui.flg.START = TRUE;
    }
  }     
  else
  {
    Ctl.Tim.OnOffnms = 0; 
  }
  #endif
//=====================================================
//---------------------------------------------------
//正反转，档位命令给定
////Xn按键命令给定  -> 命令执行 轻触型开发
#if(0)
  if(X1.Kflg == TRUE)
  {
    X1.Kflg = FALSE;
    X1.KNum = FALSE;
    Ctl.Spd.SpeedShift++;

   if(Ctl.Spd.SpeedShift > 3)
    {
      Ctl.Spd.SpeedShift = 1;
    }
    Rom.WriteValue = (u8)Ctl.Spd.SpeedShift;//掉电档位存储
  }

  if(Ctl.Spd.SpeedShift == 1)
  {
    tSpeedShiftcoefficient = 0.8;
  }
  else if(Ctl.Spd.SpeedShift == 2)
  {
   tSpeedShiftcoefficient = 0.9;
  }
  else if(Ctl.Spd.SpeedShift == 3)
  {
    tSpeedShiftcoefficient = 1.0;
  }
#endif
//Xn端子命令给定  -> 命令执行   常开常闭型
#if(0)
  if(X1.XCount == KEY_FILTERCOUNT)
  { 
    Ui.flg.FR = CCW;
  }
  else if(X1.XCount == -KEY_FILTERCOUNT)
  {
    Ui.flg.FR = CW;
  }
#endif

//Xn按键命令给定  -> 命令执行 轻触型开发
#if(0)
  if(X0.Kflg == TRUE)
  {
    X0.Kflg = FALSE;
    X0.KNum = FALSE;
     
    if(Ui.flg.FR == CW)
    {
      Ui.flg.FR = CCW;
//      Ctl.gDirectionC = Ui.flg.FR;    //方向控制
//      SetBit(DRV_CR, DDIR); 

    }
    else if(Ui.flg.FR == CCW)
    {
      Ui.flg.FR = CW;
//      Ctl.gDirectionC = Ui.flg.FR;    //方向控制
//      ClrBit(DRV_CR, DDIR);
    }
  }

  if(X3.Kflg == TRUE)
  {
    X3.Kflg = FALSE;
    X3.KNum = FALSE;
     
//    if( Ctl.Is.refCur == 2000)
//    {
//      Ctl.Is.refCur = 10000;  
//      Ctl.Is.refTar = 10000;
//    }
//    else
//    {
//      Ctl.Is.refCur = 2000;  
//      Ctl.Is.refTar = 2000;    
//    }
  }  
#endif

  //Drv.AdcMeas.Sref = _IQ(1.0);  
//---------------------------------------------------
//启动命令给定 ADC启动、按键启动、端子启动
//Xn按键命令给定  -> 命令执行 轻触型开发
#if (KEYONOFF_EN == 1) 
  if(X2.Kflg == TRUE)
  {
    X2.Kflg = FALSE;
    X2.KNum = FALSE;
    
    if(Ui.flg.START == TRUE)
    {
      Ui.flg.START = FALSE;
      if((Ctl.SysError != NONE)&&(Ctl.SysError !=E_ERR9))
      {  
        Ctl.SysError = NONE;  //清除故障
        Ctl.State = MOTOR_STOP; 
        DRV_OE_ON;
      }      
    }
    else
    {
      Ui.flg.START = TRUE;
    }      
  }
#elif (KEYONOFF_EN == 2)
  if(X0.XCount == KEY_FILTERCOUNT)
  { 
    Ui.flg.START = TRUE;
    tChipIDLEConter = 0;
  }
  else if(X0.XCount == -KEY_FILTERCOUNT)
  {
    Ui.flg.START = FALSE;
//    if(tChipIDLEConter >= 5000)        //5秒后进入低功耗模式 芯片睡眠电流1mA 不睡眠9mA
//    {                                  //外部中断唤醒睡眠模式 需要打开外部中断
//      tChipIDLEConter = 5000;
//      SetBit(PCON, STOP);           //芯片休眠
//    }
  }
#elif(ADCONOFF_EN)
  //电位计开关
  //Drv.AdcMeas.Sref = _IQ(0.8);
  if(Drv.AdcMeas.Sref > SREF_IN_ONVALUE)
  {
    Ctl.Spd.SwitchCount++;
  }
  //else if((Drv.AdcMeas.Sref < _IQ(0.02))&&(Drv.PWM.DutyCur < _IQ(0.02)))
  else if(Drv.AdcMeas.Sref < SREF_IN_OFFVALUE)
  {
    Ctl.Spd.SwitchCount--;
  }
  if(Ctl.Spd.SwitchCount > SREFCOUNT)
  {
    Ctl.Spd.SwitchCount = SREFCOUNT;
    Ui.flg.START = TRUE;
  }   
  else if(Ctl.Spd.SwitchCount < -SREFCOUNT) 
  {
    Ctl.Spd.SwitchCount = -SREFCOUNT;
    Ui.flg.START = FALSE;
    Ctl.Spd.MechCircleNum = 0;
    if((Ctl.SysError != NONE)&&(Ctl.SysError !=E_ERR9))
    {  
     Ctl.SysError = NONE;  //清除故障
     Ctl.State = MOTOR_STOP; 
      DRV_OE_ON;
    }
  }  
#endif

//============================================================
//速度给定
#if(ADCSREF_EN) //ADC 调速
  //ADC->>MDUControl.SpeedSref->>MDUControl.TargetSpeed->>Ctl.Spd.refTar
  
  MDUFlag.TargetSpeed = 1;  //速度给定计算请求 
  //MDUControl.SpeedSref = Drv.AdcMeas.Sref*tSpeedShiftcoefficient;
  MDUControl.SpeedSref = Drv.AdcMeas.Sref;
  trefTar = MDUControl.TargetSpeed;
  
  #if(SPEED_CLOSE_EN) //速度闭环限速
    if(trefTar > SPEED_INMAX)
    {
      Ctl.Spd.refTar = trefTar;
    }
  #endif
  
   Ctl.Spd.refTar = (Ui.flg.FR == CCW)?  -trefTar: trefTar;

#endif
//=====================================================
//无HALL 输出限幅 比较器触发模式
#if (POS_FB_MODE == SensorLess) 

  #if (SPEED_CLOSE_EN)
    if(Ctl.Spd.refTar < Ctl.Ramp.DutyEnd)
    {
      Ctl.Spd.refTar = Ctl.Ramp.DutyEnd ;
    }  
  #endif  

  if(Ctl.State == MOTOR_NORMAL)
  {
    Drv.PWM.DutyTar = Ctl.Spd.refTar;
    //高速关闭位置捕获中断 TOM++
    if(TIM1__BCCR < STB6C)
    {
      ClrBit(TIM1_IER, T1PDIE);       
    }
    else if(TIM1__BCCR > STB5C)
    {                                                                    
      SetBit(TIM1_IER, T1PDIE);      
    }    
  }
#else
  Drv.PWM.DutyTar = Ctl.Spd.refTar;
#endif
  
//-----------------------------------------
//命令执行
  if((Ui.flg.START == TRUE)&&(Ui.flg.FR != NONE))  
  {
    Ctl.gStartC = TRUE;
    gUserNoOperationDelaynms = 0;
  } 
  else if(Ui.flg.START == FALSE)
  {                   
    if(Ctl.gStopmodeC == FREE_DOWN)
    {
      Ctl.gStartC = FALSE;
    }
    else if(Ctl.gStopmodeC == SLOWING_DOWN)
    {
      Ctl.Spd.refTar = 0;
      if(Ctl.State == MOTOR_NORMAL)
      {
        #if(SPEED_CLOSE_EN)
//        if(((Ctl.Spd.refCur<_IQ(0.05))))
//        {
//          Ctl.gStartC = FALSE;
//        }      
//        #else
//        if((Drv.PWM.DutyCur < _IQ(0.05)))
//        {
//          Ctl.gStartC = FALSE;
//        }      
        #endif
      }
      else
      {
        Ctl.gStartC = FALSE;
      }
    } 
    else if(Ctl.gStopmodeC == BREAK_DOWN)
    {
      Ctl.gStartC = FALSE;
    }      
  }
}
