console_example_main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. static const char* TAG = "example";
  21. /* Console command history can be stored to and loaded from a file.
  22. * The easiest way to do this is to use FATFS filesystem on top of
  23. * wear_levelling library.
  24. */
  25. #if CONFIG_STORE_HISTORY
  26. #define MOUNT_PATH "/data"
  27. #define HISTORY_PATH MOUNT_PATH "/history.txt"
  28. static void initialize_filesystem()
  29. {
  30. static wl_handle_t wl_handle;
  31. const esp_vfs_fat_mount_config_t mount_config = {
  32. .max_files = 4,
  33. .format_if_mount_failed = true
  34. };
  35. esp_err_t err = esp_vfs_fat_spiflash_mount(MOUNT_PATH, "storage", &mount_config, &wl_handle);
  36. if (err != ESP_OK) {
  37. ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
  38. return;
  39. }
  40. }
  41. #endif // CONFIG_STORE_HISTORY
  42. static void initialize_nvs()
  43. {
  44. esp_err_t err = nvs_flash_init();
  45. if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
  46. ESP_ERROR_CHECK( nvs_flash_erase() );
  47. err = nvs_flash_init();
  48. }
  49. ESP_ERROR_CHECK(err);
  50. }
  51. static void initialize_console()
  52. {
  53. /* Disable buffering on stdin and stdout */
  54. setvbuf(stdin, NULL, _IONBF, 0);
  55. setvbuf(stdout, NULL, _IONBF, 0);
  56. /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
  57. esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
  58. /* Move the caret to the beginning of the next line on '\n' */
  59. esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
  60. /* Install UART driver for interrupt-driven reads and writes */
  61. ESP_ERROR_CHECK( uart_driver_install(CONFIG_CONSOLE_UART_NUM,
  62. 256, 0, 0, NULL, 0) );
  63. /* Tell VFS to use UART driver */
  64. esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM);
  65. /* Initialize the console */
  66. esp_console_config_t console_config = {
  67. .max_cmdline_args = 8,
  68. .max_cmdline_length = 256,
  69. #if CONFIG_LOG_COLORS
  70. .hint_color = atoi(LOG_COLOR_CYAN)
  71. #endif
  72. };
  73. ESP_ERROR_CHECK( esp_console_init(&console_config) );
  74. /* Configure linenoise line completion library */
  75. /* Enable multiline editing. If not set, long commands will scroll within
  76. * single line.
  77. */
  78. linenoiseSetMultiLine(1);
  79. /* Tell linenoise where to get command completions and hints */
  80. linenoiseSetCompletionCallback(&esp_console_get_completion);
  81. linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
  82. /* Set command history size */
  83. linenoiseHistorySetMaxLen(100);
  84. #if CONFIG_STORE_HISTORY
  85. /* Load command history from filesystem */
  86. linenoiseHistoryLoad(HISTORY_PATH);
  87. #endif
  88. }
  89. void app_main()
  90. {
  91. initialize_nvs();
  92. #if CONFIG_STORE_HISTORY
  93. initialize_filesystem();
  94. #endif
  95. initialize_console();
  96. /* Register commands */
  97. esp_console_register_help_command();
  98. register_system();
  99. register_wifi();
  100. /* Prompt to be printed before each line.
  101. * This can be customized, made dynamic, etc.
  102. */
  103. const char* prompt = LOG_COLOR_I "esp32> " LOG_RESET_COLOR;
  104. printf("\n"
  105. "This is an example of ESP-IDF console component.\n"
  106. "Type 'help' to get the list of commands.\n"
  107. "Use UP/DOWN arrows to navigate through command history.\n"
  108. "Press TAB when typing command name to auto-complete.\n");
  109. /* Figure out if the terminal supports escape sequences */
  110. int probe_status = linenoiseProbe();
  111. if (probe_status) { /* zero indicates success */
  112. printf("\n"
  113. "Your terminal application does not support escape sequences.\n"
  114. "Line editing and history features are disabled.\n"
  115. "On Windows, try using Putty instead.\n");
  116. linenoiseSetDumbMode(1);
  117. #if CONFIG_LOG_COLORS
  118. /* Since the terminal doesn't support escape sequences,
  119. * don't use color codes in the prompt.
  120. */
  121. prompt = "esp32> ";
  122. #endif //CONFIG_LOG_COLORS
  123. }
  124. /* Main loop */
  125. while(true) {
  126. /* Get a line using linenoise.
  127. * The line is returned when ENTER is pressed.
  128. */
  129. char* line = linenoise(prompt);
  130. if (line == NULL) { /* Ignore empty lines */
  131. continue;
  132. }
  133. /* Add the command to the history */
  134. linenoiseHistoryAdd(line);
  135. #if CONFIG_STORE_HISTORY
  136. /* Save command history to filesystem */
  137. linenoiseHistorySave(HISTORY_PATH);
  138. #endif
  139. /* Try to run the command */
  140. int ret;
  141. esp_err_t err = esp_console_run(line, &ret);
  142. if (err == ESP_ERR_NOT_FOUND) {
  143. printf("Unrecognized command\n");
  144. } else if (err == ESP_ERR_INVALID_ARG) {
  145. // command was empty
  146. } else if (err == ESP_OK && ret != ESP_OK) {
  147. printf("Command returned non-zero error code: 0x%x (%s)\n", ret, esp_err_to_name(err));
  148. } else if (err != ESP_OK) {
  149. printf("Internal error: %s\n", esp_err_to_name(err));
  150. }
  151. /* linenoise allocates line buffer on the heap, so need to free it */
  152. linenoiseFree(line);
  153. }
  154. }