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.