Refactor NVS and WiFi handlers for improved memory management and logging

- Updated KVStorageHandler interface to use std::string instead of char* for key-value operations.
- Enhanced NVSStorageHandler to utilize ESP_LOG for error and info messages instead of printf.
- Refactored WifiHandler to manage WiFi credentials using JSON format for better structure and storage.
- Replaced raw pointers with std::unique_ptr in WifiHandler and NetworkHandler for automatic memory management.
- Removed unused TouchHandler and EInkTouchHandler classes to clean up the codebase.
- Adjusted CMakeLists.txt to remove unnecessary include directories.
- Updated lv_conf.h to enable FreeRTOS and gesture recognition features.
This commit is contained in:
GW_MC
2026-01-21 14:00:04 +08:00
parent 14f4b8fdc0
commit 44fb9aa632
12 changed files with 302 additions and 304 deletions

View File

@@ -1,10 +1,3 @@
/*
* SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: CC0-1.0
*/
#include <stdio.h>
#include <inttypes.h>
#include <stdexcept>
@@ -16,18 +9,19 @@
#include "esp_system.h"
#include "esp_log.h"
//
//
#include "common/constants.h"
#include "common/queue_defs.h"
#include "io/nvs_handler.h"
#include "info/info.h"
#include "display/display.h"
#include "touch/touch.h"
#include <tick/lv_tick.h>
#include "network.h"
// nvs storage namespaces, 15 characters max
#define DEFAULT_STORAGE_NAMESPACE "storage"
#define WIFI_CREDENTIALS_STORAGE_NAMESPACE "wifi_credentials"
#define WIFI_CREDENTIALS_STORAGE_NAMESPACE "wifi_cred"
#define TAG "Main"
extern "C" void app_main(void);
@@ -50,7 +44,7 @@ void app_main(void) {
vTaskDelay(5000 / portTICK_PERIOD_MS);
return esp_restart();
}
printf("Queues initialized.\n");
ESP_LOGI(TAG, "Queues initialized.\n");
SemaphoreHandle_t lvgl_mutex = xSemaphoreCreateMutex();
if (lvgl_mutex == NULL) {
ESP_LOGE("Main", "Failed to create LVGL mutex");
@@ -58,28 +52,33 @@ void app_main(void) {
return esp_restart();
}
//
WifiHandler wifi_handler(
new NVSStorageHandler(WIFI_CREDENTIALS_STORAGE_NAMESPACE)
);
NetworkHandler* network_handler = new NetworkHandler(std::move(wifi_handler));
KVStorageHandler* kv_storage_handler = new NVSStorageHandler(
DEFAULT_STORAGE_NAMESPACE
);
DisplayHandler* display_handler = new EInkDisplayHandler(touch_event_queue, lvgl_mutex);
TouchHandler* touch_handler = new EInkTouchHandler(touch_event_queue);
auto wifi_handler = std::make_unique<WifiHandler>(
std::unique_ptr<KVStorageHandler>(new NVSStorageHandler(WIFI_CREDENTIALS_STORAGE_NAMESPACE))
);
NetworkHandler* network_handler = new NetworkHandler(std::move(wifi_handler));
// DisplayHandler* display_handler = new EInkDisplayHandler(touch_event_queue, lvgl_mutex);
//
network_handler->init(system_event_group);
kv_storage_handler->init(system_event_group);
display_handler->init(system_event_group);
touch_handler->init(system_event_group);
network_handler->init(system_event_group);
// display_handler->init(system_event_group);
//
// LVGL tick timer
auto lvgl_tick_timer_callback = [](TimerHandle_t xTimer) {
lv_tick_inc(5);
};
TickType_t lvgl_tick_period = pdMS_TO_TICKS(5);
if (lvgl_tick_period == 0) {
lvgl_tick_period = 1; // ensure at least 1 tick to avoid FreeRTOS assert
}
TimerHandle_t lvgl_tick_timer = xTimerCreate(
"lvgl_tick_timer",
pdMS_TO_TICKS(5),
lvgl_tick_period,
pdTRUE,
NULL,
lvgl_tick_timer_callback
@@ -92,20 +91,21 @@ void app_main(void) {
xTimerStart(lvgl_tick_timer, 0);
//
printf("Waiting for system to be ready...\n");
ESP_LOGI(TAG, "Waiting for system to be ready...\n");
xEventGroupWaitBits(
system_event_group,
DISPLAY_READY_BIT | TOUCH_CALIBRATED_BIT | STORAGE_READY_BIT | NETWORK_READY_BIT,
// DISPLAY_READY_BIT | TOUCH_CALIBRATED_BIT |
STORAGE_READY_BIT | NETWORK_READY_BIT,
// do not clear on exit, require explicit reset
pdFALSE,
pdTRUE,
portMAX_DELAY
);
printf("System is ready. Starting main application...\n");
ESP_LOGI(TAG, "System is ready. Starting main application...\n");
// starting event loops
display_handler->start_event_loop();
touch_handler->start_event_loop();
// display_handler->start_event_loop();
// wait for shutdown signal
ESP_LOGI(TAG, "Waiting for shutdown signal...\n");
EventBits_t bits = xEventGroupWaitBits(
system_lifecycle_event_group,
SYSTEM_SHUTDOWN_BIT | SYSTEM_RESTART_BIT,
@@ -114,27 +114,26 @@ void app_main(void) {
pdFALSE,
portMAX_DELAY
);
printf("Shutdown signal received. Cleaning up...\n");
ESP_LOGI(TAG, "Shutdown signal received. Cleaning up...\n");
// cleanup
shutdown_display_handlerFunc shutdown_display_handler = display_handler->get_shutdown_display_handler();
restart_display_handlerFunc restart_display_handler = display_handler->get_restart_display_handler();
delete display_handler;
delete touch_handler;
// shutdown_display_handlerFunc shutdown_display_handler = display_handler->get_shutdown_display_handler();
// restart_display_handlerFunc restart_display_handler = display_handler->get_restart_display_handler();
// delete display_handler;
vSemaphoreDelete(lvgl_mutex);
vEventGroupDelete(system_event_group);
vQueueDelete(touch_event_queue);
printf("Cleanup complete.\n");
ESP_LOGI(TAG, "Cleanup complete.\n");
// handle shutdown or restart
if (bits & SYSTEM_SHUTDOWN_BIT) {
if (shutdown_display_handler != nullptr) {
printf("Calling display shutdown handler...\n");
shutdown_display_handler();
} else {
printf("No display shutdown handler to call.\n");
}
printf("System is shutting down.\n");
// if (shutdown_display_handler != nullptr) {
// ESP_LOGI(TAG, "Calling display shutdown handler...\n");
// shutdown_display_handler();
// } else {
// ESP_LOGI(TAG, "No display shutdown handler to call.\n");
// }
ESP_LOGI(TAG, "System is shutting down.\n");
fflush(stdout);
// wait for start bit to be set again if future restart is desired, else expect manual power cycle
EventBits_t bits = xEventGroupWaitBits(
@@ -145,24 +144,24 @@ void app_main(void) {
portMAX_DELAY
);
if (bits & SYSTEM_START_BIT) {
printf("SYSTEM_START_BIT received, restarting system.\n");
ESP_LOGI(TAG, "SYSTEM_START_BIT received, restarting system.\n");
} else {
printf("No restart signal received, waiting for manual power cycle.\n");
ESP_LOGW(TAG, "No restart signal received, waiting for manual power cycle.\n");
while (true) {
vTaskDelay(portMAX_DELAY);
}
}
} else if (bits & SYSTEM_RESTART_BIT) {
if (restart_display_handler != nullptr) {
printf("Calling display restart handler...\n");
restart_display_handler();
} else {
printf("No display restart handler to call.\n");
}
printf("System is restarting.\n");
// if (restart_display_handler != nullptr) {
// ESP_LOGI(TAG, "Calling display restart handler...\n");
// restart_display_handler();
// } else {
// ESP_LOGI(TAG, "No display restart handler to call.\n");
// }
ESP_LOGI(TAG, "System is restarting.\n");
fflush(stdout);
} else {
printf("Unknown shutdown signal received. Restarting by default.\n");
ESP_LOGW(TAG, "Unknown shutdown signal received. Restarting by default.\n");
fflush(stdout);
}