Squash of branch setup
This commit is contained in:
390
main/display/lvgl_handler.cpp
Normal file
390
main/display/lvgl_handler.cpp
Normal file
@@ -0,0 +1,390 @@
|
||||
#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)) {
|
||||
lvgl_mutex_ = xSemaphoreCreateMutex();
|
||||
if (lvgl_mutex_ == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create LVGL mutex");
|
||||
}
|
||||
}
|
||||
|
||||
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 (framebuffer_ != nullptr) {
|
||||
heap_caps_free(framebuffer_);
|
||||
framebuffer_ = nullptr;
|
||||
}
|
||||
if (lvgl_mutex_ != nullptr) {
|
||||
vSemaphoreDelete(lvgl_mutex_);
|
||||
lvgl_mutex_ = 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 || handler->framebuffer_ == nullptr) {
|
||||
ESP_LOGE(TAG, "Invalid handler or framebuffer 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);
|
||||
// take mutex
|
||||
SemaphoreGuard guard(handler->lvgl_mutex_);
|
||||
if (!guard.take(pdMS_TO_TICKS(5000))) {
|
||||
ESP_LOGE(TAG, "LVGL mutex timeout in flush callback");
|
||||
lv_display_flush_ready(disp);
|
||||
return;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
// Check if content actually changed before triggering expensive e-ink refresh
|
||||
if (memcmp(handler->framebuffer_, pixel_data, DISPLAY_BUFFER_SIZE) == 0) {
|
||||
ESP_LOGD(TAG, "Full screen flush with no changes - skipping e-ink refresh");
|
||||
lv_display_flush_ready(disp);
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(TAG, "Full screen update");
|
||||
memcpy(handler->framebuffer_, pixel_data, DISPLAY_BUFFER_SIZE);
|
||||
// invert the framebuffer for e-ink display
|
||||
for (size_t i = 0; i < DISPLAY_BUFFER_SIZE; ++i) {
|
||||
handler->framebuffer_[i] = ~handler->framebuffer_[i];
|
||||
}
|
||||
// request full refresh
|
||||
esp_err_t err = handler->display_handler_->full_write(handler->framebuffer_, true);
|
||||
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);
|
||||
// update the framebuffer with the partial data
|
||||
for (int32_t row = 0; row < area_h; ++row) {
|
||||
int32_t fb_y = area->y1 + row;
|
||||
int32_t fb_x_byte_start = area->x1 / 8;
|
||||
int32_t fb_x_byte_end = area->x2 / 8;
|
||||
uint8_t* fb_ptr = &handler->framebuffer_[fb_y * (DISPLAY_WIDTH / 8) + fb_x_byte_start];
|
||||
const uint8_t* src_ptr = &pixel_data[row * (area_w / 8)];
|
||||
// invert the partial framebuffer data for e-ink display
|
||||
for (int32_t i = 0; i < (fb_x_byte_end - fb_x_byte_start + 1); ++i) {
|
||||
fb_ptr[i] = ~src_ptr[i];
|
||||
}
|
||||
}
|
||||
// update the refresh area
|
||||
handler->refresh_area_.expand_to_include(area->x1, area->y1, area->x2, area->y2);
|
||||
//
|
||||
|
||||
if (lv_display_flush_is_last(disp) && !handler->refresh_area_.is_empty()) {
|
||||
ESP_LOGI(TAG, "Last flush in batch - performing partial refresh");
|
||||
ESP_LOGI(TAG, "Refresh area: x1=%d, y1=%d, x2=%d, y2=%d",
|
||||
handler->refresh_area_.x1, handler->refresh_area_.y1,
|
||||
handler->refresh_area_.x2, handler->refresh_area_.y2);
|
||||
// copy the area to refresh
|
||||
uint8_t* partial_buffer = new uint8_t[handler->refresh_area_.area() / 8];
|
||||
if (partial_buffer == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to allocate partial buffer for refresh");
|
||||
lv_display_flush_ready(disp);
|
||||
return;
|
||||
}
|
||||
// loop the refresh area and copy data
|
||||
uint32_t x1 = handler->refresh_area_.x1;
|
||||
uint32_t x2 = handler->refresh_area_.x2;
|
||||
uint32_t y1 = handler->refresh_area_.y1;
|
||||
uint32_t y2 = handler->refresh_area_.y2;
|
||||
uint32_t height = y2 - y1 + 1;
|
||||
uint32_t width = x2 - x1 + 1;
|
||||
|
||||
for (uint32_t row = 0; row < height; ++row) {
|
||||
uint32_t fb_y = y1 + row;
|
||||
uint32_t fb_x_byte_start = x1 / 8;
|
||||
uint32_t fb_x_byte_end = x2 / 8;
|
||||
uint8_t* fb_ptr = &handler->framebuffer_[fb_y * (DISPLAY_WIDTH / 8) + fb_x_byte_start];
|
||||
uint8_t* dest_ptr = &partial_buffer[row * (width / 8)];
|
||||
for (uint32_t i = 0; i < (fb_x_byte_end - fb_x_byte_start + 1); ++i) {
|
||||
dest_ptr[i] = ~fb_ptr[i];
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t err = handler->display_handler_->partial_refresh(partial_buffer,
|
||||
handler->refresh_area_);
|
||||
delete[] partial_buffer;
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Partial refresh request failed: %s", esp_err_to_name(err));
|
||||
}
|
||||
handler->refresh_area_.reset();
|
||||
}
|
||||
}
|
||||
//
|
||||
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;
|
||||
}
|
||||
|
||||
// set framebuffer
|
||||
framebuffer_ = (uint8_t*)heap_caps_malloc(LVGL_BUFFER_SIZE, MALLOC_CAP_SPIRAM);
|
||||
if (framebuffer_ != nullptr) {
|
||||
framebuffer_in_psram_ = true;
|
||||
ESP_LOGI(TAG, "Framebuffer allocated in PSRAM (%zu bytes)", LVGL_BUFFER_SIZE);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "PSRAM not available, allocating framebuffer in internal RAM");
|
||||
framebuffer_ = (uint8_t*)heap_caps_malloc(LVGL_BUFFER_SIZE, MALLOC_CAP_INTERNAL);
|
||||
framebuffer_in_psram_ = false;
|
||||
if (framebuffer_ == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to allocate framebuffer");
|
||||
lv_display_delete(lvgl_display_);
|
||||
lvgl_display_ = nullptr;
|
||||
lvgl_port_unlock();
|
||||
return ESP_FAIL;
|
||||
}
|
||||
ESP_LOGI(TAG, "Framebuffer allocated in internal RAM (%zu bytes)", LVGL_BUFFER_SIZE);
|
||||
}
|
||||
memset(framebuffer_, 0xFF, LVGL_BUFFER_SIZE); // Initialize to white
|
||||
// Create a draw buffer covering the entire display
|
||||
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");
|
||||
heap_caps_free(framebuffer_);
|
||||
framebuffer_ = nullptr;
|
||||
lv_display_delete(lvgl_display_);
|
||||
lvgl_display_ = nullptr;
|
||||
lvgl_port_unlock();
|
||||
return ESP_FAIL;
|
||||
}
|
||||
lv_display_set_draw_buffers(lvgl_display_, lvgl_draw_buf_, nullptr);
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user