2 Commits

5 changed files with 384 additions and 86 deletions

View File

@@ -15,9 +15,12 @@
#define BUSY_INACTIVE_LEVEL 1
#define DMA_TRANSFER_CHUNK_SIZE 4096 // 4KB chunk size for DMA transfers
static uint8_t white_data[DISPLAY_BUFFER_SIZE] = { 0xFF }; // all white data
static uint8_t white_data[DISPLAY_BUFFER_SIZE]; // all white data
static uint8_t black_data[DISPLAY_BUFFER_SIZE]; // all black data
EInkDisplayHandler::EInkDisplayHandler() {
memset(white_data, 0xFF, sizeof(white_data));
memset(black_data, 0x00, sizeof(black_data));
spi_mutex_ = xSemaphoreCreateMutex();
if (spi_mutex_ == nullptr) {
ESP_LOGE(TAG, "Failed to create SPI mutex");
@@ -52,10 +55,52 @@ EInkDisplayHandler::~EInkDisplayHandler() {
}
}
esp_err_t EInkDisplayHandler::deep_sleep_display(void) {
ESP_LOGI(TAG, "Putting display into deep sleep mode...");
if (is_deep_sleep_) {
ESP_LOGI(TAG, "Display is already in deep sleep mode");
return ESP_OK;
}
{
esp_err_t err = ESP_OK;
TransactionGuard transaction_guard(*this);
err = transaction_guard.begin(pdMS_TO_TICKS(5000));
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to begin transaction for deep sleep: %s", esp_err_to_name(err));
return err;
}
wait_for_idle();
err = epd_write_cmd(0x02, transaction_guard.transaction_id()); // power off
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to send power off command: %s", esp_err_to_name(err));
return err;
}
wait_for_idle();
err = epd_write_cmd(0x07, transaction_guard.transaction_id()); //deep sleep
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to send deep sleep command: %s", esp_err_to_name(err));
return err;
}
err = epd_write_data(0xA5, transaction_guard.transaction_id());
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to send deep sleep data: %s", esp_err_to_name(err));
return err;
}
is_deep_sleep_ = true;
return err;
}
}
esp_err_t EInkDisplayHandler::refresh_display() {
esp_err_t err = ESP_OK;
ESP_LOGI(TAG, "Waiting for display to be idle...");
if (is_deep_sleep_) {
epd_init_();
}
{
ESP_LOGI(TAG, "Waiting for display to be idle...");
TransactionGuard transaction_guard(*this);
err = transaction_guard.begin(pdMS_TO_TICKS(10000));
if (err != ESP_OK) {
@@ -88,13 +133,24 @@ esp_err_t EInkDisplayHandler::refresh_display() {
force_full_refresh_ = false;
}
err = deep_sleep_display();
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to enter deep sleep after refresh: %s", esp_err_to_name(err));
return err;
}
ESP_LOGI(TAG, "Refresh complete");
return ESP_OK;
}
esp_err_t EInkDisplayHandler::full_write(const uint8_t* framebuffer) {
esp_err_t EInkDisplayHandler::full_write(const uint8_t* framebuffer, const bool white_basemap) {
ESP_LOGI(TAG, "Starting full refresh (3 seconds)...");
esp_err_t err = ESP_OK;
if (is_deep_sleep_) {
epd_init_();
}
{
TransactionGuard transaction_guard(*this);
err = transaction_guard.begin(pdMS_TO_TICKS(10000));
@@ -104,7 +160,12 @@ esp_err_t EInkDisplayHandler::full_write(const uint8_t* framebuffer) {
}
wait_for_idle();
// Step 0: Enter normal mode
err = epd_write_cmd(0x92, transaction_guard.transaction_id()); // enter normal mode
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to enter normal mode: %s", esp_err_to_name(err));
return err;
}
// Step 1: Write old data (0x10) - Arduino uses 0xFF (all white) for base map
{
err = epd_write_cmd(0x10, transaction_guard.transaction_id());
@@ -112,7 +173,7 @@ esp_err_t EInkDisplayHandler::full_write(const uint8_t* framebuffer) {
ESP_LOGE(TAG, "Failed to send old data command: %s", esp_err_to_name(err));
return err;
}
err = transfer_spi_data(white_data, DISPLAY_BUFFER_SIZE, transaction_guard.transaction_id()); // Send all white data
err = transfer_spi_data(white_basemap ? white_data : black_data, DISPLAY_BUFFER_SIZE, transaction_guard.transaction_id()); // Send all white data (0xFF)
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to send all white data for old data: %s", esp_err_to_name(err));
return err;
@@ -147,6 +208,12 @@ esp_err_t EInkDisplayHandler::full_write(const uint8_t* framebuffer) {
wait_for_idle();
}
err = deep_sleep_display();
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to enter deep sleep after full refresh: %s", esp_err_to_name(err));
return err;
}
ESP_LOGI(TAG, "Full refresh complete");
return ESP_OK;
}
@@ -155,6 +222,11 @@ esp_err_t EInkDisplayHandler::partial_refresh(const uint8_t* partial_framebuffer
ESP_LOGI(TAG, "Starting partial refresh (0.3 seconds)...");
esp_err_t err = ESP_OK;
// Calculate partial buffer size based on the refresh area
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;
{
TransactionGuard transaction_guard(*this);
err = transaction_guard.begin(pdMS_TO_TICKS(5000));
@@ -162,6 +234,16 @@ esp_err_t EInkDisplayHandler::partial_refresh(const uint8_t* partial_framebuffer
ESP_LOGE(TAG, "Failed to begin transaction for partial refresh: %s", esp_err_to_name(err));
return err;
}
// Wake display from deep sleep INSIDE the transaction to prevent race conditions
if (is_deep_sleep_) {
err = epd_init_partial_internal_(transaction_guard.transaction_id());
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize EPD for partial refresh: %s", esp_err_to_name(err));
return err;
}
}
wait_for_idle();
// Step 1 VCOM setting
@@ -183,22 +265,43 @@ esp_err_t EInkDisplayHandler::partial_refresh(const uint8_t* partial_framebuffer
ESP_LOGE(TAG, "Partial refresh area x1 and x2 must be byte-aligned (x1 %% 8 == 0 and x2 %% 8 == 7)");
return ESP_ERR_INVALID_ARG;
}
// no rounding needed, area is expected to be aligned
// ------DD
// DDDDD000
// ------DD
// DDDDD111
// ------DD
// DDDDDDDD
// ------DD
// DDDDDDDD
// -------D
// area should be multiple of 8 in x direction
const int32_t x_bank_start = area.x1 >> 3;
const int32_t x_bank_end = area.x2 >> 3;
std::vector<uint8_t> window_data = {
// x start
static_cast<uint8_t>((area.x1 >> 8) & 0xFF), // x start high byte
static_cast<uint8_t>(area.x1 & 0x07), // x start low byte
// x end
static_cast<uint8_t>((area.x2 >> 8) & 0xFF),
static_cast<uint8_t>(area.x2 & 0x07),
// y start
static_cast<uint8_t>((area.y1 >> 8) & 0xFF),
// x start, [9:8] bit -> 6 and 7 bits of x_bank_start
static_cast<uint8_t>((x_bank_start >> 5) & 0x03),
// x start, [7:3] bit + 3 bits of 0 -> 5 bits of x_bank_start and pad 3 LSBs as 0
static_cast<uint8_t>((x_bank_start & 0x1F) << 3),
// x end, [9:8] bit
static_cast<uint8_t>((x_bank_end >> 5) & 0x03),
// x end, [7:3] bit + 3 bits of 1
static_cast<uint8_t>(((x_bank_end & 0x1F) << 3) | 0x07),
// y start, [9:8] bit
static_cast<uint8_t>((area.y1 >> 8) & 0x03),
// y start, [7:0] bit
static_cast<uint8_t>(area.y1 & 0xFF),
// y end
static_cast<uint8_t>((area.y2 >> 8) & 0xFF),
// y end, [9:8] bit
static_cast<uint8_t>((area.y2 >> 8) & 0x03),
// y end, [7:0] bit
static_cast<uint8_t>(area.y2 & 0xFF),
0x01 // Gates scan both inside and outside of the partial window
};
ESP_LOGI(TAG, "Setting partial window: x1=%d, y1=%d, x2=%d, y2=%d",
area.x1, area.y1, area.x2, area.y2);
ESP_LOGI(TAG, "Partial window data: %02X %02X %02X %02X %02X %02X %02X %02X",
window_data[0], window_data[1], window_data[2], window_data[3], window_data[4],
window_data[5], window_data[6], window_data[7]);
err = epd_write_cmd_with_data(0x90, window_data, transaction_guard.transaction_id()); // Set partial window
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to send set partial window command: %s", esp_err_to_name(err));
@@ -214,15 +317,18 @@ esp_err_t EInkDisplayHandler::partial_refresh(const uint8_t* partial_framebuffer
return err;
}
err = transfer_spi_data(partial_framebuffer, DISPLAY_BUFFER_SIZE, transaction_guard.transaction_id()); // Send new framebuffer data
// Send only the partial area data, not the full display buffer
ESP_LOGI(TAG, "Sending partial buffer: %zu bytes (area: %dx%d)",
partial_buffer_size, area_width_bytes * 8, area_height);
err = transfer_spi_data(partial_framebuffer, partial_buffer_size, transaction_guard.transaction_id());
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to send partial_framebuffer data for partial refresh: %s", esp_err_to_name(err));
return err;
}
}
// Step 5: Trigger partial display refresh (DRF)
err = epd_write_cmd(0x12, transaction_guard.transaction_id());
// Step 5: Trigger partial display refresh (DRF) by ending the data write
err = epd_write_cmd(0x11, transaction_guard.transaction_id());
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to send display refresh command for partial refresh: %s", esp_err_to_name(err));
return err;
@@ -265,13 +371,20 @@ esp_err_t EInkDisplayHandler::partial_refresh(const uint8_t* partial_framebuffer
partial_refresh_count_ = 0;
}
}
err = deep_sleep_display();
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to enter deep sleep after partial refresh: %s", esp_err_to_name(err));
return err;
}
return ESP_OK;
}
esp_err_t EInkDisplayHandler::clear_display(void) {
ESP_LOGI(TAG, "Clearing display to all white...");
esp_err_t err = full_write(white_data);
esp_err_t err = full_write(black_data, false);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to clear display: %s", esp_err_to_name(err));
return err;
@@ -280,6 +393,8 @@ esp_err_t EInkDisplayHandler::clear_display(void) {
return ESP_OK;
}
// Request a full refresh on next flush
void EInkDisplayHandler::request_full_refresh(void) {
SemaphoreGuard guard(refresh_mutex_);
@@ -330,6 +445,11 @@ esp_err_t EInkDisplayHandler::init_devices(EventGroupHandle_t system_event_group
ESP_LOGE(TAG, "Failed to initialize touch: %s", esp_err_to_name(err));
return err;
}
err = deep_sleep_display();
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to put display into deep sleep: %s", esp_err_to_name(err));
return err;
}
// if system_event_group is provided, set display ready bits
if (system_event_group != nullptr) {
@@ -489,9 +609,76 @@ esp_err_t EInkDisplayHandler::epd_init_(void) {
return err;
}
}
is_deep_sleep_ = false;
return err;
}
esp_err_t EInkDisplayHandler::epd_init_partial_(void) {
TransactionGuard transaction_guard(*this);
esp_err_t begin_err = transaction_guard.begin();
if (begin_err != ESP_OK) {
ESP_LOGE(TAG, "Failed to begin transaction: %s", esp_err_to_name(begin_err));
return begin_err;
}
return epd_init_partial_internal_(transaction_guard.transaction_id());
}
// Internal version that uses an existing transaction (no separate TransactionGuard)
esp_err_t EInkDisplayHandler::epd_init_partial_internal_(uint32_t transaction_id) {
ESP_LOGI(TAG, "Initializing EPD for partial refresh (internal)...");
esp_err_t err = ESP_OK;
// 1. Hardware Reset
err = gpio_set_level(PIN_RST, 0);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to set PIN_RST low: %s", esp_err_to_name(err));
return err;
}
vTaskDelay(pdMS_TO_TICKS(MINIMUM_PIN_SETUP_DELAY_MS));
err = gpio_set_level(PIN_RST, 1);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to set PIN_RST high: %s", esp_err_to_name(err));
return err;
}
vTaskDelay(pdMS_TO_TICKS(MINIMUM_PIN_SETUP_DELAY_MS));
// 2. Panel Setting
std::vector<uint8_t> panel_setting_data = { 0x1F };
err = epd_write_cmd_with_data(0x00, panel_setting_data, transaction_id);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to send Panel Setting command: %s", esp_err_to_name(err));
return err;
}
vTaskDelay(pdMS_TO_TICKS(MINIMUM_PIN_SETUP_DELAY_MS));
// 3. Power ON
err = epd_write_cmd(0x04, transaction_id);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to send Power ON command: %s", esp_err_to_name(err));
return err;
}
vTaskDelay(pdMS_TO_TICKS(MINIMUM_POWER_ON_DELAY_MS));
wait_for_idle();
// 4. Partial initialization sequence - Enhanced Display Drive
std::vector<uint8_t> e0_data = { 0x02 };
err = epd_write_cmd_with_data(0xE0, e0_data, transaction_id);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to send Enhanced Display Drive command (E0): %s", esp_err_to_name(err));
return err;
}
std::vector<uint8_t> e5_data = { 0x6E };
err = epd_write_cmd_with_data(0xE5, e5_data, transaction_id);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to send Enhanced Display Drive command (E5): %s", esp_err_to_name(err));
return err;
}
is_deep_sleep_ = false;
ESP_LOGI(TAG, "EPD partial init (internal) complete");
return ESP_OK;
}
esp_err_t EInkDisplayHandler::init_touch_() {
ESP_LOGI(TAG, "Initializing touch...");

View File

@@ -4,6 +4,7 @@
#include "esp_lcd_touch_gt911.h"
#include "common/semaphore_guard.h"
#include <vector>
#include <atomic>
// Refresh mode configuration
#define PARTIAL_REFRESH_THRESHOLD 10 // Full refresh every N partial refreshes
@@ -21,6 +22,28 @@ public:
int32_t y1;
int32_t x2;
int32_t y2;
// reset to empty area
void reset() {
x1 = y1 = x2 = y2 = 0;
}
// expand area to include another area
void expand_to_include(const RefreshArea& other) {
expand_to_include(other.x1, other.y1, other.x2, other.y2);
}
void expand_to_include(int32_t x1, int32_t y1, int32_t x2, int32_t y2) {
const bool force_update = is_empty();
if (x1 < this->x1 || force_update) this->x1 = x1;
if (y1 < this->y1 || force_update) this->y1 = y1;
if (x2 > this->x2 || force_update) this->x2 = x2;
if (y2 > this->y2 || force_update) this->y2 = y2;
}
bool is_empty() const {
return (x1 == 0 && y1 == 0 && x2 == 0 && y2 == 0);
}
uint32_t area() const {
if (is_empty()) return 0;
return (x2 - x1 + 1) * (y2 - y1 + 1);
}
};
class EInkDisplayHandler {
@@ -32,9 +55,10 @@ public:
esp_err_t refresh_display(void);
esp_err_t full_write(const uint8_t* framebuffer);
esp_err_t full_write(const uint8_t* framebuffer, const bool white_basemap = true);
esp_err_t partial_refresh(const uint8_t* framebuffer, const RefreshArea& area);
esp_err_t clear_display(void);
esp_err_t deep_sleep_display(void);
// Request a full refresh on next flush
void request_full_refresh(void);
@@ -53,7 +77,9 @@ protected:
private:
esp_err_t init_display_pins_(void);
esp_err_t epd_init_(void);
esp_err_t epd_init_(void); // full fast refresh init
esp_err_t epd_init_partial_(void); // partial refresh init (standalone)
esp_err_t epd_init_partial_internal_(uint32_t transaction_id); // partial refresh init (within existing transaction)
esp_err_t init_touch_(void);
esp_err_t dangerous_epd_write_cmd_without_lock_(const uint8_t cmd);
esp_err_t dangerous_epd_write_data_without_lock_(const uint8_t data);
@@ -67,6 +93,7 @@ private:
uint32_t partial_refresh_count_ = 0;
bool force_full_refresh_ = false;
std::atomic<bool> is_deep_sleep_ { false };
SemaphoreHandle_t spi_mutex_ = nullptr;
SemaphoreHandle_t spi_transaction_mutex_ = nullptr;

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");

View File

@@ -7,7 +7,6 @@
#include "esp_err.h"
#include <memory>
class LVGLHandler {
public:
LVGLHandler(
@@ -34,6 +33,8 @@ private:
lv_draw_buf_t* lvgl_draw_buf_ = nullptr;
uint8_t* framebuffer_ = nullptr;
bool framebuffer_in_psram_ = false;
RefreshArea refresh_area_ = { 0, 0, 0, 0 };
SemaphoreHandle_t lvgl_mutex_ = nullptr;
};

View File

@@ -83,20 +83,32 @@ void EInk_Checkerboard(
}
}
// Yield and reset watchdog periodically
if (y % 50 == 0) {
const size_t YIELD_INTERVAL = 16;
if (y % YIELD_INTERVAL == 0) {
if (wdt_err == ESP_OK) {
esp_task_wdt_reset();
}
vTaskDelay(1 / portTICK_PERIOD_MS);
// partial refresh to show progress
int32_t y_start = static_cast<int32_t>((y >= YIELD_INTERVAL - 1) ? (y - (YIELD_INTERVAL - 1)) : 0);
int32_t y_end = static_cast<int32_t>(y);
// get the partial framebuffer for this area
uint8_t* partial_framebuffer = &framebuffer[y_start * (DISPLAY_WIDTH / 8)];
esp_err_t err = display_handler->partial_refresh(partial_framebuffer, RefreshArea { 0, y_start, DISPLAY_WIDTH - 1, y_end });
if (err != ESP_OK) {
ESP_LOGE(TAG, "Partial refresh failed at y=%d: %s", y, esp_err_to_name(err));
}
// wait for 4 seconds to prevent spamming the display
// vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}
// Perform full write to display
esp_err_t err = display_handler->full_write(framebuffer);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Checkerboard full write failed: %s", esp_err_to_name(err));
} else {
ESP_LOGI(TAG, "Checkerboard pattern displayed successfully.");
}
// esp_err_t err = display_handler->full_write(framebuffer);
// if (err != ESP_OK) {
// ESP_LOGE(TAG, "Checkerboard full write failed: %s", esp_err_to_name(err));
// } else {
// ESP_LOGI(TAG, "Checkerboard pattern displayed successfully.");
// }
delete[] framebuffer;
// Remove task from watchdog before deletion
@@ -144,19 +156,33 @@ void LVGL_Checkerboard(
// Add safety checks
if (!handler) {
ESP_LOGE("LVGL", "Handler is null!");
delete params;
vTaskDelete(NULL);
return;
}
ESP_LOGI("HEAP", "Free: %d", esp_get_free_heap_size());
// Wait for LVGL system to fully initialize
vTaskDelay(pdMS_TO_TICKS(200));
// Acquire LVGL lock with proper timeout
if (!lvgl_port_lock(pdMS_TO_TICKS(1000))) {
if (!lvgl_port_lock(pdMS_TO_TICKS(5000))) {
ESP_LOGE(TAG, "Failed to acquire LVGL lock for checkerboard");
delete params;
vTaskDelete(NULL);
return;
}
// Verify LVGL is properly initialized
if (lv_display_get_default() == nullptr) {
ESP_LOGE(TAG, "LVGL default display not available");
lvgl_port_unlock();
delete params;
vTaskDelete(NULL);
return;
}
// Create LVGL objects for checkerboard
lv_obj_t* scr = lv_scr_act();
if (scr == nullptr) {
@@ -175,25 +201,42 @@ void LVGL_Checkerboard(
return;
}
lv_obj_set_size(checkerboard, DISPLAY_WIDTH, DISPLAY_HEIGHT);
lv_obj_center(checkerboard);
// remove border and padding
lv_obj_set_style_pad_all(checkerboard, 0, 0);
lv_obj_set_style_border_width(checkerboard, 0, 0);
const int CELL_SIZE = 40;
lvgl_port_unlock();
// Create checkerboard pattern using LVGL
for (int y = 0; y < DISPLAY_HEIGHT; y += CELL_SIZE) {
lvgl_port_lock(pdMS_TO_TICKS(1000));
for (int x = 0; x < DISPLAY_WIDTH; x += CELL_SIZE) {
lv_color_t color = (((x / CELL_SIZE) % 2) == ((y / CELL_SIZE) % 2)) ? lv_color_hex(0xFFFFFF) : lv_color_hex(0x000000);
lv_obj_t* cell = lv_obj_create(checkerboard);
if (cell == nullptr) {
ESP_LOGE(TAG, "Failed to create LVGL checkerboard cell");
lvgl_port_unlock();
continue;
}
lv_obj_set_size(cell, CELL_SIZE, CELL_SIZE);
lv_obj_set_style_bg_color(cell, color, 0);
lv_obj_set_pos(cell, x, y);
// remove border and padding
lv_obj_set_style_pad_all(cell, 0, 0);
lv_obj_set_style_border_width(cell, 0, 0);
lv_obj_t* label = lv_label_create(cell);
if (label != nullptr) {
lv_label_set_text_fmt(label, "(%d,%d)", x, y);
lv_obj_center(label);
}
}
lvgl_port_unlock();
// Yield to allow LVGL to process rendering
vTaskDelay(5000 / portTICK_PERIOD_MS);
}
ESP_LOGI(TAG, "LVGL Checkerboard pattern displayed successfully.");
lvgl_port_unlock();
delete params;
vTaskDelete(NULL);
};
@@ -239,8 +282,8 @@ void app_main(void) {
// NetworkHandler* network_handler = new NetworkHandler(std::move(wifi_handler));
EInkDisplayHandler* display_handler = new EInkDisplayHandler();
// Initialize display and touch
// display_handler->init_devices(system_event_group);
display_handler->init_devices();
display_handler->clear_display();
ESP_LOGI(TAG, "E-Ink display handler initialized.\n");
// LVGL Handler
std::unique_ptr<EInkDisplayHandler> display_uptr(display_handler);
@@ -269,7 +312,11 @@ void app_main(void) {
);
ESP_LOGI(TAG, "System is ready. Starting main application...\n");
// Allow LVGL system to stabilize before creating objects
vTaskDelay(pdMS_TO_TICKS(100));
// Show checkerboard pattern on display for testing
// EInk_Checkerboard(display_handler);
LVGL_Checkerboard(&lvgl_handler);
// Register apps with AppRegistry by creating their descriptors