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