From 58a83590caaa7edeb4616bf705a130dfa23f5592 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Fri, 14 Feb 2025 15:50:35 -0500 Subject: [PATCH] Add ScopedLock for LVGL. --- esp/cpp/07_lcd-panel/main/display.h | 8 ++++++++ esp/cpp/07_lcd-panel/main/main.cpp | 10 ++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/esp/cpp/07_lcd-panel/main/display.h b/esp/cpp/07_lcd-panel/main/display.h index 027fed9..e98d290 100644 --- a/esp/cpp/07_lcd-panel/main/display.h +++ b/esp/cpp/07_lcd-panel/main/display.h @@ -32,6 +32,14 @@ static const char *TAG = "lcd-panel"; #define PIN_SCL GPIO_NUM_22 #define PIN_RST -1 +struct ScopedLock { + explicit ScopedLock(_lock_t& lock) : lock_(&lock) { _lock_acquire(lock_); } + ~ScopedLock() { _lock_release(lock_); } + +private: + _lock_t* lock_; +}; + class Display { public: Display(); diff --git a/esp/cpp/07_lcd-panel/main/main.cpp b/esp/cpp/07_lcd-panel/main/main.cpp index 5c79374..93c30cf 100644 --- a/esp/cpp/07_lcd-panel/main/main.cpp +++ b/esp/cpp/07_lcd-panel/main/main.cpp @@ -7,12 +7,12 @@ void setup() { Display d; - ESP_LOGI(TAG, "Display LVGL Scroll Text"); - // Lock the mutex due to the LVGL APIs are not thread-safe - _lock_acquire(&Display::lvgl_api_lock_); - // UI function { + // Lock the mutex due to the LVGL APIs are not thread-safe + ScopedLock lock(Display::lvgl_api_lock_); + + ESP_LOGI(TAG, "Display LVGL Scroll Text"); lv_obj_t *scr = lv_display_get_screen_active(d.get_display()); lv_obj_t *label = lv_label_create(scr); // Circular scroll @@ -26,8 +26,6 @@ void setup() lv_display_get_horizontal_resolution(d.get_display())); lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 0); } - - _lock_release(&Display::lvgl_api_lock_); } void loop() { }