#include "EspLcdDisplay.h" #include "EspLcdNativeDisplay.h" #include #include #include #include #include constexpr char* TAG = "EspLcdDisplay"; EspLcdDisplay::~EspLcdDisplay() { if (nativeDisplay != nullptr && nativeDisplay.use_count() > 1) { tt_crash("NativeDisplay is still in use. This will cause memory access violations."); } } bool EspLcdDisplay::start() { if (!createIoHandle(ioHandle)) { TT_LOG_E(TAG, "Failed to create IO handle"); return false; } if (!createPanelHandle(ioHandle, panelHandle)) { TT_LOG_E(TAG, "Failed to create panel handle"); esp_lcd_panel_io_del(ioHandle); return false; } return true; } bool EspLcdDisplay::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 EspLcdDisplay::startLvgl() { assert(lvglDisplay == nullptr); if (nativeDisplay != nullptr && nativeDisplay.use_count() > 1) { TT_LOG_W(TAG, "NativeDisplay is still in use."); } lvglPortDisplayConfig = getLvglPortDisplayConfig(ioHandle, panelHandle); lvglDisplay = lvgl_port_add_disp(&lvglPortDisplayConfig); auto touch_device = getTouchDevice(); if (touch_device != nullptr) { touch_device->start(lvglDisplay); } return lvglDisplay != nullptr; } bool EspLcdDisplay::stopLvgl() { if (lvglDisplay == nullptr) { return false; } auto touch_device = getTouchDevice(); if (touch_device != nullptr) { touch_device->stop(); } lvgl_port_remove_disp(lvglDisplay); lvglDisplay = nullptr; return true; } std::shared_ptr EspLcdDisplay::getNativeDisplay() { assert(lvglDisplay == nullptr); // Still attached to LVGL context. Call stopLvgl() first. if (nativeDisplay == nullptr) { nativeDisplay = std::make_shared( panelHandle, lvglPortDisplayConfig ); } return nativeDisplay; }