Compare commits
4 Commits
30dfdd630a
...
feature/mt
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
de2f166151 | ||
|
|
dc2a76e131 | ||
|
|
5f491dff6e | ||
|
|
6fbbfcde4f |
@@ -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,14 +208,25 @@ 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;
|
||||
}
|
||||
|
||||
esp_err_t EInkDisplayHandler::partial_refresh(const uint8_t* framebuffer, const RefreshArea& area) {
|
||||
esp_err_t EInkDisplayHandler::partial_refresh(const uint8_t* partial_framebuffer, const RefreshArea& area) {
|
||||
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* framebuffer, const
|
||||
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
|
||||
@@ -179,21 +261,47 @@ esp_err_t EInkDisplayHandler::partial_refresh(const uint8_t* framebuffer, const
|
||||
}
|
||||
// Step 3: Set partial window
|
||||
{
|
||||
if (area.x1 % 8 != 0 || area.x2 % 8 != 7) {
|
||||
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;
|
||||
}
|
||||
// ------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 & 0xFF), // x start low byte
|
||||
// x end
|
||||
static_cast<uint8_t>((area.x2 >> 8) & 0xFF),
|
||||
static_cast<uint8_t>(area.x2 & 0xFF),
|
||||
// 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));
|
||||
@@ -209,15 +317,18 @@ esp_err_t EInkDisplayHandler::partial_refresh(const uint8_t* framebuffer, const
|
||||
return err;
|
||||
}
|
||||
|
||||
err = transfer_spi_data(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 framebuffer data for partial refresh: %s", esp_err_to_name(err));
|
||||
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;
|
||||
@@ -260,13 +371,20 @@ esp_err_t EInkDisplayHandler::partial_refresh(const uint8_t* framebuffer, const
|
||||
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;
|
||||
@@ -275,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_);
|
||||
@@ -325,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) {
|
||||
@@ -484,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...");
|
||||
|
||||
@@ -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;
|
||||
|
||||
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;
|
||||
}
|
||||
40
main/display/lvgl_handler.h
Normal file
40
main/display/lvgl_handler.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
#include "lvgl.h"
|
||||
#include "esp_lvgl_port.h"
|
||||
#include "display/eink_display_handler.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_err.h"
|
||||
#include <memory>
|
||||
|
||||
class LVGLHandler {
|
||||
public:
|
||||
LVGLHandler(
|
||||
// an owning pointer to the display handler
|
||||
// The display handler must outlive the LVGLHandler
|
||||
// The display handler must be fully initialized before calling initLVGLDisplay
|
||||
std::unique_ptr<EInkDisplayHandler> display_handler_in
|
||||
);
|
||||
~LVGLHandler();
|
||||
esp_err_t initLVGL(EventGroupHandle_t system_event_group = nullptr);
|
||||
|
||||
private:
|
||||
void rounder_cb_(lv_display_t* disp, lv_area_t* area);
|
||||
void flush_cb_(lv_display_t* disp, const lv_area_t* area, uint8_t* px_map);
|
||||
void touch_read_cb_(lv_indev_t* indev, lv_indev_data_t* data);
|
||||
esp_err_t initLVGLDisplay_();
|
||||
esp_err_t registerLVGLTouch_();
|
||||
esp_err_t initLVGLPort_();
|
||||
|
||||
std::unique_ptr<EInkDisplayHandler> display_handler_ = nullptr;
|
||||
|
||||
lv_display_t* lvgl_display_ = nullptr;
|
||||
lv_indev_t* lvgl_touch_indev_ = nullptr;
|
||||
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;
|
||||
};
|
||||
358
main/main.cpp
358
main/main.cpp
@@ -15,6 +15,7 @@
|
||||
#include "io/nvs_handler.h"
|
||||
#include "info/info.h"
|
||||
#include "display/eink_display_handler.h"
|
||||
#include "display/lvgl_handler.h"
|
||||
#include "ui/ui_handler.h"
|
||||
#include "ui/app_registry.h"
|
||||
#include "ui/apps/shutdown_app.h"
|
||||
@@ -26,6 +27,8 @@
|
||||
#include "network.h"
|
||||
#include <esp_task_wdt.h>
|
||||
|
||||
#include "lvgl.h"
|
||||
|
||||
// nvs storage namespaces, 15 characters max
|
||||
#define DEFAULT_STORAGE_NAMESPACE "storage"
|
||||
#define WIFI_CREDENTIALS_STORAGE_NAMESPACE "wifi_cred"
|
||||
@@ -40,120 +43,10 @@ void init_queues(
|
||||
);
|
||||
|
||||
|
||||
void app_main(void) {
|
||||
display_chip_info();
|
||||
|
||||
QueueHandle_t touch_event_queue = NULL;
|
||||
EventGroupHandle_t system_event_group = NULL, system_lifecycle_event_group = NULL;
|
||||
|
||||
init_queues(touch_event_queue, system_event_group, system_lifecycle_event_group);
|
||||
if (touch_event_queue == NULL || system_event_group == NULL || system_lifecycle_event_group == NULL) {
|
||||
ESP_LOGE("Main", "Failed to create one or more queues/event groups");
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
return esp_restart();
|
||||
}
|
||||
ESP_LOGI(TAG, "Queues initialized.\n");
|
||||
|
||||
// Initialize LVGL
|
||||
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_restart();
|
||||
}
|
||||
ESP_LOGI(TAG, "LVGL port initialized successfully.\n");
|
||||
|
||||
SemaphoreHandle_t lvgl_mutex = xSemaphoreCreateMutex();
|
||||
if (lvgl_mutex == NULL) {
|
||||
ESP_LOGE("Main", "Failed to create LVGL mutex");
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
return esp_restart();
|
||||
}
|
||||
//
|
||||
// KVStorageHandler* kv_storage_handler = new NVSStorageHandler(
|
||||
// DEFAULT_STORAGE_NAMESPACE
|
||||
// );
|
||||
|
||||
// auto wifi_handler = std::make_unique<WifiHandler>(
|
||||
// std::unique_ptr<KVStorageHandler>(new NVSStorageHandler(WIFI_CREDENTIALS_STORAGE_NAMESPACE))
|
||||
// );
|
||||
// NetworkHandler* network_handler = new NetworkHandler(std::move(wifi_handler));
|
||||
EInkDisplayHandler* display_handler = new EInkDisplayHandler();
|
||||
//
|
||||
// kv_storage_handler->init(system_event_group);
|
||||
// network_handler->init(system_event_group);
|
||||
|
||||
// Initialize display and touch
|
||||
display_handler->init_devices(system_event_group);
|
||||
display_handler->clear_display();
|
||||
// ESP_LOGV(TAG, "Starting touch task...\n");
|
||||
// display_handler->start_touch_task();
|
||||
// ESP_LOGV(TAG, "Touch task started.\n");
|
||||
//
|
||||
// LVGL tick timer
|
||||
// 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_restart();
|
||||
// }
|
||||
// ESP_LOGV(TAG, "Starting LVGL tick timer...\n");
|
||||
// xTimerStart(lvgl_tick_timer, 0);
|
||||
|
||||
//
|
||||
ESP_LOGI(TAG, "Waiting for system to be ready...\n");
|
||||
xEventGroupWaitBits(
|
||||
system_event_group,
|
||||
// DISPLAY_READY_BIT | STORAGE_READY_BIT | NETWORK_READY_BIT,
|
||||
DISPLAY_READY_BIT,
|
||||
// do not clear on exit, require explicit reset
|
||||
pdFALSE,
|
||||
pdTRUE,
|
||||
portMAX_DELAY
|
||||
);
|
||||
ESP_LOGI(TAG, "System is ready. Starting main application...\n");
|
||||
|
||||
// Register apps with AppRegistry by creating their descriptors
|
||||
// Each descriptor will create and register the app instance
|
||||
// DemoAppDescriptor* demo_descriptor = new DemoAppDescriptor();
|
||||
// ShutdownAppDescriptor* shutdown_descriptor = new ShutdownAppDescriptor();
|
||||
// DiscordAppDescriptor::instance(); // Use singleton pattern for Discord app
|
||||
// MtrAppDescriptor* mtr_descriptor = new MtrAppDescriptor();
|
||||
|
||||
// Pass network handler to MtrApp so it can fetch arrival data
|
||||
// MtrApp* mtr_app = dynamic_cast<MtrApp*>(mtr_descriptor->get_app_instance());
|
||||
// if (mtr_app) {
|
||||
// mtr_app->set_network_handler(network_handler);
|
||||
// }
|
||||
|
||||
// ESP_LOGI(TAG, "Apps registered with AppRegistry\n");
|
||||
|
||||
// Initialize UI Handler (will render app icons from registry)
|
||||
// UIHandler ui_handler;
|
||||
// if (ui_handler.init() != ESP_OK) {
|
||||
// ESP_LOGE(TAG, "Failed to initialize UI handler");
|
||||
// vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
// return esp_restart();
|
||||
// }
|
||||
// ESP_LOGI(TAG, "UI handler initialized successfully\n");
|
||||
// ESP_LOGI(TAG, "Main screen displayed with app icons. Tap an icon to launch an app.\n");
|
||||
|
||||
// Run checkerboard draw in its own FreeRTOS task to avoid watchdog triggers
|
||||
void EInk_Checkerboard(
|
||||
EInkDisplayHandler* display_handler
|
||||
) {
|
||||
struct CheckerboardTaskParams {
|
||||
EInkDisplayHandler* display_handler;
|
||||
};
|
||||
@@ -190,20 +83,32 @@ void app_main(void) {
|
||||
}
|
||||
}
|
||||
// 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
|
||||
@@ -230,6 +135,216 @@ void app_main(void) {
|
||||
ESP_LOGE(TAG, "Failed to create checkerboard task");
|
||||
delete checker_params;
|
||||
}
|
||||
}
|
||||
|
||||
void LVGL_Checkerboard(
|
||||
LVGLHandler* lvgl_handler
|
||||
) {
|
||||
struct CheckerboardTaskParams {
|
||||
LVGLHandler* lvgl_handler;
|
||||
};
|
||||
auto checkerboard_task_fn = [](void* pvParameters) {
|
||||
CheckerboardTaskParams* params = static_cast<CheckerboardTaskParams*>(pvParameters);
|
||||
if (params == nullptr || params->lvgl_handler == nullptr) {
|
||||
ESP_LOGE(TAG, "Invalid parameters for LVGL checkerboard task");
|
||||
delete params;
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
auto* handler = static_cast<LVGLHandler*>(params->lvgl_handler);
|
||||
|
||||
// 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(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) {
|
||||
ESP_LOGE(TAG, "Failed to get active LVGL screen");
|
||||
lvgl_port_unlock();
|
||||
delete params;
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
lv_obj_t* checkerboard = lv_obj_create(scr);
|
||||
if (checkerboard == nullptr) {
|
||||
ESP_LOGE(TAG, "Failed to create LVGL checkerboard object");
|
||||
lvgl_port_unlock();
|
||||
delete params;
|
||||
vTaskDelete(NULL);
|
||||
return;
|
||||
}
|
||||
lv_obj_set_size(checkerboard, DISPLAY_WIDTH, DISPLAY_HEIGHT);
|
||||
// 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.");
|
||||
delete params;
|
||||
vTaskDelete(NULL);
|
||||
};
|
||||
CheckerboardTaskParams* checker_params = new CheckerboardTaskParams();
|
||||
checker_params->lvgl_handler = lvgl_handler;
|
||||
BaseType_t res = xTaskCreate(
|
||||
checkerboard_task_fn,
|
||||
"lvgl_checkerboard_task",
|
||||
8192,
|
||||
static_cast<void*>(checker_params),
|
||||
tskIDLE_PRIORITY + 1,
|
||||
NULL
|
||||
);
|
||||
if (res != pdPASS) {
|
||||
ESP_LOGE(TAG, "Failed to create LVGL checkerboard task");
|
||||
delete checker_params;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void app_main(void) {
|
||||
display_chip_info();
|
||||
|
||||
QueueHandle_t touch_event_queue = NULL;
|
||||
EventGroupHandle_t system_event_group = NULL, system_lifecycle_event_group = NULL;
|
||||
|
||||
init_queues(touch_event_queue, system_event_group, system_lifecycle_event_group);
|
||||
if (touch_event_queue == NULL || system_event_group == NULL || system_lifecycle_event_group == NULL) {
|
||||
ESP_LOGE("Main", "Failed to create one or more queues/event groups");
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
return esp_restart();
|
||||
}
|
||||
ESP_LOGI(TAG, "Queues initialized.\n");
|
||||
|
||||
//
|
||||
// KVStorageHandler* kv_storage_handler = new NVSStorageHandler(
|
||||
// DEFAULT_STORAGE_NAMESPACE
|
||||
// );
|
||||
|
||||
// auto wifi_handler = std::make_unique<WifiHandler>(
|
||||
// std::unique_ptr<KVStorageHandler>(new NVSStorageHandler(WIFI_CREDENTIALS_STORAGE_NAMESPACE))
|
||||
// );
|
||||
// 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();
|
||||
ESP_LOGI(TAG, "E-Ink display handler initialized.\n");
|
||||
// LVGL Handler
|
||||
std::unique_ptr<EInkDisplayHandler> display_uptr(display_handler);
|
||||
LVGLHandler lvgl_handler(std::move(display_uptr));
|
||||
esp_err_t err = lvgl_handler.initLVGL(system_event_group);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to initialize LVGL handler: %s", esp_err_to_name(err));
|
||||
vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
return esp_restart();
|
||||
}
|
||||
|
||||
//
|
||||
// kv_storage_handler->init(system_event_group);
|
||||
// network_handler->init(system_event_group);
|
||||
|
||||
//
|
||||
ESP_LOGI(TAG, "Waiting for system to be ready...\n");
|
||||
xEventGroupWaitBits(
|
||||
system_event_group,
|
||||
// DISPLAY_READY_BIT | STORAGE_READY_BIT | NETWORK_READY_BIT,
|
||||
DISPLAY_READY_BIT,
|
||||
// do not clear on exit, require explicit reset
|
||||
pdFALSE,
|
||||
pdTRUE,
|
||||
portMAX_DELAY
|
||||
);
|
||||
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
|
||||
// Each descriptor will create and register the app instance
|
||||
// DemoAppDescriptor* demo_descriptor = new DemoAppDescriptor();
|
||||
// ShutdownAppDescriptor* shutdown_descriptor = new ShutdownAppDescriptor();
|
||||
// DiscordAppDescriptor::instance(); // Use singleton pattern for Discord app
|
||||
// MtrAppDescriptor* mtr_descriptor = new MtrAppDescriptor();
|
||||
|
||||
// Pass network handler to MtrApp so it can fetch arrival data
|
||||
// MtrApp* mtr_app = dynamic_cast<MtrApp*>(mtr_descriptor->get_app_instance());
|
||||
// if (mtr_app) {
|
||||
// mtr_app->set_network_handler(network_handler);
|
||||
// }
|
||||
|
||||
// ESP_LOGI(TAG, "Apps registered with AppRegistry\n");
|
||||
|
||||
// Initialize UI Handler (will render app icons from registry)
|
||||
// UIHandler ui_handler;
|
||||
// if (ui_handler.init() != ESP_OK) {
|
||||
// ESP_LOGE(TAG, "Failed to initialize UI handler");
|
||||
// vTaskDelay(5000 / portTICK_PERIOD_MS);
|
||||
// return esp_restart();
|
||||
// }
|
||||
// ESP_LOGI(TAG, "UI handler initialized successfully\n");
|
||||
// ESP_LOGI(TAG, "Main screen displayed with app icons. Tap an icon to launch an app.\n");
|
||||
|
||||
|
||||
|
||||
// wait for shutdown signal
|
||||
ESP_LOGI(TAG, "Waiting for shutdown signal...\n");
|
||||
@@ -255,8 +370,7 @@ void app_main(void) {
|
||||
// delete demo_descriptor;
|
||||
// delete shutdown_descriptor;
|
||||
// delete mtr_descriptor;
|
||||
delete display_handler;
|
||||
vSemaphoreDelete(lvgl_mutex);
|
||||
|
||||
vEventGroupDelete(system_event_group);
|
||||
vQueueDelete(touch_event_queue);
|
||||
ESP_LOGI(TAG, "Cleanup complete.\n");
|
||||
|
||||
2591
sdkconfig.default
Normal file
2591
sdkconfig.default
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user