Fix dependencies for each component.

Add ability to clear LCD screen, and write at a label position.
This commit is contained in:
2025-11-02 16:43:35 -05:00
parent adf081cf1a
commit 164af20d56
9 changed files with 267 additions and 176 deletions

View File

@@ -26,14 +26,13 @@ struct LCD
/// LVGL display handle.
lv_display_t* lv_display_;
// TODO: Should be a dynamic array.
lv_obj_t* lv_objects_;
lv_array_t lv_objs_;
struct IPanelDevice* device_;
};
/**
* Construct a new Display using an object that implements IPanelDevice.
* Construct a new LCD using an object that implements IPanelDevice.
*
* @param device An object that implements the IPanelDevice interface.
*/
@@ -86,7 +85,7 @@ static struct LCD LCD_init(struct IPanelDevice* device)
ESP_LOGI(LCD_TAG, "Creating LVGL display");
display.lv_display_ = lv_display_create(device->width_, device->height_);
// assert(display.lv_display_);
assert(display.lv_display_);
// associate the i2c panel handle to the display
lv_display_set_user_data(display.lv_display_, display.esp_panel_);
@@ -94,6 +93,8 @@ static struct LCD LCD_init(struct IPanelDevice* device)
device->register_rendering_data_cb(display.lv_display_, display.esp_io_,
device->lv_buf_, device->lv_buf_size_);
device->register_lvgl_tick_timer_cb();
ESP_LOGI(LCD_TAG, "Initializing LVGL array");
lv_array_init(&display.lv_objs_, 1, sizeof(lv_obj_t*));
return display;
}
@@ -103,36 +104,40 @@ static struct LCD LCD_init(struct IPanelDevice* device)
*
* @param display
* @param text Text to write to the display.
* @param name Name for the LVGL label object associated with this text.
* @param long_mode LVGL long mode for text wider than the current display.
* @param align LVGL alignment to use for placing the label on the display.
* @return The index of the inserted label on the LVGL screen
*/
static void LCD_set_text_with_mode(struct LCD* display, const char* text,
const char* name,
lv_label_long_mode_t long_mode,
lv_align_t align)
static uint32_t LCD_set_text_with_mode(struct LCD* display, const char* text,
lv_label_long_mode_t long_mode,
lv_align_t align)
{
// Lock the mutex due to the LVGL APIs are not thread-safe.
_lock_acquire(&lv_lock_);
ESP_LOGI(LCD_TAG, "Display LVGL Scroll Text");
ESP_LOGI(LCD_TAG, "Setting new text: %s", text);
lv_obj_t* scr = lv_display_get_screen_active(display->lv_display_);
// Create the label if it's `name` doesn't already exist in the map keys.
display->lv_objects_ = lv_label_create(scr);
lv_obj_t* new_object = lv_label_create(scr);
// Set text and long mode.
lv_label_set_long_mode(display->lv_objects_, long_mode);
lv_label_set_text(display->lv_objects_, text);
lv_label_set_long_mode(new_object, long_mode);
lv_label_set_text(new_object, text);
// Set the size of the screen.
// If you use rotation 90 or 270 use lv_display_get_vertical_resolution.
lv_obj_set_width(
display->lv_objects_,
lv_display_get_horizontal_resolution(display->lv_display_));
lv_obj_align(display->lv_objects_, align, 0, 0);
new_object, lv_display_get_horizontal_resolution(display->lv_display_));
lv_obj_align(new_object, align, 0, 0);
uint32_t index = lv_array_size(&display->lv_objs_);
if (lv_array_push_back(&display->lv_objs_, &new_object) != LV_RESULT_OK)
{
ESP_LOGE(LCD_TAG, "Failed to add new object to array");
}
_lock_release(&lv_lock_);
return 1;
}
/**
@@ -141,13 +146,108 @@ static void LCD_set_text_with_mode(struct LCD* display, const char* text,
*
* @param display
* @param text Text to write to the display.
* @param name Name for the LVGL label object associated with this text.
* @return The index of the inserted label on the LVGL screen
*/
static void LCD_set_text(struct LCD* display, const char* text,
const char* name)
static uint32_t LCD_set_text(struct LCD* display, const char* text)
{
LCD_set_text_with_mode(display, text, name, LV_LABEL_LONG_SCROLL_CIRCULAR,
LV_ALIGN_TOP_MID);
return LCD_set_text_with_mode(display, text, LV_LABEL_LONG_SCROLL_CIRCULAR,
LV_ALIGN_TOP_MID);
}
static void LCD_set_text_at_with_mode(struct LCD* display, const char* text,
uint32_t i,
lv_label_long_mode_t long_mode,
lv_align_t align)
{
// Lock the mutex due to the LVGL APIs are not thread-safe.
_lock_acquire(&lv_lock_);
if (lv_array_is_empty(&display->lv_objs_))
{
ESP_LOGI(LCD_TAG, "Cannot set text at index %d; The array is empty.");
_lock_release(&lv_lock_);
return;
}
lv_obj_t** ptr = (lv_obj_t**)lv_array_at(&display->lv_objs_, i);
lv_obj_t* label = (lv_obj_t*)*ptr;
if (label == NULL)
{
ESP_LOGE(LCD_TAG, "Failed to set text at index %d; Label is null", i);
_lock_release(&lv_lock_);
return;
}
lv_label_set_text(label, text);
ESP_LOGI(LCD_TAG, "Setting text at index %d:", i);
// Set text and long mode.
lv_label_set_long_mode(label, long_mode);
lv_label_set_text(label, text);
// Set the size of the screen.
// If you use rotation 90 or 270 use lv_display_get_vertical_resolution.
lv_obj_set_width(
label, lv_display_get_horizontal_resolution(display->lv_display_));
lv_obj_align(label, align, 0, 0);
_lock_release(&lv_lock_);
}
static void LCD_set_text_at(struct LCD* display, const char* text, uint32_t i)
{
// Lock the mutex due to the LVGL APIs are not thread-safe.
_lock_acquire(&lv_lock_);
if (lv_array_is_empty(&display->lv_objs_))
{
ESP_LOGI(LCD_TAG, "Cannot set text at index %d; The array is empty.");
_lock_release(&lv_lock_);
return;
}
lv_obj_t** ptr = (lv_obj_t**)lv_array_at(&display->lv_objs_, i);
lv_obj_t* label = (lv_obj_t*)*ptr;
if (label == NULL)
{
ESP_LOGE(LCD_TAG, "Failed to set text at index %d; Label is null", i);
_lock_release(&lv_lock_);
return;
}
lv_label_set_text(label, text);
_lock_release(&lv_lock_);
}
static void LCD_clear(struct LCD* display)
{
_lock_acquire(&lv_lock_);
uint32_t size = lv_array_size(&display->lv_objs_);
ESP_LOGI(LCD_TAG, "Clearing %d LVGL objects", size);
for (uint32_t i = 0; i < size && size > 0; i++)
{
ESP_LOGI(LCD_TAG, "Checking array index %d", i);
lv_obj_t** ptr_to_delete =
(lv_obj_t**)lv_array_at(&display->lv_objs_, i);
lv_obj_t* to_delete = (lv_obj_t*)*ptr_to_delete;
if (to_delete == NULL)
{
ESP_LOGE(LCD_TAG, "Failed to clear all LVGL objects");
continue;
}
if (lv_obj_is_valid(to_delete))
{
ESP_LOGI(LCD_TAG, "Removing LVGL object");
lv_label_set_text(to_delete, "test");
lv_obj_delete(to_delete);
}
else
{
ESP_LOGE(LCD_TAG, "Error: LVGL object is not valid");
}
}
lv_array_clear(&display->lv_objs_);
_lock_release(&lv_lock_);
}
#endif // DISPLAY_H

View File

@@ -13,9 +13,12 @@
#include <esp_lcd_panel_ops.h>
#include <esp_log.h>
#include <esp_timer.h>
#include <sys/unistd.h>
// TODO: Remove
#include <i2c.h>
#include <pixel.h>
static esp_timer_handle_t esp_timer_;
#include <display/lv_display.h>
@@ -50,115 +53,6 @@ static _lock_t lv_lock_;
#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.
*
* 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.
*
@@ -228,7 +122,6 @@ struct IPanelDevice
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.
@@ -256,7 +149,13 @@ struct IPanelDevice
* @return Pointer to uint8 draw buffer data.
* @sa register_rendering_data for overriding LVGL rendering callbacks.
*/
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;
}
/**
* The callback invoked when panel IO finishes transferring color data.
@@ -268,8 +167,8 @@ struct IPanelDevice
* @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,
static bool lvgl_flush_ready_cb(esp_lcd_panel_io_handle_t,
esp_lcd_panel_io_event_data_t*,
void* user_ctx)
{
lv_display_t* disp = (lv_display_t*)user_ctx;
@@ -405,7 +304,7 @@ 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*)
{
// Tell LVGL how many milliseconds has elapsed
lv_tick_inc(LVGL_TICK_PERIOD_MS);
@@ -423,7 +322,7 @@ 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*)
{
// Optionally initialize some LVGL objects here before entering loop below.
@@ -478,8 +377,8 @@ static void register_lvgl_tick_timer()
}
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)
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");
@@ -528,7 +427,7 @@ static struct IPanelDevice LCD_new_panel()
return (struct IPanelDevice){
.width_ = LCD_H_RES,
.height_ = LCD_V_RES,
.rst_num_ = -1,
.rst_num_ = I2C_DEFAULT_PIN_RST,
.lv_buf_size_ = LCD_H_RES * LCD_V_RES / 8 + LVGL_PALETTE_SIZE,
.esp_io_config_ =
(esp_lcd_panel_io_i2c_config_t){

View File

@@ -0,0 +1,98 @@
/*#############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2025 Shaun Reed, all rights reserved ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com ##
##############################################################################
*/
/**
* 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);
}
/**
* 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;
}
/**
* 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); }