/*############################################################################# ## Author: Shaun Reed ## ## Legal: All Content (c) 2025 Shaun Reed, all rights reserved ## ## ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ## ############################################################################## */ #ifndef PANEL_DEVICE_H #define PANEL_DEVICE_H #include #include #include #include // 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 #define LVGL_TICK_PERIOD_MS 5 #define LVGL_TASK_STACK_SIZE (4 * 1024) #define LVGL_TASK_PRIORITY 2 #define LCD_H_RES 128 #define LCD_V_RES 64 /** * Calculate byte offset for the pixel at [x,y] within a horizontally-mapped * monochrome uint8 draw buffer, using the initialized horizontal resolution. * * We use `>> 3` because each pixel requires 1 bit, but each uint8 in the draw * buffer can hold 8 bits. To find the uint8 value in our draw buffer that * stores this pixel's value we must compensate for this when using pixel * coordinates in byte math. * * Therefore, each uint8 in the draw buffer stores the state of 8 pixels. * Below is an example of calculating for [x, y] pixel coordinates [20, 10]. * The example uses a horizontal resolution of 128. * * For the horizontal case, each row (y) of the image is represented by * `hor_res >> 3` bytes (16). The byte-offset of the first pixel in the 10th * row for example is `16 * 10` = 160. * * Since the pixels are stored horizontally we must calculate the 20th pixel * column (x) as `160 + (20 >> 3)`, or `160 + (20 / 8)` to get a final offset * of 162. * * @param x X pixel coordinate to find byte offset. * @param y Y pixel coordinate to find byte offset. * @param hor_res horizontal resolution of the display. * @return byte offset for a single-byte monochrome pixel at [x,y]. */ static ptrdiff_t horizontal_byte_offset_long(const int32_t x, const int32_t y, const int32_t hor_res) { // Convert pixel (bit) coordinates to byte coordinates in the draw buffer. return (hor_res >> 3) * y + (x >> 3); } static ptrdiff_t horizontal_byte_offset(const int32_t x, const int32_t y) { return horizontal_byte_offset_long(x, y, LCD_V_RES); } /** * Calculate byte offset for the pixel at [x,y] within a vertically-mapped * monochrome uint8 draw buffer, using the initialized horizontal resolution. * * We use `>> 3` because each pixel requires 1 bit, but each uint8 in the draw * buffer can hold 8 bits. To find the uint8 value in our draw buffer that * stores this pixel's value we must compensate for this when using pixel * coordinates in byte math. * * Therefore, each uint8 in the draw buffer stores the state of 8 pixels. * Below is an example of calculating for [x, y] pixel coordinates [20, 10]. * The example uses a horizontal resolution of 128. * * For the vertical case, each row (y) of the image is represented by * `hor_res` bytes (128) - one for each column (x). Because the pixels are * stored vertically, the byte-offset of the first pixel in the 10th row is * `128 * (10 >> 3)` or * `128 * (10 / 8)` = 128. * * From this location we can simply calculate the 20th pixel column (x) as * `128 + 20` to get a final offset of 148, because the pixels are stored in a * columnar format. * * @param x X pixel coordinate to find byte offset. * @param y Y pixel coordinate to find byte offset. * @param hor_res horizontal resolution of the display. * @return byte offset for a single-byte monochrome pixel at [x,y]. */ static ptrdiff_t vertical_byte_offset_long(const int32_t x, const int32_t y, const int32_t hor_res) { // Convert pixel (bit) coordinates to byte coordinates in the draw buffer. return hor_res * (y >> 3) + x; } static ptrdiff_t vertical_byte_offset(const int32_t x, const int32_t y) { return vertical_byte_offset_long(x, y, LCD_V_RES); } /** * Finds the Most Significant Bit location of bit `i` in a byte. * * MSB LSB * bits 7 6 5 4 3 2 1 0 * data 8 7 6 5 4 3 2 1 * Left Right * * @return bitmask for MSB location of `i`. */ static uint8_t msb_mask(const int32_t i) { return 1 << (7 - i % 8); } /** * Finds the Least Significant Bit location of bit `i` in a byte. * * LSB MSB * bits 0 1 2 3 4 5 6 7 * data 1 2 3 4 5 6 7 8 * Left Right * * @return bitmask for LSB location of `i`. */ static uint8_t lsb_mask(const int32_t i) { return 1 << (i % 8); } static uint8_t* get_additional_draw_buffer() { // Static to the scope of this function, not the compilation unit. // For LV_COLOR_FORMAT_I1 we need an extra buffer to hold converted data. static uint8_t oled_buffer[LCD_H_RES * LCD_V_RES / 8]; return oled_buffer; } /** * Retrieve the device specific vendor configuration structure. * * @return Address of vendor configuration structure. */ typedef void* (*vendor_config_cb_t)(); /** * 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. */ typedef void (*register_lvgl_tick_timer_cb_t)(); /** * Initializes the ESP panel using vendor specific APIs and configurations. * This method should implement any setup logic specific to the device. * * @param config ESP LCD panel configuration. * @param io ESP LCD panel IO handle. * @param [out] panel ESP LCD panel handle output pointer location. */ typedef void (*init_panel_cb_t)(esp_lcd_panel_dev_config_t* config, esp_lcd_panel_io_handle_t io, esp_lcd_panel_handle_t* panel); /** * Registers LVGL draw buffers and callbacks for this display. * * An implementation of the interface can optionally override this method to * provide custom LVGL callbacks and display configurations. * * @param display_handle LVGL display handle to use for rendering. * @param io_handle IO handle for the ESP LCD panel. */ typedef void (*register_rendering_data_cb_t)( lv_display_t* display_handle, esp_lcd_panel_io_handle_t io_handle, void*, size_t); /** * Encapsulates vendor specific ESP LCD panel initialization logic. * This pure virtual interface can be inherited from for using new LCD devices. * See SSD1306 as an example to implement IPanelDevice for NT35510 or ST7789. * * At this time only I2C is supported. * Classes that inherit from this interface should likely be marked final. */ struct IPanelDevice { /// Width of the device screen in pixels. int32_t width_; /// Height of the device screen in pixels. int32_t height_; /// RST GPIO pin number. int rst_num_; /// LVGL draw buffer size for the device. size_t lv_buf_size_; /// ESP LCD panel IO configuration. esp_lcd_panel_io_i2c_config_t esp_io_config_; /// LVGL draw buffer associated with this Display's lv_display_t. void* lv_buf_; /// Callback used to initialize the ESP panel. /// TODO: Assert by default? init_panel_cb_t init_panel_cb; /// Callback used to register LVGL tick timer. register_lvgl_tick_timer_cb_t register_lvgl_tick_timer_cb; /// Callback used to register LVGL draw buffers. register_rendering_data_cb_t register_rendering_data_cb; /// Callback used to fetch the display vendor configuration. vendor_config_cb_t vendor_config_cb; }; /** * Static accessor to a static buffer to store draw buffer data for the panel. * * This method is protected to allow an implementation to provide a custom * callback method similar to IPanelDevice::lvgl_flush_cb. * * The buffer is allocated statically within the scope of this function to * allow creating multiple panels that _each_ manage their own statically * allocated draw buffer data. This simplifies implementing the interface by * taking this responsibility off of the implementor. The buffer will only be * allocated if this method is called, so the memory is only used if required. * * @return Pointer to uint8 draw buffer data. * @sa register_rendering_data for overriding LVGL rendering callbacks. */ /** * The callback invoked when panel IO finishes transferring color data. * This signals that the panel is ready to flush image data to the display. * * @param panel LCD panel IO handles. * @param data Panel IO event data, fed by driver. * @param user_ctx User data, passed from `esp_lcd_panel_io_xxx_config_t`. * @return Whether a high priority task has been waken up by this function. * @sa register_rendering_data for overriding this callback. */ static bool lvgl_flush_ready_cb(esp_lcd_panel_io_handle_t panel, esp_lcd_panel_io_event_data_t* data, void* user_ctx); /** * The callback invoked for flushing the rendered image to the display. * * `px_map` contains the rendered image as raw pixel map and it should be * copied to `area` on the display. * * The following details are crucial for understanding the logic surrounding * flushing to the display in this example. * * The order of bits within the px_map from _LVGL_ is MSB first. * MSB LSB * bits 7 6 5 4 3 2 1 0 * pixels 0 1 2 3 4 5 6 7 * Left Right * * The bytes from _LVGL_ are mapped to pixel rows of the display * 8 bits (pixels) per byte - * [0, 0, 0, 0, 0, 0, 0, 0] * [0, 0, 0, 0, 0, 0, 0, 0] * [0, 0, 0, 0, 0, 0, 0, 0] * * The order of bits expected by the _display driver_ is LSB first. * We must preserve pairing of each bit and pixel when writing to the display. * LSB MSB * bits 0 1 2 3 4 5 6 7 * pixels 7 6 5 4 3 2 1 0 * Left Right * * Bytes expected by the _display driver_ map to pixel columns of the display. * 8 bits (pixels) per byte - * [0, [0, [0, [0, * 0, 0, 0, 0, * 0, 0, 0, 0, * 0, 0, 0, 0, * 0, 0, 0, 0, * 0, 0, 0, 0, * 0, 0, 0, 0, * 0] 0] 0] 0] * * These layouts in memory have no opinion on the shape of the image. The * beginning and end of a row or a column for example is entirely dependent * on how the data is accessed. The vertical and horitzontal resolution may * vary between displays. * * For the LV_COLOR_FORMAT_I1 color format we are using, an additional buffer * is needed for transposing the bits to the vertical arrangement required by * the display driver that is outlined above. * * This callback implementation is an example of handling this transposition * and flushing the data to the display in the expected format. * * @param display LVGL display handle to use for rendering. * @param area Area of the display being flushed. * @param px_map Rendered image data for writing to the display area. * @sa register_rendering_data for overriding this callback. * @sa get_additional_draw_buffer */ static void lvgl_flush_cb(lv_display_t* display, const lv_area_t* area, uint8_t* px_map); /** * Callback invoked for every period of the timer. * * This callback _must_ call lv_tick_inc to inform LVGL how much time has * elapsed since the last period of the timer. * * @param data User data passed to the callback. * @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); /** * FreeRTOS task callback invoked for handling LVGL events or updating the UI. * * This function is intentionally an endless loop and should never return. * LVGL initialization logic can optionally be added before entering the loop. * Input logic can optionally be handled within the loop. * * This callback _must_ call lv_timer_handler to handle LVGL periodic timers. * * @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); /** * 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(); 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); inline 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 .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 }; } #endif // PANEL_DEVICE_H