From 99983b84a045e48c5d7396d036dbb8b1d8659307 Mon Sep 17 00:00:00 2001 From: DJGummikuh Date: Mon, 12 Aug 2024 21:46:02 +0000 Subject: [PATCH] Code beautification, fixing bugs with setting different segmentation values. --- .../components/waveshare_epaper/display.py | 8 ++-- .../waveshare_epaper/waveshare_epaper.cpp | 41 ++++++++++--------- .../waveshare_epaper/waveshare_epaper.h | 26 +++++++++--- 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/esphome/components/waveshare_epaper/display.py b/esphome/components/waveshare_epaper/display.py index af2016c94a..db138c6ff5 100644 --- a/esphome/components/waveshare_epaper/display.py +++ b/esphome/components/waveshare_epaper/display.py @@ -182,8 +182,8 @@ CONFIG_SCHEMA = cv.All( cv.positive_time_period_milliseconds, cv.Range(max=core.TimePeriod(milliseconds=500)), ), - cv.Optional(CONF_NUM_SEGMENTS_X): cv.int_range(min=1, max=40), - cv.Optional(CONF_NUM_SEGMENTS_Y): cv.int_range(min=1, max=40), + cv.Optional(CONF_NUM_SEGMENTS_X): cv.int_range(min=1, max=100), + cv.Optional(CONF_NUM_SEGMENTS_Y): cv.int_range(min=1, max=100), } ) .extend(cv.polling_component_schema("1s")) @@ -213,9 +213,7 @@ async def to_code(config): if CONF_NUM_SEGMENTS_X in config: cg.add(var.set_num_segments_x(config["num_segments_x"])) if CONF_NUM_SEGMENTS_Y in config: - cg.add(var.set_num_segments_x(config["num_segments_y"])) - if "abcdefghijklmnop1" in config: - cg.add(var.blablablubb()) + cg.add(var.set_num_segments_y(config["num_segments_y"])) if CONF_LAMBDA in config: lambda_ = await cg.process_lambda( config[CONF_LAMBDA], [(display.DisplayRef, "it")], return_type=cg.void diff --git a/esphome/components/waveshare_epaper/waveshare_epaper.cpp b/esphome/components/waveshare_epaper/waveshare_epaper.cpp index 6402bad9a1..03013955a8 100644 --- a/esphome/components/waveshare_epaper/waveshare_epaper.cpp +++ b/esphome/components/waveshare_epaper/waveshare_epaper.cpp @@ -1520,10 +1520,10 @@ void GDEY075Z08::calculate_CRCs_(bool fullSync) { uint16_t seg_x, seg_y, x, y; // loop count variables bool found_change = false; // reset first and last X segment so we can recalculate it here. - first_segment_x_ = this->seg_x_ + 1; - last_segment_x_ = 0; + this->first_segment_x_ = this->seg_x_ + 1; + this->last_segment_x_ = 0; ESP_LOGD(TAG, "width_b: %d, height_px: %d, segment_size: %d, buffer_half_size: %d, seg_x_: %d, seg_y_: %d", width_b, - height_px, segment_size, buffer_half_size, seg_x_, seg_y_); + height_px, segment_size, buffer_half_size, this->seg_x_, this->seg_y_); ESP_LOGD(TAG, "Entering CRC calculation Loop"); for (seg_y = 0; seg_y < this->seg_y_; seg_y++) { // vertically iterate through the number of lines (px) for (seg_x = 0; seg_x < this->seg_x_; seg_x++) { // horizontally iterate through number of columns (px) @@ -1543,32 +1543,31 @@ void GDEY075Z08::calculate_CRCs_(bool fullSync) { uint16_t segment_crc = crc16(segment, segment_size * 2, 65535U, 40961U, false, false); if (fullSync) { // no need to compare, we're in the first run, just place it. This is called by full refresh only - checksums_[seg_x + seg_y * seg_x_] = segment_crc; + this->checksums_[seg_x + seg_y * seg_x_] = segment_crc; } else { // Partial Update, compare checksums while replacing and record the X and Y block position of the top left and // bottom right corner of the changed elements. Afterwards, we can partially update only the segment that has // been altered. - bool changed = checksums_[seg_x + seg_y * seg_x_] != segment_crc; - checksums_[seg_x + seg_y * seg_x_] = segment_crc; + bool changed = this->checksums_[seg_x + seg_y * seg_x_] != segment_crc; + this->checksums_[seg_x + seg_y * seg_x_] = segment_crc; if (changed && !found_change) { found_change = true; // We need to span the x segment, with the lowest segment found making the first segment and the highest // segment found the last segment. - if (seg_x < first_segment_x_) - first_segment_x_ = seg_x; - if (seg_x > last_segment_x_) - last_segment_x_ = seg_x; - first_segment_y_ = seg_y; - + if (seg_x < this->first_segment_x_) + this->first_segment_x_ = seg_x; + if (seg_x > this->last_segment_x_) + this->last_segment_x_ = seg_x; + this->first_segment_y_ = seg_y; } else if (changed) { // We need to span the x segment, with the lowest segment found making the first segment and the highest // segment found the last segment. - if (seg_x < first_segment_x_) - first_segment_x_ = seg_x; - if (seg_x > last_segment_x_) - last_segment_x_ = seg_x; + if (seg_x < this->first_segment_x_) + this->first_segment_x_ = seg_x; + if (seg_x > this->last_segment_x_) + this->last_segment_x_ = seg_x; // Segment changed but we already found a change before, so set it as last segment - last_segment_y_ = seg_y; + this->last_segment_y_ = seg_y; } else { // do nothing, segment didn't change. } @@ -1576,8 +1575,12 @@ void GDEY075Z08::calculate_CRCs_(bool fullSync) { delete segment; // Delete the heap element again, this is not java... >.< } } - ESP_LOGD(TAG, "CRC Calculation finished. Found changes: %02d:%02d to %02d:%02d", first_segment_x_, first_segment_y_, - last_segment_x_, last_segment_y_); + if (found_change) { + ESP_LOGD(TAG, "CRC Calculation finished. Found changes: %02d:%02d to %02d:%02d", first_segment_x_, first_segment_y_, + last_segment_x_, last_segment_y_); + } else { + ESP_LOGD(TAG, "CRC Calculation finished. No changes found."); + } } void GDEY075Z08::set_full_update_every(uint32_t full_update_every) { this->full_update_every_ = full_update_every; } diff --git a/esphome/components/waveshare_epaper/waveshare_epaper.h b/esphome/components/waveshare_epaper/waveshare_epaper.h index 60359f337f..90d29846da 100644 --- a/esphome/components/waveshare_epaper/waveshare_epaper.h +++ b/esphome/components/waveshare_epaper/waveshare_epaper.h @@ -238,18 +238,32 @@ class GDEY075Z08 : public WaveshareEPaperBWR { void deep_sleep() override; void set_full_update_every(uint32_t full_update_every); void set_num_segments_x(uint8_t value) { - ESP_LOGD("TAG", "Setting num segments X to %d", value); - this->seg_x_ = value; + if (this->get_width_internal() % (value * 8) != 0) { + ESP_LOGD(TAG, + "Invalid number of X Segments (%d) The display width divided by number of segments must be divisible by " + "8 for " + "proper byte boundaries. Setting num_segments_x to 20.", + value); + } else { + this->seg_x_ = value; + } } void set_num_segments_y(uint8_t value) { - ESP_LOGD("TAG", "Setting num segments Y to %d", value); - this->seg_y_ = value; + if (this->get_height_internal() % value != 0) { + ESP_LOGD(TAG, + "Invalid number of Y Segments (%d). The display height (480px) must be divisible by the number of y " + "segments for equal segment height. Setting num_segments_y to 10.", + value); + uint8_t replacementvalue = this->get_height_internal() / + } else { + this->seg_y_ = value; + } } protected: bool wait_until_idle_(); - uint16_t get_width_internal() override { return 800; } - uint16_t get_height_internal() override { return 480; } + virtual int get_width_internal() override { return 800; } + virtual int get_height_internal() override { return 480; } private: uint32_t full_update_every_{30};