mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
148 lines
4.5 KiB
C
148 lines
4.5 KiB
C
|
/* Console example
|
||
|
|
||
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||
|
|
||
|
Unless required by applicable law or agreed to in writing, this
|
||
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||
|
CONDITIONS OF ANY KIND, either express or implied.
|
||
|
*/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include "esp_system.h"
|
||
|
#include "esp_log.h"
|
||
|
#include "esp_console.h"
|
||
|
#include "esp_vfs_dev.h"
|
||
|
#include "driver/uart.h"
|
||
|
#include "linenoise/linenoise.h"
|
||
|
#include "argtable3/argtable3.h"
|
||
|
#include "cmd_decl.h"
|
||
|
#include "esp_vfs_fat.h"
|
||
|
|
||
|
static const char* TAG = "example";
|
||
|
|
||
|
/* 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_STORE_HISTORY
|
||
|
|
||
|
#define MOUNT_PATH "/data"
|
||
|
#define HISTORY_PATH MOUNT_PATH "/history.txt"
|
||
|
|
||
|
static void initialize_filesystem()
|
||
|
{
|
||
|
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(MOUNT_PATH, "storage", &mount_config, &wl_handle);
|
||
|
if (err != ESP_OK) {
|
||
|
ESP_LOGE(TAG, "Failed to mount FATFS (0x%x)", err);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
#endif // CONFIG_STORE_HISTORY
|
||
|
|
||
|
static void initialize_console()
|
||
|
{
|
||
|
/* Disable buffering on stdin and stdout */
|
||
|
setvbuf(stdin, NULL, _IONBF, 0);
|
||
|
setvbuf(stdout, NULL, _IONBF, 0);
|
||
|
|
||
|
/* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
|
||
|
esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
|
||
|
/* Move the caret to the beginning of the next line on '\n' */
|
||
|
esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
|
||
|
|
||
|
/* Install UART driver for interrupt-driven reads and writes */
|
||
|
ESP_ERROR_CHECK( uart_driver_install(CONFIG_CONSOLE_UART_NUM,
|
||
|
256, 0, 0, NULL, 0) );
|
||
|
|
||
|
/* Tell VFS to use UART driver */
|
||
|
esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM);
|
||
|
|
||
|
/* Initialize the console */
|
||
|
esp_console_config_t console_config = {
|
||
|
.max_cmdline_args = 8,
|
||
|
.max_cmdline_length = 256,
|
||
|
#if CONFIG_LOG_COLORS
|
||
|
.hint_color = atoi(LOG_COLOR_CYAN)
|
||
|
#endif
|
||
|
};
|
||
|
ESP_ERROR_CHECK( esp_console_init(&console_config) );
|
||
|
|
||
|
/* Configure linenoise line completion library */
|
||
|
/* Enable multiline editing. If not set, long commands will scroll within
|
||
|
* single line.
|
||
|
*/
|
||
|
linenoiseSetMultiLine(1);
|
||
|
|
||
|
/* Tell linenoise where to get command completions and hints */
|
||
|
linenoiseSetCompletionCallback(&esp_console_get_completion);
|
||
|
linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
|
||
|
|
||
|
/* Set command history size */
|
||
|
linenoiseHistorySetMaxLen(100);
|
||
|
|
||
|
#if CONFIG_STORE_HISTORY
|
||
|
/* Load command history from filesystem */
|
||
|
linenoiseHistoryLoad(HISTORY_PATH);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
void app_main()
|
||
|
{
|
||
|
#if CONFIG_STORE_HISTORY
|
||
|
initialize_filesystem();
|
||
|
#endif
|
||
|
|
||
|
initialize_console();
|
||
|
|
||
|
/* Register commands */
|
||
|
esp_console_register_help_command();
|
||
|
register_system();
|
||
|
register_wifi();
|
||
|
|
||
|
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");
|
||
|
|
||
|
/* Prompt to be printed before each line.
|
||
|
* This can be customized, made dynamic, etc.
|
||
|
*/
|
||
|
const char* prompt = LOG_COLOR_I "[esp32]> " LOG_RESET_COLOR;
|
||
|
|
||
|
/* Main loop */
|
||
|
char *line;
|
||
|
/* Get a line using linenoise.
|
||
|
* The line is returned when ENTER is pressed.
|
||
|
*/
|
||
|
while((line = linenoise(prompt)) != NULL) {
|
||
|
if (strlen(line) > 0) { /* Ignore empty lines */
|
||
|
/* Add the command to the history */
|
||
|
linenoiseHistoryAdd(line);
|
||
|
#if CONFIG_STORE_HISTORY
|
||
|
/* Save command history to filesystem */
|
||
|
linenoiseHistorySave(HISTORY_PATH);
|
||
|
#endif
|
||
|
|
||
|
/* 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_OK && ret != ESP_OK) {
|
||
|
printf("Command returned non-zero error code: 0x%x\n", ret);
|
||
|
} else if (err != ESP_OK) {
|
||
|
printf("Internal error: 0x%x\n", err);
|
||
|
}
|
||
|
}
|
||
|
/* linenoise allocates line buffer on the heap, so need to free it */
|
||
|
linenoiseFree(line);
|
||
|
}
|
||
|
}
|