From a30de954aec22df4ee8da3969a424df02e3a6a33 Mon Sep 17 00:00:00 2001 From: Ken Van Hoeylandt Date: Fri, 15 Aug 2025 22:20:27 +0200 Subject: [PATCH] Update RgbDisplay driver --- Drivers/RgbDisplay/CMakeLists.txt | 2 +- Drivers/RgbDisplay/Source/RgbDisplay.cpp | 77 +++++++++++++++++------- Drivers/RgbDisplay/Source/RgbDisplay.h | 30 ++++++--- 3 files changed, 78 insertions(+), 31 deletions(-) diff --git a/Drivers/RgbDisplay/CMakeLists.txt b/Drivers/RgbDisplay/CMakeLists.txt index 8fd62cfe..f0e2b6c2 100644 --- a/Drivers/RgbDisplay/CMakeLists.txt +++ b/Drivers/RgbDisplay/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRC_DIRS "Source" INCLUDE_DIRS "Source" - REQUIRES Tactility esp_lvgl_port esp_lcd + REQUIRES Tactility EspLcdCompat ) diff --git a/Drivers/RgbDisplay/Source/RgbDisplay.cpp b/Drivers/RgbDisplay/Source/RgbDisplay.cpp index d75da726..f8cc2c2a 100644 --- a/Drivers/RgbDisplay/Source/RgbDisplay.cpp +++ b/Drivers/RgbDisplay/Source/RgbDisplay.cpp @@ -6,8 +6,16 @@ #include #include #include +#include +#include -#define TAG "RgbDisplay" +constexpr auto TAG = "RgbDisplay"; + +RgbDisplay::~RgbDisplay() { + if (nativeDisplay != nullptr && nativeDisplay.use_count() > 1) { + tt_crash("NativeDisplay is still in use. This will cause memory access violations."); + } +} bool RgbDisplay::start() { TT_LOG_I(TAG, "Starting"); @@ -42,25 +50,47 @@ bool RgbDisplay::start() { return false; } - auto horizontal_resolution = configuration->panelConfig.timings.h_res; - auto vertical_resolution = configuration->panelConfig.timings.v_res; + return true; +} - uint32_t buffer_size; - if (configuration->bufferConfiguration.size == 0) { - buffer_size = horizontal_resolution * vertical_resolution / 15; - } else { - buffer_size = configuration->bufferConfiguration.size; +bool RgbDisplay::stop() { + if (lvglDisplay != nullptr) { + stopLvgl(); + lvglDisplay = nullptr; + } + + if (panelHandle != nullptr && esp_lcd_panel_del(panelHandle) != ESP_OK) { + return false; + } + + if (ioHandle != nullptr && esp_lcd_panel_io_del(ioHandle) != ESP_OK) { + return false; + } + + if (nativeDisplay != nullptr && nativeDisplay.use_count() > 1) { + TT_LOG_W(TAG, "NativeDisplay is still in use."); + } + + return true; +} + + +bool RgbDisplay::startLvgl() { + assert(lvglDisplay == nullptr); + + if (nativeDisplay != nullptr && nativeDisplay.use_count() > 1) { + TT_LOG_W(TAG, "NativeDisplay is still in use."); } const lvgl_port_display_cfg_t display_config = { .io_handle = ioHandle, .panel_handle = panelHandle, .control_handle = nullptr, - .buffer_size = buffer_size, + .buffer_size = configuration->bufferConfiguration.size, .double_buffer = configuration->bufferConfiguration.doubleBuffer, .trans_size = 0, - .hres = horizontal_resolution, - .vres = vertical_resolution, + .hres = configuration->panelConfig.timings.h_res, + .vres = configuration->panelConfig.timings.v_res, .monochrome = false, .rotation = { .swap_xy = configuration->swapXY, @@ -85,25 +115,28 @@ bool RgbDisplay::start() { } }; - displayHandle = lvgl_port_add_disp_rgb(&display_config, &rgb_config); + lvglDisplay = lvgl_port_add_disp_rgb(&display_config, &rgb_config); TT_LOG_I(TAG, "Finished"); - return displayHandle != nullptr; + auto touch_device = getTouchDevice(); + if (touch_device != nullptr) { + touch_device->startLvgl(lvglDisplay); + } + + return lvglDisplay != nullptr; } -bool RgbDisplay::stop() { - assert(displayHandle != nullptr); - - lvgl_port_remove_disp(displayHandle); - - if (esp_lcd_panel_del(panelHandle) != ESP_OK) { +bool RgbDisplay::stopLvgl() { + if (lvglDisplay == nullptr) { return false; } - if (esp_lcd_panel_io_del(ioHandle) != ESP_OK) { - return false; + auto touch_device = getTouchDevice(); + if (touch_device != nullptr) { + touch_device->stopLvgl(); } - displayHandle = nullptr; + lvgl_port_remove_disp(lvglDisplay); + lvglDisplay = nullptr; return true; } diff --git a/Drivers/RgbDisplay/Source/RgbDisplay.h b/Drivers/RgbDisplay/Source/RgbDisplay.h index 3da18c35..2e41df47 100644 --- a/Drivers/RgbDisplay/Source/RgbDisplay.h +++ b/Drivers/RgbDisplay/Source/RgbDisplay.h @@ -51,16 +51,22 @@ public: mirrorX(mirrorX), mirrorY(mirrorY), invertColor(invertColor), - backlightDutyFunction(std::move(backlightDutyFunction)) - {} + backlightDutyFunction(std::move(backlightDutyFunction)) { + if (this->bufferConfiguration.size == 0) { + auto horizontal_resolution = panelConfig.timings.h_res; + auto vertical_resolution = panelConfig.timings.v_res; + this->bufferConfiguration.size = horizontal_resolution * vertical_resolution / 15; + } + } }; private: - std::unique_ptr configuration = nullptr; - esp_lcd_panel_io_handle_t ioHandle = nullptr; - esp_lcd_panel_handle_t panelHandle = nullptr; - lv_display_t* displayHandle = nullptr; + std::unique_ptr _Nullable configuration = nullptr; + esp_lcd_panel_io_handle_t _Nullable ioHandle = nullptr; + esp_lcd_panel_handle_t _Nullable panelHandle = nullptr; + lv_display_t* _Nullable lvglDisplay = nullptr; + std::shared_ptr _Nullable nativeDisplay; public: @@ -68,6 +74,8 @@ public: assert(configuration != nullptr); } + ~RgbDisplay(); + std::string getName() const final { return "RGB Display"; } std::string getDescription() const final { return "RGB Display"; } @@ -75,7 +83,13 @@ public: bool stop() override; - std::shared_ptr _Nullable createTouch() final { return configuration->touch; } + bool supportsLvgl() const override { return true; } + + bool startLvgl() override; + + bool stopLvgl() override; + + std::shared_ptr _Nullable getTouchDevice() final { return configuration->touch; } void setBacklightDuty(uint8_t backlightDuty) final { if (configuration->backlightDutyFunction != nullptr) { @@ -85,7 +99,7 @@ public: bool supportsBacklightDuty() const final { return configuration->backlightDutyFunction != nullptr; } - lv_display_t* _Nullable getLvglDisplay() const override { return displayHandle; } + lv_display_t* _Nullable getLvglDisplay() const override { return lvglDisplay; } }; std::shared_ptr createDisplay();