3 This example code is in the Public Domain (or CC0 licensed, at your option.)
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
12 #include "esp_system.h"
14 #include "esp_console.h"
15 #include "esp_vfs_dev.h"
16 #include "driver/uart.h"
17 #include "linenoise/linenoise.h"
18 #include "argtable3/argtable3.h"
20 #include "esp_vfs_fat.h"
22 #include "nvs_flash.h"
24 static const char* TAG = "example";
26 /* Console command history can be stored to and loaded from a file.
27 * The easiest way to do this is to use FATFS filesystem on top of
28 * wear_levelling library.
30 #if CONFIG_STORE_HISTORY
32 #define MOUNT_PATH "/data"
33 #define HISTORY_PATH MOUNT_PATH "/history.txt"
35 static void initialize_filesystem()
37 static wl_handle_t wl_handle;
38 const esp_vfs_fat_mount_config_t mount_config = {
40 .format_if_mount_failed = true
42 esp_err_t err = esp_vfs_fat_spiflash_mount(MOUNT_PATH, "storage", &mount_config, &wl_handle);
44 ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
48 #endif // CONFIG_STORE_HISTORY
50 static void initialize_nvs()
52 esp_err_t err = nvs_flash_init();
53 if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
54 ESP_ERROR_CHECK( nvs_flash_erase() );
55 err = nvs_flash_init();
60 static void initialize_console()
62 /* Disable buffering on stdin and stdout */
63 setvbuf(stdin, NULL, _IONBF, 0);
64 setvbuf(stdout, NULL, _IONBF, 0);
66 /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
67 esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
68 /* Move the caret to the beginning of the next line on '\n' */
69 esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
71 /* Install UART driver for interrupt-driven reads and writes */
72 ESP_ERROR_CHECK( uart_driver_install(CONFIG_CONSOLE_UART_NUM,
73 256, 0, 0, NULL, 0) );
75 /* Tell VFS to use UART driver */
76 esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM);
78 /* Initialize the console */
79 esp_console_config_t console_config = {
80 .max_cmdline_args = 8,
81 .max_cmdline_length = 256,
83 .hint_color = atoi(LOG_COLOR_CYAN)
86 ESP_ERROR_CHECK( esp_console_init(&console_config) );
88 /* Configure linenoise line completion library */
89 /* Enable multiline editing. If not set, long commands will scroll within
92 linenoiseSetMultiLine(1);
94 /* Tell linenoise where to get command completions and hints */
95 linenoiseSetCompletionCallback(&esp_console_get_completion);
96 linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
98 /* Set command history size */
99 linenoiseHistorySetMaxLen(100);
101 #if CONFIG_STORE_HISTORY
102 /* Load command history from filesystem */
103 linenoiseHistoryLoad(HISTORY_PATH);
111 #if CONFIG_STORE_HISTORY
112 initialize_filesystem();
115 initialize_console();
117 /* Register commands */
118 esp_console_register_help_command();
122 /* Prompt to be printed before each line.
123 * This can be customized, made dynamic, etc.
125 const char* prompt = LOG_COLOR_I "esp32> " LOG_RESET_COLOR;
128 "This is an example of ESP-IDF console component.\n"
129 "Type 'help' to get the list of commands.\n"
130 "Use UP/DOWN arrows to navigate through command history.\n"
131 "Press TAB when typing command name to auto-complete.\n");
133 /* Figure out if the terminal supports escape sequences */
134 int probe_status = linenoiseProbe();
135 if (probe_status) { /* zero indicates success */
137 "Your terminal application does not support escape sequences.\n"
138 "Line editing and history features are disabled.\n"
139 "On Windows, try using Putty instead.\n");
140 linenoiseSetDumbMode(1);
141 #if CONFIG_LOG_COLORS
142 /* Since the terminal doesn't support escape sequences,
143 * don't use color codes in the prompt.
146 #endif //CONFIG_LOG_COLORS
151 /* Get a line using linenoise.
152 * The line is returned when ENTER is pressed.
154 char* line = linenoise(prompt);
155 if (line == NULL) { /* Ignore empty lines */
158 /* Add the command to the history */
159 linenoiseHistoryAdd(line);
160 #if CONFIG_STORE_HISTORY
161 /* Save command history to filesystem */
162 linenoiseHistorySave(HISTORY_PATH);
165 /* Try to run the command */
167 esp_err_t err = esp_console_run(line, &ret);
168 if (err == ESP_ERR_NOT_FOUND) {
169 printf("Unrecognized command\n");
170 } else if (err == ESP_ERR_INVALID_ARG) {
172 } else if (err == ESP_OK && ret != ESP_OK) {
173 printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(err));
174 } else if (err != ESP_OK) {
175 printf("Internal error: %s\n", esp_err_to_name(err));
177 /* linenoise allocates line buffer on the heap, so need to free it */