【嵌入式】LIBSCLI - Simple Command Line Interface

Repository link: proj.tq.libscli: simple command-line interfacehttps://gitee.com/tq797/proj.tq.libscli

SCLI is a command line interface designed for MCU systems. Usage example, on PC with default configuration(use heap and binary tree search method):

scli_cmd_t scli_cmd_test = 
{
    .name = "test",
    .handler = cmd_handler,
    .hinter = NULL,
};

static void cmd_handler(char *params)
{
    SCLI_LOGI("test command, params:");
    char *next = params;
    uint8_t param[SCLI_MAX_WORD_LEN];
    next = scli_next_word(next, param);
    while (next) {
        next = scli_next_word(next, param);
        SCLI_LOGI("\"%s\"", param);
    }
}

int main()
{

    // hooks will be copied, so use local variables to save memory
    scli_env_hooks_t s_scli_hooks =
    {
#if SCLI_USE_MALLOC == 1
        .malloc = (malloc_ft)malloc,
        .free   = (free_ft)free,
#endif
        .log    = (log_ft)printf,
        .msleep = (msleep_ft)msleep,
    };

    scli_term_hooks_t s_term_hooks =
    {
        .output_c = output_c,
        .output_s = output_s,
    };

    scli_init(&s_scli_hooks, SCLI_LOG_LEVEL_INFO);

#if SCLI_USE_MALLOC == 1
    scli_term_t *term = scli_create_terminal(&s_term_hooks, "testerm", "/");
    if (term) {
        disable_terminal_return();
    } else {
        SCLI_LOGI("Create terminal failed\n");
        return -1;
    }

    scli_register_cmd(term, &scli_cmd_test);
    scli_terminal_start(term);
#else
    static scli_term_t term;
    disable_terminal_return();
    scli_terminal_init(&term, &s_term_hooks, "term_without_malloc", (char *)"/");

    scli_register_cmd(&term, &scli_cmd_test);
    scli_terminal_start(&term);
#endif

    
    while (true) {
        char c = input_c();
        // SCLI_LOGI("char:'%c', 0x%.2X", c, c);
#if SCLI_USE_MALLOC == 1
        scli_terminal_input(term, c);
#else
        scli_terminal_input(&term, c);
#endif
        // p_hooks->output_c(c);
        // SCLI_LOGE("%c", c);
        msleep(10);
    }
    return 0;
}

On STM32F030(with limited RAM and ROM space), with configurations as follow(do not use heap and scli_debug):

#ifndef __LIBSCLI_CONFIG_H__
#define __LIBSCLI_CONFIG_H__

#define SCLI_VERSION_MAJOR      1
#define SCLI_VERSION_MINOR      3
#define SCLI_VERSION_PATCH      0

#define SCLI_USE_HINT           0
#define SCLI_USE_MALLOC         1       // Use dynamic memory allocation for command nodes otherwise static command array is used
#define SCLI_USE_BINARY_TREE    1       // Use binary search tree otherwise link list is used
#define SCLI_USE_HISTORY        0
#define SCLI_USE_SCLI_DEBUG     1

#define SCLI_MAX_DIR_LEN        64
#define SCLI_MAX_IBUF_LEN       64
#define SCLI_MAX_HBUF_LEN       256
#define SCLI_MAX_WORD_LEN       32
#if SCLI_USE_MALLOC == 0
#define SCLI_MAX_CMD_LIST       16      // Maximum number of commands of a scli_terminal
#endif

#endif

Running result:

Be warned, ARMCC in MDK doesn't recognize escape sequence \e, so use \x1B instead when using MDK.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值