#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 "pin_registry.h" // Only the wrapper the configured model needs gets included - see // display.py's USE_GXEPD2_* defines. Including all four unconditionally // fails to parse under ESP-IDF's toolchain: the un-instantiated template // bodies of the unused ones hit the same bare WIDTH/HEIGHT names, and // GCC's two-phase lookup only resolves those once instantiated. #ifdef USE_GXEPD2_BW #include #endif #ifdef USE_GXEPD2_3C #include #endif #ifdef USE_GXEPD2_4C #include #endif #ifdef USE_GXEPD2_7C #include #endif namespace esphome::gxepd2 { static const char *const TAG = "gxepd2"; // Selected by codegen from catalog.json's "wrapper" field. enum class GxEPD2ColorMode { BW, THREE_COLOR, // black/white plus one accent ink (red or yellow panels alike) FOUR_COLOR, // black/white/red/yellow(-ish) 4-ink panels SEVEN_COLOR, // full ACeP-style palette }; // DriverT: a GxEPD2 panel driver class (e.g. GxEPD2_290_T94). // DisplayT: that driver wrapped in its GxEPD2 color-depth template (e.g. // GxEPD2_BW). // DualCS: whether DriverT takes 5 constructor args (cs, dc, rst, busy, cs2) // instead of 4. All four are chosen by codegen from catalog.json. // // cs_pin/dc_pin/reset_pin/busy_pin(/cs2_pin) are synthetic PinRegistry ids, // not real esphome::GPIOPin; digitalWrite()/digitalRead()/pinMode() in // arduino_compat/Arduino.h resolve them back to the real pin. // // display::DisplayBuffer already derives PollingComponent via Display - do // not add PollingComponent again here, it creates an ambiguous base. template class GxEPD2Display : public display::DisplayBuffer { public: GxEPD2Display(int16_t cs_pin, int16_t dc_pin, int16_t reset_pin, int16_t busy_pin) : cs_(cs_pin), dc_(dc_pin), rst_(reset_pin), busy_(busy_pin) { static_assert(!DualCS, "use the 5-pin constructor for a dual-CS model"); } GxEPD2Display(int16_t cs_pin, int16_t dc_pin, int16_t reset_pin, int16_t busy_pin, int16_t cs2_pin) : cs_(cs_pin), dc_(dc_pin), rst_(reset_pin), busy_(busy_pin), cs2_(cs2_pin) { static_assert(DualCS, "the 5-pin constructor is only for a dual-CS model"); } ~GxEPD2Display() { if (this->driver_ != nullptr) { this->driver_->~DisplayT(); RAMAllocator().deallocate(reinterpret_cast(this->driver_), 0); } } void set_pin(int16_t id, GPIOPin *pin) { PinRegistry::set(id, pin); } void set_spi_parent(spi::SPIComponent *parent) { this->spi_.set_spi_parent(parent); } // DisplayT (holds the page buffer, by far the biggest thing in this // component) is allocated here via RAMAllocator, not as a plain member - // ESPHome's Pvariable placement-news components into static storage, so // this is the one deliberate, explicitly-sized dynamic allocation that // lets the buffer prefer PSRAM when available, made once at boot and // never freed. void setup() override { RAMAllocator allocator; void *mem = allocator.allocate(sizeof(DisplayT)); if (mem == nullptr) { ESP_LOGE(TAG, "Failed to allocate %u bytes for display buffer", (unsigned) sizeof(DisplayT)); this->mark_failed(); return; } if constexpr (DualCS) { this->driver_ = new (mem) DisplayT(DriverT(this->cs_, this->dc_, this->rst_, this->busy_, this->cs2_)); } else { this->driver_ = new (mem) DisplayT(DriverT(this->cs_, this->dc_, this->rst_, this->busy_)); } this->spi_.spi_setup(); this->driver_->epd2.selectSPI(this->spi_, SPISettings()); this->driver_->init(0); } void update() override { this->driver_->setFullWindow(); this->driver_->firstPage(); do { this->do_update_(); } while (this->driver_->nextPage()); } void dump_config() override { ESP_LOGCONFIG(TAG, "GxEPD2 Display"); LOG_UPDATE_INTERVAL(this); } display::DisplayType get_display_type() override { if constexpr (ColorMode == GxEPD2ColorMode::BW) { return display::DisplayType::DISPLAY_TYPE_BINARY; } else { return display::DisplayType::DISPLAY_TYPE_COLOR; } } protected: void draw_absolute_pixel_internal(int x, int y, Color color) override { if constexpr (ColorMode == GxEPD2ColorMode::BW) { this->driver_->drawPixel(x, y, color.is_on() ? GxEPD_BLACK : GxEPD_WHITE); } else if constexpr (ColorMode == GxEPD2ColorMode::THREE_COLOR) { if (color.red > color.green && color.red > color.blue && color.red > 0x7f) { this->driver_->drawPixel(x, y, GxEPD_RED); } else { // get_display_type() reports DISPLAY_TYPE_COLOR for this mode, so // callers draw with literal RGB colors (Color(255,255,255,...) for // white, Color(0,0,0,...) for black) - not the on/off convention // is_on() (raw_32 != 0) assumes, which is only correct for the BW // path above (DISPLAY_TYPE_BINARY). Using is_on() here inverted // black and white for any lambda drawing literal colors. bool bright = color.red > 0x7f || color.green > 0x7f || color.blue > 0x7f; this->driver_->drawPixel(x, y, bright ? GxEPD_WHITE : GxEPD_BLACK); } } else { uint16_t rgb565 = ((color.red & 0xF8) << 8) | ((color.green & 0xFC) << 3) | (color.blue >> 3); this->driver_->drawPixel(x, y, rgb565); } } int get_width_internal() override { return this->driver_->width(); } int get_height_internal() override { return this->driver_->height(); } int16_t cs_; int16_t dc_; int16_t rst_; int16_t busy_; int16_t cs2_{-1}; DisplayT *driver_{nullptr}; SPIClass spi_; }; } // namespace esphome::gxepd2