Improve ScopedLock.
This commit is contained in:
		
							parent
							
								
									8aaed133e8
								
							
						
					
					
						commit
						2dd099f26e
					
				@ -13,7 +13,9 @@
 | 
				
			|||||||
// To use LV_COLOR_FORMAT_I1, we need an extra buffer to hold the converted data
 | 
					// 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];
 | 
					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() :
 | 
					Display::Display() :
 | 
				
			||||||
    io_handle_(nullptr),
 | 
					    io_handle_(nullptr),
 | 
				
			||||||
@ -149,9 +151,9 @@ void Display::lvgl_port_task(void *arg)
 | 
				
			|||||||
  ESP_LOGI(TAG, "Starting LVGL task");
 | 
					  ESP_LOGI(TAG, "Starting LVGL task");
 | 
				
			||||||
  uint32_t time_till_next_ms = 0;
 | 
					  uint32_t time_till_next_ms = 0;
 | 
				
			||||||
  while (1) {
 | 
					  while (1) {
 | 
				
			||||||
    _lock_acquire(&lvgl_api_lock_);
 | 
					    _lock_acquire(&ScopedLock::lock_);
 | 
				
			||||||
    time_till_next_ms = lv_timer_handler();
 | 
					    time_till_next_ms = lv_timer_handler();
 | 
				
			||||||
    _lock_release(&lvgl_api_lock_);
 | 
					    _lock_release(&ScopedLock::lock_);
 | 
				
			||||||
    usleep(1000 * time_till_next_ms);
 | 
					    usleep(1000 * time_till_next_ms);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -4,8 +4,10 @@
 | 
				
			|||||||
#include <esp_lcd_types.h>
 | 
					#include <esp_lcd_types.h>
 | 
				
			||||||
#include <esp_lcd_panel_ssd1306.h>
 | 
					#include <esp_lcd_panel_ssd1306.h>
 | 
				
			||||||
#include <driver/i2c_types.h>
 | 
					#include <driver/i2c_types.h>
 | 
				
			||||||
 | 
					#include <driver/i2c_master.h>
 | 
				
			||||||
#include "misc/lv_types.h"
 | 
					#include "misc/lv_types.h"
 | 
				
			||||||
#include "misc/lv_area.h"
 | 
					#include "misc/lv_area.h"
 | 
				
			||||||
 | 
					#include "display/lv_display.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define I2C_BUS_PORT  0
 | 
					#define I2C_BUS_PORT  0
 | 
				
			||||||
#define LVGL_TICK_PERIOD_MS    5
 | 
					#define LVGL_TICK_PERIOD_MS    5
 | 
				
			||||||
@ -33,11 +35,13 @@ static const char *TAG = "lcd-panel";
 | 
				
			|||||||
#define PIN_RST                -1
 | 
					#define PIN_RST                -1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct ScopedLock {
 | 
					struct ScopedLock {
 | 
				
			||||||
  explicit ScopedLock(_lock_t& lock) : lock_(&lock) { _lock_acquire(lock_); }
 | 
					  explicit ScopedLock() { _lock_acquire(&lock_); }
 | 
				
			||||||
  ~ScopedLock() { _lock_release(lock_); }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					  ~ScopedLock() { _lock_release(&lock_); }
 | 
				
			||||||
  _lock_t* 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 {
 | 
					class Display {
 | 
				
			||||||
@ -59,19 +63,16 @@ public:
 | 
				
			|||||||
                               esp_lcd_panel_io_event_data_t *data,
 | 
					                               esp_lcd_panel_io_event_data_t *data,
 | 
				
			||||||
                               void *user_ctx);
 | 
					                               void *user_ctx);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static void
 | 
					  static void lvgl_flush_cb(lv_display_t *display,
 | 
				
			||||||
  lvgl_flush_cb(lv_display_t *display, const lv_area_t *area, uint8_t *px_map);
 | 
					                            const lv_area_t *area,
 | 
				
			||||||
 | 
					                            uint8_t *px_map);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static void lvgl_increase_tick(void *arg);
 | 
					  static void lvgl_increase_tick(void *arg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static void lvgl_port_task(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];
 | 
					  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:
 | 
					protected:
 | 
				
			||||||
  esp_lcd_panel_io_handle_t io_handle_;
 | 
					  esp_lcd_panel_io_handle_t io_handle_;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -10,7 +10,7 @@ void setup()
 | 
				
			|||||||
  // UI function
 | 
					  // UI function
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    // Lock the mutex due to the LVGL APIs are not thread-safe
 | 
					    // 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");
 | 
					    ESP_LOGI(TAG, "Display LVGL Scroll Text");
 | 
				
			||||||
    lv_obj_t *scr = lv_display_get_screen_active(d.get());
 | 
					    lv_obj_t *scr = lv_display_get_screen_active(d.get());
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user