From f3dfc4f43fa7679bb3ec71e58049249b56e9518b Mon Sep 17 00:00:00 2001 From: GW_MC <72297530+GWMCwing@users.noreply.github.com> Date: Sun, 25 Jan 2026 19:39:21 +0800 Subject: [PATCH] refactor: improve watchdog handling and screen loading in UI and display handlers --- main/display/eink_display_handler.cpp | 8 +++++--- main/ui/ui_handler.cpp | 11 +++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/main/display/eink_display_handler.cpp b/main/display/eink_display_handler.cpp index 310b339..a4dc068 100644 --- a/main/display/eink_display_handler.cpp +++ b/main/display/eink_display_handler.cpp @@ -3,6 +3,7 @@ #include "common/constants.h" #include "esp_log.h" #include "esp_heap_caps.h" +#include "esp_task_wdt.h" #include #define TAG "EInkDisplayHandler" @@ -382,10 +383,10 @@ void EInkDisplayHandler::_perform_full_refresh(const uint8_t* framebuffer) { break; } - // Yield every 1000 bytes to prevent watchdog timeout - if (i % 1000 == 999) { + // Yield every 100 bytes to prevent watchdog timeout + if (i % 100 == 99) { xSemaphoreGive(_spi_mutex); - vTaskDelay(pdMS_TO_TICKS(1)); // Small delay + vTaskDelay(pdMS_TO_TICKS(5)); // Increased delay for better yielding if (xSemaphoreTake(_spi_mutex, pdMS_TO_TICKS(5000)) != pdTRUE) { ESP_LOGE(TAG, "SPI mutex timeout during yield at byte %zu", i); return; @@ -501,6 +502,7 @@ void EInkDisplayHandler::_wait_for_busy() { int timeout = 0; while (gpio_get_level(PIN_BUSY) == BUSY_ACTIVE_LEVEL) { // 0=BUSY, 1=FREE vTaskDelay(pdMS_TO_TICKS(100)); + esp_task_wdt_reset(); // Feed the watchdog during long wait timeout++; if (timeout > 100) { // 10 second timeout ESP_LOGE(TAG, "Display BUSY timeout! Pin level: %d", gpio_get_level(PIN_BUSY)); diff --git a/main/ui/ui_handler.cpp b/main/ui/ui_handler.cpp index e31ebfd..d962dbe 100644 --- a/main/ui/ui_handler.cpp +++ b/main/ui/ui_handler.cpp @@ -2,6 +2,7 @@ #include "ui/root_layout.h" #include "ui/app_registry.h" #include "esp_log.h" +#include "lvgl.h" #define TAG "UIHandler" @@ -45,8 +46,14 @@ esp_err_t UIHandler::init(void) { ESP_LOGW(TAG, "Failed to render app icons"); } - // Load the main screen - lv_screen_load(_main_screen); + // Defer screen loading to prevent blocking during initialization + // Use LVGL timer to load screen after allowing watchdog reset + lv_timer_create([](lv_timer_t* timer) { + lv_obj_t* screen = static_cast(lv_timer_get_user_data(timer)); + ESP_LOGI("UIHandler", "Loading main screen via timer"); + lv_screen_load(screen); + lv_timer_del(timer); + }, 100, _main_screen); // 100ms delay to allow watchdog reset ESP_LOGI(TAG, "UIHandler initialized successfully"); return ESP_OK;