gxepd2_display.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #pragma once
  2. #include "esphome/core/component.h"
  3. #include "esphome/core/helpers.h"
  4. #include "esphome/components/display/display_buffer.h"
  5. #include "esphome/components/spi/spi.h"
  6. #include "cycle_engine.h"
  7. #include "panel_213_z98c.h"
  8. #include "panel_290c_gdey029f51.h"
  9. #include "panel_426_gdeq0426t82.h"
  10. #include "panel_579c_gdey0579f51.h"
  11. #include "panel_579c_gdey0579f51_master_only.h"
  12. #include "panel_583_gdeq0583t31.h"
  13. namespace esphome::gxepd2 {
  14. static const char *const TAG = "gxepd2";
  15. // Native chunked-engine replacement for the old GxEPD2-template/FreeRTOS-task
  16. // component - see the plan this was built from (and project memory entry
  17. // gxepd2_task_watchdog_status) for why. Panels arrive as hand-written
  18. // (later, extracted) descriptors selected at runtime via set_descriptor(),
  19. // not by templating this class over GxEPD2's own driver classes.
  20. class GxEPD2Display : public display::DisplayBuffer {
  21. public:
  22. void set_cs_pin(GPIOPin *pin) { this->cs_ = pin; }
  23. void set_dc_pin(GPIOPin *pin) { this->dc_ = pin; }
  24. void set_reset_pin(GPIOPin *pin) { this->rst_ = pin; }
  25. void set_busy_pin(GPIOPin *pin) { this->busy_ = pin; }
  26. void set_spi_parent(spi::SPIComponent *parent) { this->spi_.set_spi_parent(parent); }
  27. void set_descriptor(const EpdPanelDescriptor *desc) { this->desc_ = desc; }
  28. void setup() override {
  29. const auto *desc = this->desc_;
  30. // window_per_plane panels (GDEY0579F51's master+slave) size each
  31. // plane's own buffer from plane_width (each region's own local
  32. // width), not the descriptor's overall width (the full logical canvas
  33. // ESPHome draws into) - both regions are the same size here, so one
  34. // plane_bytes_ value covers both buffers.
  35. uint32_t w = desc->window_per_plane ? desc->plane_width : desc->width;
  36. this->plane_bytes_ = desc->packed_2bpp_plane ? (w * desc->height) / 4 : (w * desc->height) / 8;
  37. RAMAllocator<uint8_t> allocator;
  38. this->plane0_ = allocator.allocate(this->plane_bytes_);
  39. bool need_plane1 = desc->plane_count > 1;
  40. this->plane1_ = need_plane1 ? allocator.allocate(this->plane_bytes_) : nullptr;
  41. if (this->plane0_ == nullptr || (need_plane1 && this->plane1_ == nullptr)) {
  42. ESP_LOGE(TAG, "Failed to allocate %u-byte plane buffer(s)", (unsigned) this->plane_bytes_);
  43. this->mark_failed();
  44. return;
  45. }
  46. // Packed 2bpp: code 0x01 (white) repeated 4x/byte = 0x55, matching
  47. // GxEPD2's own writeScreenBuffer()'s white fill value exactly - not
  48. // 0xFF (that's the 1bpp planes' white) or 0x00 (that's black here).
  49. memset(this->plane0_, desc->packed_2bpp_plane ? 0x55 : 0xFF, this->plane_bytes_);
  50. if (this->plane1_ != nullptr) {
  51. // plane1_ is a second packed region (master/slave) for a
  52. // window_per_plane panel - also defaults to white (0x55), not the
  53. // 1bpp "red" plane's 0=not-red convention.
  54. memset(this->plane1_, desc->packed_2bpp_plane ? 0x55 : 0x00, this->plane_bytes_);
  55. }
  56. this->spi_.spi_setup();
  57. this->engine_.begin(desc, this->cs_, this->dc_, this->rst_, this->busy_, &this->spi_);
  58. }
  59. // Arms the state machine and returns immediately - the real several-
  60. // seconds-long write/refresh/busy-wait cycle happens across many
  61. // subsequent loop() ticks, not before this call returns.
  62. void update() override {
  63. if (!this->engine_.idle()) {
  64. ESP_LOGW(TAG, "Skipping update: previous display refresh is still running");
  65. return;
  66. }
  67. this->do_update_();
  68. this->engine_.request_full_update(this->plane0_, this->plane1_);
  69. }
  70. void loop() override { this->engine_.step(); }
  71. void dump_config() override {
  72. ESP_LOGCONFIG(TAG, "GxEPD2 Display");
  73. LOG_UPDATE_INTERVAL(this);
  74. }
  75. display::DisplayType get_display_type() override { return display::DisplayType::DISPLAY_TYPE_COLOR; }
  76. protected:
  77. void draw_absolute_pixel_internal(int x, int y, Color color) override {
  78. if (x < 0 || y < 0 || x >= this->desc_->width || y >= this->desc_->height)
  79. return;
  80. bool red = color.red > color.green && color.red > color.blue && color.red > 0x7f;
  81. bool bright = color.red > 0x7f || color.green > 0x7f || color.blue > 0x7f;
  82. if (this->desc_->packed_2bpp_plane) {
  83. // JD79667/HX8717-family panels: 2 bits/pixel, 4 pixels/byte, MSB
  84. // first (matches GxEPD2's own out_data <<= 2 packing order in e.g.
  85. // GxEPD2_290c_GDEY029F51::writeImage). Codes match GxEPD2_4C.h's own
  86. // color4() table exactly: black=0x00, white=0x01, yellow=0x02,
  87. // red=0x03 - confirmed against that table after first getting
  88. // black/white backwards by misreading writeImage's own
  89. // black_data-bit ternary (2026-09-19).
  90. bool yellow = color.red > 0x7f && color.green > 0x7f && color.blue <= 0x7f;
  91. uint8_t code;
  92. if (red)
  93. code = 0x03;
  94. else if (yellow)
  95. code = 0x02;
  96. else if (bright)
  97. code = 0x01;
  98. else
  99. code = 0x00;
  100. // window_per_plane (GDEY0579F51's master+slave): each region is its
  101. // own local 396-wide buffer, not one 792-wide one - route by which
  102. // half of the global x range this pixel falls in.
  103. uint16_t stride = this->desc_->width;
  104. uint8_t *buf = this->plane0_;
  105. int local_x = x;
  106. if (this->desc_->window_per_plane && this->desc_->plane_count > 1) {
  107. stride = this->desc_->plane_width;
  108. if (x >= stride) {
  109. buf = this->plane1_;
  110. local_x = x - stride;
  111. }
  112. }
  113. uint32_t px = static_cast<uint32_t>(y) * stride + local_x;
  114. uint32_t idx = px / 4;
  115. uint8_t shift = static_cast<uint8_t>((3 - (px % 4)) * 2);
  116. buf[idx] = static_cast<uint8_t>((buf[idx] & ~(0x03 << shift)) | (code << shift));
  117. return;
  118. }
  119. uint32_t idx = (static_cast<uint32_t>(y) * this->desc_->width + x) / 8;
  120. uint8_t mask = 0x80 >> (x % 8);
  121. // Same classification the old task-based component used: red/accent
  122. // pixels go to the color plane (when the panel has one), everything
  123. // else is black/white on the black plane. get_display_type() reports
  124. // DISPLAY_TYPE_COLOR, so callers draw literal RGB colors, not the
  125. // on/off is_on() convention, even on a plane_count==1 (BW-only) panel -
  126. // there just is no color plane for a "red" classification to land on.
  127. //
  128. // Buffer convention here is 1=red/0=not-red (matching the reference
  129. // epaper_spi driver's EPaperWeAct3C internal convention), transformed
  130. // to the wire value by plane_invert[1] - see PANEL_213_Z98C's comment
  131. // for why that inversion flag and this buffer convention are coupled,
  132. // not independent facts.
  133. if (red && this->plane1_ != nullptr) {
  134. this->plane1_[idx] |= mask;
  135. } else {
  136. if (this->plane1_ != nullptr)
  137. this->plane1_[idx] &= ~mask;
  138. if (bright) {
  139. this->plane0_[idx] |= mask;
  140. } else {
  141. this->plane0_[idx] &= ~mask;
  142. }
  143. }
  144. }
  145. int get_width_internal() override { return this->desc_->width; }
  146. int get_height_internal() override { return this->desc_->height; }
  147. const EpdPanelDescriptor *desc_{nullptr};
  148. GPIOPin *cs_{nullptr};
  149. GPIOPin *dc_{nullptr};
  150. GPIOPin *rst_{nullptr};
  151. GPIOPin *busy_{nullptr};
  152. EpdSPIDevice spi_;
  153. CycleEngine engine_;
  154. uint8_t *plane0_{nullptr};
  155. uint8_t *plane1_{nullptr};
  156. uint32_t plane_bytes_{0};
  157. };
  158. } // namespace esphome::gxepd2