ESPHome 2025.6.0
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 ESP_LOGCONFIG(TAG, "Running setup");
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
20 SSD1306::setup();
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,
28 " External VCC: %s\n"
29 " Flip X: %s\n"
30 " Flip Y: %s\n"
31 " Offset X: %d\n"
32 " Offset Y: %d\n"
33 " Inverted Color: %s",
34 YESNO(this->external_vcc_), YESNO(this->flip_x_), YESNO(this->flip_y_), this->offset_x_,
35 this->offset_y_, YESNO(this->invert_));
36 LOG_UPDATE_INTERVAL(this);
37
38 if (this->error_code_ == COMMUNICATION_FAILED) {
39 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
40 }
41}
42void I2CSSD1306::command(uint8_t value) { this->write_byte(0x00, value); }
44 if (this->is_sh1106_() || this->is_sh1107_()) {
45 uint32_t i = 0;
46 for (uint8_t page = 0; page < (uint8_t) this->get_height_internal() / 8; page++) {
47 this->command(0xB0 + page); // row
48 if (this->is_sh1106_()) {
49 this->command(0x02); // lower column - 0x02 is historical SH1106 value
50 } else {
51 // Other SH1107 drivers use 0x00
52 // Column values dont change and it seems they can be set only once,
53 // but we follow SH1106 implementation and resend them
54 this->command(0x00);
55 }
56 this->command(0x10); // higher column
57 for (uint8_t x = 0; x < (uint8_t) this->get_width_internal() / 16; x++) {
58 uint8_t data[16];
59 for (uint8_t &j : data)
60 j = this->buffer_[i++];
61 this->write_bytes(0x40, data, sizeof(data));
62 }
63 }
64 } else {
65 size_t block_size = 16;
66 if ((this->get_buffer_length_() & 8) == 8) {
67 // use smaller block size for e.g. 72x40 displays where buffer size is multiple of 8, not 16
68 block_size = 8;
69 }
70
71 for (uint32_t i = 0; i < this->get_buffer_length_();) {
72 uint8_t data[block_size];
73 for (uint8_t &j : data)
74 j = this->buffer_[i++];
75 this->write_bytes(0x40, data, sizeof(data));
76 }
77 }
78}
79
80} // namespace ssd1306_i2c
81} // 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