ESPHome  2024.11.0
st7567_i2c.cpp
Go to the documentation of this file.
1 #include "st7567_i2c.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace st7567_i2c {
6 
7 static const char *const TAG = "st7567_i2c";
8 
10  ESP_LOGCONFIG(TAG, "Setting up I2C ST7567 display...");
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  ST7567::setup();
20 }
21 
23  LOG_DISPLAY("", "I2CST7567", 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, " Mirror X: %s", YESNO(this->mirror_x_));
28  ESP_LOGCONFIG(TAG, " Mirror Y: %s", YESNO(this->mirror_y_));
29  ESP_LOGCONFIG(TAG, " Invert Colors: %s", YESNO(this->invert_colors_));
30  LOG_UPDATE_INTERVAL(this);
31 
32  if (this->error_code_ == COMMUNICATION_FAILED) {
33  ESP_LOGE(TAG, "Communication with I2C ST7567 failed!");
34  }
35 }
36 
37 void I2CST7567::command(uint8_t value) { this->write_byte(0x00, value); }
38 
40  // ST7567A has built-in RAM with 132x65 bit capacity which stores the display data.
41  // but only first 128 pixels from each line are shown on screen
42  // if screen got flipped horizontally then it shows last 128 pixels,
43  // so we need to write x coordinate starting from column 4, not column 0
44  this->command(esphome::st7567_base::ST7567_SET_START_LINE + this->start_line_);
45  for (uint8_t y = 0; y < (uint8_t) this->get_height_internal() / 8; y++) {
46  this->command(esphome::st7567_base::ST7567_PAGE_ADDR + y); // Set Page
47  this->command(esphome::st7567_base::ST7567_COL_ADDR_H); // Set MSB Column address
48  this->command(esphome::st7567_base::ST7567_COL_ADDR_L + this->get_offset_x_()); // Set LSB Column address
49 
50  static const size_t BLOCK_SIZE = 64;
51  for (uint8_t x = 0; x < (uint8_t) this->get_width_internal(); x += BLOCK_SIZE) {
52  this->write_register(esphome::st7567_base::ST7567_SET_START_LINE, &buffer_[y * this->get_width_internal() + x],
53  this->get_width_internal() - x > BLOCK_SIZE ? BLOCK_SIZE : this->get_width_internal() - x,
54  true);
55  }
56  }
57 }
58 
59 } // namespace st7567_i2c
60 } // namespace esphome
void setup()
void command(uint8_t value) override
Definition: st7567_i2c.cpp:37
uint16_t x
Definition: tt21100.cpp:17
void write_display_data() override
Definition: st7567_i2c.cpp:39
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:186
uint16_t y
Definition: tt21100.cpp:18
No error found during execution of method.
Definition: i2c_bus.h:13
int get_height_internal() override
int get_width_internal() override
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a specific register in the I²C device
Definition: i2c.cpp:25