ID.LODA 发表于 2020-9-3 20:13

【ESP32-S2-Kaluga-1测评】控制台组件的使用

之前安装环境已经搭建完成,现在就可以进行应用开发了。DEMO 的例程在 ESP32 上面应该都是可以运行的,但是 ESP32S2 上面存在兼容性的问题,有一些外设和蓝牙的例程会运行失败,我这边也没有全部测试。

## 控制台例程
类似于 linux 的控制台,可以通过输入指令的方式运行你期望的函数,前提功能实现需要提前预制,是非常实用的一个组件,只需要一个串口。

### 工程源码

### 控制台的初始化

```c
static void initialize_console(void)
{
    /* Drain stdout before reconfiguring it */
    fflush(stdout);
    fsync(fileno(stdout));

    /* Disable buffering on stdin */
    setvbuf(stdin, NULL, _IONBF, 0);

    /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
    esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
    /* Move the caret to the beginning of the next line on '\n' */
    esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);

    /* Configure UART. Note that REF_TICK is used so that the baud rate remains
   * correct while APB frequency is changing in light sleep mode.
   */
    const uart_config_t uart_config = {
            .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
            .data_bits = UART_DATA_8_BITS,
            .parity = UART_PARITY_DISABLE,
            .stop_bits = UART_STOP_BITS_1,
            .source_clk = UART_SCLK_REF_TICK,
    };
    /* Install UART driver for interrupt-driven reads and writes */
    ESP_ERROR_CHECK( uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM,
            256, 0, 0, NULL, 0) );
    ESP_ERROR_CHECK( uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config) );

    /* Tell VFS to use UART driver */
    esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);

    /* Initialize the console */
    esp_console_config_t console_config = {
            .max_cmdline_args = 8,
            .max_cmdline_length = 256,
#if CONFIG_LOG_COLORS
            .hint_color = atoi(LOG_COLOR_CYAN)
#endif
    };
    ESP_ERROR_CHECK( esp_console_init(&console_config) );

    /* Configure linenoise line completion library */
    /* Enable multiline editing. If not set, long commands will scroll within
   * single line.
   */
    linenoiseSetMultiLine(1);

    /* Tell linenoise where to get command completions and hints */
    linenoiseSetCompletionCallback(&esp_console_get_completion);
    linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);

    /* Set command history size */
    linenoiseHistorySetMaxLen(100);

    /* Don't return empty lines */
    linenoiseAllowEmpty(false);

#if CONFIG_STORE_HISTORY
    /* Load command history from filesystem */
    linenoiseHistoryLoad(HISTORY_PATH);
#endif
}
```

### 控制台函数注册
通过注册自己需要的命令,esp_console_cmd_register(),函数传参是一个 esp_console_cmd_t 的结构体
```c
typedef struct {
    /**
   * Command name. Must not be NULL, must not contain spaces.
   * The pointer must be valid until the call to esp_console_deinit.
   */
    const char *command;
    /**
   * Help text for the command, shown by help command.
   * If set, the pointer must be valid until the call to esp_console_deinit.
   * If not set, the command will not be listed in 'help' output.
   */
    const char *help;
    /**
   * Hint text, usually lists possible arguments.
   * If set to NULL, and 'argtable' field is non-NULL, hint will be generated
   * automatically
   */
    const char *hint;
    /**
   * Pointer to a function which implements the command.
   */
    esp_console_cmd_func_t func;
    /**
   * Array or structure of pointers to arg_xxx structures, may be NULL.
   * Used to generate hint text if 'hint' is set to NULL.
   * Array/structure which this field points to must end with an arg_end.
   * Only used for the duration of esp_console_cmd_register call.
   */
    void *argtable;
} esp_console_cmd_t;
```


### 运行演示
可以看到输入提前注册的指令之后就能运行想对应的函数

## 结尾
控制台可以脱离仿真器,辅助用户进行代码调试和问题检测,是一个非常不错的组件
页: [1]
查看完整版本: 【ESP32-S2-Kaluga-1测评】控制台组件的使用