console: simplify examples

1. simplify console examples
2. add "quit" command
3. support console command overwrite
4. add API reference
This commit is contained in:
suda-morris
2020-02-03 18:01:04 +08:00
parent 13623ef430
commit 75cadc2e41
31 changed files with 794 additions and 1288 deletions

View File

@@ -36,8 +36,6 @@ To run this example, you should have one ESP32 dev board (e.g. ESP32-WROVER Kit)
Open the project configuration menu (`idf.py menuconfig`). Then go into `Example Configuration` menu.
- You can choose whether or not to save command history into flash in `Store command history in flash` option.
- You can set the maximum number of command line arguments under `Maximum number of command line arguments` option.
- You can set the command line buffer length under `Command line buffer length` option.
### Build and Flash
@@ -197,9 +195,6 @@ esp32> i2cget -c 0x5b -r 0x02 -l 8
* Reset you I2C device, and then run `i2cdetect` again.
* I cant get the right content when running `i2cdump` command.
* Currently the `i2cdump` only support those who have the same content length of registers inside the I2C device. For example, if a device have three register addresses, and the content length at these address are 1 byte, 2 bytes and 4 bytes. In this case you should not expect this command to dump the register correctly.
* I really input argument correctly, but the command line “discard” the last few arguements from time to time.
* Enlarge the maximum number of arguments in the menuconfig.
(For any technical queries, please open an [issue](https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you as soon as possible.)

View File

@@ -10,7 +10,7 @@ def test_i2ctools_example(env, extra_data):
# Get device under test, flash and start example. "i2ctool" must be defined in EnvConfig
dut = env.get_dut('i2ctools', 'examples/peripherals/i2c/i2c_tools', dut_class=ttfw_idf.ESP32DUT)
dut.start_app()
dut.expect("esp32>", timeout=EXPECT_TIMEOUT)
dut.expect("i2c-tools>", timeout=EXPECT_TIMEOUT)
# Get i2c address
dut.write("i2cdetect")
dut.expect("5b", timeout=EXPECT_TIMEOUT)

View File

@@ -7,19 +7,4 @@ menu "Example Configuration"
Linenoise line editing library provides functions to save and load
command history. If this option is enabled, initalizes a FAT filesystem
and uses it to store command history.
config EXAMPLE_MAX_CMD_ARGUMENTS
int "Maximum number of command line arguments"
default 16
range 8 256
help
maximum number of command line arguments to parse
config EXAMPLE_MAX_CMD_LENGTH
int "Command line buffer length"
default 256
range 256 512
help
length of command line buffer, in bytes
endmenu

View File

@@ -1,21 +0,0 @@
/* cmd_decl.h
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.
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include "cmd_system.h"
#include "cmd_i2ctools.h"
#ifdef __cplusplus
}
#endif

View File

@@ -9,18 +9,12 @@
#include <stdio.h>
#include <string.h>
#include "esp_system.h"
#include "sdkconfig.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"
#include "nvs.h"
#include "nvs_flash.h"
#include "sdkconfig.h"
#include "cmd_system.h"
#include "cmd_i2ctools.h"
static const char *TAG = "i2c-tools";
@@ -44,92 +38,22 @@ static void initialize_filesystem(void)
}
#endif // CONFIG_EXAMPLE_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);
}
static void initialize_console(void)
{
/* Disable buffering on stdin */
setvbuf(stdin, 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);
/* Configure UART. Note that REF_TICK is used so that the baud rate remains
* correct while APB frequency is changing in light sleep mode.
*/
const uart_config_t uart_config = {
.baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.source_clk = UART_SCLK_REF_TICK,
};
/* Install UART driver for interrupt-driven reads and writes */
ESP_ERROR_CHECK(uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM,
256, 0, 0, NULL, 0));
ESP_ERROR_CHECK(uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config));
/* Tell VFS to use UART driver */
esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
/* Initialize the console */
esp_console_config_t console_config = {
.max_cmdline_args = CONFIG_EXAMPLE_MAX_CMD_ARGUMENTS,
.max_cmdline_length = CONFIG_EXAMPLE_MAX_CMD_LENGTH,
#if CONFIG_LOG_COLORS
.hint_color = atoi(LOG_COLOR_CYAN)
#endif
};
ESP_ERROR_CHECK(esp_console_init(&console_config));
/* Configure linenoise line completion library */
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_EXAMPLE_STORE_HISTORY
/* Load command history from filesystem */
linenoiseHistoryLoad(HISTORY_PATH);
#endif
}
void app_main(void)
{
initialize_nvs();
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
#if CONFIG_EXAMPLE_STORE_HISTORY
initialize_filesystem();
repl_config.history_save_path = HISTORY_PATH;
#endif
repl_config.prompt = "i2c-tools>";
// initialize console REPL environment
ESP_ERROR_CHECK(esp_console_repl_init(&repl_config));
initialize_console();
/* Register commands */
esp_console_register_help_command();
register_i2ctools();
register_system();
/* Prompt to be printed before each line.
* This can be customized, made dynamic, etc.
*/
const char *prompt = LOG_COLOR_I "esp32> " LOG_RESET_COLOR;
printf("\n ==============================================================\n");
printf(" | Steps to Use i2c-tools on ESP32 |\n");
printf(" | Steps to Use i2c-tools |\n");
printf(" | |\n");
printf(" | 1. Try 'help', check all supported commands |\n");
printf(" | 2. Try 'i2cconfig' to configure your I2C bus |\n");
@@ -140,53 +64,6 @@ void app_main(void)
printf(" | |\n");
printf(" ==============================================================\n\n");
/* Figure out if the terminal supports escape sequences */
int probe_status = linenoiseProbe();
if (probe_status) {
/* zero indicates success */
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");
linenoiseSetDumbMode(1);
#if CONFIG_LOG_COLORS
/* Since the terminal doesn't support escape sequences,
* don't use color codes in the prompt.
*/
prompt = "esp32> ";
#endif //CONFIG_LOG_COLORS
}
/* Main loop */
while (true) {
/* Get a line using linenoise.
* The line is returned when ENTER is pressed.
*/
char *line = linenoise(prompt);
if (line == NULL) {
/* Ignore empty lines */
continue;
}
/* Add the command to the history */
linenoiseHistoryAdd(line);
#if CONFIG_EXAMPLE_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_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);
}
// start console REPL
ESP_ERROR_CHECK(esp_console_repl_start());
}