#include "led_strip_shell.h"
#include "../pin_io/pin_io.h"

static int32_t s_value = 0;

SH_CMD_FN(set);
SH_CMD_FN(start);
SH_CMD_FN(clr);

//设置子命令
SH_DEF_SUB_CMD(
    sub_led,
    SH_SETUP_CMD("set", "点亮灯珠 <引脚号> <灯珠号> [红] [绿] [蓝]", set, NULL), //
    SH_SETUP_CMD("start", "开启按键检测 1:初始化 2:检测", start, NULL), //
    SH_SETUP_CMD("clr", "熄灭全部灯珠 <引脚号>", clr, NULL),                     //

);
//设置主命令
SH_DEF_CMD(
    _register_cmd,
    SH_SETUP_CMD("led", "操作灯带驱动", NULL, sub_led), //
);
//设置存储解析到的数据
static int _set_value(const char *argv[]) { return SET_VAR(&s_value, 0, 0x100); }

void led_strip_shell_register(void)
{
    sh_register_cmd(&_register_cmd);
}

void led_strip_shell_unregister(void)
{

}
SH_CMD_FN(set)
{
    uint32_t pin = 0, index = 0, red = 0, green = 0, blue = 0;
    if (argc < 5)
    {
        sh_echo(sh_hdl, "输入少于 5 个参数\r\n");
        return -1;
    }

    if (argc >= 1 && _set_value(&argv[0]))
        return -1;
    pin = *(volatile __typeof(s_value) *)&s_value;

    if (argc >= 2 && _set_value(&argv[1]))
        return -1;
    index = *(volatile __typeof(s_value) *)&s_value;

    if (argc >= 3 && _set_value(&argv[2]))
        return -1;
    red = *(volatile __typeof(s_value) *)&s_value;

    if (argc >= 4 && _set_value(&argv[3]))
        return -1;
    green = *(volatile __typeof(s_value) *)&s_value;

    if (argc >= 5 && _set_value(&argv[4]))
        return -1;
    blue = *(volatile __typeof(s_value) *)&s_value;

    led_strip_set_pixel(pin,index,green,red,blue);
    return 0;
}

SH_CMD_FN(start)
{
    static uint32_t start_flag;
    if (argc < 1)
    {
        sh_echo(sh_hdl, "输入少于 1 个参数\r\n");
        return -1;
    }

   if (argc >= 1 && _set_value(&argv[0]))
        return -1;
    start_flag = *(volatile __typeof(s_value) *)&s_value;

    if (start_flag == 1)
    {
        // pin_cfg_input(&g_cfg_board->key_reset);
    }
    else if (start_flag == 2)
    {
        // bool data_flag = pin_get_valid(&g_cfg_board->key_reset);
        // sh_echo(sh_hdl, "数据 %d\r\n", data_flag);
    }
    return 0;
}
SH_CMD_FN(clr)
{
    if (argc < 1)
    {
        sh_echo(sh_hdl, "输入少于 1 个参数\r\n");
        return -1;
    }
    return 0;
}