#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include "./hmi/hmi_cmd.h"
#include "./hmi/hmi_rx_parse.h"
#include "./hmi/hmi_hex_parse.h"

#include "./hmi_test.h"
#include "./hmi_btn_example.h"
#include "./hmi_curve_example.h"
#include "./hmi_pointer_example.h"
#include "./hmi_slider_example.h"
#include "./hmi_qr_example.h"
#include "./hmi_select_text_example.h"
#include "./hmi_spinner_example.h"
#include "./hmi_progerss_bar_example.h"


 int cur_page_id = 0;

static const char *prints_cmd_tab[] =
    {
        "cur_page=",
};

void hmi_test_init(void)
{
    bool res;
    cmd_res_t *back;

    res = API_ACK();
    if (!res)
    {
        printf("HMI DEVICE NO ACK!");
    }
    res = API_VERSION();
    if (res)
    {
        back = hmi_cmd_back_res();
        printf("%s\r\n", back->val.str);
    }

    res = API_GET_CODE();
    if (res)
    {
        back = hmi_cmd_back_res();
        printf("%s\r\n", back->val.str);
    }

    //    API_PAGE(12);

    res = API_GET(sys.pid);
    if (res)
    {
        back = hmi_cmd_back_res();
        printf("%d\r\n", back->val.value);
        cur_page_id = back->val.value; /*  获取当前页面ID  */
    }
}

/**
 * @brief    串口屏发送prints指令解析回调
 */
void hmi_str_cmd_parse(char *str_cmd)
{
    /*
        hmi工程在每个页面初始化前事件，写脚本：prints("cur_page=%d", sys.pid);
        即发送当前页面ID给STM32。
    */
    if (strstr(str_cmd, prints_cmd_tab[0]))
    {
        sscanf(str_cmd, "%*9s%d", &cur_page_id); /*  从字符粗中获取页面id  */
    }

    switch (cur_page_id)
    {
    case 0:
        
        break;

    case 1:
        hmi_btn_example_str_cmd(str_cmd);
        break;
    }
}

/**
 * @brief    hex十六进制解析成功回调处理
 */
void hmi_hex_cmd_parse(struct hex_cmd_fmt *fram)
{
    switch (cur_page_id)
    {
    case 0:
        break;

    case 1:
        hmi_btn_example_hex_cmd(fram);
        break;
    case 2:
        hmi_curve_example_hex_cmd(fram);
        break;
    case 3:
        break;
    case 4:
        hmi_slider_example_hex_cmd(fram);
        break;

    case 5:
        hmi_qr_example_hex_cmd(fram);
        break;
    case 6:
        hmi_select_text_example_hex_cmd(fram);
        break;
    case 7:
        hmi_spinner_example_hex_cmd(fram);
        break;
    case 8:
        break;
    }
}

void hmi_update_ui_50ms(void)
{
    hmi_btn_example_timer_update_50ms(cur_page_id);
    hmi_curve_example_timer_update_50ms(cur_page_id);
    hmi_pointer_example_timer_update_50ms(cur_page_id);
    hmi_progerss_bar_example_timer_update_50ms(cur_page_id);
}

void hmi_update_ui_100ms(void)
{

}
