From 12e99f896a73d87771b4eb5e7974c87d02765218 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Thu, 9 Jul 2026 22:27:44 -0400 Subject: [PATCH] perf: Rate-limit progress bar updates to prevent redundant layout invalidation and redraws --- Apps/BookPlayer/main/Source/main.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/Apps/BookPlayer/main/Source/main.c b/Apps/BookPlayer/main/Source/main.c index 4640669..57b6b44 100644 --- a/Apps/BookPlayer/main/Source/main.c +++ b/Apps/BookPlayer/main/Source/main.c @@ -59,6 +59,7 @@ typedef struct { cJSON* pages_array; int page_count; int current_page; + int last_pct; // UI elements AppHandle app; @@ -212,6 +213,7 @@ static void load_page(AppCtx* ctx, int page_index, bool start_audio) { if (page_index < 0 || page_index >= ctx->page_count) return; ctx->current_page = page_index; + ctx->last_pct = -1; cJSON* page = cJSON_GetArrayItem(ctx->pages_array, page_index); if (!page) return; @@ -451,9 +453,12 @@ static void play_mp3(AppCtx* ctx) { int pct = (int)((bytes_read_total - buffered_bytes) * 100 / file_size); if (pct < 0) pct = 0; if (pct > 100) pct = 100; - tt_lvgl_lock(portMAX_DELAY); - lv_bar_set_value(ctx->bar_progress, pct, LV_ANIM_OFF); - tt_lvgl_unlock(); + if (pct != ctx->last_pct) { + ctx->last_pct = pct; + tt_lvgl_lock(portMAX_DELAY); + lv_bar_set_value(ctx->bar_progress, pct, LV_ANIM_OFF); + tt_lvgl_unlock(); + } } taskYIELD(); @@ -611,9 +616,14 @@ static void play_wav(AppCtx* ctx) { // Update progress bar int pct = (int)((total_played * 100) / data_size); - tt_lvgl_lock(portMAX_DELAY); - lv_bar_set_value(ctx->bar_progress, pct, LV_ANIM_OFF); - tt_lvgl_unlock(); + if (pct < 0) pct = 0; + if (pct > 100) pct = 100; + if (pct != ctx->last_pct) { + ctx->last_pct = pct; + tt_lvgl_lock(portMAX_DELAY); + lv_bar_set_value(ctx->bar_progress, pct, LV_ANIM_OFF); + tt_lvgl_unlock(); + } taskYIELD(); }