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

#include "./hmi/hmi_cmd.h"
#include "./hmi/hmi_rx_parse.h"
#include "./BSP/USART2/usart2.h"
#include "./SYSTEM/delay/delay.h"


#define  END_CHAR       "\r\n"  /*  指令结尾  */
#define  DEFAULT_CMD_RX_TIMEOUT   500   /*  有返回的指令500ms内没返回则超时  */


static void _wait_parse_idle(void);



void hmi_cmd_send(char *cmd)
{
    if (!cmd) return;

    usart2_send_data((uint8_t *)cmd, strlen(cmd));
    usart2_send_data((uint8_t *)END_CHAR, strlen(END_CHAR));
}

cmd_res_t *hmi_cmd_back_res(void)
{
    return hmi_rx_parse_get_res();
}

void api_page(char *name)
{
    char buf[128];
    snprintf(buf, 128, "page(%s);", name);
    hmi_cmd_send(buf);
}

void api_play(char *path, uint8_t mode)
{
    char buf[128];
    uint32_t len = strlen(path);
    if (len > 236) return; /*  预览20个字节空间，存放play(,);  */

    strcpy(buf, "play(");
    char *buf_path = &buf[5];
    for (int i = 0; i < len; i++)
    {
        *buf_path = *path;
        // if (*path == '\\' && *(path+ 1) != '\\')
        // {
        //     buf_path++;
        //     *buf_path = '\\';
        // }
        buf_path++;
        path++;
    }
    sprintf(buf_path, ",%d);", mode);
    hmi_cmd_send(buf);
}

void api_move(char *name, int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t time, uint8_t mode)
{
    char buf[128];
    snprintf(buf, 128, "move(%s,%d,%d,%d,%d,%d,%d);", name, x0, y0, x1, y1, time, mode);
    hmi_cmd_send(buf);
}

void api_click(char *name, uint8_t event)
{
    char buf[128];
    snprintf(buf, 128, "click(%s,%d);", name, event);
    hmi_cmd_send(buf);
}

void api_vis(char *name, uint8_t state)
{
    char buf[128];
    snprintf(buf, 128, "vis(%s,%d);", name, state);
    hmi_cmd_send(buf);
}



void api_cadd(char *name, uint8_t ch, uint8_t value)
{
    char buf[128];
    snprintf(buf, 128, "cadd(%s,%d,%d);", name, ch, value);
    hmi_cmd_send(buf);
}

void api_covx(char *attr0, char *attr1, uint16_t lenth, uint16_t format)
{
    char buf[128];
    snprintf(buf, 128, "covx(%s,%s,%d,%d);", attr0, attr1, lenth, format);
    hmi_cmd_send(buf);
}

void api_rtcs(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec)
{
    char buf[128];
    snprintf(buf, 128, "rtcs(%d,%d,%d,%d,%d,%d);", year, month, day, hour, min, sec);
    hmi_cmd_send(buf);
}

void api_beep(uint16_t time, uint16_t frequency)
{
    char buf[128];
    snprintf(buf, 128, "beep(%d,%d);", time, frequency);
    hmi_cmd_send(buf);
}

void api_randset(int min, int max)
{
    char buf[128];
    snprintf(buf, 128, "randset(%d,%d);", min, max);
    hmi_cmd_send(buf);
}

void api_cclear(char *name, uint8_t ch)
{
    char buf[128];
    snprintf(buf, 128, "cclear(%s,%d);", name, ch);
    hmi_cmd_send(buf);
}

bool api_get(char *attr)
{
    _wait_parse_idle();

    char buf[128];
    snprintf(buf, 128, "get(%s);", attr);
    hmi_cmd_send(buf);
    return hmi_cmd_wait_parse(RET_GET);
}

bool api_reset(void)
{
    _wait_parse_idle();

    hmi_cmd_send("reset();");
    return hmi_cmd_wait_parse(RET_RST);
}

bool api_ack(void)
{
    _wait_parse_idle();

    hmi_cmd_send("ack();");

    return hmi_cmd_wait_parse(RET_ACK);
}

bool api_version(void)
{
    _wait_parse_idle();

    hmi_cmd_send("version();");
    return hmi_cmd_wait_parse(RET_VERSION);
}

bool api_caddt(char *name, uint8_t ch, uint16_t points)
{
    _wait_parse_idle();

    char buf[128];
    snprintf(buf, 128, "caddt(%s,%d,%d);", name, ch, points);
    hmi_cmd_send(buf);
    return hmi_cmd_wait_parse(RET_CADDT);
}

bool api_get_code(void)
{
    _wait_parse_idle();

    hmi_cmd_send("get_code();");
    return hmi_cmd_wait_parse(RET_GET_CODE);
}


/******************************************************************************/
bool hmi_cmd_wait_parse(enum back_cmd cmd_code)
{
    hmi_rx_parse_set_cmd_code(cmd_code);

    uint32_t tick0 = HAL_GetTick();
    while (1)
    {
        int res = hmi_rx_parse_task();
        if (res >= 0)
        {
            if (res == cmd_code) return true;
            else return false;
        }

        if (HAL_GetTick() - tick0 > DEFAULT_CMD_RX_TIMEOUT)
        {
            return false;
        }
    }
}

static void _wait_parse_idle(void)
{
    while (hmi_rx_parse_task() != -2) {};
}


/*******************************END OF FILE************************************/



