Select one of the symbols to view example projects that use it.
 
Outline
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "esp_system.h"
#include "esp_log.h"
#include "esp_console.h"
#include "linenoise/linenoise.h"
#include "argtable3/argtable3.h"
#include "esp_vfs_fat.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "soc/soc_caps.h"
#include "cmd_system.h"
#include "cmd_wifi.h"
#include "cmd_nvs.h"
#include "console_settings.h"
TAG
#define PROMPT_STR
#define MOUNT_PATH
#define HISTORY_PATH
initialize_filesystem()
#define HISTORY_PATH
initialize_nvs()
app_main()
Files
loading...
SourceVuESP-IDF Framework and Examplesadvanced samplemain/console_example_main.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ #include <stdio.h> #include <string.h> #include <unistd.h> #include "esp_system.h" #include "esp_log.h" #include "esp_console.h" #include "linenoise/linenoise.h" #include "argtable3/argtable3.h" #include "esp_vfs_fat.h" #include "nvs.h" #include "nvs_flash.h" #include "soc/soc_caps.h" #include "cmd_system.h" #include "cmd_wifi.h" #include "cmd_nvs.h" #include "console_settings.h"16 includes /* * We warn if a secondary serial console is enabled. A secondary serial console is always output-only and * hence not very useful for interactive console applications. If you encounter this warning, consider disabling * the secondary serial console in menuconfig unless you know what you are doing. *//* ... */ #if SOC_USB_SERIAL_JTAG_SUPPORTED #if !CONFIG_ESP_CONSOLE_SECONDARY_NONE #warning "A secondary serial console is not useful when using the console component. Please disable it in menuconfig." #endif/* ... */ #endif static const char* TAG = "example"; #define PROMPT_STR CONFIG_IDF_TARGET /* Console command history can be stored to and loaded from a file. * The easiest way to do this is to use FATFS filesystem on top of * wear_levelling library. *//* ... */ #if CONFIG_CONSOLE_STORE_HISTORY #define MOUNT_PATH "/data" #define HISTORY_PATH MOUNT_PATH "/history.txt" static void initialize_filesystem(void) { static wl_handle_t wl_handle; const esp_vfs_fat_mount_config_t mount_config = { .max_files = 4, .format_if_mount_failed = true }{...}; esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_PATH, "storage", &mount_config, &wl_handle); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err)); return; }{...} }{ ... } #else/* ... */ #define HISTORY_PATH NULL #endif // CONFIG_CONSOLE_STORE_HISTORY static void initialize_nvs(void) { esp_err_t err = nvs_flash_init(); if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK( nvs_flash_erase() ); err = nvs_flash_init(); }{...} ESP_ERROR_CHECK(err); }{ ... } void app_main(void) { initialize_nvs(); #if CONFIG_CONSOLE_STORE_HISTORY initialize_filesystem(); ESP_LOGI(TAG, "Command history enabled");/* ... */ #else ESP_LOGI(TAG, "Command history disabled"); #endif /* Initialize console output periheral (UART, USB_OTG, USB_JTAG) */ initialize_console_peripheral(); /* Initialize linenoise library and esp_console*/ initialize_console_library(HISTORY_PATH); /* Prompt to be printed before each line. * This can be customized, made dynamic, etc. *//* ... */ const char *prompt = setup_prompt(PROMPT_STR ">"); /* Register commands */ esp_console_register_help_command(); register_system_common(); #if SOC_LIGHT_SLEEP_SUPPORTED register_system_light_sleep(); #endif #if SOC_DEEP_SLEEP_SUPPORTED register_system_deep_sleep(); #endif #if (CONFIG_ESP_WIFI_ENABLED || CONFIG_ESP_HOST_WIFI_ENABLED) register_wifi(); #endif register_nvs(); printf("\n" "This is an example of ESP-IDF console component.\n" "Type 'help' to get the list of commands.\n" "Use UP/DOWN arrows to navigate through command history.\n" "Press TAB when typing command name to auto-complete.\n" "Ctrl+C will terminate the console environment.\n"); if (linenoiseIsDumbMode()) { printf("\n" "Your terminal application does not support escape sequences.\n" "Line editing and history features are disabled.\n" "On Windows, try using Putty instead.\n"); }{...} /* Main loop */ while(true) { /* Get a line using linenoise. * The line is returned when ENTER is pressed. *//* ... */ char* line = linenoise(prompt); #if CONFIG_CONSOLE_IGNORE_EMPTY_LINES if (line == NULL) { /* Ignore empty lines */ continue;; }{...} #else/* ... */ if (line == NULL) { /* Break on EOF or error */ break; }{...} #endif/* ... */ // CONFIG_CONSOLE_IGNORE_EMPTY_LINES /* Add the command to the history if not empty*/ if (strlen(line) > 0) { linenoiseHistoryAdd(line); #if CONFIG_CONSOLE_STORE_HISTORY /* Save command history to filesystem */ linenoiseHistorySave(HISTORY_PATH);/* ... */ #endif // CONFIG_CONSOLE_STORE_HISTORY }{...} /* Try to run the command */ int ret; esp_err_t err = esp_console_run(line, &ret); if (err == ESP_ERR_NOT_FOUND) { printf("Unrecognized command\n"); }{...} else if (err == ESP_ERR_INVALID_ARG) { // command was empty }{...} else if (err == ESP_OK && ret != ESP_OK) { printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(ret)); }{...} else if (err != ESP_OK) { printf("Internal error: %s\n", esp_err_to_name(err)); }{...} /* linenoise allocates line buffer on the heap, so need to free it */ linenoiseFree(line); }{...} ESP_LOGE(TAG, "Error or end-of-input, terminating console"); esp_console_deinit(); }{ ... }
Details