Enhance EInkDisplayHandler and LVGLHandler with deep sleep functionality and partial refresh improvements

This commit is contained in:
GW_MC
2026-01-27 19:12:37 +08:00
parent dc2a76e131
commit de2f166151
5 changed files with 343 additions and 80 deletions

View File

@@ -6,6 +6,7 @@
#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(
@@ -96,9 +97,14 @@ void LVGLHandler::rounder_cb_(lv_display_t* disp, lv_area_t* area) {
}
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");
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;
}
@@ -117,60 +123,81 @@ void LVGLHandler::flush_cb_(lv_display_t* disp, const lv_area_t* area, uint8_t*
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(framebuffer_, pixel_data, DISPLAY_BUFFER_SIZE);
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_->refresh_display();
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);
// 1. Calculate Strides
int32_t fb_stride_bytes = DISPLAY_WIDTH / 8; // Stride of the full framebuffer
int32_t src_stride_bytes = area_w / 8; // Stride of the LVGL partial buffer
// 2. Safety: Ensure we don't write out of bounds
// (The rounder_cb should prevent this, but clipping is safe practice)
int32_t safe_h = area_h;
if (area->y1 + safe_h > DISPLAY_HEIGHT) {
safe_h = DISPLAY_HEIGHT - area->y1;
ESP_LOGW(TAG, "Clipping height to %d to prevent OOB", safe_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);
//
// 3. Iterate Rows
uint8_t* partial_buffer = new uint8_t[src_stride_bytes * safe_h];
if (partial_buffer == nullptr) {
ESP_LOGE(TAG, "Failed to allocate partial buffer for refresh");
lv_display_flush_ready(disp);
return;
}
for (int32_t y = 0; y < safe_h; y++) {
// Calculate Absolute Y in Framebuffer
int32_t fb_y = area->y1 + y;
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;
// Calculate Source Pointer (Start of current line in partial buffer)
// NOTE: Use src_stride_bytes, NOT fb_stride_bytes
uint8_t* src_line = pixel_data + (y * src_stride_bytes);
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];
}
}
// Calculate Destination Pointer (Start of current line in full framebuffer)
// Offset = (Row * Stride) + (X_Start_Byte)
uint8_t* dst_line = handler->framebuffer_ + (fb_y * fb_stride_bytes) + (area->x1 / 8);
esp_err_t err = handler->display_handler_->partial_refresh(partial_buffer,
handler->refresh_area_);
delete[] partial_buffer;
// 4. Block Copy
// Since x1 is byte-aligned by rounder_cb, we can copy the whole row directly.
memcpy(dst_line, src_line, src_stride_bytes);
// also copy to partial_buffer for refresh
memcpy(partial_buffer + (y * src_stride_bytes), src_line, src_stride_bytes);
}
// 5. Request partial refresh
esp_err_t err = handler->display_handler_->partial_refresh(partial_buffer,
RefreshArea(area->x1, area->y1, area->x2, area->y2));
delete[] partial_buffer;
if (err != ESP_OK) {
ESP_LOGE(TAG, "Partial refresh request failed: %s", esp_err_to_name(err));
if (err != ESP_OK) {
ESP_LOGE(TAG, "Partial refresh request failed: %s", esp_err_to_name(err));
}
handler->refresh_area_.reset();
}
}
//
@@ -228,7 +255,7 @@ esp_err_t LVGLHandler::initLVGLDisplay_() {
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(1000))) {
if (!lvgl_port_lock(pdMS_TO_TICKS(5000))) {
ESP_LOGE(TAG, "Failed to lock LVGL port for display initialization");
return ESP_ERR_TIMEOUT;
}
@@ -237,6 +264,7 @@ esp_err_t LVGLHandler::initLVGLDisplay_() {
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;
}
@@ -251,6 +279,8 @@ esp_err_t LVGLHandler::initLVGLDisplay_() {
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;
}
@@ -261,20 +291,16 @@ esp_err_t LVGLHandler::initLVGLDisplay_() {
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_DIRECT);
lv_display_set_render_mode(lvgl_display_, LV_DISPLAY_RENDER_MODE);
//
ESP_LOGI(TAG, "Performing initial display write...");
err = display_handler_->full_write(framebuffer_);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Initial display write failed: %d", err);
} else {
ESP_LOGI(TAG, "Initial display write complete");
}
// Configure LVGL display
lv_display_set_color_format(lvgl_display_, LV_COLOR_FORMAT_I1);
lv_display_set_user_data(lvgl_display_, this);
@@ -299,6 +325,16 @@ esp_err_t LVGLHandler::initLVGLDisplay_() {
});
// 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");