From 49f82d2f3d31f5ecb26f1f45d091e346da515314 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Mon, 7 Aug 2023 20:47:59 +1000 Subject: [PATCH] Use a timer to keep framerate consistent --- src/ui/lvgl_task.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/ui/lvgl_task.cpp b/src/ui/lvgl_task.cpp index 06a6b28b..340282ee 100644 --- a/src/ui/lvgl_task.cpp +++ b/src/ui/lvgl_task.cpp @@ -48,12 +48,25 @@ namespace ui { static const char* kTag = "lv_task"; +static const TickType_t kMaxFrameRate = pdMS_TO_TICKS(33); + +static int sTimerId; +static SemaphoreHandle_t sFrameSemaphore; + +auto next_frame(TimerHandle_t) { + xSemaphoreGive(sFrameSemaphore); +} void LvglMain(std::weak_ptr weak_touch_wheel, std::weak_ptr weak_display) { ESP_LOGI(kTag, "init lvgl"); lv_init(); + sFrameSemaphore = xSemaphoreCreateBinary(); + auto timer = + xTimerCreate("lvgl_frame", kMaxFrameRate, pdTRUE, &sTimerId, next_frame); + xTimerStart(timer, portMAX_DELAY); + lv_theme_t* base_theme = lv_theme_basic_init(NULL); lv_disp_set_theme(NULL, base_theme); static themes::Theme sTheme{}; @@ -80,9 +93,9 @@ void LvglMain(std::weak_ptr weak_touch_wheel, } lv_task_handler(); - // 30 FPS - // TODO(jacqueline): make this dynamic - vTaskDelay(pdMS_TO_TICKS(33)); + + // Wait for the signal to loop again. + xSemaphoreTake(sFrameSemaphore, portMAX_DELAY); } }