Tactility/Drivers/EspLcdCompat/Source/EspLcdDisplayV2.cpp
Ken Van Hoeylandt db6d3b4acb
Merge develop into main (#391)
## Improvements

- Created new base driver classes: `EspLcdDisplayV2' and `EspLcdSpiDisplay`
- Updated `St7789Display` to implement `EspLcdSpiDisplay`
- Updated all boards with ST7789 display

## Fixes

- Ensure that `tmp/` is created on startup (for all writeable filesystems)
- Fix for `lv_list` padding on small screen devices
- Fix for `PreferencesEsp` not processing result when writing string to NVS

## Other

- Remove unused build scripts
2025-10-26 13:50:17 +01:00

227 lines
7.3 KiB
C++

#include "EspLcdDisplayV2.h"
#include "EspLcdDisplayDriver.h"
#include <assert.h>
#include <esp_lvgl_port_disp.h>
#include <Tactility/Check.h>
#include <Tactility/LogEsp.h>
#include <Tactility/hal/touch/TouchDevice.h>
constexpr auto* TAG = "EspLcdDispV2";
inline unsigned int getBufferSize(const std::shared_ptr<EspLcdConfiguration>& configuration) {
if (configuration->bufferSize != DEFAULT_BUFFER_SIZE) {
return configuration->bufferSize;
} else {
return configuration->horizontalResolution * (configuration->verticalResolution / 10);
}
}
EspLcdDisplayV2::~EspLcdDisplayV2() {
if (displayDriver != nullptr && displayDriver.use_count() > 1) {
tt_crash("DisplayDriver is still in use. This will cause memory access violations.");
}
}
bool EspLcdDisplayV2::applyConfiguration() const {
if (esp_lcd_panel_reset(panelHandle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to reset panel");
return false;
}
if (esp_lcd_panel_init(panelHandle) != ESP_OK) {
TT_LOG_E(TAG, "Failed to init panel");
return false;
}
if (esp_lcd_panel_invert_color(panelHandle, configuration->invertColor) != ESP_OK) {
TT_LOG_E(TAG, "Failed to set panel to invert");
return false;
}
// Warning: it looks like LVGL rotation is broken when "gap" is set and the screen is moved to a non-default orientation
int gap_x = configuration->swapXY ? configuration->gapY : configuration->gapX;
int gap_y = configuration->swapXY ? configuration->gapX : configuration->gapY;
if (esp_lcd_panel_set_gap(panelHandle, gap_x, gap_y) != ESP_OK) {
TT_LOG_E(TAG, "Failed to set panel gap");
return false;
}
if (esp_lcd_panel_swap_xy(panelHandle, configuration->swapXY) != ESP_OK) {
TT_LOG_E(TAG, "Failed to swap XY ");
return false;
}
if (esp_lcd_panel_mirror(panelHandle, configuration->mirrorX, configuration->mirrorY) != ESP_OK) {
TT_LOG_E(TAG, "Failed to set panel to mirror");
return false;
}
if (esp_lcd_panel_invert_color(panelHandle, configuration->invertColor) != ESP_OK) {
TT_LOG_E(TAG, "Failed to set panel to invert");
return false;
}
if (esp_lcd_panel_disp_on_off(panelHandle, true) != ESP_OK) {
TT_LOG_E(TAG, "Failed to turn display on");
return false;
}
return true;
}
bool EspLcdDisplayV2::start() {
if (!createIoHandle(ioHandle)) {
TT_LOG_E(TAG, "Failed to create IO handle");
return false;
}
esp_lcd_panel_dev_config_t panel_config = createPanelConfig(configuration, configuration->resetPin);
if (!createPanelHandle(ioHandle, panel_config, panelHandle)) {
TT_LOG_E(TAG, "Failed to create panel handle");
esp_lcd_panel_io_del(ioHandle);
ioHandle = nullptr;
return false;
}
if (!applyConfiguration()) {
esp_lcd_panel_del(panelHandle);
panelHandle = nullptr;
esp_lcd_panel_io_del(ioHandle);
ioHandle = nullptr;
return false;
}
return true;
}
bool EspLcdDisplayV2::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 (displayDriver != nullptr && displayDriver.use_count() > 1) {
TT_LOG_W(TAG, "DisplayDriver is still in use.");
}
return true;
}
bool EspLcdDisplayV2::startLvgl() {
assert(lvglDisplay == nullptr);
if (displayDriver != nullptr && displayDriver.use_count() > 1) {
TT_LOG_W(TAG, "DisplayDriver is still in use.");
}
auto lvgl_port_config = getLvglPortDisplayConfig(configuration, ioHandle, panelHandle);
if (isRgbPanel()) {
auto rgb_config = getLvglPortDisplayRgbConfig(ioHandle, panelHandle);
lvglDisplay = lvgl_port_add_disp_rgb(&lvgl_port_config , &rgb_config);
} else {
lvglDisplay = lvgl_port_add_disp(&lvgl_port_config );
}
auto touch_device = getTouchDevice();
if (touch_device != nullptr && touch_device->supportsLvgl()) {
touch_device->startLvgl(lvglDisplay);
}
return lvglDisplay != nullptr;
}
bool EspLcdDisplayV2::stopLvgl() {
if (lvglDisplay == nullptr) {
return false;
}
auto touch_device = getTouchDevice();
if (touch_device != nullptr) {
touch_device->stopLvgl();
}
lvgl_port_remove_disp(lvglDisplay);
lvglDisplay = nullptr;
return true;
}
lvgl_port_display_cfg_t EspLcdDisplayV2::getLvglPortDisplayConfig(std::shared_ptr<EspLcdConfiguration> configuration, esp_lcd_panel_io_handle_t ioHandle, esp_lcd_panel_handle_t panelHandle) {
return lvgl_port_display_cfg_t {
.io_handle = ioHandle,
.panel_handle = panelHandle,
.control_handle = nullptr,
.buffer_size = getBufferSize(configuration),
.double_buffer = false,
.trans_size = 0,
.hres = configuration->horizontalResolution,
.vres = configuration->verticalResolution,
.monochrome = configuration->monochrome,
.rotation = {
.swap_xy = configuration->swapXY,
.mirror_x = configuration->mirrorX,
.mirror_y = configuration->mirrorY,
},
.color_format = configuration->lvglColorFormat,
.flags = {
.buff_dma = 1,
.buff_spiram = 0,
.sw_rotate = 0,
.swap_bytes = configuration->lvglSwapBytes,
.full_refresh = 0,
.direct_mode = 0
}
};
}
std::shared_ptr<tt::hal::display::DisplayDriver> EspLcdDisplayV2::getDisplayDriver() {
assert(lvglDisplay == nullptr); // Still attached to LVGL context. Call stopLvgl() first.
if (displayDriver == nullptr) {
auto lvgl_port_config = getLvglPortDisplayConfig(configuration, ioHandle, panelHandle);
auto panel_config = createPanelConfig(configuration, GPIO_NUM_NC);
tt::hal::display::ColorFormat color_format;
if (lvgl_port_config.color_format == LV_COLOR_FORMAT_I1) {
color_format = tt::hal::display::ColorFormat::Monochrome;
} else if (lvgl_port_config.color_format == LV_COLOR_FORMAT_RGB565) {
if (panel_config.rgb_ele_order == LCD_RGB_ELEMENT_ORDER_RGB) {
if (lvgl_port_config.flags.swap_bytes) {
color_format = tt::hal::display::ColorFormat::RGB565Swapped;
} else {
color_format = tt::hal::display::ColorFormat::RGB565;
}
} else {
if (lvgl_port_config.flags.swap_bytes) {
color_format = tt::hal::display::ColorFormat::BGR565Swapped;
} else {
color_format = tt::hal::display::ColorFormat::BGR565;
}
}
} else if (lvgl_port_config.color_format == LV_COLOR_FORMAT_RGB888) {
color_format = tt::hal::display::ColorFormat::RGB888;
} else {
tt_crash("unsupported driver");
}
displayDriver = std::make_shared<EspLcdDisplayDriver>(
panelHandle,
lock,
lvgl_port_config.hres,
lvgl_port_config.vres,
color_format
);
}
return displayDriver;
}