Add IPanelDevice.

This commit is contained in:
Shaun Reed 2025-02-15 17:12:06 -05:00
parent 670a523a16
commit b3d830cdeb
6 changed files with 136 additions and 123 deletions

View File

@ -17,8 +17,8 @@
// We must use a mutex to protect it.
_lock_t ScopedLock::lock_;
Display::Display(const I2C &i2c) :
panel_(i2c)
Display::Display(IPanelDevice *device) :
panel_(device)
{
if (!lv_is_initialized()) {
ESP_LOGI(TAG, "Initialize LVGL");
@ -26,26 +26,25 @@ Display::Display(const I2C &i2c) :
}
// Create a lvgl display.
// TODO: Use PanelDevice functions to get SSD1306 specs
display_ = lv_display_create(LCD_H_RES, LCD_V_RES);
lv_display_ = panel_.device_->create_display();
// associate the i2c panel handle to the display
lv_display_set_user_data(display_, panel_.get());
lv_display_set_user_data(lv_display_, panel_.esp_panel_);
// Create draw buffer.
ESP_LOGI(TAG, "Allocate separate LVGL draw buffers");
// TODO: Use PanelDevice functions to get SSD1306 specs
buf_size_ = LCD_H_RES * LCD_V_RES / 8 + LVGL_PALETTE_SIZE;
buf_ = heap_caps_calloc(1, buf_size_, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
assert(buf_);
lv_buf_ = heap_caps_calloc(1, panel_.device_->lv_buf_size_,
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
assert(lv_buf_);
// LVGL9 suooprt new monochromatic format.
lv_display_set_color_format(display_, LV_COLOR_FORMAT_I1);
lv_display_set_color_format(lv_display_, LV_COLOR_FORMAT_I1);
// Initialize LVGL draw buffers.
lv_display_set_buffers(display_, buf_, nullptr, buf_size_,
lv_display_set_buffers(lv_display_, lv_buf_, nullptr,
panel_.device_->lv_buf_size_,
LV_DISPLAY_RENDER_MODE_FULL);
lv_display_set_rotation(display_, LV_DISPLAY_ROTATION_0);
lv_display_set_rotation(lv_display_, LV_DISPLAY_ROTATION_0);
// Set callback which can copy the rendered image to an area of the display.
lv_display_set_flush_cb(display_, Display::lvgl_flush_cb);
lv_display_set_flush_cb(lv_display_, Display::lvgl_flush_cb);
ESP_LOGI(TAG, "Register io panel callback for LVGL flush ready notification");
const esp_lcd_panel_io_callbacks_t cbs = {
@ -54,7 +53,7 @@ Display::Display(const I2C &i2c) :
/* Register done callback */
ESP_ERROR_CHECK(
esp_lcd_panel_io_register_event_callbacks(panel_.io_handle_, &cbs,
display_));
lv_display_));
// TODO: What is this
ESP_LOGI(TAG, "Use esp_timer as LVGL tick timer");
@ -81,7 +80,7 @@ void Display::set_text(const char *text,
ScopedLock lock;
ESP_LOGI(TAG, "Display LVGL Scroll Text");
lv_obj_t *scr = lv_display_get_screen_active(display_);
lv_obj_t *scr = lv_display_get_screen_active(lv_display_);
objects_[name] = lv_label_create(scr);
// Circular scroll.
@ -91,7 +90,7 @@ void Display::set_text(const char *text,
// Set the size of the screen.
// If you use rotation 90 or 270 use lv_display_get_vertical_resolution.
lv_obj_set_width(obj, lv_display_get_horizontal_resolution(display_));
lv_obj_set_width(obj, lv_display_get_horizontal_resolution(lv_display_));
lv_obj_align(obj, align, 0, 0);
}
@ -166,13 +165,13 @@ void Display::lvgl_increase_tick(void *)
}
}
I2C::I2C() :
I2C::I2C(gpio_num_t sda, gpio_num_t scl) :
i2c_bus_(nullptr),
bus_config_(
(i2c_master_bus_config_t) {
.i2c_port = I2C_BUS_PORT,
.sda_io_num = PIN_SDA,
.scl_io_num = PIN_SCL,
.sda_io_num = sda,
.scl_io_num = scl,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.flags {
@ -185,43 +184,29 @@ I2C::I2C() :
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config_, &i2c_bus_));
}
Panel::Panel(i2c_master_bus_handle_t i2c) :
Panel::Panel(IPanelDevice *device) :
device_(device),
io_handle_(nullptr),
panel_(nullptr),
esp_panel_(nullptr),
// According to SSD1306 datasheet
panel_config_(
(esp_lcd_panel_dev_config_t) {
.reset_gpio_num = PIN_RST,
.reset_gpio_num = device_->reset_gpio_num_,
.bits_per_pixel = 1,
}
),
// According to SSD1306 datasheet
io_config_(
(esp_lcd_panel_io_i2c_config_t) {
.dev_addr = I2C_HW_ADDR,
.control_phase_bytes = 1,
.dc_bit_offset = 6,
.lcd_cmd_bits = LCD_CMD_BITS,
.lcd_param_bits = LCD_CMD_BITS,
.scl_speed_hz = LCD_PIXEL_CLOCK_HZ,
// .vendor_config should be set in IPanelDevice::init_panel override
}
)
{
ESP_LOGI(TAG, "Install panel IO");
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(i2c, &io_config_, &io_handle_));
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(
device_->i2c_bus_, &device_->io_config_, &io_handle_));
ESP_LOGI(TAG, "Install SSD1306 panel driver");
SSD1306 ssd1306(
(esp_lcd_panel_ssd1306_config_t) {
.height = LCD_V_RES,
}
);
ssd1306.create_panel(panel_config_, io_handle_, panel_);
device_->create_panel(panel_config_, io_handle_, esp_panel_);
ESP_LOGI(TAG, "Resetting panel display");
ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_));
ESP_ERROR_CHECK(esp_lcd_panel_reset(esp_panel_));
ESP_LOGI(TAG, "Initializing panel display");
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_));
ESP_ERROR_CHECK(esp_lcd_panel_init(esp_panel_));
ESP_LOGI(TAG, "Turning on panel display");
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(esp_panel_, true));
}

View File

@ -20,10 +20,8 @@
#define LVGL_TASK_STACK_SIZE (4 * 1024)
#define LVGL_TASK_PRIORITY 2
static const char *TAG = "lcd-panel";
struct I2C {
I2C();
I2C(gpio_num_t sda, gpio_num_t scl);
~I2C() = default;
@ -45,40 +43,29 @@ struct ScopedLock {
class Panel {
public:
explicit Panel(i2c_master_bus_handle_t i2c);
explicit Panel(const I2C &i2c) : Panel(i2c.i2c_bus_) { }
explicit Panel(IPanelDevice *device);
~Panel() = default;
[[nodiscard]] inline const esp_lcd_panel_t *get() const { return panel_; }
[[nodiscard]] inline esp_lcd_panel_t *get() { return panel_; }
[[nodiscard]] inline const esp_lcd_panel_t *
operator*() const { return get(); }
[[nodiscard]] inline esp_lcd_panel_t *operator*() { return get(); }
IPanelDevice *device_;
esp_lcd_panel_io_handle_t io_handle_;
esp_lcd_panel_handle_t panel_;
esp_lcd_panel_handle_t esp_panel_;
private:
esp_lcd_panel_dev_config_t panel_config_;
esp_lcd_panel_io_i2c_config_t io_config_;
};
class Display {
public:
explicit Display(const I2C &i2c);
explicit Display(IPanelDevice *device);
~Display() = default;
[[nodiscard]] inline const lv_display_t *get() const { return display_; }
[[nodiscard]] inline const lv_display_t *get() const { return lv_display_; }
[[nodiscard]] inline lv_display_t *get() { return display_; }
[[nodiscard]] inline lv_display_t *get() { return lv_display_; }
[[nodiscard]] inline const lv_display_t *operator*() const { return get(); }
@ -104,13 +91,10 @@ public:
private:
Panel panel_;
lv_display_t *display_;
// Size of the draw buffer associated with the lv_display_t.
size_t buf_size_;
lv_display_t *lv_display_;
// Draw buffer associated with the lv_display_t.
void *buf_;
void *lv_buf_;
// Objects stored in the screen associated with this display.
// @sa Display::set_text

View File

@ -1,11 +1,19 @@
#include "display.h"
#include "ssd1306.h"
// Pin may vary based on your schematic.
#define PIN_SDA GPIO_NUM_21
#define PIN_SCL GPIO_NUM_22
#define PIN_RST -1
// TODO: Can this be static since there can only be one initialization?
I2C i2c;
// TODO: Store RST in I2C and retrieve within SSD instead of the #define
I2C i2c(PIN_SDA, PIN_SCL);
void setup()
{
Display d(i2c);
SSD1306 ssd1306(i2c.i2c_bus_);
Display d(&ssd1306);
d.set_text("Test test 12345678910",
"test-text1",

View File

@ -4,62 +4,62 @@
#include <esp_lcd_panel_dev.h>
#include <esp_lcd_panel_ops.h>
#include <esp_lcd_panel_ssd1306.h>
#include <driver/i2c_types.h>
#include <esp_lcd_panel_io.h>
#include <esp_log.h>
#include "display/lv_display.h"
#define LVGL_PALETTE_SIZE 8
template<typename T>
class PanelDeviceInterface {
static const char *TAG = "lcd-panel";
class IPanelDevice {
public:
PanelDeviceInterface(size_t width, size_t height) : width_(width), height_(height) { }
explicit IPanelDevice(i2c_master_bus_handle_t i2c,
int reset_gpio_num,
esp_lcd_panel_io_i2c_config_t io_config) :
reset_gpio_num_(reset_gpio_num),
i2c_bus_(i2c),
io_config_(io_config) { }
~PanelDeviceInterface() = default;
[[nodiscard]] size_t get_width() const { return width_; }
[[nodiscard]] size_t get_height() const { return height_; }
[[nodiscard]] size_t get_area() const { return width_ * height_; }
[[nodiscard]] size_t get_draw_buffer_size() const
{
// LVGL reserves 2x4 bytes in the buffer to be used as a palette.
return width_ * height_ / 8 + LVGL_PALETTE_SIZE;
}
virtual ~IPanelDevice() = default;
[[nodiscard]] lv_display_t *create_display() const
{
return lv_display_create(width_, height_);
auto display = lv_display_create(width_, height_);
assert(display);
return display;
}
[[nodiscard]] T &get_vendor_config() const { return vendor_config_; }
void create_panel(esp_lcd_panel_dev_config_t &panel_config,
esp_lcd_panel_io_handle_t io_handle,
esp_lcd_panel_handle_t &panel_handle);
private:
size_t width_;
size_t height_;
protected:
T vendor_config_;
};
template<typename T>
void PanelDeviceInterface<T>::create_panel(esp_lcd_panel_dev_config_t &config,
void create_panel(esp_lcd_panel_dev_config_t &config,
esp_lcd_panel_io_handle_t io,
esp_lcd_panel_handle_t &panel)
{
{
// If the passed handle is already allocated, delete it.
if (panel != nullptr) {
ESP_LOGI(TAG, "Removing unused panel");
esp_lcd_panel_del(panel);
}
// Set the vendor config and allocate a new panel handle.
config.vendor_config = &vendor_config_;
// TODO: This needs moved to SSD1306
ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(io, &config, &panel));
}
ESP_LOGI(TAG, "Install SSD1306 panel driver");
init_panel(config, io, panel);
}
int32_t width_;
int32_t height_;
int reset_gpio_num_;
// LVGL reserves 2x4 bytes in the buffer to be used as a palette.
size_t lv_buf_size_;
// TODO: Can we use a static accessor in I2C instead?
i2c_master_bus_handle_t i2c_bus_;
esp_lcd_panel_io_i2c_config_t io_config_;
private:
virtual void init_panel(esp_lcd_panel_dev_config_t &config,
esp_lcd_panel_io_handle_t io,
esp_lcd_panel_handle_t &panel) = 0;
};
#endif // PANEL_DEVICE_H

View File

@ -4,10 +4,34 @@
// To use LV_COLOR_FORMAT_I1 we need an extra buffer to hold the converted data.
uint8_t SSD1306::oled_buffer_[LCD_H_RES * LCD_V_RES / 8];
SSD1306::SSD1306(esp_lcd_panel_ssd1306_config_t ssd_config,
SSD1306::SSD1306(i2c_master_bus_handle_t i2c,
esp_lcd_panel_ssd1306_config_t config,
int width,
int height) :
PanelDeviceInterface<esp_lcd_panel_ssd1306_config_t>(width, height)
IPanelDevice(i2c,
PIN_RST,
(esp_lcd_panel_io_i2c_config_t) {
.dev_addr = I2C_HW_ADDR,
.control_phase_bytes = 1,
.dc_bit_offset = 6,
.lcd_cmd_bits = LCD_CMD_BITS,
.lcd_param_bits = LCD_CMD_BITS,
.scl_speed_hz = LCD_PIXEL_CLOCK_HZ,
}
),
ssd1306_config_(config)
{
vendor_config_ = ssd_config;
this->width_ = width;
this->height_ = height;
this->reset_gpio_num_ = PIN_RST;
this->lv_buf_size_ = width_ * height_ / 8 + LVGL_PALETTE_SIZE;
}
void SSD1306::init_panel(esp_lcd_panel_dev_config_t &config,
esp_lcd_panel_io_handle_t io,
esp_lcd_panel_handle_t &panel)
{
// Allocate SSD1306 panel handle.
config.vendor_config = &ssd1306_config_;
ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(io, &config, &panel));
}

View File

@ -17,20 +17,32 @@
#define LCD_CMD_BITS 8
#define LCD_PARAM_BITS 8
// Pin may vary based on your schematic.
#define PIN_SDA GPIO_NUM_21
#define PIN_SCL GPIO_NUM_22
#define PIN_RST -1
class SSD1306 : public PanelDeviceInterface<esp_lcd_panel_ssd1306_config_t> {
class SSD1306 : public IPanelDevice {
public:
explicit SSD1306(esp_lcd_panel_ssd1306_config_t ssd_config,
int width = SCREEN_WIDTH, int height = SCREEN_HEIGHT);
// Constructors allow overriding ssd1306 config.
explicit SSD1306(i2c_master_bus_handle_t i2c) :
SSD1306(i2c, {.height = SCREEN_HEIGHT}) { }
~SSD1306() = default;
explicit SSD1306(i2c_master_bus_handle_t i2c,
esp_lcd_panel_ssd1306_config_t config,
int width = SCREEN_WIDTH,
int height = SCREEN_HEIGHT);
virtual ~SSD1306() = default;
// The configuration structure specific to the SSD1306.
esp_lcd_panel_ssd1306_config_t ssd1306_config_;
// For LV_COLOR_FORMAT_I1 we need an extra buffer to hold the converted data.
static uint8_t oled_buffer_[LCD_H_RES * LCD_V_RES / 8];
private:
void init_panel(esp_lcd_panel_dev_config_t &config,
esp_lcd_panel_io_handle_t io,
esp_lcd_panel_handle_t &panel) override;
};
#endif // SSD1306_H