console_example_main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Console example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "esp_system.h"
  10. #include "esp_log.h"
  11. #include "esp_console.h"
  12. #include "esp_vfs_dev.h"
  13. #include "driver/uart.h"
  14. #include "linenoise/linenoise.h"
  15. #include "argtable3/argtable3.h"
  16. #include "cmd_decl.h"
  17. #include "esp_vfs_fat.h"
  18. #include "nvs.h"
  19. #include "nvs_flash.h"
  20. #ifdef CONFIG_ESP_CONSOLE_USB_CDC
  21. #error This example is incompatible with USB CDC console. Please try "console_usb" example instead.
  22. #endif // CONFIG_ESP_CONSOLE_USB_CDC
  23. static const char* TAG = "example";
  24. #define PROMPT_STR CONFIG_IDF_TARGET
  25. /* Console command history can be stored to and loaded from a file.
  26. * The easiest way to do this is to use FATFS filesystem on top of
  27. * wear_levelling library.
  28. */
  29. #if CONFIG_STORE_HISTORY
  30. #define MOUNT_PATH "/data"
  31. #define HISTORY_PATH MOUNT_PATH "/history.txt"
  32. static void initialize_filesystem(void)
  33. {
  34. static wl_handle_t wl_handle;
  35. const esp_vfs_fat_mount_config_t mount_config = {
  36. .max_files = 4,
  37. .format_if_mount_failed = true
  38. };
  39. esp_err_t err = esp_vfs_fat_spiflash_mount(MOUNT_PATH, "storage", &mount_config, &wl_handle);
  40. if (err != ESP_OK) {
  41. ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
  42. return;
  43. }
  44. }
  45. #endif // CONFIG_STORE_HISTORY
  46. static void initialize_nvs(void)
  47. {
  48. esp_err_t err = nvs_flash_init();
  49. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  50. ESP_ERROR_CHECK( nvs_flash_erase() );
  51. err = nvs_flash_init();
  52. }
  53. ESP_ERROR_CHECK(err);
  54. }
  55. static void initialize_console(void)
  56. {
  57. /* Drain stdout before reconfiguring it */
  58. fflush(stdout);
  59. fsync(fileno(stdout));
  60. /* Disable buffering on stdin */
  61. setvbuf(stdin, NULL, _IONBF, 0);
  62. /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
  63. esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
  64. /* Move the caret to the beginning of the next line on '\n' */
  65. esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
  66. /* Configure UART. Note that REF_TICK is used so that the baud rate remains
  67. * correct while APB frequency is changing in light sleep mode.
  68. */
  69. const uart_config_t uart_config = {
  70. .baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
  71. .data_bits = UART_DATA_8_BITS,
  72. .parity = UART_PARITY_DISABLE,
  73. .stop_bits = UART_STOP_BITS_1,
  74. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
  75. .source_clk = UART_SCLK_REF_TICK,
  76. #else
  77. .source_clk = UART_SCLK_XTAL,
  78. #endif
  79. };
  80. /* Install UART driver for interrupt-driven reads and writes */
  81. ESP_ERROR_CHECK( uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM,
  82. 256, 0, 0, NULL, 0) );
  83. ESP_ERROR_CHECK( uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config) );
  84. /* Tell VFS to use UART driver */
  85. esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
  86. /* Initialize the console */
  87. esp_console_config_t console_config = {
  88. .max_cmdline_args = 8,
  89. .max_cmdline_length = 256,
  90. #if CONFIG_LOG_COLORS
  91. .hint_color = atoi(LOG_COLOR_CYAN)
  92. #endif
  93. };
  94. ESP_ERROR_CHECK( esp_console_init(&console_config) );
  95. /* Configure linenoise line completion library */
  96. /* Enable multiline editing. If not set, long commands will scroll within
  97. * single line.
  98. */
  99. linenoiseSetMultiLine(1);
  100. /* Tell linenoise where to get command completions and hints */
  101. linenoiseSetCompletionCallback(&esp_console_get_completion);
  102. linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
  103. /* Set command history size */
  104. linenoiseHistorySetMaxLen(100);
  105. /* Set command maximum length */
  106. linenoiseSetMaxLineLen(console_config.max_cmdline_length);
  107. /* Don't return empty lines */
  108. linenoiseAllowEmpty(false);
  109. #if CONFIG_STORE_HISTORY
  110. /* Load command history from filesystem */
  111. linenoiseHistoryLoad(HISTORY_PATH);
  112. #endif
  113. }
  114. void app_main(void)
  115. {
  116. initialize_nvs();
  117. #if CONFIG_STORE_HISTORY
  118. initialize_filesystem();
  119. ESP_LOGI(TAG, "Command history enabled");
  120. #else
  121. ESP_LOGI(TAG, "Command history disabled");
  122. #endif
  123. initialize_console();
  124. /* Register commands */
  125. esp_console_register_help_command();
  126. register_system();
  127. register_wifi();
  128. register_nvs();
  129. /* Prompt to be printed before each line.
  130. * This can be customized, made dynamic, etc.
  131. */
  132. const char* prompt = LOG_COLOR_I PROMPT_STR "> " LOG_RESET_COLOR;
  133. printf("\n"
  134. "This is an example of ESP-IDF console component.\n"
  135. "Type 'help' to get the list of commands.\n"
  136. "Use UP/DOWN arrows to navigate through command history.\n"
  137. "Press TAB when typing command name to auto-complete.\n"
  138. "Press Enter or Ctrl+C will terminate the console environment.\n");
  139. /* Figure out if the terminal supports escape sequences */
  140. int probe_status = linenoiseProbe();
  141. if (probe_status) { /* zero indicates success */
  142. printf("\n"
  143. "Your terminal application does not support escape sequences.\n"
  144. "Line editing and history features are disabled.\n"
  145. "On Windows, try using Putty instead.\n");
  146. linenoiseSetDumbMode(1);
  147. #if CONFIG_LOG_COLORS
  148. /* Since the terminal doesn't support escape sequences,
  149. * don't use color codes in the prompt.
  150. */
  151. prompt = PROMPT_STR "> ";
  152. #endif //CONFIG_LOG_COLORS
  153. }
  154. /* Main loop */
  155. while(true) {
  156. /* Get a line using linenoise.
  157. * The line is returned when ENTER is pressed.
  158. */
  159. char* line = linenoise(prompt);
  160. if (line == NULL) { /* Break on EOF or error */
  161. break;
  162. }
  163. /* Add the command to the history if not empty*/
  164. if (strlen(line) > 0) {
  165. linenoiseHistoryAdd(line);
  166. #if CONFIG_STORE_HISTORY
  167. /* Save command history to filesystem */
  168. linenoiseHistorySave(HISTORY_PATH);
  169. #endif
  170. }
  171. /* Try to run the command */
  172. int ret;
  173. esp_err_t err = esp_console_run(line, &ret);
  174. if (err == ESP_ERR_NOT_FOUND) {
  175. printf("Unrecognized command\n");
  176. } else if (err == ESP_ERR_INVALID_ARG) {
  177. // command was empty
  178. } else if (err == ESP_OK && ret != ESP_OK) {
  179. printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(ret));
  180. } else if (err != ESP_OK) {
  181. printf("Internal error: %s\n", esp_err_to_name(err));
  182. }
  183. /* linenoise allocates line buffer on the heap, so need to free it */
  184. linenoiseFree(line);
  185. }
  186. ESP_LOGE(TAG, "Error or end-of-input, terminating console");
  187. esp_console_deinit();
  188. }