#define  GLOBALS_APP_CAN
#include "../app_Headers/g_includes.h"


//1000  //2000  //3000  //4000 //5000   //6000  //7000 //7600 //8000
INT16U    EngSpd[10] ={0X0000,0x1F40, 0x3E80, 0x5DC0, 0x7D00, 0x9D40, 0xBB80, 0xDAC0,0XFA00 };
INT16U    EngSpeed[11] = {0x0163, 0x02C7, 0x042A, 0x058E, 0x06F1, 0x0855, 0x09B8,0X0B1C,0X0C7E,0X0DE2,0X0F45 };

/*-------------------------------------------------------------------------
* Function Name  : app_Can_handle
* Description    :
* Input          : None
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void app_Can_handle(void)
{
    app_Can_handle_PA5();
    
  //  G_Button.LED_STEP = 1;
    
    //================================================
    //
    if(api_TimerOver1((_Clock*)&g_Clock)) //50ms
    {
        api_SetTimer1(TT_50ms,(_Clock*)&g_Clock); 
       
        TxDelayTime = 10;
        
        if(G_Button.LED_STEP == 1)
        {
          WheelSpeedN = WheelSpeedN + glztest;//20kmh
          if(WheelSpeedN >= 64255)
          {
              WheelSpeedN = WheelSpeedN - 64255;
          }
          g_Msg08FFF00B.data[4].bbyte = WheelSpeedN % 256;
          g_Msg08FFF00B.data[5].bbyte = WheelSpeedN / 256;
          
        }
          CS_OUT_N =CS_FRset[G_Button.LED_STEP]; 
          ZS_OUT_N=ZS_FRset[G_Button.LED_STEP];	
        
        if(G_Button.LED_STEP == 2)
        {
          WheelSpeedN = WheelSpeedN + 32;//40kmh
          if(WheelSpeedN >= 64255)
          {
              WheelSpeedN = WheelSpeedN - 64255;
          }
          g_Msg08FFF00B.data[4].bbyte = WheelSpeedN % 256;
          g_Msg08FFF00B.data[5].bbyte = WheelSpeedN / 256;
        }
        
        if(G_Button.LED_STEP == 3)
        {
          WheelSpeedN = WheelSpeedN + 48;//60kmh
          if(WheelSpeedN >= 64255)
          {
              WheelSpeedN = WheelSpeedN - 64255;
          }
          g_Msg08FFF00B.data[4].bbyte = WheelSpeedN % 256;
          g_Msg08FFF00B.data[5].bbyte = WheelSpeedN / 256;
        }                
        if(G_Button.LED_STEP == 4)
        {
          WheelSpeedN = WheelSpeedN + 64;//80kmh
          if(WheelSpeedN >= 64255)
          {
              WheelSpeedN = WheelSpeedN - 64255;
          }
          g_Msg08FFF00B.data[4].bbyte = WheelSpeedN % 256;
          g_Msg08FFF00B.data[5].bbyte = WheelSpeedN / 256;
        }
        if(G_Button.LED_STEP == 5)
        {
          WheelSpeedN = WheelSpeedN + 80;//100kmh
          if(WheelSpeedN >= 64255)
          {
              WheelSpeedN = WheelSpeedN - 64255;
          }
          g_Msg08FFF00B.data[4].bbyte = WheelSpeedN % 256;
          g_Msg08FFF00B.data[5].bbyte = WheelSpeedN / 256;
        }        
        if(G_Button.LED_STEP == 6)
        {
          WheelSpeedN = WheelSpeedN + 96;//120kmh
          if(WheelSpeedN >= 64255)
          {
              WheelSpeedN = WheelSpeedN - 64255;
          }
          g_Msg08FFF00B.data[4].bbyte = WheelSpeedN % 256;
          g_Msg08FFF00B.data[5].bbyte = WheelSpeedN / 256;
        }        
        if(G_Button.LED_STEP == 7)
        {
          WheelSpeedN = WheelSpeedN + 112;//140kmh
          if(WheelSpeedN >= 64255)
          {
              WheelSpeedN = WheelSpeedN - 64255;
          }
          g_Msg08FFF00B.data[4].bbyte = WheelSpeedN % 256;
          g_Msg08FFF00B.data[5].bbyte = WheelSpeedN / 256;
        }
        if(G_Button.LED_STEP == 8)
        {
          WheelSpeedN = WheelSpeedN + 128;//160kmh
          if(WheelSpeedN >= 64255)
          {
              WheelSpeedN = WheelSpeedN - 64255;
          }
          g_Msg08FFF00B.data[4].bbyte = WheelSpeedN % 256;
          g_Msg08FFF00B.data[5].bbyte = WheelSpeedN / 256;
        }        
        if(G_Button.LED_STEP == 9)
        {
          WheelSpeedN = WheelSpeedN + 480;//180kmh
          if(WheelSpeedN >= 64255)
          {
              WheelSpeedN = WheelSpeedN - 64255;
          }
          g_Msg08FFF00B.data[4].bbyte = WheelSpeedN % 256;
          g_Msg08FFF00B.data[5].bbyte = WheelSpeedN / 256;
        } 
         if(G_Button.LED_STEP == 10)
        {
          WheelSpeedN = WheelSpeedN + 480;//180kmh
          if(WheelSpeedN >= 64255)
          {
              WheelSpeedN = WheelSpeedN - 64255;
          }
          g_Msg08FFF00B.data[4].bbyte = WheelSpeedN % 256;
          g_Msg08FFF00B.data[5].bbyte = WheelSpeedN / 256;
        }                                     
    } 
    
    {
     
      if(api_TimerOver0((_Clock*)&g_Clock))
      {
          api_SetTimer0(250,(_Clock*)&g_Clock); //1s
          // g_bReturn = bsp_CANSendFrame(g_Msg1B5.id,g_Msg1B5.prio,g_Msg1B5.dlc,g_Msg1B5.data);  
        //   g_bReturn = bsp_CANSendFrame(g_Msg402.id,g_Msg402.prio,g_Msg402.dlc,g_Msg402.data);  
        //   g_bReturn = bsp_CANSendFrame(g_Msg101.id,g_Msg101.prio,g_Msg101.dlc,g_Msg101.data);
        //   g_bReturn = bsp_CANSendFrame(g_Msg321.id,g_Msg321.prio,g_Msg321.dlc,g_Msg321.data);
        //   g_bReturn = bsp_CANSendFrame(g_Msg322.id,g_Msg322.prio,g_Msg322.dlc,g_Msg322.data);
        //   g_bReturn = bsp_CANSendFrame(g_Msg323.id,g_Msg323.prio,g_Msg323.dlc,g_Msg323.data);
        //   g_bReturn = bsp_CANSendFrame(g_Msg141.id,g_Msg141.prio,g_Msg141.dlc,g_Msg141.data);
        
        if(screentest.beginflag == 0)
        {
          if(screentest.setstep < 4)
          {
            screentest.setstep++;
          }
        }

        g_bReturn = bsp_CANSendFrame(g_Msg772.id,g_Msg772.prio,g_Msg772.dlc,g_Msg772.data);  
      }
     if(api_TimerOver3((_Clock*)&g_Clock)) //70ms
      {
          api_SetTimer3(63,(_Clock*)&g_Clock); 
          if(G_Button.LED_STEP != 0)
          {
        //   g_bReturn = bsp_CANSendFrame(g_Msg7E8.id,g_Msg7E8.prio,g_Msg7E8.dlc,g_Msg7E8.data);
        //   g_bReturn = bsp_CANSendFrame(g_Msg7E82.id,g_Msg7E82.prio,g_Msg7E82.dlc,g_Msg7E82.data);
          }      
      }
     if(api_TimerOver6((_Clock*)&g_Clock))
      {
          api_SetTimer6(50,(_Clock*)&g_Clock); //100ms
        //   g_bReturn = bsp_CANSendExtFrame(g_Msg0A31FFE8.id,g_Msg0A31FFE8.prio,g_Msg0A31FFE8.dlc,g_Msg0A31FFE8.data);      
      } 
      
      if(api_TimerOver5((_Clock*)&g_Clock)) //
      {
          api_SetTimer5(5,(_Clock*)&g_Clock); //10ms
        //   g_bReturn = bsp_CANSendExtFrame(g_Msg0A30FFE8.id,g_Msg0A30FFE8.prio,g_Msg0A30FFE8.dlc,g_Msg0A30FFE8.data);
      }
    } 
    
}
/*-------------------------------------------------------------------------
* Function Name  : app_Can_handle
* Description    :
* Input          : None
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void app_Can_IdInit(void)
{
    api_SetTimer0(1,(_Clock*)&g_Clock);
    api_SetTimer1(1,(_Clock*)&g_Clock);
    api_SetTimer2(1,(_Clock*)&g_Clock);
    api_SetTimer3(1,(_Clock*)&g_Clock);
    api_SetTimer4(1,(_Clock*)&g_Clock);
    api_SetTimer5(1,(_Clock*)&g_Clock);
    api_SetTimer6(1,(_Clock*)&g_Clock);
    api_SetTimer7(1,(_Clock*)&g_Clock);
  
    g_Msg772.id = 0X772;
    g_Msg772.prio = 0;
    g_Msg772.dlc = 8;

}



void app_Can_handle_PA5(void) {
    
    
#if PA5_EN


    if(screentest.beginflag == 0)
    {
      if(screentest.setstep == 1)  //发送4条消息进入EOL
        {
          g_Msg772.data[0].bbyte = 0x01;
          g_Msg772.data[1].bbyte = 0x01;
          g_Msg772.data[2].bbyte = 0x12;
          g_Msg772.data[3].bbyte = 0x34;
          g_Msg772.data[4].bbyte = 0x56;
          g_Msg772.data[5].bbyte = 0x78;
          g_Msg772.data[6].bbyte = 0x3E;
          g_Msg772.data[7].bbyte = 0x7A;
        }
        else if(screentest.setstep == 2)
        {
          g_Msg772.data[0].bbyte = 0x07;
          g_Msg772.data[1].bbyte = 0x35;
          g_Msg772.data[2].bbyte = 0xF1;
          g_Msg772.data[3].bbyte = 0xB7;
          g_Msg772.data[4].bbyte = 0x80;
          g_Msg772.data[5].bbyte = 0x00;
          g_Msg772.data[6].bbyte = 0x00;
          g_Msg772.data[7].bbyte = 0x00;
        }
        else if(screentest.setstep == 3)
        {
          g_Msg772.data[0].bbyte = 0x01;
          g_Msg772.data[1].bbyte = 0x02;
          g_Msg772.data[2].bbyte = 0x6D;
          g_Msg772.data[3].bbyte = 0xE8;
          g_Msg772.data[4].bbyte = 0xBB;
          g_Msg772.data[5].bbyte = 0x30;
          g_Msg772.data[6].bbyte = 0x5B;
          g_Msg772.data[7].bbyte = 0xB5;
        }
        else if(screentest.setstep == 4)
        {
          g_Msg772.data[0].bbyte = 0x07;
          g_Msg772.data[1].bbyte = 0xE9;
          g_Msg772.data[2].bbyte = 0x1C;
          g_Msg772.data[3].bbyte = 0xA5;
          g_Msg772.data[4].bbyte = 0xA9;
          g_Msg772.data[5].bbyte = 0x00;
          g_Msg772.data[6].bbyte = 0x00;
          g_Msg772.data[7].bbyte = 0x00;

          screentest.beginflag = 1;
        }

        return ;
    }

    if(G_Button.LED_STEP == 0)
    {
        g_Msg772.data[0].bbyte = 0x00;
        g_Msg772.data[1].bbyte = 0x10;
        g_Msg772.data[2].bbyte = 0xFF;    //红色
        g_Msg772.data[3].bbyte = 0x00;
        g_Msg772.data[4].bbyte = 0x00;
        g_Msg772.data[5].bbyte = 0x00;
        g_Msg772.data[6].bbyte = 0x01;
        g_Msg772.data[7].bbyte = 0x00;
    }
    if(G_Button.LED_STEP == 1)
    {
        g_Msg772.data[0].bbyte = 0x00;
        g_Msg772.data[1].bbyte = 0x10;
        g_Msg772.data[2].bbyte = 0x00;
        g_Msg772.data[3].bbyte = 0xFF;    //绿色
        g_Msg772.data[4].bbyte = 0x00;
        g_Msg772.data[5].bbyte = 0x00;
        g_Msg772.data[6].bbyte = 0x01;
        g_Msg772.data[7].bbyte = 0x00;
    }
    if(G_Button.LED_STEP == 2)
    {
        g_Msg772.data[0].bbyte = 0x00;
        g_Msg772.data[1].bbyte = 0x10;
        g_Msg772.data[2].bbyte = 0x00;
        g_Msg772.data[3].bbyte = 0x00;
        g_Msg772.data[4].bbyte = 0xFF;    //蓝色
        g_Msg772.data[5].bbyte = 0x00;
        g_Msg772.data[6].bbyte = 0x01;
        g_Msg772.data[7].bbyte = 0x00;
    }
    if(G_Button.LED_STEP == 3) 
    {
        g_Msg1B5.data[3].bbyte = 0x3C;
        g_Msg1B5.data[4].bbyte = 0x3C;
        g_Msg1B5.data[5].bbyte = 0x3C;
        g_Msg1B5.data[6].bbyte = 0x00;
        g_Msg1B5.data[7].bbyte = 0x28;      //ECO指示灯，READY指示灯,R档
    }
    if(G_Button.LED_STEP == 4)
    {
        g_Msg1B5.data[3].bbyte = 0x50;
        g_Msg1B5.data[4].bbyte = 0x50;
        g_Msg1B5.data[5].bbyte = 0x50;
        g_Msg1B5.data[6].bbyte = 0x06;      //OBD指示灯
        g_Msg1B5.data[7].bbyte = 0x44;      //保养指示灯，D档
    }
    if(G_Button.LED_STEP == 5)
    {
        g_Msg1B5.data[3].bbyte = 0x64;
        g_Msg1B5.data[4].bbyte = 0x64;
        g_Msg1B5.data[5].bbyte = 0x64;
        g_Msg1B5.data[6].bbyte = 0x4C;      //锁定状态
        g_Msg1B5.data[7].bbyte = 0xB8;      //N档，保养指示灯，驻车状态，故障指示灯
        // g_Msg1B5.data[6].bbyte = 0xFF;
        // g_Msg1B5.data[7].bbyte = 0xFF;
    }
    if(G_Button.LED_STEP == 6)
    {

    }
    if(G_Button.LED_STEP == 7)
    {

    }
    if(G_Button.LED_STEP >= 8)
    {
g_Msg12B.data[2].bbyte=0x0D;
        g_Msg12B.data[3].bbyte=0xA0;

        g_Msg240.data[4].bbyte=0;
        g_Msg240.data[7].bbyte=0;
        g_Msg240.data[7].bbyte=240;
   
        g_Msg125.data[2].bbyte=0x0;
        g_Msg125.data[3].bbyte=0x1;
        g_Msg125.data[4].bbyte=0x0;

        g_Msg101.data[0].bbyte=0xBB;
        g_Msg101.data[1].bbyte=0x80;

        g_Msg7E8.data[0].bbyte=0x04;
        g_Msg7E8.data[1].bbyte=0x62;
        g_Msg7E8.data[2].bbyte=0x00;
        g_Msg7E8.data[3].bbyte=0x05;
        g_Msg7E8.data[4].bbyte=0xA0;

        g_Msg0A31FFE8.data[0].bbyte=0xCA;
        g_Msg0A31FFE8.data[1].bbyte=0x05;

        g_Msg141.data[0].bbyte = 0x6E;
        g_Msg141.data[1].bbyte = 0xA2;

        g_Msg402.data[4].bbyte=0x04;        //TCS指示灯开
        g_Msg322.data[1].bbyte = 0x06;      //变速箱指示灯，变速箱故障指示灯开
        g_Msg323.data[0].bbyte=0x0F;        //故障码指示灯
        g_Msg323.data[1].bbyte=0xA1;
        g_Msg321.data[4].bbyte=0x02;        //S档开
    }
    if(G_Button.LED_STEP >= 9)
    {
g_Msg12B.data[2].bbyte=0x0D;
        g_Msg12B.data[3].bbyte=0xA0;

        g_Msg240.data[4].bbyte=0;
        g_Msg240.data[7].bbyte=240;
   
         g_Msg125.data[2].bbyte=0x08;
        g_Msg125.data[3].bbyte=0x0;
        g_Msg125.data[4].bbyte=0x0;

        g_Msg101.data[4].bbyte=0x0F;
        g_Msg101.data[5].bbyte=0x80;

        g_Msg7E8.data[0].bbyte=0x04;
        g_Msg7E8.data[1].bbyte=0x62;
        g_Msg7E8.data[2].bbyte=0x00;
        g_Msg7E8.data[3].bbyte=0x05;
        g_Msg7E8.data[4].bbyte=0xA0;

        g_Msg0A31FFE8.data[0].bbyte=0x80;
        g_Msg0A31FFE8.data[1].bbyte=0x06;

    }
    if(G_Button.LED_STEP >= 10)
    {
g_Msg12B.data[2].bbyte=0x0E;
        g_Msg12B.data[3].bbyte=0xA0;

        g_Msg240.data[4].bbyte=0x08;
        g_Msg240.data[7].bbyte=240;
   
        g_Msg125.data[2].bbyte=0x0;
        g_Msg125.data[3].bbyte=0x0;
        g_Msg125.data[4].bbyte=0x0;

        g_Msg402.data[7].bbyte=0x80;
        g_Msg101.data[4].bbyte=0x0F;
        g_Msg101.data[5].bbyte=0x80;

        g_Msg125.data[3].bbyte=0x0;
        g_Msg125.data[4].bbyte=0x0;
        g_Msg7E8.data[0].bbyte=0x04;
        g_Msg7E8.data[1].bbyte=0x62;
        g_Msg7E8.data[2].bbyte=0x00;
        g_Msg7E8.data[3].bbyte=0x05;
        g_Msg7E8.data[4].bbyte=0xA0;

        g_Msg7E82.data[0].bbyte=0x06;
        g_Msg7E82.data[1].bbyte=0x41;
        g_Msg7E82.data[2].bbyte=0x01;
        g_Msg7E82.data[3].bbyte=0x80;

        g_Msg0A31FFE8.data[0].bbyte=0x5B;
        g_Msg0A31FFE8.data[1].bbyte=0x07;
        g_Msg0A30FFE8.data[0].bbyte=0x09;

    }
    if(G_Button.LED_STEP >= 11)
    {
//       g_Msg240.data[7].bbyte=0xE4;
//       g_Msg240.data[4].bbyte=0;
//      // g_Msg402.data[7].bbyte=0;
//       g_Msg101.data[0].bbyte=0x9C;
//_Msg101.data[1].bbyte=0x40;
//_Msg101.data[2].bbyte=0xC7;
//_Msg101.data[4].bbyte=0x0F;
//_Msg101.data[5].bbyte=0xBE;
//_Msg402.data[7].bbyte=0x80;
//   g_Msg084.data[3].bbyte=0x01;    
//       
//       g_Msg12B.data[0].bbyte=0; 
//       g_Msg12B.data[1].bbyte=0xC0;
//       g_Msg125.data[3].bbyte=0x01;
//       g_Msg125.data[2].bbyte=0x00;
//       g_Msg125.data[4].bbyte=0x01;
G_Button.LED_STEP=0;
    }
//    if(G_Button.LED_STEP >= 12)
//    {
//        g_Msg240.data[7].bbyte=0xE4;
//        g_Msg240.data[4].bbyte=0;
//        g_Msg402.data[7].bbyte=0;
//        g_Msg101.data[0].bbyte=0;
//g_Msg101.data[1].bbyte=0;
//g_Msg101.data[2].bbyte=0;
//g_Msg101.data[4].bbyte=0;
//g_Msg101.data[5].bbyte=0;
//        
//        
//        g_Msg12B.data[0].bbyte=0; 
//        g_Msg12B.data[1].bbyte=0xC4;
//        g_Msg125.data[3].bbyte=0x02;
//        g_Msg125.data[2].bbyte=0x00;
//        g_Msg125.data[4].bbyte=0x00;
//    }
//    if(G_Button.LED_STEP >= 13)
//    {
//        g_Msg240.data[7].bbyte=0xE4;
//        g_Msg240.data[4].bbyte=0;
//        g_Msg402.data[7].bbyte=0;
//        g_Msg101.data[2].bbyte=0x82;//����
//        g_Msg101.data[4].bbyte=0x0F;//ˮ��
//        g_Msg101.data[5].bbyte=0x5A; 
//        g_Msg12B.data[0].bbyte=0; 
//        g_Msg12B.data[1].bbyte=0xD0;
//    }
//    if(G_Button.LED_STEP >= 14)
//    {
//        g_Msg240.data[7].bbyte=0xE4;
//        g_Msg240.data[4].bbyte=0x08;
//        g_Msg402.data[7].bbyte=0;
//        g_Msg101.data[2].bbyte=0x94;//����
//        g_Msg101.data[4].bbyte=0x0F;//ˮ��
//        g_Msg101.data[5].bbyte=0x5A; 
//        g_Msg12B.data[0].bbyte=0; 
//        g_Msg12B.data[1].bbyte=0xD5;
//        
//    }
//    if(G_Button.LED_STEP >= 15)
//    {
//        g_Msg240.data[7].bbyte=0xE4;
//        g_Msg240.data[4].bbyte=0;
//        g_Msg402.data[7].bbyte=0;
//        g_Msg101.data[2].bbyte=0xA7;//����
//        g_Msg101.data[4].bbyte=0x0F;//ˮ��
//        g_Msg101.data[5].bbyte=0x5A; 
//        g_Msg12B.data[0].bbyte=0x01; 
//        g_Msg12B.data[1].bbyte=0x1C;
//        
//    }
//    if(G_Button.LED_STEP >= 16)
//    {
//        g_Msg240.data[7].bbyte=0xE4;
//        g_Msg240.data[4].bbyte=0;
//        g_Msg402.data[7].bbyte=0;
//        g_Msg101.data[2].bbyte=0xBE;//����
//        g_Msg101.data[4].bbyte=0x0F;//ˮ��
//        g_Msg101.data[5].bbyte=0x5A; 
//        g_Msg12B.data[0].bbyte=0x01; 
//        g_Msg12B.data[1].bbyte=0x40;
//    }
//    if(G_Button.LED_STEP >= 17)
//    {
//        g_Msg240.data[7].bbyte=0xE4;
//        g_Msg240.data[4].bbyte=0;
//        g_Msg402.data[7].bbyte=0;
//        g_Msg101.data[2].bbyte=0xBE;//����
//        g_Msg101.data[4].bbyte=0x0F;//ˮ��
//        g_Msg101.data[5].bbyte=0x5A; 
//        g_Msg12B.data[0].bbyte=0x01; 
//        g_Msg12B.data[1].bbyte=0x66;
//    }
//    if(G_Button.LED_STEP >= 18)
//    {
//        g_Msg240.data[7].bbyte=0xE4;
//        g_Msg240.data[4].bbyte=0x08;
//        g_Msg402.data[7].bbyte=0x80;
//        g_Msg101.data[2].bbyte=0xBE;//����
//        g_Msg101.data[4].bbyte=0x0F;//ˮ��
//        g_Msg101.data[5].bbyte=0x5A; 
//        g_Msg12B.data[0].bbyte=0x01; 
//        g_Msg12B.data[1].bbyte=0x66;
//        
//    }
#endif
}

