From ef7a027cf0d2363a965a1f714341e49d83b21132 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Fri, 14 Feb 2025 16:48:04 -0500 Subject: [PATCH] Factor out I2C. --- esp/cpp/07_lcd-panel/main/display.cpp | 46 ++++++++++++--------------- esp/cpp/07_lcd-panel/main/display.h | 21 +++++++----- esp/cpp/07_lcd-panel/main/main.cpp | 6 ++-- 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/esp/cpp/07_lcd-panel/main/display.cpp b/esp/cpp/07_lcd-panel/main/display.cpp index 383e463..ab50384 100644 --- a/esp/cpp/07_lcd-panel/main/display.cpp +++ b/esp/cpp/07_lcd-panel/main/display.cpp @@ -17,13 +17,22 @@ uint8_t Display::oled_buffer_[LCD_H_RES * LCD_V_RES / 8]; // We must use a mutex to protect it. _lock_t ScopedLock::lock_; -Display::Display() : +Display::Display(const I2C &i2c) : io_handle_(nullptr), panel_handle_(nullptr), - i2c_bus_(nullptr), buf_(nullptr) { - init_i2c(); + ESP_LOGI(TAG, "Install panel IO"); + esp_lcd_panel_io_i2c_config_t io_config = { + .dev_addr = I2C_HW_ADDR, + .control_phase_bytes = 1, // According to SSD1306 datasheet + .dc_bit_offset = 6, // According to SSD1306 datasheet + .lcd_cmd_bits = LCD_CMD_BITS, // According to SSD1306 datasheet + .lcd_param_bits = LCD_CMD_BITS, // According to SSD1306 datasheet + .scl_speed_hz = LCD_PIXEL_CLOCK_HZ, + }; + ESP_ERROR_CHECK( + esp_lcd_new_panel_io_i2c(i2c.i2c_bus_, &io_config, &io_handle_)); ESP_LOGI(TAG, "Install SSD1306 panel driver"); ssd1306_config_ = { @@ -158,7 +167,15 @@ void Display::lvgl_port_task(void *arg) } } -void Display::init_i2c() +//SSD1306::SSD1306() { +// +//} +//void *SSD1306::vendor_config() +//{ +// return &ssd1306_config_; +//} + +I2C::I2C() : i2c_bus_(nullptr) { ESP_LOGI(TAG, "Initialize I2C bus"); i2c_master_bus_config_t bus_config = { @@ -172,25 +189,4 @@ void Display::init_i2c() }, }; ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config, &i2c_bus_)); - - ESP_LOGI(TAG, "Install panel IO"); - esp_lcd_panel_io_i2c_config_t io_config = { - .dev_addr = I2C_HW_ADDR, - .control_phase_bytes = 1, // According to SSD1306 datasheet - .dc_bit_offset = 6, // According to SSD1306 datasheet - .lcd_cmd_bits = LCD_CMD_BITS, // According to SSD1306 datasheet - .lcd_param_bits = LCD_CMD_BITS, // According to SSD1306 datasheet - .scl_speed_hz = LCD_PIXEL_CLOCK_HZ, - }; - ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(i2c_bus_, &io_config, &io_handle_)); } - -//SSD1306::SSD1306() { -// -//} -//void *SSD1306::vendor_config() -//{ -// return &ssd1306_config_; -//} - - diff --git a/esp/cpp/07_lcd-panel/main/display.h b/esp/cpp/07_lcd-panel/main/display.h index e16e50b..983708f 100644 --- a/esp/cpp/07_lcd-panel/main/display.h +++ b/esp/cpp/07_lcd-panel/main/display.h @@ -34,6 +34,14 @@ static const char *TAG = "lcd-panel"; #define PIN_SCL GPIO_NUM_22 #define PIN_RST -1 +struct I2C { + I2C(); + + ~I2C() = default; + + i2c_master_bus_handle_t i2c_bus_; +}; + struct ScopedLock { explicit ScopedLock() { _lock_acquire(&lock_); } @@ -46,11 +54,10 @@ struct ScopedLock { class Display { public: - Display(); + explicit Display(const I2C &i2c); ~Display() = default; - [[nodiscard]] inline const lv_display_t *get() const { return display_; } [[nodiscard]] inline lv_display_t *get() { return display_; } @@ -73,6 +80,7 @@ public: // 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]; + protected: esp_lcd_panel_io_handle_t io_handle_; @@ -82,17 +90,14 @@ protected: esp_lcd_panel_ssd1306_config_t ssd1306_config_; - i2c_master_bus_handle_t i2c_bus_; - private: - void init_i2c(); - // virtual void *vendor_config() = 0; lv_display_t *display_; - void * vendor_config_; - void * buf_; + void *vendor_config_; + + void *buf_; }; //class SSD1306 : public Display { diff --git a/esp/cpp/07_lcd-panel/main/main.cpp b/esp/cpp/07_lcd-panel/main/main.cpp index cb81458..1df6c26 100644 --- a/esp/cpp/07_lcd-panel/main/main.cpp +++ b/esp/cpp/07_lcd-panel/main/main.cpp @@ -3,11 +3,13 @@ #include "esp_log.h" #include "lvgl.h" +I2C i2c; + void setup() { - Display d; + Display d(i2c); - // UI function + // UI function scope. { // Lock the mutex due to the LVGL APIs are not thread-safe ScopedLock lock;