ESPHome  2023.11.6
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_()) {
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  this->command(0x02); // lower column
46  this->command(0x10); // higher column
47 
48  for (uint8_t x = 0; x < (uint8_t) this->get_width_internal() / 16; x++) {
49  uint8_t data[16];
50  for (uint8_t &j : data)
51  j = this->buffer_[i++];
52  this->write_bytes(0x40, data, sizeof(data));
53  }
54  }
55  } else {
56  size_t block_size = 16;
57  if ((this->get_buffer_length_() & 8) == 8) {
58  // use smaller block size for e.g. 72x40 displays where buffer size is multiple of 8, not 16
59  block_size = 8;
60  }
61 
62  for (uint32_t i = 0; i < this->get_buffer_length_();) {
63  uint8_t data[block_size];
64  for (uint8_t &j : data)
65  j = this->buffer_[i++];
66  this->write_bytes(0x40, data, sizeof(data));
67  }
68  }
69 }
70 
71 } // namespace ssd1306_i2c
72 } // namespace esphome
void setup()
uint16_t x
Definition: tt21100.cpp:17
ErrorCode write(const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:72
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:127
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:112
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:113