gxepd2_display.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. namespace esphome::gxepd2 {
  9. static const char *const TAG = "gxepd2";
  10. // Native chunked-engine replacement for the old GxEPD2-template/FreeRTOS-task
  11. // component - see the plan this was built from (and project memory entry
  12. // gxepd2_task_watchdog_status) for why. Only GxEPD2_213_Z98c is wired up so
  13. // far; more panels arrive as hand-written (later, extracted) descriptors,
  14. // not by templating this class over GxEPD2's own driver classes.
  15. class GxEPD2Display : public display::DisplayBuffer {
  16. public:
  17. void set_cs_pin(GPIOPin *pin) { this->cs_ = pin; }
  18. void set_dc_pin(GPIOPin *pin) { this->dc_ = pin; }
  19. void set_reset_pin(GPIOPin *pin) { this->rst_ = pin; }
  20. void set_busy_pin(GPIOPin *pin) { this->busy_ = pin; }
  21. void set_spi_parent(spi::SPIComponent *parent) { this->spi_.set_spi_parent(parent); }
  22. void setup() override {
  23. const auto *desc = &PANEL_213_Z98C;
  24. this->plane_bytes_ = (static_cast<uint32_t>(desc->width) * desc->height) / 8;
  25. RAMAllocator<uint8_t> allocator;
  26. this->plane0_ = allocator.allocate(this->plane_bytes_);
  27. this->plane1_ = desc->plane_count > 1 ? allocator.allocate(this->plane_bytes_) : nullptr;
  28. if (this->plane0_ == nullptr || (desc->plane_count > 1 && this->plane1_ == nullptr)) {
  29. ESP_LOGE(TAG, "Failed to allocate %u-byte plane buffer(s)", (unsigned) this->plane_bytes_);
  30. this->mark_failed();
  31. return;
  32. }
  33. memset(this->plane0_, 0xFF, this->plane_bytes_); // 1 = white
  34. if (this->plane1_ != nullptr)
  35. memset(this->plane1_, 0x00, this->plane_bytes_); // 0 = not-red
  36. this->spi_.spi_setup();
  37. this->engine_.begin(desc, this->cs_, this->dc_, this->rst_, this->busy_, &this->spi_);
  38. }
  39. // Arms the state machine and returns immediately - the real several-
  40. // seconds-long write/refresh/busy-wait cycle happens across many
  41. // subsequent loop() ticks, not before this call returns.
  42. void update() override {
  43. if (!this->engine_.idle()) {
  44. ESP_LOGW(TAG, "Skipping update: previous display refresh is still running");
  45. return;
  46. }
  47. this->do_update_();
  48. this->engine_.request_full_update(this->plane0_, this->plane1_);
  49. }
  50. void loop() override { this->engine_.step(); }
  51. void dump_config() override {
  52. ESP_LOGCONFIG(TAG, "GxEPD2 Display");
  53. LOG_UPDATE_INTERVAL(this);
  54. }
  55. display::DisplayType get_display_type() override { return display::DisplayType::DISPLAY_TYPE_COLOR; }
  56. protected:
  57. void draw_absolute_pixel_internal(int x, int y, Color color) override {
  58. if (x < 0 || y < 0 || x >= PANEL_213_Z98C.width || y >= PANEL_213_Z98C.height)
  59. return;
  60. uint32_t idx = (static_cast<uint32_t>(y) * PANEL_213_Z98C.width + x) / 8;
  61. uint8_t mask = 0x80 >> (x % 8);
  62. // Same classification the old task-based component used: red/accent
  63. // pixels go to the color plane, everything else is black/white on the
  64. // black plane. get_display_type() reports DISPLAY_TYPE_COLOR, so
  65. // callers draw literal RGB colors, not the on/off is_on() convention.
  66. //
  67. // Buffer convention here is 1=red/0=not-red (matching the reference
  68. // epaper_spi driver's EPaperWeAct3C internal convention), transformed
  69. // to the wire value by PANEL_213_Z98C's plane_invert[1]=true - this
  70. // panel (waveshare-2.13in-bv4) puts "red" on the wire as 0, not 1 (see
  71. // EpaperWaveshareB::transform_red_byte in the working epaper_spi
  72. // config's driver: "Waveshare uses 0=red, 1=no-red"). Getting this
  73. // backwards was the actual bug behind the solid-red/black hardware
  74. // runs: with zero red pixels in a frame, the whole color plane comes
  75. // out as one uniform value either way - so whichever convention is
  76. // backwards paints the entire panel that color instead of the intended
  77. // "no red anywhere".
  78. bool red = color.red > color.green && color.red > color.blue && color.red > 0x7f;
  79. bool bright = color.red > 0x7f || color.green > 0x7f || color.blue > 0x7f;
  80. if (red) {
  81. this->plane1_[idx] |= mask;
  82. } else {
  83. this->plane1_[idx] &= ~mask;
  84. if (bright) {
  85. this->plane0_[idx] |= mask;
  86. } else {
  87. this->plane0_[idx] &= ~mask;
  88. }
  89. }
  90. }
  91. int get_width_internal() override { return PANEL_213_Z98C.width; }
  92. int get_height_internal() override { return PANEL_213_Z98C.height; }
  93. GPIOPin *cs_{nullptr};
  94. GPIOPin *dc_{nullptr};
  95. GPIOPin *rst_{nullptr};
  96. GPIOPin *busy_{nullptr};
  97. EpdSPIDevice spi_;
  98. CycleEngine engine_;
  99. uint8_t *plane0_{nullptr};
  100. uint8_t *plane1_{nullptr};
  101. uint32_t plane_bytes_{0};
  102. };
  103. } // namespace esphome::gxepd2