Factor out I2C.
This commit is contained in:
		
							parent
							
								
									2dd099f26e
								
							
						
					
					
						commit
						ef7a027cf0
					
				@ -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_;
 | 
			
		||||
//}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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,16 +90,13 @@ 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_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user