From b5fbe0b1451ac6f709c2aced14918977d284f584 Mon Sep 17 00:00:00 2001 From: unhold Date: Wed, 12 Apr 2023 23:19:52 +0200 Subject: [PATCH] Fix cut-off on 2.13" waveshare/ttgo epaper displays (#4255) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The controller of the 2.13" waveshare/ttgo epaper displays operates with a 128x250 buffer, while only 122x250 pixels are shown. While this can be ignored for rotations 0/270°, with roations 90/180°, 6 pixels are cut-off from the top or left edge. This change introduces a distinction between object width and controller width, resulting in pixel-perfect rotations. --- .../waveshare_epaper/waveshare_epaper.cpp | 22 ++++++++++++++++--- .../waveshare_epaper/waveshare_epaper.h | 4 ++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/esphome/components/waveshare_epaper/waveshare_epaper.cpp b/esphome/components/waveshare_epaper/waveshare_epaper.cpp index 8fd5c2e1f3..8c4b137514 100644 --- a/esphome/components/waveshare_epaper/waveshare_epaper.cpp +++ b/esphome/components/waveshare_epaper/waveshare_epaper.cpp @@ -137,7 +137,7 @@ void HOT WaveshareEPaper::draw_absolute_pixel_internal(int x, int y, Color color if (x >= this->get_width_internal() || y >= this->get_height_internal() || x < 0 || y < 0) return; - const uint32_t pos = (x + y * this->get_width_internal()) / 8u; + const uint32_t pos = (x + y * this->get_width_controller()) / 8u; const uint8_t subpos = x & 0x07; // flip logic if (!color.is_on()) { @@ -146,7 +146,9 @@ void HOT WaveshareEPaper::draw_absolute_pixel_internal(int x, int y, Color color this->buffer_[pos] &= ~(0x80 >> subpos); } } -uint32_t WaveshareEPaper::get_buffer_length_() { return this->get_width_internal() * this->get_height_internal() / 8u; } +uint32_t WaveshareEPaper::get_buffer_length_() { + return this->get_width_controller() * this->get_height_internal() / 8u; +} void WaveshareEPaper::start_command_() { this->dc_pin_->digital_write(false); this->enable(); @@ -291,7 +293,7 @@ void HOT WaveshareEPaperTypeA::display() { // COMMAND SET RAM X ADDRESS START END POSITION this->command(0x44); this->data(0x00); - this->data((this->get_width_internal() - 1) >> 3); + this->data((this->get_width_controller() - 1) >> 3); // COMMAND SET RAM Y ADDRESS START END POSITION this->command(0x45); this->data(this->get_height_internal() - 1); @@ -392,12 +394,26 @@ int WaveshareEPaperTypeA::get_width_internal() { case TTGO_EPAPER_2_13_IN_B73: case TTGO_EPAPER_2_13_IN_B74: case TTGO_EPAPER_2_13_IN_B1: + return 122; case WAVESHARE_EPAPER_2_9_IN: case WAVESHARE_EPAPER_2_9_IN_V2: return 128; } return 0; } +// The controller of the 2.13" displays has a buffer larger than screen size +int WaveshareEPaperTypeA::get_width_controller() { + switch (this->model_) { + case WAVESHARE_EPAPER_2_13_IN: + case TTGO_EPAPER_2_13_IN: + case TTGO_EPAPER_2_13_IN_B73: + case TTGO_EPAPER_2_13_IN_B74: + case TTGO_EPAPER_2_13_IN_B1: + return 128; + default: + return this->get_width_internal(); + } +} int WaveshareEPaperTypeA::get_height_internal() { switch (this->model_) { case WAVESHARE_EPAPER_1_54_IN: diff --git a/esphome/components/waveshare_epaper/waveshare_epaper.h b/esphome/components/waveshare_epaper/waveshare_epaper.h index 848b293c45..a674d3af0c 100644 --- a/esphome/components/waveshare_epaper/waveshare_epaper.h +++ b/esphome/components/waveshare_epaper/waveshare_epaper.h @@ -54,6 +54,8 @@ class WaveshareEPaper : public PollingComponent, } } + virtual int get_width_controller() { return this->get_width_internal(); }; + uint32_t get_buffer_length_(); uint32_t reset_duration_{200}; @@ -111,6 +113,8 @@ class WaveshareEPaperTypeA : public WaveshareEPaper { int get_height_internal() override; + int get_width_controller() override; + uint32_t full_update_every_{30}; uint32_t at_update_{0}; WaveshareEPaperTypeAModel model_;