Fix LVGL issues and test.
The display renders correctly
This commit is contained in:
		
							parent
							
								
									e0b583f8c1
								
							
						
					
					
						commit
						adf081cf1a
					
				@ -17,24 +17,6 @@
 | 
				
			|||||||
#include <esp_lcd_panel_ops.h>
 | 
					#include <esp_lcd_panel_ops.h>
 | 
				
			||||||
#include <esp_lcd_types.h>
 | 
					#include <esp_lcd_types.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 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
 | 
					struct LCD
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    esp_lcd_panel_handle_t esp_panel_;
 | 
					    esp_lcd_panel_handle_t esp_panel_;
 | 
				
			||||||
 | 
				
			|||||||
@ -10,10 +10,23 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include <esp_lcd_panel_dev.h>
 | 
					#include <esp_lcd_panel_dev.h>
 | 
				
			||||||
#include <esp_lcd_panel_io.h>
 | 
					#include <esp_lcd_panel_io.h>
 | 
				
			||||||
 | 
					#include <esp_lcd_panel_ops.h>
 | 
				
			||||||
#include <esp_log.h>
 | 
					#include <esp_log.h>
 | 
				
			||||||
 | 
					#include <esp_timer.h>
 | 
				
			||||||
 | 
					#include <sys/unistd.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// TODO: Remove
 | 
				
			||||||
 | 
					static esp_timer_handle_t esp_timer_;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <display/lv_display.h>
 | 
					#include <display/lv_display.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// 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.
 | 
					// LVGL reserves 2x4 bytes in the buffer to be used as a palette.
 | 
				
			||||||
// This additional space must be added to the IPanelDevice::buf_size_.
 | 
					// This additional space must be added to the IPanelDevice::buf_size_.
 | 
				
			||||||
#define LVGL_PALETTE_SIZE 8
 | 
					#define LVGL_PALETTE_SIZE 8
 | 
				
			||||||
@ -24,6 +37,19 @@
 | 
				
			|||||||
#define LCD_H_RES 128
 | 
					#define LCD_H_RES 128
 | 
				
			||||||
#define LCD_V_RES 64
 | 
					#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
 | 
					 * Calculate byte offset for the pixel at [x,y] within a horizontally-mapped
 | 
				
			||||||
 * monochrome uint8 draw buffer, using the initialized horizontal resolution.
 | 
					 * 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,
 | 
					static bool lvgl_flush_ready_cb(esp_lcd_panel_io_handle_t panel,
 | 
				
			||||||
                                esp_lcd_panel_io_event_data_t* data,
 | 
					                                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.
 | 
					 * 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
 | 
					 * @sa get_additional_draw_buffer
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
static void lvgl_flush_cb(lv_display_t* display, const lv_area_t* area,
 | 
					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.
 | 
					 * 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
 | 
					 * @sa register_lvgl_tick_timer for setting user data and the tick period of
 | 
				
			||||||
 *        the timer, or overriding this callback entirely.
 | 
					 *        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.
 | 
					 * 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.
 | 
					 * @param data User data passed to the callback.
 | 
				
			||||||
 * @sa register_lvgl_tick_timer for overriding this 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.
 | 
					 * 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
 | 
					 * An implementation of the interface can optionally override this method to
 | 
				
			||||||
 * provide custom LVGL callbacks and tick configurations.
 | 
					 * 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,
 | 
					                             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){
 | 
					    return (struct IPanelDevice){
 | 
				
			||||||
        .width_ = LCD_H_RES,
 | 
					        .width_ = LCD_H_RES,
 | 
				
			||||||
        .height_ = LCD_V_RES,
 | 
					        .height_ = LCD_V_RES,
 | 
				
			||||||
        .rst_num_ = -1,
 | 
					        .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_lvgl_tick_timer_cb = register_lvgl_tick_timer,
 | 
				
			||||||
        .register_rendering_data_cb = register_rendering_data,
 | 
					        .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
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -7,7 +7,3 @@
 | 
				
			|||||||
*/
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "include/lcd.h"
 | 
					#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_;
 | 
					 | 
				
			||||||
 | 
				
			|||||||
@ -10,162 +10,3 @@
 | 
				
			|||||||
#include <esp_timer.h>
 | 
					#include <esp_timer.h>
 | 
				
			||||||
#include <lcd.h>
 | 
					#include <lcd.h>
 | 
				
			||||||
#include <sys/unistd.h>
 | 
					#include <sys/unistd.h>
 | 
				
			||||||
 | 
					 | 
				
			||||||
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);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
				
			|||||||
@ -76,18 +76,9 @@ SSD1306_config_new(esp_lcd_panel_ssd1306_config_t vendor_config, int width,
 | 
				
			|||||||
    SSD1306_config = vendor_config;
 | 
					    SSD1306_config = vendor_config;
 | 
				
			||||||
    // Must initialize device with LCD_new_panel to use default LVGL callbacks.
 | 
					    // Must initialize device with LCD_new_panel to use default LVGL callbacks.
 | 
				
			||||||
    struct IPanelDevice device = LCD_new_panel();
 | 
					    struct IPanelDevice device = LCD_new_panel();
 | 
				
			||||||
 | 
					    // TODO: Helper to set width and update lv_buf_size.
 | 
				
			||||||
    device.width_ = width, device.height_ = height;
 | 
					    device.width_ = width, device.height_ = height;
 | 
				
			||||||
    device.esp_io_config_ = (esp_lcd_panel_io_i2c_config_t){
 | 
					    device.lv_buf_size_ = width * height / 8 + LVGL_PALETTE_SIZE;
 | 
				
			||||||
        .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.init_panel_cb = SSD1306_init_panel_cb;
 | 
					    device.init_panel_cb = SSD1306_init_panel_cb;
 | 
				
			||||||
    device.vendor_config_cb = SSD1306_vendor_config_cb;
 | 
					    device.vendor_config_cb = SSD1306_vendor_config_cb;
 | 
				
			||||||
    return device;
 | 
					    return device;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user