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