337 lines
11 KiB
C++
337 lines
11 KiB
C++
#include "display/lvgl_handler.h"
|
|
#include "esp_log.h"
|
|
#include "common/semaphore_guard.h"
|
|
#include "common/constants.h"
|
|
#include <portmacro.h>
|
|
|
|
#define DISPLAY_BUFFER_SIZE (DISPLAY_WIDTH * DISPLAY_HEIGHT) / 8 // 1 bit per pixels
|
|
#define LVGL_BUFFER_SIZE (DISPLAY_BUFFER_SIZE + 8) // 1 bit per pixels + 8 bytes for palette
|
|
#define LV_DISPLAY_RENDER_MODE LV_DISPLAY_RENDER_MODE_PARTIAL
|
|
#define TAG "LVGLHandler"
|
|
|
|
LVGLHandler::LVGLHandler(
|
|
std::unique_ptr<EInkDisplayHandler> display_handler_in
|
|
) : display_handler_(std::move(display_handler_in)) { }
|
|
|
|
LVGLHandler::~LVGLHandler() {
|
|
if (lvgl_display_ != nullptr) {
|
|
lv_display_delete(lvgl_display_);
|
|
lvgl_display_ = nullptr;
|
|
}
|
|
if (lvgl_touch_indev_ != nullptr) {
|
|
lvgl_port_remove_touch(lvgl_touch_indev_);
|
|
lvgl_touch_indev_ = nullptr;
|
|
}
|
|
if (lvgl_draw_buf_ != nullptr) {
|
|
lv_draw_buf_destroy(lvgl_draw_buf_);
|
|
lvgl_draw_buf_ = nullptr;
|
|
}
|
|
if (lvgl_draw_buf_2_ != nullptr) {
|
|
lv_draw_buf_destroy(lvgl_draw_buf_2_);
|
|
lvgl_draw_buf_2_ = nullptr;
|
|
}
|
|
}
|
|
|
|
esp_err_t LVGLHandler::initLVGL(EventGroupHandle_t system_event_group) {
|
|
esp_err_t err = initLVGLPort_();
|
|
if (err != ESP_OK) {
|
|
return err;
|
|
}
|
|
err = initLVGLDisplay_();
|
|
if (err != ESP_OK) {
|
|
return err;
|
|
}
|
|
err = registerLVGLTouch_();
|
|
if (err != ESP_OK) {
|
|
return err;
|
|
}
|
|
|
|
|
|
auto lvgl_tick_timer_callback = [](TimerHandle_t xTimer) {
|
|
lv_tick_inc(5);
|
|
};
|
|
TickType_t lvgl_tick_period = pdMS_TO_TICKS(5);
|
|
if (lvgl_tick_period == 0) {
|
|
lvgl_tick_period = 1; // ensure at least 1 tick to avoid FreeRTOS assert
|
|
}
|
|
ESP_LOGV(TAG, "Creating LVGL tick timer with period %u ticks...\n", (unsigned)lvgl_tick_period);
|
|
TimerHandle_t lvgl_tick_timer = xTimerCreate(
|
|
"lvgl_tick_timer",
|
|
lvgl_tick_period,
|
|
pdTRUE,
|
|
NULL,
|
|
lvgl_tick_timer_callback
|
|
);
|
|
if (lvgl_tick_timer == NULL) {
|
|
ESP_LOGE("Main", "Failed to create LVGL tick timer");
|
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
|
return ESP_ERR_NO_MEM;
|
|
}
|
|
ESP_LOGV(TAG, "Starting LVGL tick timer...\n");
|
|
xTimerStart(lvgl_tick_timer, 0);
|
|
|
|
if (system_event_group != nullptr) {
|
|
xEventGroupSetBits(system_event_group, DISPLAY_READY_BIT | TOUCH_CALIBRATED_BIT);
|
|
}
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
//
|
|
// Private methods
|
|
//
|
|
|
|
void LVGLHandler::rounder_cb_(lv_display_t* disp, lv_area_t* area) {
|
|
// align x to byte boundary
|
|
area->x1 = (area->x1 & ~0x7);
|
|
area->x2 = (area->x2 | 0x7);
|
|
}
|
|
|
|
void LVGLHandler::flush_cb_(lv_display_t* disp, const lv_area_t* area, uint8_t* px_map) {
|
|
if (disp == nullptr || area == nullptr || px_map == nullptr) {
|
|
ESP_LOGE(TAG, "Null parameters in flush callback");
|
|
if (disp != nullptr) lv_display_flush_ready(disp);
|
|
return;
|
|
}
|
|
LVGLHandler* handler = static_cast<LVGLHandler*>(lv_display_get_user_data(disp));
|
|
if (handler == nullptr || handler->display_handler_ == nullptr) {
|
|
ESP_LOGE(TAG, "Invalid handler in flush callback");
|
|
lv_display_flush_ready(disp);
|
|
return;
|
|
}
|
|
uint8_t* pixel_data = px_map + 8; // Skip palette
|
|
//
|
|
ESP_LOGI(TAG, "Flush callback: x1=%d, y1=%d, x2=%d, y2=%d", area->x1, area->y1, area->x2, area->y2);
|
|
|
|
// copy data to framebuffer
|
|
int32_t area_w = lv_area_get_width(area);
|
|
int32_t area_h = lv_area_get_height(area);
|
|
if (area->x1 == 0 && area->y1 == 0 && area_w == DISPLAY_WIDTH && area_h == DISPLAY_HEIGHT) {
|
|
// revert the pixel data for e-ink (LVGL: 1=white, 0=black; E-Ink: 1=black, 0=white)
|
|
for (size_t i = 0; i < DISPLAY_BUFFER_SIZE; ++i) {
|
|
pixel_data[i] = ~pixel_data[i];
|
|
}
|
|
esp_err_t err = handler->display_handler_->full_write(
|
|
pixel_data,
|
|
true // white basemap
|
|
);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "Full refresh request failed: %s", esp_err_to_name(err));
|
|
}
|
|
} else {
|
|
// partial update
|
|
ESP_LOGI(TAG, "Partial update: x1=%d, y1=%d, w=%d, h=%d", area->x1, area->y1, area_w, area_h);
|
|
|
|
// Prepare partial buffer
|
|
const uint32_t area_width_bytes = (area->x2 - area->x1 + 1) / 8;
|
|
const uint32_t area_height = area->y2 - area->y1 + 1;
|
|
const size_t partial_buffer_size = area_width_bytes * area_height;
|
|
uint8_t* partial_buffer = new uint8_t[partial_buffer_size];
|
|
if (partial_buffer == nullptr) {
|
|
ESP_LOGE(TAG, "Failed to allocate partial buffer for flush callback");
|
|
lv_display_flush_ready(disp);
|
|
return;
|
|
}
|
|
// Copy pixel data to partial buffer and invert for e-ink
|
|
for (int32_t row = 0; row < area_height; ++row) {
|
|
for (int32_t col = 0; col < area_width_bytes; ++col) {
|
|
size_t src_index = row * area_width_bytes + col;
|
|
partial_buffer[src_index] = ~pixel_data[src_index];
|
|
}
|
|
}
|
|
|
|
esp_err_t err = handler->display_handler_->partial_refresh(partial_buffer,
|
|
RefreshArea {
|
|
area->x1,
|
|
area->y1,
|
|
area->x2,
|
|
area->y2
|
|
}, lv_display_flush_is_last(disp));
|
|
delete[] partial_buffer;
|
|
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "Partial refresh request failed: %s", esp_err_to_name(err));
|
|
}
|
|
}
|
|
//
|
|
lv_display_flush_ready(disp);
|
|
}
|
|
|
|
void LVGLHandler::touch_read_cb_(lv_indev_t* indev, lv_indev_data_t* data) {
|
|
LVGLHandler* handler = static_cast<LVGLHandler*>(lv_indev_get_user_data(indev));
|
|
if (handler == nullptr || handler->display_handler_ == nullptr) {
|
|
data->state = LV_INDEV_STATE_RELEASED;
|
|
ESP_LOGE(TAG, "Invalid handler in touch read callback");
|
|
return;
|
|
}
|
|
|
|
// Disable touch input during display refresh (BUSY)
|
|
if (handler->display_handler_->is_busy()) {
|
|
data->state = LV_INDEV_STATE_RELEASED;
|
|
data->continue_reading = false;
|
|
return;
|
|
}
|
|
|
|
esp_lcd_touch_handle_t tp_handle = handler->display_handler_->get_touch_handle();
|
|
if (tp_handle == nullptr) {
|
|
data->state = LV_INDEV_STATE_RELEASED;
|
|
return;
|
|
}
|
|
|
|
// Read touch data from GT911
|
|
esp_err_t ret = esp_lcd_touch_read_data(tp_handle);
|
|
if (ret == ESP_OK) {
|
|
uint8_t touch_cnt = 0;
|
|
// Get touch data using new API
|
|
esp_lcd_touch_point_data_t point_data[1];
|
|
esp_lcd_touch_get_data(tp_handle, point_data, &touch_cnt, 1);
|
|
|
|
if (touch_cnt > 0) {
|
|
// ESP_LOGI(TAG, "Touch data read successfully: x=%d, y=%d", point_data[0].x, point_data[0].y);
|
|
data->point.x = point_data[0].x;
|
|
data->point.y = point_data[0].y;
|
|
data->state = LV_INDEV_STATE_PRESSED;
|
|
} else {
|
|
data->state = LV_INDEV_STATE_RELEASED;
|
|
}
|
|
} else {
|
|
data->state = LV_INDEV_STATE_RELEASED;
|
|
}
|
|
|
|
data->continue_reading = false;
|
|
}
|
|
|
|
esp_err_t LVGLHandler::initLVGLDisplay_() {
|
|
if (display_handler_ == nullptr) {
|
|
return ESP_ERR_INVALID_STATE;
|
|
}
|
|
esp_err_t err = ESP_OK;
|
|
|
|
// Lock LVGL to prevent the timer task from accessing partially initialized display
|
|
if (!lvgl_port_lock(pdMS_TO_TICKS(5000))) {
|
|
ESP_LOGE(TAG, "Failed to lock LVGL port for display initialization");
|
|
return ESP_ERR_TIMEOUT;
|
|
}
|
|
|
|
// Create LVGL display
|
|
lvgl_display_ = lv_display_create(DISPLAY_WIDTH, DISPLAY_HEIGHT);
|
|
if (lvgl_display_ == nullptr) {
|
|
ESP_LOGE(TAG, "Failed to create LVGL display");
|
|
lvgl_port_unlock();
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
// Create two draw buffers for double buffering to improve performance
|
|
lvgl_draw_buf_ = lv_draw_buf_create(DISPLAY_WIDTH, DISPLAY_HEIGHT, LV_COLOR_FORMAT_I1, LV_STRIDE_AUTO);
|
|
if (lvgl_draw_buf_ == nullptr) {
|
|
ESP_LOGE(TAG, "Failed to create LVGL draw buffer 1");
|
|
lv_display_delete(lvgl_display_);
|
|
lvgl_display_ = nullptr;
|
|
lvgl_port_unlock();
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
lvgl_draw_buf_2_ = lv_draw_buf_create(DISPLAY_WIDTH, DISPLAY_HEIGHT, LV_COLOR_FORMAT_I1, LV_STRIDE_AUTO);
|
|
if (lvgl_draw_buf_2_ == nullptr) {
|
|
ESP_LOGE(TAG, "Failed to create LVGL draw buffer 2");
|
|
lv_draw_buf_destroy(lvgl_draw_buf_);
|
|
lvgl_draw_buf_ = nullptr;
|
|
lv_display_delete(lvgl_display_);
|
|
lvgl_display_ = nullptr;
|
|
lvgl_port_unlock();
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
// Set both buffers for double buffering
|
|
lv_display_set_draw_buffers(lvgl_display_, lvgl_draw_buf_, lvgl_draw_buf_2_);
|
|
lv_display_set_render_mode(lvgl_display_, LV_DISPLAY_RENDER_MODE);
|
|
|
|
//
|
|
// Configure LVGL display
|
|
lv_display_set_color_format(lvgl_display_, LV_COLOR_FORMAT_I1);
|
|
lv_display_set_user_data(lvgl_display_, this);
|
|
|
|
lv_display_add_event_cb(lvgl_display_, [](lv_event_t* e) {
|
|
LVGLHandler* handler = static_cast<LVGLHandler*>(lv_display_get_user_data(static_cast<lv_display_t*>(lv_event_get_target(e))));
|
|
if (handler != nullptr) {
|
|
handler->rounder_cb_(static_cast<lv_display_t*>(lv_event_get_target(e)),
|
|
static_cast<lv_area_t*>(lv_event_get_param(e)));
|
|
} else {
|
|
ESP_LOGE(TAG, "Invalid handler in rounder callback");
|
|
}
|
|
}, LV_EVENT_INVALIDATE_AREA, lvgl_display_);
|
|
|
|
lv_display_set_flush_cb(lvgl_display_, [](lv_display_t* disp, const lv_area_t* area, uint8_t* px_map) {
|
|
LVGLHandler* handler = static_cast<LVGLHandler*>(lv_display_get_user_data(disp));
|
|
if (handler != nullptr) {
|
|
handler->flush_cb_(disp, area, px_map);
|
|
} else {
|
|
lv_display_flush_ready(disp);
|
|
}
|
|
});
|
|
|
|
// Unlock LVGL now that display is fully initialized
|
|
|
|
ESP_LOGI(TAG, "Performing initial display write...");
|
|
// err = display_handler_->full_write(framebuffer_, false);
|
|
err = display_handler_->clear_display();
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "Initial display write failed: %d", err);
|
|
} else {
|
|
ESP_LOGI(TAG, "Initial display write complete");
|
|
}
|
|
|
|
lvgl_port_unlock();
|
|
|
|
ESP_LOGI(TAG, "LVGL display registered");
|
|
return err;
|
|
}
|
|
|
|
esp_err_t LVGLHandler::registerLVGLTouch_() {
|
|
if (display_handler_ == nullptr) {
|
|
return ESP_ERR_INVALID_STATE;
|
|
}
|
|
esp_lcd_touch_handle_t tp_handle = display_handler_->get_touch_handle();
|
|
if (tp_handle == nullptr) {
|
|
ESP_LOGE(TAG, "Touch handle is NULL — touch initialization failed; skipping LVGL touch registration");
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
const lvgl_port_touch_cfg_t touch_cfg = {
|
|
.disp = lvgl_display_,
|
|
.handle = tp_handle,
|
|
.scale = {}, // Default scaling
|
|
};
|
|
|
|
lvgl_touch_indev_ = lvgl_port_add_touch(&touch_cfg);
|
|
if (lvgl_touch_indev_ == nullptr) {
|
|
ESP_LOGE(TAG, "Failed to register LVGL touch input");
|
|
return ESP_FAIL;
|
|
}
|
|
|
|
lv_indev_set_user_data(lvgl_touch_indev_, this);
|
|
lv_indev_set_read_cb(lvgl_touch_indev_, [](lv_indev_t* indev, lv_indev_data_t* data) {
|
|
LVGLHandler* handler = static_cast<LVGLHandler*>(lv_indev_get_user_data(indev));
|
|
if (handler != nullptr) {
|
|
handler->touch_read_cb_(indev, data);
|
|
} else {
|
|
data->state = LV_INDEV_STATE_RELEASED;
|
|
}
|
|
});
|
|
|
|
ESP_LOGI(TAG, "LVGL touch input registered");
|
|
return ESP_OK;
|
|
}
|
|
|
|
esp_err_t LVGLHandler::initLVGLPort_() {
|
|
const lvgl_port_cfg_t lvgl_cfg = ESP_LVGL_PORT_INIT_CONFIG();
|
|
esp_err_t err = lvgl_port_init(&lvgl_cfg);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "LVGL port initialization failed: %s", esp_err_to_name(err));
|
|
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
|
return ESP_ERR_INVALID_STATE;
|
|
}
|
|
ESP_LOGI(TAG, "LVGL port initialized successfully.\n");
|
|
return ESP_OK;
|
|
}
|