Improve ScopedLock.

This commit is contained in:
Shaun Reed 2025-02-14 16:33:41 -05:00
parent 8aaed133e8
commit 2dd099f26e
3 changed files with 18 additions and 15 deletions

View File

@ -13,7 +13,9 @@
// To use LV_COLOR_FORMAT_I1, we need an extra buffer to hold the converted data
uint8_t Display::oled_buffer_[LCD_H_RES * LCD_V_RES / 8];
_lock_t Display::lvgl_api_lock_;
// LVGL library is not thread-safe, this example calls LVGL APIs from tasks.
// We must use a mutex to protect it.
_lock_t ScopedLock::lock_;
Display::Display() :
io_handle_(nullptr),
@ -149,9 +151,9 @@ void Display::lvgl_port_task(void *arg)
ESP_LOGI(TAG, "Starting LVGL task");
uint32_t time_till_next_ms = 0;
while (1) {
_lock_acquire(&lvgl_api_lock_);
_lock_acquire(&ScopedLock::lock_);
time_till_next_ms = lv_timer_handler();
_lock_release(&lvgl_api_lock_);
_lock_release(&ScopedLock::lock_);
usleep(1000 * time_till_next_ms);
}
}

View File

@ -4,8 +4,10 @@
#include <esp_lcd_types.h>
#include <esp_lcd_panel_ssd1306.h>
#include <driver/i2c_types.h>
#include <driver/i2c_master.h>
#include "misc/lv_types.h"
#include "misc/lv_area.h"
#include "display/lv_display.h"
#define I2C_BUS_PORT 0
#define LVGL_TICK_PERIOD_MS 5
@ -33,11 +35,13 @@ static const char *TAG = "lcd-panel";
#define PIN_RST -1
struct ScopedLock {
explicit ScopedLock(_lock_t& lock) : lock_(&lock) { _lock_acquire(lock_); }
~ScopedLock() { _lock_release(lock_); }
explicit ScopedLock() { _lock_acquire(&lock_); }
private:
_lock_t* lock_;
~ScopedLock() { _lock_release(&lock_); }
// LVGL library is not thread-safe, this example calls LVGL APIs from tasks.
// We must use a mutex to protect it.
static _lock_t lock_;
};
class Display {
@ -59,19 +63,16 @@ public:
esp_lcd_panel_io_event_data_t *data,
void *user_ctx);
static void
lvgl_flush_cb(lv_display_t *display, const lv_area_t *area, uint8_t *px_map);
static void lvgl_flush_cb(lv_display_t *display,
const lv_area_t *area,
uint8_t *px_map);
static void lvgl_increase_tick(void *arg);
static void lvgl_port_task(void *arg);
// To use LV_COLOR_FORMAT_I1, we need an extra buffer to hold the converted data
// For LV_COLOR_FORMAT_I1 we need an extra buffer to hold the converted data.
static uint8_t oled_buffer_[LCD_H_RES * LCD_V_RES / 8];
// LVGL library is not thread-safe, this example will call LVGL APIs from different tasks, so use a mutex to protect it
static _lock_t lvgl_api_lock_;
protected:
esp_lcd_panel_io_handle_t io_handle_;

View File

@ -10,7 +10,7 @@ void setup()
// UI function
{
// Lock the mutex due to the LVGL APIs are not thread-safe
ScopedLock lock(Display::lvgl_api_lock_);
ScopedLock lock;
ESP_LOGI(TAG, "Display LVGL Scroll Text");
lv_obj_t *scr = lv_display_get_screen_active(d.get());