1 /* USB console example
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
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.
8 */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <fcntl.h>
13 #include "esp_system.h"
14 #include "esp_log.h"
15 #include "esp_console.h"
16 #include "linenoise/linenoise.h"
17 #include "argtable3/argtable3.h"
18 #include "cmd_nvs.h"
19 #include "cmd_system.h"
20 #include "esp_vfs_cdcacm.h"
21 #include "nvs.h"
22 #include "nvs_flash.h"
23
initialize_nvs(void)24 static void initialize_nvs(void)
25 {
26 esp_err_t err = nvs_flash_init();
27 if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
28 ESP_ERROR_CHECK( nvs_flash_erase() );
29 err = nvs_flash_init();
30 }
31 ESP_ERROR_CHECK(err);
32 }
33
initialize_console(void)34 static void initialize_console(void)
35 {
36 /* Disable buffering on stdin */
37 setvbuf(stdin, NULL, _IONBF, 0);
38
39 /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
40 esp_vfs_dev_cdcacm_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
41 /* Move the caret to the beginning of the next line on '\n' */
42 esp_vfs_dev_cdcacm_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
43
44 /* Enable non-blocking mode on stdin and stdout */
45 fcntl(fileno(stdout), F_SETFL, 0);
46 fcntl(fileno(stdin), F_SETFL, 0);
47
48 /* Initialize the console */
49 esp_console_config_t console_config = {
50 .max_cmdline_args = 8,
51 .max_cmdline_length = 256,
52 #if CONFIG_LOG_COLORS
53 .hint_color = atoi(LOG_COLOR_CYAN)
54 #endif
55 };
56 ESP_ERROR_CHECK( esp_console_init(&console_config) );
57
58 /* Configure linenoise line completion library */
59 /* Enable multiline editing. If not set, long commands will scroll within
60 * single line.
61 */
62 linenoiseSetMultiLine(1);
63
64 /* Tell linenoise where to get command completions and hints */
65 linenoiseSetCompletionCallback(&esp_console_get_completion);
66 linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
67
68 /* Set command history size */
69 linenoiseHistorySetMaxLen(10);
70 }
71
app_main(void)72 void app_main(void)
73 {
74 initialize_nvs();
75
76 initialize_console();
77
78 /* Register commands */
79 esp_console_register_help_command();
80 register_system_common();
81 register_system_sleep();
82 register_nvs();
83
84 /* Prompt to be printed before each line.
85 * This can be customized, made dynamic, etc.
86 */
87 const char* prompt = LOG_COLOR_I CONFIG_IDF_TARGET "> " LOG_RESET_COLOR;
88
89 printf("\n"
90 "This is an example of ESP-IDF console component.\n"
91 "Type 'help' to get the list of commands.\n"
92 "Use UP/DOWN arrows to navigate through command history.\n"
93 "Press TAB when typing command name to auto-complete.\n");
94
95 /* Figure out if the terminal supports escape sequences */
96 int probe_status = linenoiseProbe();
97 if (probe_status) { /* zero indicates success */
98 printf("\n"
99 "Your terminal application does not support escape sequences.\n"
100 "Line editing and history features are disabled.\n"
101 "On Windows, try using Putty instead.\n");
102 linenoiseSetDumbMode(1);
103 #if CONFIG_LOG_COLORS
104 /* Since the terminal doesn't support escape sequences,
105 * don't use color codes in the prompt.
106 */
107 prompt = CONFIG_IDF_TARGET "> ";
108 #endif //CONFIG_LOG_COLORS
109 }
110
111 /* Main loop */
112 while(true) {
113 /* Get a line using linenoise.
114 * The line is returned when ENTER is pressed.
115 */
116 char* line = linenoise(prompt);
117 if (line == NULL) { /* Ignore empty lines */
118 continue;
119 }
120 /* Add the command to the history */
121 linenoiseHistoryAdd(line);
122
123 /* Try to run the command */
124 int ret;
125 esp_err_t err = esp_console_run(line, &ret);
126 if (err == ESP_ERR_NOT_FOUND) {
127 printf("Unrecognized command\n");
128 } else if (err == ESP_ERR_INVALID_ARG) {
129 // command was empty
130 } else if (err == ESP_OK && ret != ESP_OK) {
131 printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(ret));
132 } else if (err != ESP_OK) {
133 printf("Internal error: %s\n", esp_err_to_name(err));
134 }
135 /* linenoise allocates line buffer on the heap, so need to free it */
136 linenoiseFree(line);
137 }
138 }
139