Skip to content

Commit

Permalink
fix compile warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
78 committed Dec 3, 2024
1 parent cabd1fa commit 37773c9
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 50 deletions.
4 changes: 1 addition & 3 deletions main/display/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class Display {
protected:
int width_ = 0;
int height_ = 0;
int offset_x_ = 0;
int offset_y_ = 0;

lv_disp_t *disp_ = nullptr;

Expand All @@ -43,7 +41,7 @@ class Display {
esp_timer_handle_t update_timer_ = nullptr;

friend class DisplayLockGuard;
virtual void Lock() = 0;
virtual bool Lock(int timeout_ms = 0) = 0;
virtual void Unlock() = 0;

virtual void Update();
Expand Down
4 changes: 3 additions & 1 deletion main/display/no_display.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ NoDisplay::NoDisplay() {}

NoDisplay::~NoDisplay() {}

void NoDisplay::Lock() {}
bool NoDisplay::Lock(int timeout_ms) {
return true;
}

void NoDisplay::Unlock() {}
2 changes: 1 addition & 1 deletion main/display/no_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class NoDisplay : public Display {
private:
virtual void Lock() override;
virtual bool Lock(int timeout_ms = 0) override;
virtual void Unlock() override;

public:
Expand Down
4 changes: 2 additions & 2 deletions main/display/ssd1306_display.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ Ssd1306Display::~Ssd1306Display() {
lvgl_port_deinit();
}

void Ssd1306Display::Lock() {
lvgl_port_lock(0);
bool Ssd1306Display::Lock(int timeout_ms) {
return lvgl_port_lock(timeout_ms);
}

void Ssd1306Display::Unlock() {
Expand Down
2 changes: 1 addition & 1 deletion main/display/ssd1306_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Ssd1306Display : public Display {
lv_obj_t* container_ = nullptr;
lv_obj_t* side_bar_ = nullptr;

virtual void Lock() override;
virtual bool Lock(int timeout_ms = 0) override;
virtual void Unlock() override;

void SetupUI_128x64();
Expand Down
72 changes: 31 additions & 41 deletions main/display/st7789_display.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include <esp_log.h>
#include <esp_err.h>
#include <esp_lvgl_port.h>
#include <driver/ledc.h>
#include <vector>

Expand All @@ -20,7 +19,6 @@ LV_FONT_DECLARE(font_puhui_14_1);
LV_FONT_DECLARE(font_awesome_30_1);
LV_FONT_DECLARE(font_awesome_14_1);

static SemaphoreHandle_t lvgl_mux = NULL;
static lv_disp_drv_t disp_drv;
static void st7789_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
{
Expand Down Expand Up @@ -83,36 +81,17 @@ static void st7789_lvgl_port_update_callback(lv_disp_drv_t *drv)
break;
}
}
static void st7789_increase_lvgl_tick(void *arg)
{
/* Tell LVGL how many milliseconds has elapsed */
lv_tick_inc(ST7789_LVGL_TICK_PERIOD_MS);
}
static bool st7789_lvgl_lock(int timeout_ms)
{
// Convert timeout in milliseconds to FreeRTOS ticks
// If `timeout_ms` is set to -1, the program will block until the condition is met
const TickType_t timeout_ticks = (timeout_ms == -1) ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms);
return xSemaphoreTakeRecursive(lvgl_mux, timeout_ticks) == pdTRUE;
}

static void st7789_lvgl_unlock(void)
{
xSemaphoreGiveRecursive(lvgl_mux);
}

static void st7789_lvgl_port_task(void *arg)
{
void St7789Display::LvglTask() {
ESP_LOGI(TAG, "Starting LVGL task");
uint32_t task_delay_ms = ST7789_LVGL_TASK_MAX_DELAY_MS;
while (1)
{
// Lock the mutex due to the LVGL APIs are not thread-safe
if (st7789_lvgl_lock(-1))
if (Lock())
{
task_delay_ms = lv_timer_handler();
// Release the mutex
st7789_lvgl_unlock();
Unlock();
}
if (task_delay_ms > ST7789_LVGL_TASK_MAX_DELAY_MS)
{
Expand Down Expand Up @@ -173,28 +152,39 @@ St7789Display::St7789Display(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_h
disp_drv.draw_buf = &disp_buf;
disp_drv.user_data = panel_;

lv_disp_t *disp = lv_disp_drv_register(&disp_drv);
lv_disp_drv_register(&disp_drv);

ESP_LOGI(TAG, "Install LVGL tick timer");
// Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
const esp_timer_create_args_t lvgl_tick_timer_args = {
.callback = &st7789_increase_lvgl_tick,
.name = "lvgl_tick"};
esp_timer_handle_t lvgl_tick_timer = NULL;
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, ST7789_LVGL_TICK_PERIOD_MS * 1000));

lvgl_mux = xSemaphoreCreateRecursiveMutex();
assert(lvgl_mux);
.callback = [](void* arg) {
lv_tick_inc(ST7789_LVGL_TICK_PERIOD_MS);
},
.arg = NULL,
.dispatch_method = ESP_TIMER_TASK,
.name = "LVGL Tick Timer",
.skip_unhandled_events = false
};
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer_));
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer_, ST7789_LVGL_TICK_PERIOD_MS * 1000));

lvgl_mutex_ = xSemaphoreCreateRecursiveMutex();
assert(lvgl_mutex_ != nullptr);
ESP_LOGI(TAG, "Create LVGL task");
xTaskCreate(st7789_lvgl_port_task, "LVGL", ST7789_LVGL_TASK_STACK_SIZE, NULL, ST7789_LVGL_TASK_PRIORITY, NULL);
xTaskCreate([](void *arg) {
static_cast<St7789Display*>(arg)->LvglTask();
vTaskDelete(NULL);
}, "LVGL", ST7789_LVGL_TASK_STACK_SIZE, this, ST7789_LVGL_TASK_PRIORITY, NULL);

SetBacklight(100);

SetupUI();
}

St7789Display::~St7789Display() {
ESP_ERROR_CHECK(esp_timer_stop(lvgl_tick_timer_));
ESP_ERROR_CHECK(esp_timer_delete(lvgl_tick_timer_));

if (content_ != nullptr) {
lv_obj_del(content_);
}
Expand All @@ -214,7 +204,7 @@ St7789Display::~St7789Display() {
if (panel_io_ != nullptr) {
esp_lcd_panel_io_del(panel_io_);
}
lvgl_port_deinit();
vSemaphoreDelete(lvgl_mutex_);
}

void St7789Display::InitializeBacklight(gpio_num_t backlight_pin) {
Expand Down Expand Up @@ -264,21 +254,21 @@ void St7789Display::SetBacklight(uint8_t brightness) {
ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LCD_LEDC_CH));
}

void St7789Display::Lock() {
// lvgl_port_lock(0);
st7789_lvgl_lock(0);
bool St7789Display::Lock(int timeout_ms) {
// Convert timeout in milliseconds to FreeRTOS ticks
// If `timeout_ms` is set to 0, the program will block until the condition is met
const TickType_t timeout_ticks = (timeout_ms == 0) ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms);
return xSemaphoreTakeRecursive(lvgl_mutex_, timeout_ticks) == pdTRUE;
}

void St7789Display::Unlock() {
// lvgl_port_unlock();
st7789_lvgl_unlock();
xSemaphoreGiveRecursive(lvgl_mutex_);
}

void St7789Display::SetupUI() {
DisplayLockGuard lock(this);

auto screen = lv_disp_get_scr_act(lv_disp_get_default());
// auto screen = lv_scr_act();
lv_obj_set_style_text_font(screen, &font_puhui_14_1, 0);
lv_obj_set_style_text_color(screen, lv_color_black(), 0);

Expand Down
10 changes: 9 additions & 1 deletion main/display/st7789_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

#include "display.h"

#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <driver/gpio.h>
#include <esp_lcd_panel_io.h>
#include <esp_lcd_panel_ops.h>
#include <esp_timer.h>

class St7789Display : public Display {
private:
Expand All @@ -16,6 +19,10 @@ class St7789Display : public Display {
bool mirror_x_ = false;
bool mirror_y_ = false;
bool swap_xy_ = false;
int offset_x_ = 0;
int offset_y_ = 0;
SemaphoreHandle_t lvgl_mutex_ = nullptr;
esp_timer_handle_t lvgl_tick_timer_ = nullptr;

lv_obj_t* status_bar_ = nullptr;
lv_obj_t* content_ = nullptr;
Expand All @@ -25,8 +32,9 @@ class St7789Display : public Display {
void InitializeBacklight(gpio_num_t backlight_pin);
void SetBacklight(uint8_t brightness);
void SetupUI();
void LvglTask();

virtual void Lock() override;
virtual bool Lock(int timeout_ms = 0) override;
virtual void Unlock() override;

public:
Expand Down

0 comments on commit 37773c9

Please sign in to comment.