| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- #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"
- 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. Only GxEPD2_213_Z98c is wired up so
- // far; more panels arrive as hand-written (later, extracted) descriptors,
- // 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 setup() override {
- const auto *desc = &PANEL_213_Z98C;
- this->plane_bytes_ = (static_cast<uint32_t>(desc->width) * desc->height) / 8;
- RAMAllocator<uint8_t> allocator;
- this->plane0_ = allocator.allocate(this->plane_bytes_);
- this->plane1_ = desc->plane_count > 1 ? allocator.allocate(this->plane_bytes_) : nullptr;
- if (this->plane0_ == nullptr || (desc->plane_count > 1 && this->plane1_ == nullptr)) {
- ESP_LOGE(TAG, "Failed to allocate %u-byte plane buffer(s)", (unsigned) this->plane_bytes_);
- this->mark_failed();
- return;
- }
- memset(this->plane0_, 0xFF, this->plane_bytes_); // 1 = white
- if (this->plane1_ != nullptr)
- memset(this->plane1_, 0x00, this->plane_bytes_); // 0 = not-red
- 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");
- LOG_UPDATE_INTERVAL(this);
- }
- display::DisplayType get_display_type() override { return display::DisplayType::DISPLAY_TYPE_COLOR; }
- protected:
- void draw_absolute_pixel_internal(int x, int y, Color color) override {
- if (x < 0 || y < 0 || x >= PANEL_213_Z98C.width || y >= PANEL_213_Z98C.height)
- return;
- uint32_t idx = (static_cast<uint32_t>(y) * PANEL_213_Z98C.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, 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.
- //
- // 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 PANEL_213_Z98C's plane_invert[1]=true - this
- // panel (waveshare-2.13in-bv4) puts "red" on the wire as 0, not 1 (see
- // EpaperWaveshareB::transform_red_byte in the working epaper_spi
- // config's driver: "Waveshare uses 0=red, 1=no-red"). Getting this
- // backwards was the actual bug behind the solid-red/black hardware
- // runs: with zero red pixels in a frame, the whole color plane comes
- // out as one uniform value either way - so whichever convention is
- // backwards paints the entire panel that color instead of the intended
- // "no red anywhere".
- bool red = color.red > color.green && color.red > color.blue && color.red > 0x7f;
- bool bright = color.red > 0x7f || color.green > 0x7f || color.blue > 0x7f;
- if (red) {
- this->plane1_[idx] |= mask;
- } else {
- this->plane1_[idx] &= ~mask;
- if (bright) {
- this->plane0_[idx] |= mask;
- } else {
- this->plane0_[idx] &= ~mask;
- }
- }
- }
- int get_width_internal() override { return PANEL_213_Z98C.width; }
- int get_height_internal() override { return PANEL_213_Z98C.height; }
- 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};
- };
- } // namespace esphome::gxepd2
|