#include "tool.h"

int time_out(uint32_t* time_start, uint32_t timeout_ms)
{
    uint32_t time_new = os_get_sys_time();
    if(time_new - *time_start > timeout_ms)
    {
        *time_start = time_new;
        return 1;
    }
    return 0;
}

void printf_chill_time(uint8_t chill_time, uint16_t type)
{
    static size_t last_time[24] = {0};
    static uint16_t cnt[24] = {0};
    static uint32_t type_cnt_time[24] = {0};
    size_t now_time = os_get_sys_time();
    cnt[chill_time]++;
    type_cnt_time[chill_time] += now_time - last_time[chill_time];
    if(cnt[chill_time] % type == 0 && type_cnt_time[chill_time] / type >= now_time - last_time[chill_time] - 1)
    {
        SYS_LOG_INF("TIME_CHILL%d : %d : %d\n",chill_time, now_time - last_time[chill_time], type_cnt_time[chill_time]);
        cnt[chill_time] = 0;
        type_cnt_time[chill_time] = 0;
    }
    else if(cnt[chill_time] % type == 0)
    {
        SYS_LOG_WRN("TIME_CHILL%d : %d : %d",chill_time, now_time - last_time[chill_time], type_cnt_time[chill_time]);
        cnt[chill_time] = 0;
        type_cnt_time[chill_time] = 0;
    }
    last_time[chill_time] = now_time;
}

uint32_t parse_hex_or_dec(const char *s) {
    if (!s) return 0;
    if (s[0] == '0' && (s[1]=='x' || s[1]=='X')) return (uint32_t)strtoul(s+2, NULL, 16);
    return (uint32_t)strtoul(s, NULL, 0);
}