Clean code.

This commit is contained in:
Shaun Reed 2025-02-15 14:04:08 -05:00
parent cc5bffd1e7
commit 670a523a16
5 changed files with 28 additions and 20 deletions

View File

@ -26,29 +26,28 @@ Display::Display(const I2C &i2c) :
} }
// Create a lvgl display. // Create a lvgl display.
// TODO: Use PanelDevice functions // TODO: Use PanelDevice functions to get SSD1306 specs
display_ = lv_display_create(LCD_H_RES, LCD_V_RES); display_ = lv_display_create(LCD_H_RES, LCD_V_RES);
// associate the i2c panel handle to the display // associate the i2c panel handle to the display
lv_display_set_user_data(display_, panel_.get()); lv_display_set_user_data(display_, panel_.get());
// Create draw buffer. // Create draw buffer.
ESP_LOGI(TAG, "Allocate separate LVGL draw buffers"); ESP_LOGI(TAG, "Allocate separate LVGL draw buffers");
// TODO: Use PanelDevice functions // TODO: Use PanelDevice functions to get SSD1306 specs
size_t draw_buffer_sz = LCD_H_RES * LCD_V_RES / 8 + LVGL_PALETTE_SIZE; buf_size_ = LCD_H_RES * LCD_V_RES / 8 + LVGL_PALETTE_SIZE;
buf_ = heap_caps_calloc(1, draw_buffer_sz, buf_ = heap_caps_calloc(1, buf_size_, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
assert(buf_); assert(buf_);
// LVGL9 suooprt new monochromatic format. // LVGL9 suooprt new monochromatic format.
lv_display_set_color_format(display_, LV_COLOR_FORMAT_I1); lv_display_set_color_format(display_, LV_COLOR_FORMAT_I1);
// Initialize LVGL draw buffers. // Initialize LVGL draw buffers.
lv_display_set_buffers(display_, buf_, nullptr, draw_buffer_sz, lv_display_set_buffers(display_, buf_, nullptr, buf_size_,
LV_DISPLAY_RENDER_MODE_FULL); LV_DISPLAY_RENDER_MODE_FULL);
lv_display_set_rotation(display_, LV_DISPLAY_ROTATION_0); lv_display_set_rotation(display_, LV_DISPLAY_ROTATION_0);
// Set callback which can copy the rendered image to an area of the display. // 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(display_, Display::lvgl_flush_cb);
ESP_LOGI(TAG, ESP_LOGI(TAG, "Register io panel callback for LVGL flush ready notification");
"Register io panel event callback for LVGL flush ready notification");
const esp_lcd_panel_io_callbacks_t cbs = { const esp_lcd_panel_io_callbacks_t cbs = {
.on_color_trans_done = Display::lvgl_flush_ready, .on_color_trans_done = Display::lvgl_flush_ready,
}; };
@ -189,19 +188,21 @@ I2C::I2C() :
Panel::Panel(i2c_master_bus_handle_t i2c) : Panel::Panel(i2c_master_bus_handle_t i2c) :
io_handle_(nullptr), io_handle_(nullptr),
panel_(nullptr), panel_(nullptr),
// According to SSD1306 datasheet
panel_config_( panel_config_(
(esp_lcd_panel_dev_config_t) { (esp_lcd_panel_dev_config_t) {
.reset_gpio_num = PIN_RST, .reset_gpio_num = PIN_RST,
.bits_per_pixel = 1, .bits_per_pixel = 1,
} }
), ),
// According to SSD1306 datasheet
io_config_( io_config_(
(esp_lcd_panel_io_i2c_config_t) { (esp_lcd_panel_io_i2c_config_t) {
.dev_addr = I2C_HW_ADDR, .dev_addr = I2C_HW_ADDR,
.control_phase_bytes = 1, // According to SSD1306 datasheet .control_phase_bytes = 1,
.dc_bit_offset = 6, // According to SSD1306 datasheet .dc_bit_offset = 6,
.lcd_cmd_bits = LCD_CMD_BITS, // According to SSD1306 datasheet .lcd_cmd_bits = LCD_CMD_BITS,
.lcd_param_bits = LCD_CMD_BITS, // According to SSD1306 datasheet .lcd_param_bits = LCD_CMD_BITS,
.scl_speed_hz = LCD_PIXEL_CLOCK_HZ, .scl_speed_hz = LCD_PIXEL_CLOCK_HZ,
} }
) )

View File

@ -106,8 +106,15 @@ private:
lv_display_t *display_; lv_display_t *display_;
// Size of the draw buffer associated with the lv_display_t.
size_t buf_size_;
// Draw buffer associated with the lv_display_t.
void *buf_; void *buf_;
// Objects stored in the screen associated with this display.
// @sa Display::set_text
// @sa lv_display_get_screen_active
std::unordered_map<const char *, lv_obj_t *> objects_; std::unordered_map<const char *, lv_obj_t *> objects_;
}; };

View File

@ -9,11 +9,11 @@
#define LVGL_PALETTE_SIZE 8 #define LVGL_PALETTE_SIZE 8
template<typename T> template<typename T>
class PanelDevice { class PanelDeviceInterface {
public: public:
PanelDevice(size_t width, size_t height) : width_(width), height_(height) { } PanelDeviceInterface(size_t width, size_t height) : width_(width), height_(height) { }
~PanelDevice() = default; ~PanelDeviceInterface() = default;
[[nodiscard]] size_t get_width() const { return width_; } [[nodiscard]] size_t get_width() const { return width_; }
@ -47,9 +47,9 @@ protected:
}; };
template<typename T> template<typename T>
void PanelDevice<T>::create_panel(esp_lcd_panel_dev_config_t &config, void PanelDeviceInterface<T>::create_panel(esp_lcd_panel_dev_config_t &config,
esp_lcd_panel_io_handle_t io, esp_lcd_panel_io_handle_t io,
esp_lcd_panel_handle_t &panel) esp_lcd_panel_handle_t &panel)
{ {
// If the passed handle is already allocated, delete it. // If the passed handle is already allocated, delete it.
if (panel != nullptr) { if (panel != nullptr) {

View File

@ -7,7 +7,7 @@ uint8_t SSD1306::oled_buffer_[LCD_H_RES * LCD_V_RES / 8];
SSD1306::SSD1306(esp_lcd_panel_ssd1306_config_t ssd_config, SSD1306::SSD1306(esp_lcd_panel_ssd1306_config_t ssd_config,
int width, int width,
int height) : int height) :
PanelDevice<esp_lcd_panel_ssd1306_config_t>(width, height) PanelDeviceInterface<esp_lcd_panel_ssd1306_config_t>(width, height)
{ {
vendor_config_ = ssd_config; vendor_config_ = ssd_config;
} }

View File

@ -22,7 +22,7 @@
#define PIN_SCL GPIO_NUM_22 #define PIN_SCL GPIO_NUM_22
#define PIN_RST -1 #define PIN_RST -1
class SSD1306 : public PanelDevice<esp_lcd_panel_ssd1306_config_t> { class SSD1306 : public PanelDeviceInterface<esp_lcd_panel_ssd1306_config_t> {
public: public:
explicit SSD1306(esp_lcd_panel_ssd1306_config_t ssd_config, explicit SSD1306(esp_lcd_panel_ssd1306_config_t ssd_config,
int width = SCREEN_WIDTH, int height = SCREEN_HEIGHT); int width = SCREEN_WIDTH, int height = SCREEN_HEIGHT);