
#include "CommonInterface\CommonInterface.h"
#include "CAN_APP\CAN_CH0_CAN_Communication_Matrix.h"

static uint32_t PowerIgnOnTimeLine;
uint32_t PowerIgnOffTimeLine;

uint8_t Power_CheckSelf = 0;
static uint32_t PowerSocTimeLine;

static COMMON_PowerStatus_t PowerSts; /*1 ON ;  0 OFF*/
static uint16_t Act_V_Speed;          /*实际值,十倍的*/
static uint16_t Disp_V_Speed;         /*显示值,十倍的*/
static DataValid_t PowerSts_Valid;
volatile static DataValid_t Act_V_Speed_Valid;
static DataValid_t Disp_V_Speed_Valid;

// static uint16_t u16SelfCheckTime;
/*每次唤醒调用*/
void Common_DataInit(void)
{
    PowerSts = COMMON_POWER_UNKNOW;
    Act_V_Speed = 0xffffu;
    Disp_V_Speed = 0xffffu;

    PowerSts_Valid = COMMON_Valid_UNKNOW;
    Act_V_Speed_Valid = COMMON_Valid_UNKNOW;
    Disp_V_Speed_Valid = COMMON_Valid_UNKNOW;
    PowerIgnOnTimeLine = 0x0ul;
    PowerIgnOffTimeLine = 0x0ul;
    PowerSocTimeLine = 0x0ul;
    // u16SelfCheckTime = DisplaySelfCheckTime;
}

/*以下由各个处理模块按需调用*/
/*使用数据之前,需要先判断数据是否有效,如果无效,则不可以使用*/

uint8_t Common_Get_IG_Sts(void)
{
    return (uint8_t)PowerSts;
}

uint16_t Common_Get_Act_V_Speed(void)
{
    return Act_V_Speed;
}
uint16_t Common_Get_Act_V_Speed_ODO(void)
{
    return Act_V_Speed * 101 / 100;
}
// uint16_t Common_Get_Disp_V_Speed(void)
// {
//     return Disp_V_Speed;
// }

uint16_t Common_Get_TireSize(void)
{
    return 1u;
}

void Common_Set_IG_Sts(COMMON_PowerStatus_t Val)
{
    PowerSts = Val;
}
void Common_Set_Act_V_Speed(uint16_t Val)
{
    Act_V_Speed = Val;
}
void Common_Set_Disp_V_Speed(uint16_t Val)
{
    Disp_V_Speed = Val;
}

DataValid_t Common_Get_IG_Sts_Valid(void)
{
    return PowerSts_Valid;
}
DataValid_t Common_Get_Act_V_Speed_Valid(void)
{
    return Act_V_Speed_Valid;
}
// DataValid_t Common_Get_Disp_V_Speed_Valid(void)
// {
//     return Disp_V_Speed_Valid;
// }

void Common_Set_IG_Sts_Valid(DataValid_t Val)
{
    PowerSts_Valid = Val;
}
void Common_Set_Act_V_Speed_Valid(DataValid_t Val)
{
    Act_V_Speed_Valid = Val;
}
void Common_Set_Disp_V_Speed_Valid(DataValid_t Val)
{
    Disp_V_Speed_Valid = Val;
}

// uint16_t Common_Get_DispSpeed(void)
// {
//     uint16_t m_Res;
//     return m_Res;
// }

void Common_Input_Para(void)
{
    if (SYS_OPR_STAT_IGN_ON)
    {
        if ((Get_CAN_Power_State() != KEY_OFF))
        {
            Common_Set_IG_Sts(COMMON_POWER_ON);
            Common_Set_IG_Sts_Valid(COMMON_Valid);

            PowerIgnOffTimeLine = 0ul;

            if (PowerIgnOnTimeLine < 0x7ffffffful)
            {
                PowerIgnOnTimeLine += 2u;
            }
        }
        else
        {
            PowerIgnOnTimeLine = 0ul;
            if (PowerIgnOffTimeLine < 0x7ffffffful)
            {
                PowerIgnOffTimeLine += 2u;
            }
            Common_Set_IG_Sts(COMMON_POWER_OFF);
            Common_Set_IG_Sts_Valid(COMMON_Valid);
        }
        if ((Get_CAN_Power_State() == PKEY_ON))
        {
            PowerIgnOnTimeLine = 0ul;
        }
    }
    else
    {
        PowerIgnOnTimeLine = 0ul;
        if (PowerIgnOffTimeLine < 0x7ffffffful)
        {
            PowerIgnOffTimeLine += 2u;
        }
        Common_Set_IG_Sts(COMMON_POWER_OFF);
        Common_Set_IG_Sts_Valid(COMMON_Valid);
    }
}

uint32_t Common_GetIgnOnTime(void)
{
    return PowerIgnOnTimeLine;
}

uint32_t Common_GetIgnOffTime(void)
{
    return PowerIgnOffTimeLine;
}

uint32_t Common_GetSocTime(void)
{
    return PowerSocTimeLine;
}

