#define GLOBALS_VARIABLE
#include "../App_Headers/includes.h"

//20230314 16C23->16C22变更
//20230323 去掉IGNON初始化关转向灯指令，避免IGNOFF时点亮转向灯，IGNON时闪一下问题
//20230324 修改E01/F01前面多显示8问题
//20230325 修改驾驶员相关报警逻辑（禅道问题）
//20230403 修改车厂清零，10km以内清后下电上电恢复问题
//20231115 变更：增加远程报文清零功能（接收清零报文0x5F2，发送清零状态报文0x49E）
//20231116 修改IGNON、IGNOFF门槛值，由16.6V改为6.5V
//20231208 修改清零外发不是30帧和清零后里程继续累加及需发12帧清零指令才清零禅道问题

INT8U Read_OUT_2000km_Clear(void)
{
    INT8U temp[16];
    
    bsp_IIC_ReadPage(0xa2, 256, (INT8U *)&temp, Register_Len);
    
    return temp[0];
}  

void main(void) 
{ 
  Api_Sys_Init();
  Api_InitParamer();
  PWME_PWME0 = Disable;  
  RevMeter.MakeZero = Disable;
  VehMeter.MakeZero = Disable;
  Beep[3].Prio  = DisAlr; 
  Ht16c23_Init(ON);
  GUI_LCD_Clear();
  TIM0_TIE_C2I = Enable;  
  API_SetTimer9(0,(ClockType*)&Clock);
  API_SetTimer0(T_100ms,(ClockType*)&Clock);
  RevFlag=0;
  for(;;) 
  {       
    _FEED_COP();                                  
    API_ClockGenerate();       
    Api_GetSensorAD();  
    if (Curkeyinput == KeyON)
    {                                 
      switch(RunStep)
      {
        case SysInit_Proc:  
          Api_Sys_ON();                                                
          Api_InitParamer();                  
          OBD_LED_P=0;//lzs--jia
          Ht16c23_Init(ON);                  
          App_ReadAccKm();
          OUT_2000km_Clear = Read_OUT_2000km_Clear();                      
          CarFlag.FlagBits.SysOffEn = Enable;   
          GUI_Motor_Start();                               
          RunStep = Out_Proc;
          break;                  
        case Out_Proc:                                              
          RunStep = CAN_Proc;
          break;
        case CAN_Proc:                   
          App_GetNormalData(); 
          App_SendCANMsg();                 
          RunStep = SCI_Proc;
          break;
        case SCI_Proc:
          App_SCI_Proc();                  
          RunStep = GUI_DispProc;
          break;
        case GUI_DispProc: 
          //if(wVcc > 420) 
          { 
            GUI_LedNormalDisplay();   
          }
          GUI_LCD_Display();                                                                           
          RunStep = CalAccKm_Proc;
          break;
        case CalAccKm_Proc:
          if(ReadState == RunErr)
          {
            App_ReadAccKm();
        	}	
				/*	if(CanClk.Signal.ExistFEBF0B)
					{
						App_CalAccKmCan(); 
					}
					else */
			//		{ 								
					App_CalAccKm();								
			//		}	        					              
          App_CalFre((FreType *)&VehFre,(FreType *)&RevFre);                
          RunStep = CalMeterProc;  
          break;
        case CalMeterProc:
          App_CalBorad();
          RunStep = Out_Proc;  
          break; 
        default:
          break;           
      }
    }
    GUI_CheckKeyProc((KeyType*)&G_Button);
    GUI_BeepProc((BeepType*)Beep);        
    GUI_Light_Out();
    GUI_LedRandomDisplay();
    App_CheckCanDrop() ;
    App_ChcekSleep(); 
    
    if (Curkeyinput == KeyOFF)
    { 
      T_BOX_time=0;
      RunStep = SysInit_Proc;
      App_State_Off();
      if(CarFlag.FlagBits.AccSaveState)
      {
        CarFlag.FlagBits.AccSaveState = Disable;
        App_WriteAccReg();
      }         
      if(CarFlag.FlagBits.SysOffEn)
      {
        CarFlag.FlagBits.SysOffEn = Disable;
        GUI_LCD_Clear();
        GUI_Motor_Reset(); 
      }    
      if(bcanbussleep==1)
      {            
        Api_Sys_Off();
        PWME_PWME1 = Disable; 
        PTS_PTS5 = LOW; 
        TJA1040_STB=HIGH;
        SDADIR = 1;
        SDA = 1;
        SCL = 1;
        pBeep = &NopBeep;
        _asm    
        {
          ANDCC #0x7f;
          STOP;
        }  
      }
       
    }    
  }                      
} 

