#include "display/lvgl_handler.h" #include "esp_log.h" #include "common/semaphore_guard.h" #include "common/constants.h" #include #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 TAG "LVGLHandler" LVGLHandler::LVGLHandler( std::unique_ptr 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) { LVGLHandler* handler = static_cast(lv_display_get_user_data(disp)); if (handler == nullptr || handler->display_handler_ == nullptr) { ESP_LOGE(TAG, "Invalid handler in flush callback"); lv_display_flush_ready(disp); return; } uint8_t* pixel_data = px_map + 8; // Skip palette // ESP_LOGI(TAG, "Flush callback: x1=%d, y1=%d, x2=%d, y2=%d", area->x1, area->y1, area->x2, area->y2); // 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) { ESP_LOGI(TAG, "Full screen update"); memcpy(framebuffer_, pixel_data, DISPLAY_BUFFER_SIZE); // request full refresh esp_err_t err = handler->display_handler_->refresh_display(); 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); } // 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; // 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); // 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); // 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)); } } // lv_display_flush_ready(disp); } void LVGLHandler::touch_read_cb_(lv_indev_t* indev, lv_indev_data_t* data) { LVGLHandler* handler = static_cast(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(1000))) { 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"); 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"); 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"); lv_display_delete(lvgl_display_); 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); // 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); lv_display_add_event_cb(lvgl_display_, [](lv_event_t* e) { LVGLHandler* handler = static_cast(lv_display_get_user_data(static_cast(lv_event_get_target(e)))); if (handler != nullptr) { handler->rounder_cb_(static_cast(lv_event_get_target(e)), static_cast(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(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 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(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; }