ESPHome  2024.10.2
ssd1306_i2c.cpp
Go to the documentation of this file.
1 #include "ssd1306_i2c.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace ssd1306_i2c {
6 
7 static const char *const TAG = "ssd1306_i2c";
8 
10  ESP_LOGCONFIG(TAG, "Setting up I2C SSD1306...");
11  this->init_reset_();
12 
13  auto err = this->write(nullptr, 0);
14  if (err != i2c::ERROR_OK) {
15  this->error_code_ = COMMUNICATION_FAILED;
16  this->mark_failed();
17  return;
18  }
19 
21 }
23  LOG_DISPLAY("", "I2C SSD1306", this);
24  LOG_I2C_DEVICE(this);
25  ESP_LOGCONFIG(TAG, " Model: %s", this->model_str_());
26  LOG_PIN(" Reset Pin: ", this->reset_pin_);
27  ESP_LOGCONFIG(TAG, " External VCC: %s", YESNO(this->external_vcc_));
28  ESP_LOGCONFIG(TAG, " Flip X: %s", YESNO(this->flip_x_));
29  ESP_LOGCONFIG(TAG, " Flip Y: %s", YESNO(this->flip_y_));
30  ESP_LOGCONFIG(TAG, " Offset X: %d", this->offset_x_);
31  ESP_LOGCONFIG(TAG, " Offset Y: %d", this->offset_y_);
32  ESP_LOGCONFIG(TAG, " Inverted Color: %s", YESNO(this->invert_));
33  LOG_UPDATE_INTERVAL(this);
34 
35  if (this->error_code_ == COMMUNICATION_FAILED) {
36  ESP_LOGE(TAG, "Communication with SSD1306 failed!");
37  }
38 }
39 void I2CSSD1306::command(uint8_t value) { this->write_byte(0x00, value); }
41  if (this->is_sh1106_() || this->is_sh1107_()) {
42  uint32_t i = 0;
43  for (uint8_t page = 0; page < (uint8_t) this->get_height_internal() / 8; page++) {
44  this->command(0xB0 + page); // row
45  if (this->is_sh1106_()) {
46  this->command(0x02); // lower column - 0x02 is historical SH1106 value
47  } else {
48  // Other SH1107 drivers use 0x00
49  // Column values dont change and it seems they can be set only once,
50  // but we follow SH1106 implementation and resend them
51  this->command(0x00);
52  }
53  this->command(0x10); // higher column
54  for (uint8_t x = 0; x < (uint8_t) this->get_width_internal() / 16; x++) {
55  uint8_t data[16];
56  for (uint8_t &j : data)
57  j = this->buffer_[i++];
58  this->write_bytes(0x40, data, sizeof(data));
59  }
60  }
61  } else {
62  size_t block_size = 16;
63  if ((this->get_buffer_length_() & 8) == 8) {
64  // use smaller block size for e.g. 72x40 displays where buffer size is multiple of 8, not 16
65  block_size = 8;
66  }
67 
68  for (uint32_t i = 0; i < this->get_buffer_length_();) {
69  uint8_t data[block_size];
70  for (uint8_t &j : data)
71  j = this->buffer_[i++];
72  this->write_bytes(0x40, data, sizeof(data));
73  }
74  }
75 }
76 
77 } // namespace ssd1306_i2c
78 } // namespace esphome
void setup()
uint16_t x
Definition: tt21100.cpp:17
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition: i2c.h:186
No error found during execution of method.
Definition: i2c_bus.h:13
void command(uint8_t value) override
Definition: ssd1306_i2c.cpp:39
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:248