| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- #pragma once
- #include "esphome/core/component.h"
- #include "esphome/core/helpers.h"
- #include "esphome/components/display/display_buffer.h"
- #include "esphome/components/spi/spi.h"
- #include "cycle_engine.h"
- #include "panel_213_z98c.h"
- #include "panel_290c_gdey029f51.h"
- #include "panel_426_gdeq0426t82.h"
- #include "panel_579c_gdey0579f51.h"
- #include "panel_579c_gdey0579f51_master_only.h"
- #include "panel_583_gdeq0583t31.h"
- namespace esphome::gxepd2 {
- static const char *const TAG = "gxepd2";
- // Native chunked-engine replacement for the old GxEPD2-template/FreeRTOS-task
- // component - see the plan this was built from (and project memory entry
- // gxepd2_task_watchdog_status) for why. Panels arrive as hand-written
- // (later, extracted) descriptors selected at runtime via set_descriptor(),
- // not by templating this class over GxEPD2's own driver classes.
- class GxEPD2Display : public display::DisplayBuffer {
- public:
- void set_cs_pin(GPIOPin *pin) { this->cs_ = pin; }
- void set_dc_pin(GPIOPin *pin) { this->dc_ = pin; }
- void set_reset_pin(GPIOPin *pin) { this->rst_ = pin; }
- void set_busy_pin(GPIOPin *pin) { this->busy_ = pin; }
- void set_spi_parent(spi::SPIComponent *parent) { this->spi_.set_spi_parent(parent); }
- void set_descriptor(const EpdPanelDescriptor *desc) { this->desc_ = desc; }
- void set_invert_mono(bool invert) { this->invert_mono_ = invert; }
- void setup() override {
- const auto *desc = this->desc_;
- // window_per_plane panels (GDEY0579F51's master+slave) size each
- // plane's own buffer from plane_width (each region's own local
- // width), not the descriptor's overall width (the full logical canvas
- // ESPHome draws into) - both regions are the same size here, so one
- // plane_bytes_ value covers both buffers.
- uint32_t w = desc->window_per_plane ? desc->plane_width : desc->width;
- // width*height*bpp/8: /8 at 1bpp, /4 at 2bpp (4-color), /2 at 4bpp (7-color).
- this->plane_bytes_ = (uint32_t) w * desc->height * desc->bits_per_pixel / 8;
- RAMAllocator<uint8_t> allocator;
- this->plane0_ = allocator.allocate(this->plane_bytes_);
- bool need_plane1 = desc->plane_count > 1;
- this->plane1_ = need_plane1 ? allocator.allocate(this->plane_bytes_) : nullptr;
- if (this->plane0_ == nullptr || (need_plane1 && this->plane1_ == nullptr)) {
- ESP_LOGE(TAG, "Failed to allocate %u-byte plane buffer(s)", (unsigned) this->plane_bytes_);
- this->mark_failed();
- return;
- }
- if (desc->bits_per_pixel > 1) {
- // Packed modes: clear every pixel to the panel's white/blank code,
- // replicated across the byte - e.g. 4-color white=0x01 -> 0x55, 7-color
- // white=0x01 -> 0x11, matching GxEPD2's own writeScreenBuffer() fill
- // (color4()*0x55 / color7()|<<4). Both regions of a window_per_plane
- // panel (master/slave) get the same blank fill.
- uint8_t fill = replicate_code_(desc->blank_code, desc->bits_per_pixel);
- memset(this->plane0_, fill, this->plane_bytes_);
- if (this->plane1_ != nullptr)
- memset(this->plane1_, fill, this->plane_bytes_);
- } else {
- // 1bpp plane model: plane0 white = all bits set (0xFF); plane1 (the
- // accent/red plane, when present) = 0 = not-red.
- memset(this->plane0_, 0xFF, this->plane_bytes_);
- if (this->plane1_ != nullptr)
- memset(this->plane1_, 0x00, this->plane_bytes_);
- }
- this->spi_.spi_setup();
- this->engine_.begin(desc, this->cs_, this->dc_, this->rst_, this->busy_, &this->spi_);
- }
- // Arms the state machine and returns immediately - the real several-
- // seconds-long write/refresh/busy-wait cycle happens across many
- // subsequent loop() ticks, not before this call returns.
- void update() override {
- if (!this->engine_.idle()) {
- ESP_LOGW(TAG, "Skipping update: previous display refresh is still running");
- return;
- }
- this->do_update_();
- this->engine_.request_full_update(this->plane0_, this->plane1_);
- }
- void loop() override { this->engine_.step(); }
- void dump_config() override {
- ESP_LOGCONFIG(TAG, "GxEPD2 Display:");
- ESP_LOGCONFIG(TAG, " Dimensions: %ux%u", (unsigned) this->desc_->width, (unsigned) this->desc_->height);
- ESP_LOGCONFIG(TAG, " Bits/pixel: %u (%s)", (unsigned) this->desc_->bits_per_pixel,
- this->desc_->bits_per_pixel == 1 ? "mono/plane" : "packed palette");
- ESP_LOGCONFIG(TAG, " Planes/regions: %u", (unsigned) this->desc_->plane_count);
- ESP_LOGCONFIG(TAG, " Busy active level: %s", this->desc_->busy_active_level ? "HIGH" : "LOW");
- if (this->desc_->bits_per_pixel == 1)
- ESP_LOGCONFIG(TAG, " invert_mono: %s", YESNO(this->invert_mono_));
- LOG_UPDATE_INTERVAL(this);
- }
- display::DisplayType get_display_type() override { return display::DisplayType::DISPLAY_TYPE_COLOR; }
- protected:
- // Replicates a packed wire code across a whole byte (one code per pixel
- // slot), e.g. 2bpp code 0x01 -> 0x55, 4bpp code 0x01 -> 0x11.
- static uint8_t replicate_code_(uint8_t code, uint8_t bpp) {
- uint8_t mask = static_cast<uint8_t>((1u << bpp) - 1);
- uint8_t out = 0;
- for (uint8_t i = 0; i < 8 / bpp; i++)
- out = static_cast<uint8_t>((out << bpp) | (code & mask));
- return out;
- }
- // Maps a drawn RGB color to its panel wire code via the descriptor's
- // palette: exact matches win (so named colors are deterministic, like
- // GxEPD2's color4()/color7() switch), otherwise nearest in RGB space.
- static uint8_t classify_(Color c, const EpdPaletteEntry *pal, uint8_t len) {
- if (pal == nullptr || len == 0)
- return 0;
- uint8_t best = 0;
- long best_d = -1;
- for (uint8_t i = 0; i < len; i++) {
- long dr = static_cast<long>(c.red) - pal[i].r;
- long dg = static_cast<long>(c.green) - pal[i].g;
- long db = static_cast<long>(c.blue) - pal[i].b;
- long d = dr * dr + dg * dg + db * db;
- if (d == 0)
- return pal[i].code;
- if (best_d < 0 || d < best_d) {
- best_d = d;
- best = i;
- }
- }
- return pal[best].code;
- }
- // Packed code-per-pixel model (bits_per_pixel > 1): N bits/pixel, MSB
- // first, matching GxEPD2's own out_data packing in writeImage (4-color
- // 4px/byte, 7-color 2px/byte). Routes to the correct region for a
- // window_per_plane (master/slave) panel the same way the mono path does.
- void draw_packed_(int x, int y, Color color) {
- const uint8_t bpp = this->desc_->bits_per_pixel;
- uint8_t code = classify_(color, this->desc_->palette, this->desc_->palette_len);
- uint16_t stride = this->desc_->width;
- uint8_t *buf = this->plane0_;
- int local_x = x;
- if (this->desc_->window_per_plane && this->desc_->plane_count > 1) {
- stride = this->desc_->plane_width;
- if (x >= stride) {
- buf = this->plane1_;
- local_x = x - stride;
- }
- }
- uint32_t px = static_cast<uint32_t>(y) * stride + local_x;
- const uint8_t ppb = 8 / bpp;
- uint32_t idx = px / ppb;
- uint8_t shift = static_cast<uint8_t>((ppb - 1 - (px % ppb)) * bpp);
- uint8_t mask = static_cast<uint8_t>((1u << bpp) - 1);
- buf[idx] = static_cast<uint8_t>((buf[idx] & ~(mask << shift)) | ((code & mask) << shift));
- }
- void draw_absolute_pixel_internal(int x, int y, Color color) override {
- if (x < 0 || y < 0 || x >= this->desc_->width || y >= this->desc_->height)
- return;
- if (this->desc_->bits_per_pixel > 1) {
- this->draw_packed_(x, y, color);
- return;
- }
- bool red = color.red > color.green && color.red > color.blue && color.red > 0x7f;
- bool bright = color.red > 0x7f || color.green > 0x7f || color.blue > 0x7f;
- uint32_t idx = (static_cast<uint32_t>(y) * this->desc_->width + x) / 8;
- uint8_t mask = 0x80 >> (x % 8);
- // Same classification the old task-based component used: red/accent
- // pixels go to the color plane (when the panel has one), everything
- // else is black/white on the black plane. get_display_type() reports
- // DISPLAY_TYPE_COLOR, so callers draw literal RGB colors, not the
- // on/off is_on() convention, even on a plane_count==1 (BW-only) panel -
- // there just is no color plane for a "red" classification to land on.
- //
- // Buffer convention here is 1=red/0=not-red (matching the reference
- // epaper_spi driver's EPaperWeAct3C internal convention), transformed
- // to the wire value by plane_invert[1] - see PANEL_213_Z98C's comment
- // for why that inversion flag and this buffer convention are coupled,
- // not independent facts.
- if (red && this->plane1_ != nullptr) {
- this->plane1_[idx] |= mask;
- } else {
- if (this->plane1_ != nullptr)
- this->plane1_[idx] &= ~mask;
- // plane0 bit set = white/blank. invert_mono_ flips the mono sense so a
- // bright color (e.g. ESPHome's default COLOR_ON) becomes ink, not blank
- // - see display.py's CONF_INVERT_MONO. Red classification is unaffected.
- if (bright ^ this->invert_mono_) {
- this->plane0_[idx] |= mask;
- } else {
- this->plane0_[idx] &= ~mask;
- }
- }
- }
- int get_width_internal() override { return this->desc_->width; }
- int get_height_internal() override { return this->desc_->height; }
- const EpdPanelDescriptor *desc_{nullptr};
- GPIOPin *cs_{nullptr};
- GPIOPin *dc_{nullptr};
- GPIOPin *rst_{nullptr};
- GPIOPin *busy_{nullptr};
- EpdSPIDevice spi_;
- CycleEngine engine_;
- uint8_t *plane0_{nullptr};
- uint8_t *plane1_{nullptr};
- uint32_t plane_bytes_{0};
- bool invert_mono_{false};
- };
- } // namespace esphome::gxepd2
|