#define  GLOBALS_APP_CAN
#include "../App_Headers/includes.h"
#include "../Library/TimeCtrl.h"
#include "../Library/FT_VehSpeedCalc.h"

#define   LED_P_NUMBER       0     //���ص�����
#define   LED_N_NUMBER       0     //���ص�����
#define   CAN_MAGS_NUMBER    10    //can��������
#define   OIL_MUMBER         0     //ˮ�ͼ�����


INT8U led_P = 0,
      led_N = 0,
      can_num = 0,
      oil_num = 0;
      




void flash_1Hz_process(void)//500ms call         blink/flash
{
    Flash_1Hz = !(Flash_1Hz);
}

/*-------------------------------------------------------------------------
* Function Name  : App_Msg_Copy
* Description    : 
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void App_Msg_Copy(CAN_message * CanMessage,INT16U* data)
{
    CanMessage->Msg[0] = data[0];
    CanMessage->Msg[0] = data[1];
    CanMessage->Msg[0] = data[2];
    CanMessage->Msg[0] = data[3];
    CanMessage->Msg[0] = data[4];
    CanMessage->Msg[0] = data[5];
    CanMessage->Msg[0] = data[6];
    CanMessage->Msg[0] = data[7];
    //bReturn = Api_CANSendFrame(pMsg->id,0,pMsg->Len,pMsg->Data);
}

/*-------------------------------------------------------------------------
* Function Name  : Write_CAN_Msg
* Description    : 
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/
void Write_CAN_Msg(CAN_message * CanMessage,INT8U i,INT8U data)
{ 
    CanMessage->Msg[i] = data;  
}

void Clear_CAN_Msg(CAN_message * CanMessage)
{ 
    INT8U i = 0;
    
    for (i; i <= (CanMessage->Length - 1); i++) 
    {
      CanMessage->Msg[i]  = 0x00;
    }
    CanMessage->ID_EN = 1;
}

void Set_CAN_Msg(CAN_message * CanMessage)
{ 
    INT8U i = 0;
    
    for (i; i <= (CanMessage->Length - 1); i++) 
    {
      CanMessage->Msg[i]  = 0xFF;
    }
}

void Set_CAN_CRC(CAN_message * CanMessage){
    INT8U i;
    for(i = 1; i < CanMessage->Length; i++){
        CanMessage->Msg[0] ^= CanMessage->Msg[i];
    }
}

/*zh:
0CFF7902
0CFF7C02
18FFDC01
18FEDCD5
18FEDED5
*/
void Format_CAN_Msg() 
{
    CanMsg_521.ID       = 0x521;
    CanMsg_522.ID       = 0x522;
    CanMsg_260.ID       = 0x260;  //车速
    // CanMsg_101.ID       = 0x101;
    // CanMsg_067.ID       = 0x67;
    // CanMsg_600.ID       = 0x600;
    // CanMsg_611.ID       = 0x611;
    // CanMsg_610.ID       = 0x610;
    // CanMsg_200.ID       = 0x200;
    // CanMsg_481.ID       = 0x481;
    // CanMsg_020.ID       = 0x020;
    // CanMsg_202.ID       = 0x202;
    // CanMsg_280.ID       = 0x280;
    // CanMsg_042.ID       = 0x042;
    // CanMsg_480.ID       = 0x480;
    // CanMsg_021.ID       = 0x021;
    // CanMsg_0A33FFE3.ID       = 0x0A33FFE3;
    // CanMsg_0A10FFE0.ID       = 0x0A10FFE0;
    // CanMsg_0A10FFE1.ID       = 0x0A10FFE1;
    // CanMsg_0A20FFE2.ID       = 0x0A20FFE2;
    // CanMsg_0A30FFE8.ID       = 0x0A30FFE8;
    // CanMsg_0A31FFE3.ID       = 0x0A31FFE3;
    // CanMsg_0A30FFE3.ID       = 0x0A30FFE3;
    // CanMsg_0A32FFE4.ID       = 0x0A32FFE4;
    // CanMsg_0A37FFE3.ID       = 0x0A37FFE3;
    // CanMsg_0A18FFE0.ID       = 0x0A18FFE0;
    // CanMsg_0A40FFE2.ID       = 0x0A40FFE2;

    CanMsg_521.Length   = 8;
    CanMsg_522.Length   = 8;
    CanMsg_260.Length   = 8;
    // CanMsg_101.Length   = 8;
    // CanMsg_067.Length   = 8;
    // CanMsg_600.Length   = 8;
    // CanMsg_611.Length   = 8;
    // CanMsg_610.Length   = 8;
    // CanMsg_200.Length   = 8;
    // CanMsg_481.Length   = 8;
    // CanMsg_020.Length   = 8;
    // CanMsg_202.Length   = 8;
    // CanMsg_280.Length   = 8;
    // CanMsg_042.Length   = 8;
    // CanMsg_480.Length   = 8;
    // CanMsg_021.Length   = 8;
    // CanMsg_0A33FFE3.Length       = 8;
    // CanMsg_0A10FFE0.Length       = 8;
    // CanMsg_0A10FFE1.Length       = 8;
    // CanMsg_0A20FFE2.Length       = 8;
    // CanMsg_0A30FFE8.Length       = 8;
    // CanMsg_0A31FFE3.Length       = 8;
    // CanMsg_0A30FFE3.Length       = 8;
    // CanMsg_0A32FFE4.Length       = 8;
    // CanMsg_0A37FFE3.Length       = 8;
    // CanMsg_0A18FFE0.Length       = 8;
    // CanMsg_0A40FFE2.Length       = 8;


    CanMsg_521.ID_EN    = 1;
    CanMsg_522.ID_EN    = 1;
    CanMsg_260.ID_EN    = 1;
    // CanMsg_101.ID_EN    = 1;
    // CanMsg_067.ID_EN    = 1;
    // CanMsg_600.ID_EN    = 1;
    // CanMsg_611.ID_EN    = 1;
    // CanMsg_610.ID_EN    = 1;
    // CanMsg_200.ID_EN    = 1;
    // CanMsg_481.ID_EN    = 1;
    // CanMsg_020.ID_EN    = 1;
    // CanMsg_202.ID_EN    = 1;
    // CanMsg_280.ID_EN    = 1;
    // CanMsg_042.ID_EN    = 1;
    // CanMsg_480.ID_EN    = 1;
    // CanMsg_021.ID_EN    = 1;
    // CanMsg_0A33FFE3.ID_EN       = 1;
    // CanMsg_0A10FFE0.ID_EN       = 1;
    // CanMsg_0A10FFE1.ID_EN       = 1;
    // CanMsg_0A20FFE2.ID_EN       = 1;
    // CanMsg_0A30FFE8.ID_EN       = 1;
    // CanMsg_0A31FFE3.ID_EN       = 1;
    // CanMsg_0A30FFE3.ID_EN       = 1;
    // CanMsg_0A32FFE4.ID_EN       = 1;
    // CanMsg_0A37FFE3.ID_EN       = 1;
    // CanMsg_0A18FFE0.ID_EN       = 1;
    // CanMsg_0A40FFE2.ID_EN       = 1;
} 

void Disable_CAN_Tx(void)
{
    // CanMsg_101.ID_EN    = 0;
    // CanMsg_067.ID_EN    = 0;
    // CanMsg_600.ID_EN    = 0;
    // CanMsg_611.ID_EN    = 0;
    // CanMsg_610.ID_EN    = 0;
    // CanMsg_200.ID_EN    = 0;
    // CanMsg_481.ID_EN    = 0;
    // CanMsg_020.ID_EN    = 0;
    // CanMsg_202.ID_EN    = 0;
    // CanMsg_280.ID_EN    = 0;
    // CanMsg_042.ID_EN    = 0;
    // CanMsg_480.ID_EN    = 0;
    // CanMsg_021.ID_EN    = 0;
    // CanMsg_0A33FFE3.ID_EN       = 0;
}       

void Set_Pin(INT16U pin)
{

}
            

/*-------------------------------------------------------------------------
* Function Name  : App_Can_Proc
* Description    : 
* Input          : None
* Output         : None
* Return         : None
* onther         : 
--------------------------------------------------------------------------*/

void App_Can_Proc(void)
{
    unsigned char i;
    unsigned int gMsg;

    for(i = 0; i < CAN_List_Num; i++){
        Message_Value_Calculate(CAN_List[i]);
    }

    if(GetCtrlTime(CANMsgSend_10ms) >= 10)
    {
        ClearCtrlTime(CANMsgSend_10ms);           
        // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_0A30FFE8);
        // Clear_CAN_Msg((CAN_message *)&CanMsg_0A30FFE8);  
    }

    if(GetCtrlTime(CANMsgSend_20ms) >= 20){
        ClearCtrlTime(CANMsgSend_20ms);
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_101);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_101);      
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_067);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_067);
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_610);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_610);
    bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_522);
    Clear_CAN_Msg((CAN_message *)&CanMsg_522);
    }

    if(GetCtrlTime(CANMsgSend_50ms) >= 50){
        ClearCtrlTime(CANMsgSend_50ms);      
        // if(Key.CAN_Step != 0)
        // {
        //     bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_0A10FFE0);
        //     Clear_CAN_Msg((CAN_message *)&CanMsg_0A10FFE0); 
        //     bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_0A10FFE1);
        //     Clear_CAN_Msg((CAN_message *)&CanMsg_0A10FFE1); 
        // }
    }

    if(GetCtrlTime(CANMsgSend_100ms) >= 100){
        ClearCtrlTime(CANMsgSend_100ms);    
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_0A31FFE3);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_0A31FFE3);          
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_0A30FFE3);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_0A30FFE3); 
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_0A33FFE3);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_0A33FFE3);  
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_0A20FFE2);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_0A20FFE2);  
    // if(electricityclose == 0)
    // {
    //     bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_020);
    //     Clear_CAN_Msg((CAN_message *)&CanMsg_020);
    // } 
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_202);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_202); 
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_280);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_280); 
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_021);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_021);
    bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_521);
    Clear_CAN_Msg((CAN_message *)&CanMsg_521);
    bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_260);
    Clear_CAN_Msg((CAN_message *)&CanMsg_260);
    }

    if(GetCtrlTime(CANMsgSend_200ms) >= 200){
    ClearCtrlTime(CANMsgSend_200ms);
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_0A32FFE4);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_0A32FFE4);
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_0A37FFE3);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_0A37FFE3);
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_0A18FFE0);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_0A18FFE0);
    }

    if(GetCtrlTime(CANMsgSend_500ms) >= 500){
        flash_1Hz_process();
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_0A40FFE2);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_0A40FFE2);
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_481);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_481);  
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_480);
    // Clear_CAN_Msg((CAN_message *)&CanMsg_480);  
        ClearCtrlTime(CANMsgSend_500ms);    
    }

    if(GetCtrlTime(CANMsgSend_1s) >= 1000){

        ClearCtrlTime(CANMsgSend_1s);

    }
    
    if(GetCtrlTime(CANMsgSend_10s) >= 10000){

        ClearCtrlTime(CANMsgSend_10s);
       
    }

    //REV 0
    /*CanMessageID0CF80AF0.Msg[0] = 0x98;
    CanMessageID0CF80AF0.Msg[1] = 0x3A;
    
    CanMessageID18F816F3.Msg[3] |= 0x10; */

    

    //CurValueStr[ENUM_VSpeed         ].val = 0;
    // CurValueStr[ENUM_VSpeed                  ].val = 0;
    // CurValueStr[ECM_FaultIndicator           ].val = 0;
    // CurValueStr[ECM_StautsInvalid            ].val = 0;
    // CurValueStr[ECM_MotorOver                ].val = 0;
    // CurValueStr[ECM_ReadyIndicator           ].val = 0;
    // CurValueStr[ECM_ChargeStatus             ].val = 0;
    // CurValueStr[ECM_CruiseStatus             ].val = 0;
    // CurValueStr[ECM_GearPositionStatus       ].val = 0;
    // CurValueStr[PFC_Relag                    ].val = 0;
    // CurValueStr[LLC_UnderOutput              ].val = 0;
    // CurValueStr[LLC_OverOutput               ].val = 0;
    // CurValueStr[LLC_OutPut                   ].val = 0;
    // CurValueStr[PFC_LLC_Interal_Communication].val = 0;
    // CurValueStr[LLC_Temp                     ].val = 0;
    // CurValueStr[Battery_status               ].val = 0;
    // CurValueStr[Battery_Current              ].val = 0;
    // CurValueStr[Battery_SOC                  ].val = 0;
    // CurValueStr[Vspeed                       ].val = 0;
    // CurValueStr[hour                         ].val = 0;
    // CurValueStr[minute                       ].val = 0;
    // CurValueStr[electricity                  ].val = 0;
    // CurValueStr[fault                        ].val = 0;
    // CurValueStr[Energy_recovery              ].val = 0;
    // CurValueStr[Energy_recovery_level        ].val = 0;
    // CurValueStr[Side_brackets                ].val = 0;
    // CurValueStr[TCS_close                    ].val = 0;
    // CurValueStr[TCS_crotrul                  ].val = 0;
    // CurValueStr[TCS_mode                     ].val = 0;
    // CurValueStr[TCS_light                    ].val = 0;
    // CurValueStr[GPS                          ].val = 0;
    // CurValueStr[signal_4G                    ].val = 0;
    // CurValueStr[Battery_over                 ].val = 0;
    // CurValueStr[Motor_overheating            ].val = 0;
    // CurValueStr[Cruise_control               ].val = 0;
    // CurValueStr[Gear                         ].val = 0;
    // CurValueStr[Bluetooth_phone              ].val = 0;
    // CurValueStr[year                         ].val = 0;
    // CurValueStr[Vvspeed_H                         ].val = 0;
    // CurValueStr[Vvspeed_L                         ].val = 0;
    CurValueStr[Engine_water_temperature_display].val = 0;
    CurValueStr[EVpeed_H].val = 0;
    CurValueStr[EVpeed_L].val = 0;
    
    CurValueStr[Engine_water_temperature_display].fval = Value_Plus;
    CurValueStr[EVpeed_H].fval = Value_Plus;
    CurValueStr[EVpeed_L].fval = Value_Plus;
    // CurValueStr[ENUM_VSpeed                  ].fval = Value_Plus;
    // CurValueStr[ECM_FaultIndicator           ].fval = Value_Plus;
    // CurValueStr[ECM_StautsInvalid            ].fval = Value_Plus;
    // CurValueStr[ECM_MotorOver                ].fval = Value_Plus;
    // CurValueStr[ECM_ReadyIndicator           ].fval = Value_Plus;
    // CurValueStr[ECM_ChargeStatus             ].fval = Value_Plus;
    // CurValueStr[ECM_CruiseStatus             ].fval = Value_Plus;
    // CurValueStr[ECM_GearPositionStatus       ].fval = Value_Plus;
    // CurValueStr[PFC_Relag                    ].fval = Value_Plus;
    // CurValueStr[LLC_UnderOutput              ].fval = Value_Plus;
    // CurValueStr[LLC_OverOutput               ].fval = Value_Plus;
    // CurValueStr[LLC_OutPut                   ].fval = Value_Plus;
    // CurValueStr[PFC_LLC_Interal_Communication].fval = Value_Plus;
    // CurValueStr[LLC_Temp                     ].fval = Value_Plus;
    // CurValueStr[Battery_status               ].fval = Value_Plus;
    // CurValueStr[Battery_Current              ].fval = Value_Plus;
    // CurValueStr[Battery_SOC                  ].fval = Value_Plus;
    // CurValueStr[Vspeed                       ].fval = Value_Plus;
    // CurValueStr[hour                         ].fval = Value_Plus;
    // CurValueStr[minute                       ].fval = Value_Plus;
    // CurValueStr[electricity                  ].fval = Value_Plus;
    // CurValueStr[fault                        ].fval = Value_Plus;
    // CurValueStr[Energy_recovery_level        ].fval = Value_Plus;
    // CurValueStr[Side_brackets                ].fval = Value_Plus;
    // CurValueStr[TCS_close                    ].fval = Value_Plus;
    // CurValueStr[TCS_crotrul                  ].fval = Value_Plus;
    // CurValueStr[TCS_mode                     ].fval = Value_Plus;
    // CurValueStr[TCS_light                    ].fval = Value_Plus;
    // CurValueStr[GPS                          ].fval = Value_Plus;
    // CurValueStr[signal_4G                    ].fval = Value_Plus;
    // CurValueStr[Battery_over                 ].fval = Value_Plus;
    // CurValueStr[Motor_overheating            ].fval = Value_Plus;
    // CurValueStr[Cruise_control               ].fval = Value_Plus;
    // CurValueStr[Gear                         ].fval = Value_Plus;
    // CurValueStr[Bluetooth_phone              ].fval = Value_Plus;
    // CurValueStr[year                         ].fval = Value_Plus;
    // CurValueStr[Vvspeed_H                         ].fval = Value_Plus;
    // CurValueStr[Vvspeed_L                         ].fval = Value_Plus;
   // CurVehSpeed = 0;

    //LDW Open
    //CanMessageID18FE5BE8.Msg[0] |= 0x40;
    //FCW Open
  //  CanMessageID0CF02FA0.Msg[0] |= 0x03;
    //AutoLwBm Open
//    CanMessageID18FF0B21.Msg[5] |= 0x10;

}

