Fix dependencies for each component.

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

View File

@ -1,8 +1,6 @@
## IDF Component Manager Manifest File
dependencies:
idf: '>=5.3.0'
i2c:
path: ../../components/i2c
lcd:
path: ../../components/lcd
ssd1306:

View File

@ -18,20 +18,18 @@ extern "C" void app_main(void)
IPanelDevice lcd = SSD1306_new();
LCD d = LCD_init(&lcd);
LCD_set_text_with_mode(&d, "Test test 12345678910", "test-text1",
LV_LABEL_LONG_SCROLL, LV_ALIGN_CENTER);
LCD_set_text_with_mode(&d, "Test test 12345678910", LV_LABEL_LONG_SCROLL,
LV_ALIGN_CENTER);
// TODO: Uncomment and test once LCD::lv_obj_t is a dynamic array.
// LCD_set_text_with_mode(&d, "Test test changing text",
// "test-text1",
// LV_LABEL_LONG_SCROLL,
// LV_ALIGN_CENTER);
//
// LCD_set_text(&d, "Hello hello hello hello hello hello hello hello!",
// "test-text2");
//
// LCD_set_text_with_mode(&d, "A random sentence with no meaning at all.",
// "test-text3",
// LV_LABEL_LONG_CLIP,
// LV_ALIGN_BOTTOM_MID);
LCD_set_text(&d, "Hello hello hello hello hello hello hello hello!");
LCD_set_text_with_mode(&d, "A random sentence with no meaning at all.",
LV_LABEL_LONG_CLIP, LV_ALIGN_BOTTOM_MID);
sleep(1);
LCD_clear(&d);
LCD_set_text(&d, "Test clearing the screen");
LCD_set_text_with_mode(&d, "Test writing something and overwriting it",
LV_LABEL_LONG_SCROLL, LV_ALIGN_CENTER);
LCD_set_text_at(&d, "Overwrite.", 1);
}

View File

@ -2,4 +2,4 @@ version: "0.0.1"
description: ESP I2C helper component
url: https://git.shaunreed.com/shaunrd0/klips/tree/master/esp/cpp/components/i2c
dependencies:
idf: ">=5.3"
idf: ">=5.3"

View File

@ -9,13 +9,23 @@
#define I2C_H
#define I2C_BUS_PORT 0
#define I2C_DEFAULT_PIN_RST (-1)
#include <driver/i2c_master.h>
#include <esp_log.h>
/// Tag used for ESP logging.
static const char* I2C_TAG = "I2C component";
/**
* ESP I2C master bus handle getter.
*/
static i2c_master_bus_handle_t I2C_get()
{
i2c_master_bus_handle_t i2c = NULL;
ESP_ERROR_CHECK(i2c_master_get_bus_handle(0, &i2c));
return i2c;
}
/**
* Construct an ESP I2C master bus given a specific ESP I2C configuration.
* An I2C constructor may only be called one time in any application.
@ -24,7 +34,7 @@ static const char* I2C_TAG = "I2C component";
*/
static void I2C_config_init(const i2c_master_bus_config_t config)
{
i2c_master_bus_handle_t i2c;
i2c_master_bus_handle_t i2c = NULL;
ESP_LOGI(I2C_TAG, "Initializing new master I2C bus");
ESP_ERROR_CHECK(i2c_new_master_bus(&config, &i2c));
}
@ -38,7 +48,7 @@ static void I2C_config_init(const i2c_master_bus_config_t config)
*/
static void I2C_init(gpio_num_t sda, gpio_num_t scl)
{
return I2C_config_init((i2c_master_bus_config_t){
I2C_config_init((i2c_master_bus_config_t){
.i2c_port = I2C_BUS_PORT,
.sda_io_num = sda,
.scl_io_num = scl,
@ -51,15 +61,4 @@ static void I2C_init(gpio_num_t sda, gpio_num_t scl)
});
}
/**
* ESP I2C master bus handle getter.
* This will fail if an I2C instance was never constructed.
*/
static i2c_master_bus_handle_t I2C_get()
{
i2c_master_bus_handle_t i2c = NULL;
ESP_ERROR_CHECK(i2c_master_get_bus_handle(0, &i2c));
return i2c;
}
#endif // I2C_H

View File

@ -6,4 +6,5 @@ dependencies:
lvgl/lvgl: 9.2.0
espressif/esp_lcd_sh1107: ==1.0.0
i2c:
require: public
path: ../../components/i2c

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); }

View File

@ -5,7 +5,5 @@ dependencies:
idf: ">=5.3"
lvgl/lvgl: 9.2.0
espressif/esp_lcd_sh1107: ==1.0.0
i2c:
path: ../../components/i2c
lcd:
path: ../../components/lcd
path: ../../components/lcd