#include "Application.h" #include "Command.h" #include "LedController.h" #include "MqttClient.h" #include "cmd_nvs.h" #include "cmd_system.h" #include "cmd_wifi.h" #include "console_settings.h" #include #include #include #include #include #include #include /* * We warn if a secondary serial console is enabled. A secondary serial console is always output-only and * hence not very useful for interactive console applications. If you encounter this warning, consider disabling * the secondary serial console in menuconfig unless you know what you are doing. */ #if SOC_USB_SERIAL_JTAG_SUPPORTED #if !CONFIG_ESP_CONSOLE_SECONDARY_NONE #warning "A secondary serial console is not useful when using the console component. Please disable it in menuconfig." #endif #endif static const char *TAG = "main"; #define PROMPT_STR CONFIG_IDF_TARGET /* 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_CONSOLE_STORE_HISTORY #define MOUNT_PATH "/data" #define HISTORY_PATH MOUNT_PATH "/history.txt" static void initialize_filesystem(void) { static wl_handle_t wl_handle; const esp_vfs_fat_mount_config_t mount_config = { .format_if_mount_failed = true, .max_files = 4, }; esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(MOUNT_PATH, "storage", &mount_config, &wl_handle); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err)); return; } } #else #define HISTORY_PATH NULL #endif // CONFIG_CONSOLE_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); } extern "C" void app_main() { initialize_nvs(); #if CONFIG_CONSOLE_STORE_HISTORY initialize_filesystem(); ESP_LOGI(TAG, "Command history enabled"); #else ESP_LOGI(TAG, "Command history disabled"); #endif /* Initialize console output periheral (UART, USB_OTG, USB_JTAG) */ initialize_console_peripheral(); /* Initialize linenoise library and esp_console*/ initialize_console_library(HISTORY_PATH); /* Prompt to be printed before each line. * This can be customized, made dynamic, etc. */ const char *prompt = setup_prompt(PROMPT_STR ">"); Application::instance()->initialize(); /* Register commands */ esp_console_register_help_command(); register_system_common(); #if SOC_LIGHT_SLEEP_SUPPORTED register_system_light_sleep(); #endif #if SOC_DEEP_SLEEP_SUPPORTED register_system_deep_sleep(); #endif #if (CONFIG_ESP_WIFI_ENABLED || CONFIG_ESP_HOST_WIFI_ENABLED) register_wifi(); #endif register_nvs(); register_custom(); LedController::instance()->initialize(); if (Application::instance()->contains("brightness")) { int brightness = Application::instance()->field("brightness"); LedController::instance()->setBrightness(brightness); ESP_LOGI("main", "last brightness: %d \n", brightness); } if (Application::instance()->contains("color_temp")) { int color_temp = Application::instance()->field("color_temp"); LedController::instance()->setColorTemperature(Application::instance()->field("color_temp")); ESP_LOGI("main", "last color_temp: %d \n", color_temp); } bool connected = false; if (Application::instance()->contains("ssid")) { auto ssid = Application::instance()->field("ssid"); auto psk = Application::instance()->field("psk"); connected = Application::instance()->wifiConnect(ssid, psk); } else { ESP_LOGI("main", "please connect wifi use command.\n"); } if (connected) { if (Application::instance()->contains("mqtt_username")) { auto username = Application::instance()->field("mqtt_username"); auto password = Application::instance()->field("mqtt_password"); MqttClient::instance()->initialize(username, password); } else { ESP_LOGI("main", "please connect mqtt use command.\n"); } } if (linenoiseIsDumbMode()) { 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"); } /* Main loop */ while (true) { /* Get a line using linenoise. * The line is returned when ENTER is pressed. */ char *line = linenoise(prompt); #if CONFIG_CONSOLE_IGNORE_EMPTY_LINES if (line == NULL) { /* Ignore empty lines */ continue; ; } #else if (line == NULL) { /* Break on EOF or error */ break; } #endif // CONFIG_CONSOLE_IGNORE_EMPTY_LINES /* Add the command to the history if not empty*/ if (strlen(line) > 0) { linenoiseHistoryAdd(line); #if CONFIG_CONSOLE_STORE_HISTORY /* Save command history to filesystem */ linenoiseHistorySave(HISTORY_PATH); #endif // CONFIG_CONSOLE_STORE_HISTORY } /* 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); } ESP_LOGE(TAG, "Error or end-of-input, terminating console"); esp_console_deinit(); }