#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_260.ID = 0x260;
    CanMsg_180.ID = 0x180;  
    CanMsg_100.ID = 0x100;
    CanMsg_153.ID = 0x153;
    CanMsg_120.ID = 0x120;
    CanMsg_200.ID = 0x200;
    CanMsg_687.ID = 0x687;
    CanMsg_600.ID = 0x600;
    // CanMsg_SOC.ID = 0x18203020;
    // CanMsg_SOC1.ID = 0x18200A20;
    // CanMsg_SOC2.ID = 0x18200A21;
    // CanMsg_38E.ID = 0x38E;
    

    CanMsg_260.Length = 8;
    CanMsg_180.Length = 8;  
    CanMsg_100.Length = 8;
    CanMsg_153.Length = 8;
    CanMsg_120.Length = 8;
    CanMsg_200.Length = 8;
    CanMsg_687.Length = 8;
    CanMsg_600.Length = 8;
    // CanMsg_SOC.Length = 8;
    // CanMsg_SOC1.Length = 8;
    // CanMsg_SOC2.Length = 8;  
    // CanMsg_38E.Length = 8;

    CanMsg_260.ID_EN = 1;
    CanMsg_180.ID_EN = 1; 
    CanMsg_100.ID_EN = 1;
    CanMsg_153.ID_EN = 1;
    CanMsg_120.ID_EN = 1;
    CanMsg_200.ID_EN = 1;
    CanMsg_687.ID_EN = 1;
    CanMsg_600.ID_EN = 1;
    // CanMsg_SOC.ID_EN = 1;
    // CanMsg_SOC1.ID_EN = 1;
    // CanMsg_SOC2.ID_EN = 1;
    // CanMsg_38E.ID_EN = 1;
} 

void Disable_CAN_Tx(void){
    
    CanMsg_260.ID_EN = 0;
    CanMsg_180.ID_EN = 0; 
    CanMsg_100.ID_EN = 0;
    CanMsg_153.ID_EN = 0;
    CanMsg_120.ID_EN = 0;
    CanMsg_200.ID_EN = 0;
    CanMsg_687.ID_EN = 0;
    CanMsg_600.ID_EN = 0;
    // CanMsg_157.ID_EN = 0;
    // CanMsg_SOC.ID_EN = 0;
    // CanMsg_SOC1.ID_EN = 0;
    // CanMsg_SOC2.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_260);
        Clear_CAN_Msg((CAN_message *)&CanMsg_260);

    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_180);
    //     Clear_CAN_Msg((CAN_message *)&CanMsg_180);     
//
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_100);
    //     Clear_CAN_Msg((CAN_message *)&CanMsg_100);      
//
    bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_153);
        Clear_CAN_Msg((CAN_message *)&CanMsg_153);       
//
    bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_120);
       Clear_CAN_Msg((CAN_message *)&CanMsg_120); 
    //            
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_200);
    //    Clear_CAN_Msg((CAN_message *)&CanMsg_200);
       
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_687);
    //    Clear_CAN_Msg((CAN_message *)&CanMsg_687);
       
    // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_600);
    //    Clear_CAN_Msg((CAN_message *)&CanMsg_600);
    //bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_SOC);
    //    Clear_CAN_Msg((CAN_message *)&CanMsg_SOC);
    //bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_SOC1);
    //    Clear_CAN_Msg((CAN_message *)&CanMsg_SOC1);
    //bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_SOC2);
    //    Clear_CAN_Msg((CAN_message *)&CanMsg_SOC2);
    //bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_38E);
    //    Clear_CAN_Msg((CAN_message *)&CanMsg_38E);
        //bReturn = Api_CANSendFrame((CAN_message *)&CanMessageID18FEDCD5);
        //bReturn = Api_CANSendFrame((CAN_message *)&CanMessageID18FEDED5);
        
        //Clear_CAN_Msg((CAN_message *)&CanMessageID18FEDCD5);
        //Clear_CAN_Msg((CAN_message *)&CanMessageID18FEDED5);
    }

    if(GetCtrlTime(CANMsgSend_20ms) >= 20){
        ClearCtrlTime(CANMsgSend_20ms);
        bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_180);
        Clear_CAN_Msg((CAN_message *)&CanMsg_180); 

        bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_687);
        Clear_CAN_Msg((CAN_message *)&CanMsg_687);
        
    }

    if(GetCtrlTime(CANMsgSend_50ms) >= 50){
        ClearCtrlTime(CANMsgSend_50ms);
        
        
        // bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_341);
        // Clear_CAN_Msg((CAN_message *)&CanMsg_341);

        
    }

    if(GetCtrlTime(CANMsgSend_100ms) >= 100){
        ClearCtrlTime(CANMsgSend_100ms);
        bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_100);
        Clear_CAN_Msg((CAN_message *)&CanMsg_100);  
        
    }

    if(GetCtrlTime(CANMsgSend_200ms) >= 200){

        ClearCtrlTime(CANMsgSend_200ms);
        bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_200);
        Clear_CAN_Msg((CAN_message *)&CanMsg_200);

        bReturn = Api_CANSendFrame((CAN_message *)&CanMsg_600);
        Clear_CAN_Msg((CAN_message *)&CanMsg_600);

    }

    if(GetCtrlTime(CANMsgSend_500ms) >= 500){

        flash_1Hz_process();

        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_RSpeed          ].val = 0;
    //CurValueStr[ENUM_WaterTemp         ].val = 0;
    //CurValueStr[TCS_Warning_Lamp         ].val = 0;
    //CurValueStr[TCS_Work_Mode          ].val = 0;
    //CurValueStr[ISG_Power         ].val = 0;
    //CurValueStr[ISG_Power_Enable          ].val = 0;
    //CurValueStr[ECU_Warning_Lamp      ].val = 0;
    //CurValueStr[MMCU_PowerMode      ].val = 0;
//
    //
    // CurValueStr[ENUM_VSpeed         ].fval = Value_Plus;
    // CurValueStr[ENUM_RSpeed          ].fval = Value_Plus;
    //CurValueStr[ENUM_WaterTemp         ].fval = Value_Plus;
    //CurValueStr[TCS_Warning_Lamp         ].fval = Value_Plus;
    //CurValueStr[TCS_Work_Mode          ].fval = Value_Plus;
    //CurValueStr[ISG_Power         ].fval = Value_Plus;
    //CurValueStr[ISG_Power_Enable          ].fval = Value_Plus;    
    //CurValueStr[ECU_Warning_Lamp      ].fval = 1;
    //CurValueStr[MMCU_PowerMode      ].fval = 1;
    
    


   // CurVehSpeed = 0;

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

}

