Factor out I2C.

This commit is contained in:
Shaun Reed 2025-02-14 16:48:04 -05:00
parent 2dd099f26e
commit ef7a027cf0
3 changed files with 38 additions and 35 deletions

View File

@ -17,13 +17,22 @@ uint8_t Display::oled_buffer_[LCD_H_RES * LCD_V_RES / 8];
// We must use a mutex to protect it. // We must use a mutex to protect it.
_lock_t ScopedLock::lock_; _lock_t ScopedLock::lock_;
Display::Display() : Display::Display(const I2C &i2c) :
io_handle_(nullptr), io_handle_(nullptr),
panel_handle_(nullptr), panel_handle_(nullptr),
i2c_bus_(nullptr),
buf_(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"); ESP_LOGI(TAG, "Install SSD1306 panel driver");
ssd1306_config_ = { 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"); ESP_LOGI(TAG, "Initialize I2C bus");
i2c_master_bus_config_t bus_config = { 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_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_;
//}

View File

@ -34,6 +34,14 @@ static const char *TAG = "lcd-panel";
#define PIN_SCL GPIO_NUM_22 #define PIN_SCL GPIO_NUM_22
#define PIN_RST -1 #define PIN_RST -1
struct I2C {
I2C();
~I2C() = default;
i2c_master_bus_handle_t i2c_bus_;
};
struct ScopedLock { struct ScopedLock {
explicit ScopedLock() { _lock_acquire(&lock_); } explicit ScopedLock() { _lock_acquire(&lock_); }
@ -46,11 +54,10 @@ struct ScopedLock {
class Display { class Display {
public: public:
Display(); explicit Display(const I2C &i2c);
~Display() = default; ~Display() = default;
[[nodiscard]] inline const lv_display_t *get() const { return display_; } [[nodiscard]] inline const lv_display_t *get() const { return display_; }
[[nodiscard]] inline lv_display_t *get() { 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. // 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]; static uint8_t oled_buffer_[LCD_H_RES * LCD_V_RES / 8];
protected: protected:
esp_lcd_panel_io_handle_t io_handle_; esp_lcd_panel_io_handle_t io_handle_;
@ -82,17 +90,14 @@ protected:
esp_lcd_panel_ssd1306_config_t ssd1306_config_; esp_lcd_panel_ssd1306_config_t ssd1306_config_;
i2c_master_bus_handle_t i2c_bus_;
private: private:
void init_i2c();
// virtual void *vendor_config() = 0; // virtual void *vendor_config() = 0;
lv_display_t *display_; lv_display_t *display_;
void * vendor_config_; void *vendor_config_;
void * buf_;
void *buf_;
}; };
//class SSD1306 : public Display { //class SSD1306 : public Display {

View File

@ -3,11 +3,13 @@
#include "esp_log.h" #include "esp_log.h"
#include "lvgl.h" #include "lvgl.h"
I2C i2c;
void setup() void setup()
{ {
Display d; Display d(i2c);
// UI function // UI function scope.
{ {
// Lock the mutex due to the LVGL APIs are not thread-safe // Lock the mutex due to the LVGL APIs are not thread-safe
ScopedLock lock; ScopedLock lock;