ESPHome 2026.11.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
5
6ESPHOME_LOG_TAG(TAG, "ssd1306_i2c");
7
9 this->init_reset_();
10
11 auto err = this->write(nullptr, 0);
12 if (err != i2c::ERROR_OK) {
13 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
14 return;
15 }
16
17 SSD1306::setup();
18}
20 LOG_DISPLAY("", "I2C SSD1306", this);
21 ESP_LOGCONFIG(TAG,
22 " Model: %s\n"
23 " External VCC: %s\n"
24 " Flip X: %s\n"
25 " Flip Y: %s\n"
26 " Offset X: %d\n"
27 " Offset Y: %d\n"
28 " Inverted Color: %s",
29 LOG_STR_ARG(this->model_str_()), YESNO(this->external_vcc_), YESNO(this->flip_x_), YESNO(this->flip_y_),
30 this->offset_x_, this->offset_y_, YESNO(this->invert_));
31 LOG_I2C_DEVICE(this);
32 LOG_PIN(" Reset Pin: ", this->reset_pin_);
33 LOG_UPDATE_INTERVAL(this);
34}
35void I2CSSD1306::command(uint8_t value) { this->write_byte(0x00, value); }
37 if (this->is_sh1106_() || this->is_sh1107_()) {
38 uint32_t i = 0;
39 // Some panels wire their visible columns to a window of the controller RAM
40 // that does not start at column 0 (e.g. SH1107 M5Stack Unit OLED needs offset_x: 32).
41 // SH1106 keeps its historical 0x02 base column on top of any offset.
42 uint8_t start_column = this->offset_x_;
43 if (this->is_sh1106_()) {
44 start_column += 0x02;
45 }
46 for (uint8_t page = 0; page < (uint8_t) this->get_height_internal() / 8; page++) {
47 this->command(0xB0 + page); // row
48 this->command(start_column & 0x0F); // lower column
49 this->command(0x10 | (start_column >> 4)); // higher column
50 for (uint8_t x = 0; x < (uint8_t) this->get_width_internal() / 16; x++) {
51 uint8_t data[16];
52 for (uint8_t &j : data)
53 j = this->buffer_[i++];
54 this->write_bytes(0x40, data, sizeof(data));
55 }
56 }
57 } else {
58 size_t block_size = 16;
59 if ((this->get_buffer_length_() % 24) == 0) {
60 // use 24 byte block size for e.g. 72x40 displays where buffer size is multiple of 24, not 16
61 block_size = 24;
62 }
63
64 for (uint32_t i = 0; i < this->get_buffer_length_();) {
65 uint8_t data[block_size];
66 for (uint8_t &j : data)
67 j = this->buffer_[i++];
68 this->write_bytes(0x40, data, sizeof(data));
69 }
70 }
71}
72
73} // namespace esphome::ssd1306_i2c
void mark_failed()
Mark this component as failed.
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
void command(uint8_t value) override
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
ESPHOME_LOG_TAG(TAG, "ssd1306_i2c")
static void uint32_t
uint16_t x
Definition tt21100.cpp:5