#include "soft_timer.h"
#include "main.h"
#include "stdbool.h"
#include "soft_can.h"
#include "usart_data_handle.h"
#include "main_task.h"
#include "string.h"
extern uint32_t user_timer_cnt;

/**
  * @file    timer_function
  * @brief   发送给控制的定时器
  * @param    
  * @details 
  * @author  Zhang Sir 
 **/
bool vol_flag = false, devtype_flag = false, engine_flag = false,
     can_debug_flag = false, dev_version_flag = false,mimo360_radar_flag = false,
     pmu_heart_flag = false;

Dev_timer send_devinfo_time;

void timer_function()
{
    static uint32_t time_1hz = 0;
    static uint32_t time_2hz = 0;
    static uint32_t time_5hz = 0;
    static uint32_t time_10hz = 0;
    static uint32_t time_20hz = 0;
    static uint32_t time_50hz = 0;
    static uint32_t time_100hz = 0;
    static uint32_t time_500hz = 0;

    if (  Check_Timer_Ready(&time_1hz,_1_HZ_) )
    {
        send_devinfo_time.arm = true;
    }
    if (  Check_Timer_Ready(&time_2hz,_2_HZ_) )
    {
        
        send_devinfo_time.nozzle = true;
        send_devinfo_time.seed = true;
        send_devinfo_time.checklow = true;
        dev_version_flag = true;
    }
    if ( Check_Timer_Ready(&time_5hz,_5_HZ_))
    {
        engine_flag = true;
        
        send_devinfo_time.bms = true;

        if(planep.Candebug_flag == true)
        {
            can_debug_flag = true;
        }
        else
        {
            can_debug_flag = false;
        }
    }
    if (  Check_Timer_Ready(&time_10hz,_10_HZ_) )
    {
    }
    if ( Check_Timer_Ready(&time_20hz,_20_HZ_) )
    {
        vol_flag = true;

        mimo360_radar_flag = true;
        send_devinfo_time.weight = true;
        send_devinfo_time.radar = true;
        send_devinfo_time.flow = true;
        send_devinfo_time.L_pump1 = true;
        send_devinfo_time.L_pump2 = true;
        send_devinfo_time.pump = true;

    }
    if ( Check_Timer_Ready(&time_50hz,_50_HZ_) )
    {
        // 数据量包太多，超过了200hz极限，导致丢数据。
        send_devinfo_time.part_radar = true;
        send_devinfo_time.part_Fradar = true;
        send_devinfo_time.part_Bradar = true;
        devtype_flag = true;
        
    }
    if(Check_Timer_Ready(&time_100hz,_100_HZ_))
    {

    }
    if(Check_Timer_Ready(&time_500hz,_200_HZ_))
    {
        
    }
}

/**
  * @file    Get_Systimer_Us
  * @brief   获取系统当前时间us
  * @param    
  * @details 
  * @author  Zhang Sir 
 **/
uint32_t Get_Systimer_Us(void)
{
    uint32_t time = 0;
    rt_enter_critical();   // 进入临界 不允许调度器执行
    time = htim4.Instance->CNT + user_timer_cnt * 20000;
    rt_exit_critical();  //退出临界
    return time;
}

uint32_t test_time[10] = {0};
uint32_t test_priod = 0;
uint32_t check_period_func(uint32_t *time)
{
    uint32_t time_err = 0;

    time_err = HAL_GetTick() - *time;
    *time = HAL_GetTick();

    return time_err;
}


uint8_t isLeapYear(uint16_t year)
{
    uint8_t res=0;

    if(year%4 == 0) // 能够被4整除
    {
        if(year%100 != 0 || year%400 == 0)  //能被4整除不能被100整除或者能够被400整除
        {
            res = 1;
        }
        else
        {
            res =0;
        }
    }
    return res;
}
// 将Unix时间戳转换为北京时间
// unixTime: 需要判断的Unix时间戳
// *tempBeijing:返回的北京时间
// return：none
// note：没对输入参数正确性做判断
rtc_time_t beijing_time;
uint8_t utc_send_time = 10;
uint32_t utc_time = 0;
void covUnixTimeStp2Beijing( uint32_t unixTime, rtc_time_t *tempBeijing )
{
    uint32_t totleDayNum = 0, totleSecNum = 0;
    uint16_t remainDayofYear = 0, tempYear = 0;
    uint8_t *pr = NULL;
    static uint8_t month_day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //平年
    static uint8_t Leap_month_day[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //闰年


    totleDayNum = unixTime / ( 24 * 60 * 60 ); //总天数(注意加括号)
    totleSecNum = unixTime % ( 24 * 60 * 60 ); //当天剩余的秒速

    memset( tempBeijing, 0x00, sizeof( rtc_time_t ) );

    // 1.先计算时间 HH:MM:SS
    tempBeijing->ui8Hour = totleSecNum / 3600;
    tempBeijing->ui8Minute = ( totleSecNum % 3600 ) / 60; //error：变量搞错
    tempBeijing->ui8Second = ( totleSecNum % 3600 ) % 60;

    // 2.对时间进行时区调整（注意：这里可能造成日期 +1）
    tempBeijing->ui8Hour += TIMEZONE;
    if ( tempBeijing->ui8Hour > 23 )
    {
        //printf("modify day..\n");
        tempBeijing->ui8Hour -= 24;
        remainDayofYear++;  // 日期+1
    }

    // 3.计算哪一年
    tempBeijing->ui8Year = 1970 + ( totleDayNum / FOURYEARDAY ) * 4; // 4年为一个周期
    remainDayofYear += totleDayNum % FOURYEARDAY;

    //printf("year:%d, day:%d.\n", tempBeijing->ui8Year, remainDayofYear);
    tempYear = isLeapYear( tempBeijing->ui8Year ) ? 366 : 365;
    while ( remainDayofYear >= tempYear ) // 计算4年整数倍外的年。
    {
        tempBeijing->ui8Year++;
        remainDayofYear -= tempYear;
        tempYear = isLeapYear( tempBeijing->ui8Year ) ? 366 : 365;
    }

    // 4.计算哪一月的哪一天
    pr = isLeapYear( tempBeijing->ui8Year ) ? Leap_month_day : month_day;
    remainDayofYear++;          // 这里开始计算具体日期。remainDayofYear为 0 时其实是 1 号，所以这里要 +1
    while ( remainDayofYear > *( pr + tempBeijing->ui8Month ) )
    {
        remainDayofYear -= *( pr + tempBeijing->ui8Month );
        tempBeijing->ui8Month++;
    }
    //printf("year:%d, day:%d.\n", tempBeijing->ui8Year, remainDayofYear);
    tempBeijing->ui8Month++; //month
    tempBeijing->ui8DayOfMonth = remainDayofYear;  //day
    //printf("year:%d, day:%d.\n", tempBeijing->ui8Year, tempBeijing->ui8DayOfMonth);
}
