Move I2C into header file.

This commit is contained in:
Shaun Reed 2025-02-15 17:44:58 -05:00
parent b3d830cdeb
commit 64d817e362
6 changed files with 45 additions and 41 deletions

View File

@ -10,7 +10,6 @@
#include <esp_timer.h> #include <esp_timer.h>
#include <lv_init.h> #include <lv_init.h>
#include <display/lv_display.h> #include <display/lv_display.h>
#include <driver/i2c_master.h>
#include <mutex> #include <mutex>
// LVGL library is not thread-safe, this example calls LVGL APIs from tasks. // LVGL library is not thread-safe, this example calls LVGL APIs from tasks.
@ -165,25 +164,6 @@ void Display::lvgl_increase_tick(void *)
} }
} }
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 = sda,
.scl_io_num = scl,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.flags {
.enable_internal_pullup = true,
},
}
)
{
ESP_LOGI(TAG, "Initialize I2C bus");
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config_, &i2c_bus_));
}
Panel::Panel(IPanelDevice *device) : Panel::Panel(IPanelDevice *device) :
device_(device), device_(device),
io_handle_(nullptr), io_handle_(nullptr),
@ -191,7 +171,7 @@ Panel::Panel(IPanelDevice *device) :
// According to SSD1306 datasheet // According to SSD1306 datasheet
panel_config_( panel_config_(
(esp_lcd_panel_dev_config_t) { (esp_lcd_panel_dev_config_t) {
.reset_gpio_num = device_->reset_gpio_num_, .reset_gpio_num = PIN_RST,
.bits_per_pixel = 1, .bits_per_pixel = 1,
// .vendor_config should be set in IPanelDevice::init_panel override // .vendor_config should be set in IPanelDevice::init_panel override
} }

View File

@ -15,22 +15,10 @@
#include "panel_device.h" #include "panel_device.h"
#define I2C_BUS_PORT 0
#define LVGL_TICK_PERIOD_MS 5 #define LVGL_TICK_PERIOD_MS 5
#define LVGL_TASK_STACK_SIZE (4 * 1024) #define LVGL_TASK_STACK_SIZE (4 * 1024)
#define LVGL_TASK_PRIORITY 2 #define LVGL_TASK_PRIORITY 2
struct I2C {
I2C(gpio_num_t sda, gpio_num_t scl);
~I2C() = default;
i2c_master_bus_handle_t i2c_bus_;
private:
i2c_master_bus_config_t bus_config_;
};
struct ScopedLock { struct ScopedLock {
explicit ScopedLock() { _lock_acquire(&lock_); } explicit ScopedLock() { _lock_acquire(&lock_); }

View File

@ -0,0 +1,41 @@
#ifndef I2C_H
#define I2C_H
#define I2C_BUS_PORT 0
#include <driver/i2c_master.h>
#include "soc/gpio_num.h"
static const char *TAG = "lcd-panel";
struct I2C {
I2C(gpio_num_t sda, gpio_num_t scl, int rst = -1) :
i2c_bus_(nullptr),
rst_num_(rst),
bus_config_(
(i2c_master_bus_config_t) {
.i2c_port = I2C_BUS_PORT,
.sda_io_num = sda,
.scl_io_num = scl,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.flags {
.enable_internal_pullup = true,
},
}
)
{
ESP_LOGI(TAG, "Initialize I2C bus");
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config_, &i2c_bus_));
}
~I2C() = default;
i2c_master_bus_handle_t i2c_bus_;
int rst_num_;
private:
i2c_master_bus_config_t bus_config_;
};
#endif //I2C_H

View File

@ -8,7 +8,7 @@
// TODO: Can this be static since there can only be one initialization? // TODO: Can this be static since there can only be one initialization?
// TODO: Store RST in I2C and retrieve within SSD instead of the #define // TODO: Store RST in I2C and retrieve within SSD instead of the #define
I2C i2c(PIN_SDA, PIN_SCL); I2C i2c(PIN_SDA, PIN_SCL, PIN_RST);
void setup() void setup()
{ {

View File

@ -9,16 +9,14 @@
#include <esp_log.h> #include <esp_log.h>
#include "display/lv_display.h" #include "display/lv_display.h"
#define LVGL_PALETTE_SIZE 8 #include "i2c.h"
static const char *TAG = "lcd-panel"; #define LVGL_PALETTE_SIZE 8
class IPanelDevice { class IPanelDevice {
public: public:
explicit IPanelDevice(i2c_master_bus_handle_t i2c, explicit IPanelDevice(i2c_master_bus_handle_t i2c,
int reset_gpio_num,
esp_lcd_panel_io_i2c_config_t io_config) : esp_lcd_panel_io_i2c_config_t io_config) :
reset_gpio_num_(reset_gpio_num),
i2c_bus_(i2c), i2c_bus_(i2c),
io_config_(io_config) { } io_config_(io_config) { }
@ -47,7 +45,6 @@ public:
int32_t width_; int32_t width_;
int32_t height_; int32_t height_;
int reset_gpio_num_;
// LVGL reserves 2x4 bytes in the buffer to be used as a palette. // LVGL reserves 2x4 bytes in the buffer to be used as a palette.
size_t lv_buf_size_; size_t lv_buf_size_;

View File

@ -9,7 +9,6 @@ SSD1306::SSD1306(i2c_master_bus_handle_t i2c,
int width, int width,
int height) : int height) :
IPanelDevice(i2c, IPanelDevice(i2c,
PIN_RST,
(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, .control_phase_bytes = 1,
@ -23,7 +22,6 @@ SSD1306::SSD1306(i2c_master_bus_handle_t i2c,
{ {
this->width_ = width; this->width_ = width;
this->height_ = height; this->height_ = height;
this->reset_gpio_num_ = PIN_RST;
this->lv_buf_size_ = width_ * height_ / 8 + LVGL_PALETTE_SIZE; this->lv_buf_size_ = width_ * height_ / 8 + LVGL_PALETTE_SIZE;
} }