#include "sertrf.h"


sertrf_t sertrf;
void sertrf_init(void)
{
    uint8_t res = 0;
    res = device_init(&sertrf.device);
    if(res)
    {
        SYS_LOG_WRN("device init error");
    }
    //RGB灯
    work_rgb_led_start();
    //按键初始化
    button_work_init();
    //线程启动
    sertrf_start();
}
void sertrf_start(void)
{
    os_thread_create(&sertrf.embedded_thread,
                     "embedded_thread",
                     embedded_thread,
                     NULL,
                     4096,
                     20);
}
void embedded_thread(void* arg)
{
    while(true)
    {
        uint32_t embedded_size = embedded_device_get_rx_length(&sertrf.device);
        if(embedded_size > 0)
        {
            uint8_t data[embedded_size];
            embedded_device_read(&sertrf.device, data, embedded_size);
            // SYS_LOG_INF("data : %s", data);
            pc_device_write(&sertrf.device, data, embedded_size);
        }

        uint32_t pc_size = pc_device_get_rx_length(&sertrf.device);
        
        if(pc_size > 0)
        {
            uint8_t data[pc_size];
            pc_device_read(&sertrf.device, data, pc_size);
            // SYS_LOG_INF("data : %s", data);
            embedded_device_write(&sertrf.device, data, pc_size);
        }
        // printf_chill_time(10,1000);
        pc_link_rgb_color(&sertrf.device);
        //需要添加一些延时，否则会卡死，导致看门狗复位
        // vTaskDelay(( TickType_t ) 1000 / configTICK_RATE_HZ);
        os_thread_sleep(1);
    }
}
void pc_link_rgb_color(device_t* device)
{
    static uint8_t last_connect = 255, last_wifi_mode = 0;
    if(device->connect_pc != last_connect || last_wifi_mode != device->init_device.wifi_mode)
    {
        switch (device->connect_pc)
        {
            case DISCONNECT:
            {
                if(device->init_device.wifi_mode == WIFI_NETIF_MODE_AP)
                    rgb_color_change(0, RGB_COLOR_GREEN_WHITE);
                else
                    rgb_color_change(0, RGB_COLOR_GREEN_PURPLE);

                break;
            }    
            case CONNECT_WIFI_TCP:
            {
                if(device->init_device.wifi_mode == WIFI_NETIF_MODE_AP)
                    rgb_color_change(0, RGB_COLOR_WHITE);
                else
                    rgb_color_change(0, RGB_COLOR_PURPLE);

                break;
            }
            case CONNECT_WIFI_UDP:
            {
                if(device->init_device.wifi_mode == WIFI_NETIF_MODE_AP)
                    rgb_color_change(0, RGB_COLOR_WHITE);
                else
                    rgb_color_change(0, RGB_COLOR_PURPLE);

                break;
            }
            case CONNECT_BLE:
                rgb_color_change(0, RGB_COLOR_GREEN);
                break;
        }
        last_connect = device->connect_pc;
        last_wifi_mode = device->init_device.wifi_mode;
    }
    if(device->connect_pc == DISCONNECT)
    {
        rgb_update_cyle(50);
    }
    else if(device->connect_pc){
        rgb_update_cyle(888);
    }else{
        rgb_update_cyle(500);
    }
}

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",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;
}