DualLedController/main/main.cpp

127 lines
4.1 KiB
C++
Raw Normal View History

2024-05-17 01:08:15 +08:00
#include "CustomCommand.h"
#include "LedController.h"
2024-05-16 01:09:36 +08:00
#include "cmd_system.h"
2024-05-22 23:07:24 +08:00
#include "cmd_wifi.h"
2024-05-16 01:09:36 +08:00
#include "driver/uart.h"
#include "esp_console.h"
#include "esp_log.h"
#include "esp_system.h"
#include "esp_vfs_dev.h"
#include "esp_vfs_fat.h"
#include "linenoise/linenoise.h"
#include "nvs.h"
#include "nvs_flash.h"
2024-05-15 23:29:00 +08:00
#include <stdio.h>
2024-05-16 01:09:36 +08:00
#define PROMPT_STR CONFIG_IDF_TARGET
2024-05-15 23:29:00 +08:00
2024-05-16 01:09:36 +08:00
#define MOUNT_PATH "/data"
#define HISTORY_PATH MOUNT_PATH "/history.txt"
static const char *TAG = "main";
static void initialize_nvs();
static void initialize_filesystem();
static void initialize_console();
2024-05-17 01:08:15 +08:00
extern "C" void app_main() {
2024-05-16 01:09:36 +08:00
const char *prompt = LOG_COLOR_I PROMPT_STR "> " LOG_RESET_COLOR;
initialize_nvs();
initialize_filesystem();
initialize_console();
esp_console_register_help_command();
register_system_common();
register_system_sleep();
2024-05-22 23:07:24 +08:00
register_wifi();
2024-05-16 01:09:36 +08:00
register_custom();
2024-05-17 01:08:15 +08:00
LedController::instance()->initialize();
2024-05-16 01:09:36 +08:00
while (true) {
char *line = linenoise(prompt);
if (line == NULL) { /* Break on EOF or error */
// break;
continue;
}
if (strlen(line) > 0) {
linenoiseHistoryAdd(line);
linenoiseHistorySave(HISTORY_PATH);
}
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_console_deinit();
}
static void initialize_console() {
fflush(stdout);
fsync(fileno(stdout));
setvbuf(stdin, NULL, _IONBF, 0);
esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
2024-05-17 01:08:15 +08:00
uart_config_t uart_config = {0};
uart_config.baud_rate = CONFIG_ESP_CONSOLE_UART_BAUDRATE;
uart_config.data_bits = UART_DATA_8_BITS;
uart_config.parity = UART_PARITY_DISABLE;
uart_config.stop_bits = UART_STOP_BITS_1;
uart_config.source_clk = UART_SCLK_REF_TICK;
ESP_ERROR_CHECK(uart_driver_install(static_cast<uart_port_t>(CONFIG_ESP_CONSOLE_UART_NUM), 256, 0, 0, NULL, 0));
ESP_ERROR_CHECK(uart_param_config(static_cast<uart_port_t>(CONFIG_ESP_CONSOLE_UART_NUM), &uart_config));
2024-05-16 01:09:36 +08:00
esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
2024-05-17 01:08:15 +08:00
esp_console_config_t console_config = {0};
console_config.max_cmdline_args = 8;
console_config.max_cmdline_length = 256;
console_config.hint_color = atoi(LOG_COLOR_CYAN);
2024-05-16 01:09:36 +08:00
ESP_ERROR_CHECK(esp_console_init(&console_config));
linenoiseSetMultiLine(1);
linenoiseSetCompletionCallback(&esp_console_get_completion);
linenoiseSetHintsCallback((linenoiseHintsCallback *)&esp_console_get_hint);
linenoiseHistorySetMaxLen(100);
linenoiseSetMaxLineLen(console_config.max_cmdline_length);
linenoiseAllowEmpty(false);
linenoiseHistoryLoad(HISTORY_PATH);
}
static void initialize_nvs() {
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_filesystem(void) {
static wl_handle_t wl_handle;
2024-05-17 01:08:15 +08:00
esp_vfs_fat_mount_config_t config;
memset(&config, 0, sizeof(esp_vfs_fat_mount_config_t));
config.max_files = 4;
config.format_if_mount_failed = true;
esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_PATH, "storage", &config, &wl_handle);
2024-05-16 01:09:36 +08:00
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
return;
}
2024-05-15 23:29:00 +08:00
}