From adf081cf1addb246c52fc991f8c113e033a04463 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Sun, 2 Nov 2025 12:46:00 -0500 Subject: [PATCH] Fix LVGL issues and test. The display renders correctly --- esp/cpp/components/lcd/include/lcd.h | 18 -- esp/cpp/components/lcd/include/panel_device.h | 212 +++++++++++++++++- esp/cpp/components/lcd/lcd.c | 4 - esp/cpp/components/lcd/panel_device.cpp | 159 ------------- esp/cpp/components/ssd1306/include/ssd1306.h | 13 +- 5 files changed, 204 insertions(+), 202 deletions(-) diff --git a/esp/cpp/components/lcd/include/lcd.h b/esp/cpp/components/lcd/include/lcd.h index 6a548cf..4429471 100644 --- a/esp/cpp/components/lcd/include/lcd.h +++ b/esp/cpp/components/lcd/include/lcd.h @@ -17,24 +17,6 @@ #include #include -// According to specific display hardware. -// https://www.digikey.com/en/products/detail/winstar-display/WEA012864DWPP3N00003/20533255 -#define SCREEN_WIDTH 128 // OLED display width, in pixels. -#define SCREEN_HEIGHT 64 // OLED display height, in pixels. - -// According to SSD1306 datasheet. -// https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf -#define I2C_HW_ADDR 0x3C -#define LCD_PIXEL_CLOCK_HZ (400 * 1000) -// Bit number used to represent command and parameter -#define LCD_CMD_BITS 8 -#define LCD_PARAM_BITS 8 - -/// Tag used for ESP logging. -static const char* LCD_TAG = "LCD component"; - -static _lock_t lv_lock_; - struct LCD { esp_lcd_panel_handle_t esp_panel_; diff --git a/esp/cpp/components/lcd/include/panel_device.h b/esp/cpp/components/lcd/include/panel_device.h index ae1d57f..09efc68 100644 --- a/esp/cpp/components/lcd/include/panel_device.h +++ b/esp/cpp/components/lcd/include/panel_device.h @@ -10,10 +10,23 @@ #include #include +#include #include +#include +#include + +// TODO: Remove +static esp_timer_handle_t esp_timer_; #include +/// Tag used for ESP logging. +static const char* LCD_TAG = "LCD component"; + +// LVGL library is not thread-safe, this example calls LVGL APIs from tasks. +// We must use a mutex to protect it. +static _lock_t lv_lock_; + // LVGL reserves 2x4 bytes in the buffer to be used as a palette. // This additional space must be added to the IPanelDevice::buf_size_. #define LVGL_PALETTE_SIZE 8 @@ -24,6 +37,19 @@ #define LCD_H_RES 128 #define LCD_V_RES 64 +// According to specific display hardware. +// https://www.digikey.com/en/products/detail/winstar-display/WEA012864DWPP3N00003/20533255 +#define SCREEN_WIDTH 128 // OLED display width, in pixels. +#define SCREEN_HEIGHT 64 // OLED display height, in pixels. + +// According to SSD1306 datasheet. +// https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf +#define I2C_HW_ADDR 0x3C +#define LCD_PIXEL_CLOCK_HZ (400 * 1000) +// Bit number used to represent command and parameter +#define LCD_CMD_BITS 8 +#define LCD_PARAM_BITS 8 + /** * Calculate byte offset for the pixel at [x,y] within a horizontally-mapped * monochrome uint8 draw buffer, using the initialized horizontal resolution. @@ -244,7 +270,12 @@ struct IPanelDevice */ static bool lvgl_flush_ready_cb(esp_lcd_panel_io_handle_t panel, esp_lcd_panel_io_event_data_t* data, - void* user_ctx); + void* user_ctx) +{ + lv_display_t* disp = (lv_display_t*)user_ctx; + lv_display_flush_ready(disp); + return false; +} /** * The callback invoked for flushing the rendered image to the display. @@ -304,7 +335,65 @@ static bool lvgl_flush_ready_cb(esp_lcd_panel_io_handle_t panel, * @sa get_additional_draw_buffer */ static void lvgl_flush_cb(lv_display_t* display, const lv_area_t* area, - uint8_t* px_map); + uint8_t* px_map) +{ + esp_lcd_panel_handle_t panel_handle = + (esp_lcd_panel_handle_t)lv_display_get_user_data(display); + + // Necessary because LVGL reserves 2x4 bytes in the buffer for a palette. + // Since we are monochrome, we don't need these additional bytes. + // For more information about the monochrome, please refer to: + // https://docs.lvgl.io/9.2/porting/display.html#monochrome-displays + // Skip the palette here. + px_map += LVGL_PALETTE_SIZE; + + uint16_t hor_res = lv_display_get_physical_horizontal_resolution(display); + int32_t x1 = area->x1; + int32_t x2 = area->x2; + int32_t y1 = area->y1; + int32_t y2 = area->y2; + + + // As of 03/01/2025 master branch of LVGL contains this helper for the same. + // https://docs.lvgl.io/master/API/draw/sw/lv_draw_sw_utils.html + // lv_draw_sw_i1_convert_to_vtiled() + for (int32_t y = y1; y <= y2; y++) + { + for (int32_t x = x1; x <= x2; x++) + { + // Get the byte offset of the pixel coordinates using + // horizontal-mapping. + int h_offset = horizontal_byte_offset_long(x, y, hor_res); + // True if the pixel is lit, else false. + bool chroma_color = px_map[h_offset] & msb_mask(x); + + // We need an additional buffer for transposing the pixel data to + // the vertical format required by the display driver. + uint8_t* buf = get_additional_draw_buffer(); + // Move to the position in the auxillary buffer where the pixel is + // stored. + buf += vertical_byte_offset_long(x, y, hor_res); + + // Write the single bit to the monochrome display mapped vertically. + // Take the Least Significant Bit mask of the Y coordinate to select + // the bit representing a pixel at position y in a vertically-mapped + // display. + if (chroma_color) + { + // Set the vertically-mapped pixel to on. + *buf &= ~lsb_mask(y); + } + else + { + // Set the vertically-mapped pixel to off. + *buf |= lsb_mask(y); + } + } + } + // Pass the draw buffer to the driver. + ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap( + panel_handle, x1, y1, x2 + 1, y2 + 1, get_additional_draw_buffer())); +} /** * Callback invoked for every period of the timer. @@ -316,7 +405,11 @@ static void lvgl_flush_cb(lv_display_t* display, const lv_area_t* area, * @sa register_lvgl_tick_timer for setting user data and the tick period of * the timer, or overriding this callback entirely. */ -static void lvgl_increase_tick_cb(void* data); +static void lvgl_increase_tick_cb(void* data) +{ + // Tell LVGL how many milliseconds has elapsed + lv_tick_inc(LVGL_TICK_PERIOD_MS); +} /** * FreeRTOS task callback invoked for handling LVGL events or updating the UI. @@ -330,7 +423,24 @@ static void lvgl_increase_tick_cb(void* data); * @param data User data passed to the callback. * @sa register_lvgl_tick_timer for overriding this callback. */ -[[noreturn]] static void lvgl_port_task(void* data); +[[noreturn]] static void lvgl_port_task(void* data) +{ + // Optionally initialize some LVGL objects here before entering loop below. + + ESP_LOGI(LCD_TAG, "Starting LVGL task"); + for (uint32_t time_to_next_ms = 0; true; usleep(1000 * time_to_next_ms)) + { + // Obtain LVGL API lock before calling any LVGL methods. + _lock_acquire(&lv_lock_); + + // Optionally handle LVGL input or event logic here. + + // Update LVGL periodic timers. + time_to_next_ms = lv_timer_handler(); + + _lock_release(&lv_lock_); + } +} /** * Registers LVGL ticker timer callback for rendering this display. @@ -338,22 +448,104 @@ static void lvgl_increase_tick_cb(void* data); * An implementation of the interface can optionally override this method to * provide custom LVGL callbacks and tick configurations. */ -void register_lvgl_tick_timer(); +static void register_lvgl_tick_timer() +{ + ESP_LOGI(LCD_TAG, "Use esp_timer to increase LVGL tick"); + const esp_timer_create_args_t esp_timer_args = { + .callback = &lvgl_increase_tick_cb, + // Data to pass to the lvgl_port_task callback. + .arg = NULL, + .name = "lvgl_tick", + }; -void register_rendering_data(lv_display_t* display_handle, + /// Start periodic ESP timer handle. + ESP_LOGI(LCD_TAG, "Creating esp_timer with name: '%s'", + esp_timer_args.name); + ESP_ERROR_CHECK(esp_timer_create(&esp_timer_args, &esp_timer_)); + ESP_ERROR_CHECK( + esp_timer_start_periodic(esp_timer_, LVGL_TICK_PERIOD_MS * 1000)); + ESP_LOGI(LCD_TAG, "Starting esp_timer with name: '%s'", + esp_timer_args.name); + + // LVGL requires a FreeRTOS task for running it's event loop. + // The lvgl_port_task callback can update the UI or handle input logic. + // For this basic example we don't do either of these things. + ESP_LOGI(LCD_TAG, "Create LVGL FreeRTOS task"); + // Optionally set user data to pass to LVGL's FreeRTOS task callback here. + void* user_data = NULL; + xTaskCreate(lvgl_port_task, "LVGL", LVGL_TASK_STACK_SIZE, user_data, + LVGL_TASK_PRIORITY, NULL); +} + +static void register_rendering_data(lv_display_t* display_handle, esp_lcd_panel_io_handle_t io_handle, void* lv_buf, - size_t lv_buf_size); + size_t lv_buf_size) +{ + // Create draw buffer. + ESP_LOGI(LCD_TAG, "Allocate separate LVGL draw buffers"); + lv_buf = + heap_caps_calloc(1, lv_buf_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); + assert(lv_buf); -inline struct IPanelDevice LCD_new_panel() + ESP_LOGI(LCD_TAG, "Set LVGL draw buffers"); + // Color format must be set first, LVGL9 support new monochromatic format. + lv_display_set_color_format(display_handle, LV_COLOR_FORMAT_I1); + lv_display_set_buffers(display_handle, lv_buf, NULL, lv_buf_size, + LV_DISPLAY_RENDER_MODE_FULL); + lv_display_set_rotation(display_handle, LV_DISPLAY_ROTATION_0); + + ESP_LOGI(LCD_TAG, "Set LVGL callback for flushing to the display"); + lv_display_set_flush_cb(display_handle, lvgl_flush_cb); + + ESP_LOGI(LCD_TAG, + "Register io panel callback for LVGL flush ready notification"); + const esp_lcd_panel_io_callbacks_t cbs = { + .on_color_trans_done = lvgl_flush_ready_cb, + }; + ESP_ERROR_CHECK(esp_lcd_panel_io_register_event_callbacks(io_handle, &cbs, + display_handle)); +} + +/// Callback used to fail, indicating the device is not fully initialized. +static void* fail_vendor_config_cb() +{ + ESP_LOGE(LCD_TAG, "Callback is unset: IPanelDevice::vendor_config_cb"); + assert(false); +} + + +/// Callback used to fail, indicating the device is not fully initialized. +static void fail_init_panel_cb(esp_lcd_panel_dev_config_t*, + esp_lcd_panel_io_handle_t, + esp_lcd_panel_handle_t*) +{ + ESP_LOGE(LCD_TAG, "Callback is unset: IPanelDevice::init_panel_cb"); + assert(false); +} + +static struct IPanelDevice LCD_new_panel() { return (struct IPanelDevice){ .width_ = LCD_H_RES, .height_ = LCD_V_RES, .rst_num_ = -1, - .init_panel_cb = NULL, // Must be assigned by caller + .lv_buf_size_ = LCD_H_RES * LCD_V_RES / 8 + LVGL_PALETTE_SIZE, + .esp_io_config_ = + (esp_lcd_panel_io_i2c_config_t){ + .dev_addr = I2C_HW_ADDR, + // User data to pass to the LVGL flush_ready callback. + // See IPanelDevice::lvgl_flush_ready_cb + .user_ctx = NULL, + .control_phase_bytes = 1, + .dc_bit_offset = 6, + .lcd_cmd_bits = LCD_CMD_BITS, + .lcd_param_bits = LCD_PARAM_BITS, + .scl_speed_hz = LCD_PIXEL_CLOCK_HZ, + }, + .init_panel_cb = fail_init_panel_cb, // Must be assigned by caller .register_lvgl_tick_timer_cb = register_lvgl_tick_timer, .register_rendering_data_cb = register_rendering_data, - .vendor_config_cb = NULL, // Must be assigned by caller + .vendor_config_cb = fail_vendor_config_cb, // Must be assigned by caller }; } diff --git a/esp/cpp/components/lcd/lcd.c b/esp/cpp/components/lcd/lcd.c index 05fd806..3f068bb 100644 --- a/esp/cpp/components/lcd/lcd.c +++ b/esp/cpp/components/lcd/lcd.c @@ -7,7 +7,3 @@ */ #include "include/lcd.h" - -// LVGL library is not thread-safe, this example calls LVGL APIs from tasks. -// We must use a mutex to protect it. -static _lock_t lv_lock_; diff --git a/esp/cpp/components/lcd/panel_device.cpp b/esp/cpp/components/lcd/panel_device.cpp index 40fbf32..9ed1c26 100644 --- a/esp/cpp/components/lcd/panel_device.cpp +++ b/esp/cpp/components/lcd/panel_device.cpp @@ -10,162 +10,3 @@ #include #include #include - -static bool lvgl_flush_ready_cb(esp_lcd_panel_io_handle_t, - esp_lcd_panel_io_event_data_t*, void* user_ctx) -{ - auto* disp = (lv_display_t*)user_ctx; - lv_display_flush_ready(disp); - return false; -} - -static void lvgl_flush_cb(lv_display_t* display, const lv_area_t* area, - uint8_t* px_map) // NOLINT(*-non-const-parameter) -{ - auto panel_handle = - (esp_lcd_panel_handle_t)lv_display_get_user_data(display); - - // Necessary because LVGL reserves 2x4 bytes in the buffer for a palette. - // Since we are monochrome, we don't need these additional bytes. - // For more information about the monochrome, please refer to: - // https://docs.lvgl.io/9.2/porting/display.html#monochrome-displays - // Skip the palette here. - px_map += LVGL_PALETTE_SIZE; - - uint16_t hor_res = lv_display_get_physical_horizontal_resolution(display); - int32_t x1 = area->x1; - int32_t x2 = area->x2; - int32_t y1 = area->y1; - int32_t y2 = area->y2; - - - // As of 03/01/2025 master branch of LVGL contains this helper for the same. - // https://docs.lvgl.io/master/API/draw/sw/lv_draw_sw_utils.html - // lv_draw_sw_i1_convert_to_vtiled() - for (int32_t y = y1; y <= y2; y++) - { - for (int32_t x = x1; x <= x2; x++) - { - // Get the byte offset of the pixel coordinates using - // horizontal-mapping. - int h_offset = horizontal_byte_offset_long(x, y, hor_res); - // True if the pixel is lit, else false. - bool chroma_color = px_map[h_offset] & msb_mask(x); - - // We need an additional buffer for transposing the pixel data to - // the vertical format required by the display driver. - uint8_t* buf = get_additional_draw_buffer(); - // Move to the position in the auxillary buffer where the pixel is - // stored. - buf += vertical_byte_offset_long(x, y, hor_res); - - // Write the single bit to the monochrome display mapped vertically. - // Take the Least Significant Bit mask of the Y coordinate to select - // the bit representing a pixel at position y in a vertically-mapped - // display. - if (chroma_color) - { - // Set the vertically-mapped pixel to on. - *buf &= ~lsb_mask(y); - } - else - { - // Set the vertically-mapped pixel to off. - *buf |= lsb_mask(y); - } - } - } - // Pass the draw buffer to the driver. - ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap( - panel_handle, x1, y1, x2 + 1, y2 + 1, get_additional_draw_buffer())); -} - -static void lvgl_increase_tick_cb(void*) -{ - // Tell LVGL how many milliseconds has elapsed - lv_tick_inc(LVGL_TICK_PERIOD_MS); -} - -[[noreturn]] static void lvgl_port_task(void*) -{ - // Optionally initialize some LVGL objects here before entering loop below. - - ESP_LOGI(LCD_TAG, "Starting LVGL task"); - for (uint32_t time_to_next_ms = 0; true; usleep(1000 * time_to_next_ms)) - { - // Obtain LVGL API lock before calling any LVGL methods. - _lock_acquire(&lv_lock_); - - // Optionally handle LVGL input or event logic here. - - // Update LVGL periodic timers. - time_to_next_ms = lv_timer_handler(); - - _lock_release(&lv_lock_); - } -} - -void register_rendering_data(lv_display_t* display_handle, - esp_lcd_panel_io_handle_t io_handle, void* lv_buf, - size_t lv_buf_size) -{ - // Create draw buffer. - ESP_LOGI(LCD_TAG, "Allocate separate LVGL draw buffers"); - lv_buf = - heap_caps_calloc(1, lv_buf_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); - assert(lv_buf); - - ESP_LOGI(LCD_TAG, "Set LVGL draw buffers"); - // Color format must be set first, LVGL9 support new monochromatic format. - lv_display_set_color_format(display_handle, LV_COLOR_FORMAT_I1); - lv_display_set_buffers(display_handle, lv_buf, nullptr, lv_buf_size, - LV_DISPLAY_RENDER_MODE_FULL); - lv_display_set_rotation(display_handle, LV_DISPLAY_ROTATION_0); - - ESP_LOGI(LCD_TAG, "Set LVGL callback for flushing to the display"); - lv_display_set_flush_cb(display_handle, lvgl_flush_cb); - - ESP_LOGI(LCD_TAG, - "Register io panel callback for LVGL flush ready notification"); - const esp_lcd_panel_io_callbacks_t cbs = { - .on_color_trans_done = lvgl_flush_ready_cb, - }; - ESP_ERROR_CHECK(esp_lcd_panel_io_register_event_callbacks(io_handle, &cbs, - display_handle)); -} - -/** - * Registers LVGL ticker timer callback for rendering this display. - * - * An implementation of the interface can optionally override this method to - * provide custom LVGL callbacks and tick configurations. - */ -void register_lvgl_tick_timer() -{ - ESP_LOGI(LCD_TAG, "Use esp_timer to increase LVGL tick"); - const esp_timer_create_args_t esp_timer_args = { - .callback = &lvgl_increase_tick_cb, - // Data to pass to the lvgl_port_task callback. - .arg = nullptr, - .name = "lvgl_tick", - }; - - /// Start periodic ESP timer handle. - esp_timer_handle_t esp_timer_; - ESP_LOGI(LCD_TAG, "Creating esp_timer with name: '%s'", - esp_timer_args.name); - ESP_ERROR_CHECK(esp_timer_create(&esp_timer_args, &esp_timer_)); - ESP_ERROR_CHECK( - esp_timer_start_periodic(esp_timer_, LVGL_TICK_PERIOD_MS * 1000)); - ESP_LOGI(LCD_TAG, "Starting esp_timer with name: '%s'", - esp_timer_args.name); - - // LVGL requires a FreeRTOS task for running it's event loop. - // The lvgl_port_task callback can update the UI or handle input logic. - // For this basic example we don't do either of these things. - ESP_LOGI(LCD_TAG, "Create LVGL FreeRTOS task"); - // Optionally set user data to pass to LVGL's FreeRTOS task callback here. - void* user_data = nullptr; - xTaskCreate(lvgl_port_task, "LVGL", LVGL_TASK_STACK_SIZE, user_data, - LVGL_TASK_PRIORITY, nullptr); -} diff --git a/esp/cpp/components/ssd1306/include/ssd1306.h b/esp/cpp/components/ssd1306/include/ssd1306.h index 701baf1..6904fd0 100644 --- a/esp/cpp/components/ssd1306/include/ssd1306.h +++ b/esp/cpp/components/ssd1306/include/ssd1306.h @@ -76,18 +76,9 @@ SSD1306_config_new(esp_lcd_panel_ssd1306_config_t vendor_config, int width, SSD1306_config = vendor_config; // Must initialize device with LCD_new_panel to use default LVGL callbacks. struct IPanelDevice device = LCD_new_panel(); + // TODO: Helper to set width and update lv_buf_size. device.width_ = width, device.height_ = height; - device.esp_io_config_ = (esp_lcd_panel_io_i2c_config_t){ - .dev_addr = I2C_HW_ADDR, - // User data to pass to the LVGL flush_ready callback. - // See IPanelDevice::lvgl_flush_ready_cb - .user_ctx = NULL, - .control_phase_bytes = 1, - .dc_bit_offset = 6, - .lcd_cmd_bits = LCD_CMD_BITS, - .lcd_param_bits = LCD_PARAM_BITS, - .scl_speed_hz = LCD_PIXEL_CLOCK_HZ, - }; + device.lv_buf_size_ = width * height / 8 + LVGL_PALETTE_SIZE; device.init_panel_cb = SSD1306_init_panel_cb; device.vendor_config_cb = SSD1306_vendor_config_cb; return device;