
#include "CalcFrequency.h" 
#include "Common_Interface.h"
#include "tim4.h"
#include "MCU_Core_Protocol.h"
#include "CAN_Communication_Matrix.h"
#include "SEG_LCD.h"

//static uint16_t VehSpeedBuf[3];
//static uint8_t VehSpeedCount;
void CalcFreq_KL15_Init(void)
{
//    E_Speed_Dir = E_SPEED_SEG_UP;
//    EspeedBak = 0;
//    E_Speed_Current_Bar = 0;
//    CurEspeed = 0;
//    DesEspeed = 0;
    // VehSpeedBuf[0] = 0;
    // VehSpeedBuf[1] = 0;
    // VehSpeedBuf[2] = 0;
    // VehSpeedCount = 0;
    Capture_Init(40u);
    RevFreInit(200u, 500000u, 1u);
    //Capture_Init(40u);
    //VehFreInit(360u, 500000u, 23616u); // 23637u);  //20230602;
}
uint16_t DisVehDelay = 0;
void CalcFreqData(uint16_t deltaTime)
{

    uint16_t RevSpeed = 0;
    uint16_t VehSpeed = 0;
    uint16_t DispVehSpeed = 0;
	uint16_t RearVeh = 0;
	uint16_t DispRearVeh = 0;
    uint16_t temp16 = 0;
	uint16_t tempR = 0;
	uint8_t Valid = 0;


//    _BCM_Module bcm_module_temp;


    
	CalcRevFreqService(deltaTime);
	//CalcVehFreqService(deltaTime); 

    /*杞??*/
    RevSpeed = Get_Rev_Speed();
    if(RevSpeed > 18000){ /*杈撳叆棰戠巼澶т簬300Hz锛岃浆閫熸樉绀?0*/
        RevSpeed = 0;
    }
		else  if(RevSpeed > 12000)	RevSpeed = 12000;
    if((RevSpeed < 12100)&&(Common_GetIgnOnTime() > IGON_SelfcheckingTimer)){   /*杈撳叆棰戠巼澶т簬200HZ锛堝皬浜?300HZ锛夋椂锛屼繚鎸佸綋鍓嶅?兼樉绀恒??*/
			Common_Set_Act_E_Speed(RevSpeed);
			Common_Set_Act_E_Speed_Valid(COMMON_Valid);
			Common_Set_Disp_E_Speed(RevSpeed);
			Common_Set_Disp_E_Speed_Valid(COMMON_Valid);
    }
    

    /*杞﹂??*/
    /*temp16 = Get_Veh_Speed();
    if(temp16 >= 4344){
        temp16 = 0;
    }

    VehSpeedBuf[VehSpeedCount] = temp16;
    VehSpeedCount++;
    if(VehSpeedCount >= 3){
        VehSpeedCount = 0;
    }
    VehSpeed = (VehSpeedBuf[0] + VehSpeedBuf[1] + VehSpeedBuf[2])/3;
    Common_Set_Act_V_Speed(VehSpeed);
    Common_Set_Act_V_Speed_Valid(COMMON_Valid);

    DisVehDelay += deltaTime;
    if(DisVehDelay >= 200){
        DisVehDelay = 0;
        DispVehSpeed = ((uint32_t)VehSpeed*108)/100;
        if(DispVehSpeed  > 3600){
            DispVehSpeed = 3600;
        }
        Common_Set_Disp_V_Speed(DispVehSpeed);
        Common_Set_Disp_V_Speed_Valid(COMMON_Valid);
    }*/
    temp16 =  Get_ID_101_ECU_Vehicle_Speed();
    tempR = 0.08 * Get_ID_12B_RearVehicle_Speed();
    Valid = Get_ID_12B_RearVehicle_Speed_Valid();
    if ((temp16 < 3) || (CAN_SIG_LOST == CAN_MSG_Status(ID_ECU_Count)))
    {
        VehSpeed = 0;
        DispVehSpeed = 0;
    }
    else
    {
        VehSpeed = temp16 * 10;
        DispVehSpeed = temp16 * 108 / 10;
        if (DispVehSpeed > 1990)
        {
            DispVehSpeed = 1990;
        }
        
    }

	

        RearVeh = tempR * 10;
        DispRearVeh = tempR * 108 / 10;
		if( SEGLCDBackup.SetMode == 2)
        {
        	if (DispRearVeh > 1990)
        	{
            	DispRearVeh = 1990;
        	}
		}
		else if(SEGLCDBackup.SetMode == 1)
		{
			if (DispRearVeh > 1230)
        	{
            	DispRearVeh = 1230;
        	}
		}
        



    

    Common_Set_Act_V_Speed(VehSpeed);
    Common_Set_Act_V_Speed_Valid(COMMON_Valid);
    Common_Set_Disp_V_Speed(DispVehSpeed);
    Common_Set_Disp_V_Speed_Valid(COMMON_Valid);
	if(Valid)
	{
	Common_Set_Act_RV_Speed(RearVeh);
	Common_Set_Act_RV_Speed_Valid(COMMON_Valid);
	Common_Set_Disp_RV_Speed(DispRearVeh);
    Common_Set_Disp_RV_Speed_Valid(COMMON_Valid);
	}

}

