#include "RTE_CAN.h"
#include "CAN_Lib.h"
#include "CAN_APP.h"
#include "CAN_Communication_Matrix.h"
#include "Analog_Signals.h"
#include "RSCAN.h"
#include "Key.h"
#include "Display_Info.h"


uint8_t CrcCheckSum(uint8_t *data, uint8_t len)
{
    uint8_t crc        = 0xff;
    uint8_t CRC_POLY   = 0x2f;
    uint8_t crcxor     = 0xff;
    uint8_t bit_index  = 0;
    uint8_t byte_index = 0;

    for ( byte_index = 0; byte_index < len; ++byte_index )
    {
        crc ^= data [ byte_index ];
        for ( bit_index = 0; bit_index < 8; ++bit_index )
        {
            if ( (crc & 0x80) != 0 )
                crc = (crc << 1) ^ CRC_POLY;
            else
                crc = (crc << 1);
        }
    }

    crc = crc ^ crcxor;

    return crc;
}



void CAN_MSG_COUNTER_PLUS(void)
{
}
void  Can_Set_Buff_200(uint8_t CopyData [])
{
    uint8_t i = 0u;
    uint32_t wtemp = 0u;
    CANMsg220Union *pCANMsg;

    pCANMsg = (CANMsg220Union *)CopyData;
    
    if ( pCANMsg != ( void * )0 )
    {
        for ( i = 0u; i < 8u; i++ )
        {
            pCANMsg->Msg [ i ] = 0u;
        }
    }

    pCANMsg->Sig.Vspeeed = Vspeed;

}
void  Can_Set_Buff_481(uint8_t CopyData [])
{
    uint8_t i = 0u;
    uint32_t wtemp = 0u;
    CANMsg481Union *pCANMsg;

    pCANMsg = (CANMsg481Union *)CopyData;
    
    if ( pCANMsg != ( void * )0 )
    {
        for ( i = 0u; i < 8u; i++ )
        {
            pCANMsg->Msg [ i ] = 0u;
        }
    }
    pCANMsg->Sig.Year   = 1;
    pCANMsg->Sig.Month  = 1;
    pCANMsg->Sig.Day    = 1;
    pCANMsg->Sig.Minute = 0;
    pCANMsg->Sig.Second = 0;
    pCANMsg->Sig.Hour   = Hour;

}

void  Can_Set_Buff_020(uint8_t CopyData [])
{
    uint8_t i = 0u;
    uint32_t wtemp = 0u;
    CANMsg020Union *pCANMsg;

    pCANMsg = (CANMsg020Union *)CopyData;
    
    if ( pCANMsg != ( void * )0 )
    {
        for ( i = 0u; i < 8u; i++ )
        {
            pCANMsg->Msg [ i ] = 0u;
        }
    }


    pCANMsg->Sig.Eletricity_H = Eletricity >> 8;
    pCANMsg->Sig.Eletricity_L = Eletricity & 0xff;
}

void  Can_Set_Buff_042(uint8_t CopyData [])
{
    uint8_t i = 0u;
    uint32_t wtemp = 0u;
    CANMsg042Union *pCANMsg;

    pCANMsg = (CANMsg042Union *)CopyData;
    
    if ( pCANMsg != ( void * )0 )
    {
        for ( i = 0u; i < 8u; i++ )
        {
            pCANMsg->Msg [ i ] = 0u;
        }
    }
    if(Tcs == 1)/*亮*/
    {
        pCANMsg->Sig.TCS = 1;
         pCANMsg->Sig.Tcs_mode = 0;
         pCANMsg->Sig.Tcs_Control = 0;
    }
    else if(Tcs == 2)/*灭*/
    {
        pCANMsg->Sig.TCS = 0;//
        pCANMsg->Sig.Tcs_mode = 0;
        pCANMsg->Sig.Tcs_Control = 0;
    }
    else
    {
        pCANMsg->Sig.TCS = 0;
        pCANMsg->Sig.Tcs_mode = 0;
        pCANMsg->Sig.Tcs_Control = 0;
    }
    pCANMsg->Msg[7] = 0xff;
}

void  Can_Set_Buff_480(uint8_t CopyData [])
{
    uint8_t i = 0u;
    uint32_t wtemp = 0u;
    CANMsg480Union *pCANMsg;

    pCANMsg = (CANMsg480Union *)CopyData;
    
    if ( pCANMsg != ( void * )0 )
    {
        for ( i = 0u; i < 8u; i++ )
        {
            pCANMsg->Msg [ i ] = 0u;
        }
    }
    pCANMsg->Sig.Signal_Gps = Signal_GPS;
    pCANMsg->Sig.Signal_4g = Signal_4G;
}

void  Can_Set_Buff_280(uint8_t CopyData [])
{
    uint8_t i = 0u;
    uint32_t wtemp = 0u;
    CANMsg280Union *pCANMsg;

    pCANMsg = (CANMsg280Union *)CopyData;
    
    if ( pCANMsg != ( void * )0 )
    {
        for ( i = 0u; i < 8u; i++ )
        {
            pCANMsg->Msg [ i ] = 0u;
        }
    }
    if(Energy_recovery == 2)
    {
        pCANMsg->Sig.Energy_status = 1;
        pCANMsg->Sig.Energy_recovery = 2;
        //pCANMsg->Sig.Side_bracket = 1;
    }
    else if(Energy_recovery == 1)
    {
        pCANMsg->Sig.Energy_status = 1;
        pCANMsg->Sig.Energy_recovery = 1;
    }
    else if(Energy_recovery == 3)
    {
        pCANMsg->Sig.Energy_status = 1;
        pCANMsg->Sig.Energy_recovery = 3;
    }
    else
    {
        ;
    }

    if(Tcs == 1)
    {
        pCANMsg->Sig.Tcs_Status = 1;
    }
    if(Side_bracket == 1)
    {
        pCANMsg->Sig.Side_bracket280 = 1;
    }
    if(Side_bracket == 0)
    {
        pCANMsg->Sig.Side_bracket280 = 0;
    }

}

void  Can_Set_Buff_202(uint8_t CopyData [])
{
    uint8_t i = 0u;
    uint32_t wtemp = 0u;
    CANMsg202Union *pCANMsg;

    pCANMsg = (CANMsg202Union *)CopyData;
    
    if ( pCANMsg != ( void * )0 )
    {
        for ( i = 0u; i < 8u; i++ )
        {
            pCANMsg->Msg [ i ] = 0u;
        }
    }
    if(Gear == 0)
    {
        pCANMsg->Sig.Gear = 0;
    }
    else if(Gear == 1)
    {
        pCANMsg->Sig.Gear = 1;
    }
    else if(Gear == 2)
    {
        pCANMsg->Sig.Gear = 2;
    }

    if(Cruiste == 1)
    {
        pCANMsg->Sig.Cruiste = 1;
    }

    if(Ready == 1)
    {
        pCANMsg->Sig.Ready = 1;
    }
    else if(Ready == 2)
    {
        pCANMsg->Sig.Ready = 2;
    }
    else if(Ready == 3 )
    {
        pCANMsg->Sig.Ready = 3;
    }

    if(Fault == 1)
    {
        pCANMsg->Sig.Fault = 1;
    }

    if(Hill_Descent_control == 1)
    {
        pCANMsg->Sig.Hill_Descent_control = 1;
    }
    else if(Hill_Descent_control == 0)
    {
        pCANMsg->Sig.Hill_Descent_control = 0;
    }

    if(Side_bracket == 1)
    {
        pCANMsg->Sig.Side_bracket202 = 1;
    }
    // pCANMsg->Msg[1]=0Xff;
}
void  Can_Set_Buff_001(uint8_t CopyData [])
{
    uint8_t i = 0u;
    uint32_t wtemp = 0u;
    CANMsg001Union *pCANMsg;

    pCANMsg = (CANMsg001Union *)CopyData;
    
    if ( pCANMsg != ( void * )0 )
    {
        for ( i = 0u; i < 8u; i++ )
        {
            pCANMsg->Msg [ i ] = 0u;
        }
    }
    if(Motor_hert == 1)
    {
        pCANMsg->Sig.Motor_hert = 1;
    }
    if( Energy_recovery== 2)/*能量回收*/
    {
        pCANMsg->Sig.Msg_0x001_2b3t = 1;
    }
}

void  Can_Set_Buff_021(uint8_t CopyData [])
{
    uint8_t i = 0u;
    uint32_t wtemp = 0u;
    CANMsg021Union *pCANMsg;

    pCANMsg = (CANMsg021Union *)CopyData;
    
    if ( pCANMsg != ( void * )0 )
    {
        for ( i = 0u; i < 8u; i++ )
        {
            pCANMsg->Msg [ i ] = 0u;
        }
    }

    if(Battery_temp == 1)
    {
        pCANMsg->Sig.Battery_temp = 1;
    }
        if(Battery_temp == 1)
    {
        pCANMsg->Sig.Battery_temp = 1;
    }
}
//NFC
void  Can_Set_Buff_284(uint8_t CopyData [])
{
    uint8_t i = 0u;
    uint32_t wtemp = 0u;
    CANMsg284Union *pCANMsg;

    pCANMsg = (CANMsg284Union *)CopyData;
    
    if ( pCANMsg != ( void * )0 )
    {
        for ( i = 0u; i < 8u; i++ )
        {
            pCANMsg->Msg [ i ] = 0u;
        }
    }

    if(NFC_C_card == 1)
    {
        pCANMsg->Sig.NFC_C_card = 1;
    }
    else if((NFC_C_card == 2))
    {
        pCANMsg->Sig.NFC_C_card = 2;
    }

}
/*软件版本号*/
extern uint8_t Data_786_1[8]= {0x03,0x22,0x10,0x24,0xAA,0xAA,0xAA,0xAA};
extern uint8_t Data_786_2[8]= {0x30,0x00,0x00,0xAA,0xAA,0xAA,0xAA,0xAA};
/*硬件版本号*/
extern uint8_t Data_786_3[8]= {0x03,0x22,0xF1,0x93,0xAA,0xAA,0xAA,0xAA};
extern uint8_t Data_786_4[8]= {0x30,0x00,0x00,0xAA,0xAA,0xAA,0xAA,0xAA};

void  Can_Set_Buff_786(uint8_t CopyData [])
{
    uint8_t i = 0u;
    uint32_t wtemp = 0u;
    CANMsg786Union *pCANMsg;

    pCANMsg = (CANMsg786Union *)CopyData;
    
    if ( pCANMsg != ( void * )0 )
    {
        for ( i = 0u; i < 8u; i++ )
        {
            pCANMsg->Msg [ i ] = 0u;
        }
    }

    if(Diagnosis_banbenhao_flag == 1)
    {
        // pCANMsg->Sig.diagnosis0 = 0x03;
        for(uint8_t i = 0;i < 8;i++)
        {
            pCANMsg->Msg[i] = Data_786_1[i];
            
        }
        Diagnosis_banbenhao_flag_=1;

    }
     if(Diagnosis_banbenhao_flag == 2)
    {
        for(uint8_t i = 0;i < 8;i++)
        {
            pCANMsg->Msg[i] = Data_786_2[i];
                  
        }
        Diagnosis_banbenhao_flag_=2;  
    }
    else if(Diagnosis_banbenhao_flag == 3)
    {

        for(uint8_t i = 0;i < 8;i++)
        {
            pCANMsg->Msg[i] = Data_786_3[i];
            
        }
        Diagnosis_banbenhao_flag_=3;

    }
    else if(Diagnosis_banbenhao_flag == 4)
    {

        for(uint8_t i = 0;i < 8;i++)
        {
            pCANMsg->Msg[i] = Data_786_4[i];
            
        }
        Diagnosis_banbenhao_flag_=4;

    }

}

void  Can_Set_Buff_207(uint8_t CopyData [])
{
    uint8_t i = 0u;
    uint32_t wtemp = 0u;
    CANMsg207Union *pCANMsg;

    pCANMsg = (CANMsg207Union *)CopyData;
    
    if ( pCANMsg != ( void * )0 )
    {
        for ( i = 0u; i < 8u; i++ )
        {
            pCANMsg->Msg [ i ] = 0u;
        }
    }
    if(Tcs == 1)
    {
        pCANMsg->Sig.Tcs_mode = 1;
        pCANMsg->Sig.Tcs_Control = 1;
    }
    else if(Tcs == 2)
    {
        pCANMsg->Sig.Tcs_Control = 1;
    }
    else
    {
        ;
    }
}
