feat: Integrate LVGL and UI handling in app_main
- Initialize LVGL with appropriate configuration and error handling. - Create and initialize UIHandler to manage app icons and interactions. - Register DemoApp and ShutdownApp with AppRegistry. - Implement touch task and display initialization for EInkDisplayHandler. - Handle shutdown signal by switching to ShutdownApp and performing cleanup.
This commit is contained in:
@@ -15,7 +15,14 @@
|
||||
#include "io/nvs_handler.h"
|
||||
#include "info/info.h"
|
||||
#include "display/display.h"
|
||||
#include "display/eink_display_handler.h"
|
||||
#include "ui/ui_handler.h"
|
||||
#include "ui/app_registry.h"
|
||||
#include "ui/apps/demo_app.h"
|
||||
#include "ui/apps/shutdown_app.h"
|
||||
#include <tick/lv_tick.h>
|
||||
#include "esp_lvgl_port.h"
|
||||
#include "lvgl.h"
|
||||
#include "network.h"
|
||||
|
||||
// nvs storage namespaces, 15 characters max
|
||||
@@ -45,6 +52,17 @@ void app_main(void) {
|
||||
return esp_restart();
|
||||
}
|
||||
ESP_LOGI(TAG, "Queues initialized.\n");
|
||||
|
||||
// Initialize LVGL
|
||||
const lvgl_port_cfg_t lvgl_cfg = ESP_LVGL_PORT_INIT_CONFIG();
|
||||
esp_err_t err = lvgl_port_init(&lvgl_cfg);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "LVGL port initialization failed: %s", esp_err_to_name(err));
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
return esp_restart();
|
||||
}
|
||||
ESP_LOGI(TAG, "LVGL port initialized successfully.\n");
|
||||
|
||||
SemaphoreHandle_t lvgl_mutex = xSemaphoreCreateMutex();
|
||||
if (lvgl_mutex == NULL) {
|
||||
ESP_LOGE("Main", "Failed to create LVGL mutex");
|
||||
@@ -60,13 +78,14 @@ void app_main(void) {
|
||||
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);
|
||||
EInkDisplayHandler* display_handler = new EInkDisplayHandler(system_event_group);
|
||||
//
|
||||
kv_storage_handler->init(system_event_group);
|
||||
network_handler->init(system_event_group);
|
||||
|
||||
|
||||
// display_handler->init(system_event_group);
|
||||
// Initialize display and touch
|
||||
display_handler->init();
|
||||
display_handler->start_touch_task();
|
||||
//
|
||||
// LVGL tick timer
|
||||
auto lvgl_tick_timer_callback = [](TimerHandle_t xTimer) {
|
||||
@@ -94,16 +113,30 @@ void app_main(void) {
|
||||
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 | STORAGE_READY_BIT | NETWORK_READY_BIT,
|
||||
// do not clear on exit, require explicit reset
|
||||
pdFALSE,
|
||||
pdTRUE,
|
||||
portMAX_DELAY
|
||||
);
|
||||
ESP_LOGI(TAG, "System is ready. Starting main application...\n");
|
||||
// starting event loops
|
||||
// display_handler->start_event_loop();
|
||||
|
||||
// Register apps with AppRegistry by creating their descriptors
|
||||
// Each descriptor will create and register the app instance
|
||||
DemoAppDescriptor* demo_descriptor = new DemoAppDescriptor();
|
||||
ShutdownAppDescriptor* shutdown_descriptor = new ShutdownAppDescriptor();
|
||||
ESP_LOGI(TAG, "Apps registered with AppRegistry\n");
|
||||
|
||||
// Initialize UI Handler (will render app icons from registry)
|
||||
UIHandler ui_handler;
|
||||
if (ui_handler.init() != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to initialize UI handler");
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
return esp_restart();
|
||||
}
|
||||
ESP_LOGI(TAG, "UI handler initialized successfully\n");
|
||||
ESP_LOGI(TAG, "Main screen displayed with app icons. Tap an icon to launch an app.\n");
|
||||
|
||||
// wait for shutdown signal
|
||||
ESP_LOGI(TAG, "Waiting for shutdown signal...\n");
|
||||
EventBits_t bits = xEventGroupWaitBits(
|
||||
@@ -116,10 +149,18 @@ void app_main(void) {
|
||||
);
|
||||
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;
|
||||
// Show shutdown screen using the shutdown descriptor's app instance
|
||||
ShutdownApp* shutdown_app = dynamic_cast<ShutdownApp*>(shutdown_descriptor->get_app_instance());
|
||||
if (shutdown_app) {
|
||||
ui_handler.switch_app(shutdown_app);
|
||||
}
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS); // Display shutdown message briefly
|
||||
|
||||
// Cleanup
|
||||
ui_handler.deinit();
|
||||
delete demo_descriptor;
|
||||
delete shutdown_descriptor;
|
||||
delete display_handler;
|
||||
vSemaphoreDelete(lvgl_mutex);
|
||||
vEventGroupDelete(system_event_group);
|
||||
vQueueDelete(touch_event_queue);
|
||||
|
||||
Reference in New Issue
Block a user