DualLedController/main/main.cpp
2024-06-03 23:51:39 +08:00

131 lines
4.3 KiB
C++

#include "Application.h"
#include "CustomCommand.h"
#include "LedController.h"
#include "cmd_nvs.h"
#include "cmd_system.h"
#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"
#include <stdio.h>
#define PROMPT_STR CONFIG_IDF_TARGET
#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();
extern "C" void app_main() {
const char *prompt = LOG_COLOR_I PROMPT_STR "> " LOG_RESET_COLOR;
initialize_nvs();
initialize_filesystem();
initialize_console();
Application::instance()->initialize();
Application::instance()->setField("author", "amass");
esp_console_register_help_command();
register_system_common();
register_system_sleep();
register_custom();
register_nvs();
LedController::instance()->initialize();
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);
uart_config_t uart_config = {};
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));
esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
esp_console_config_t console_config = {};
console_config.max_cmdline_args = 8;
console_config.max_cmdline_length = 256;
console_config.hint_color = atoi(LOG_COLOR_CYAN);
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;
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);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
return;
}
}