gxepd2_display.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "pin_registry.h"
  7. // Only the wrapper the configured model needs gets included - see
  8. // display.py's USE_GXEPD2_* defines. Including all four unconditionally
  9. // fails to parse under ESP-IDF's toolchain: the un-instantiated template
  10. // bodies of the unused ones hit the same bare WIDTH/HEIGHT names, and
  11. // GCC's two-phase lookup only resolves those once instantiated.
  12. #ifdef USE_GXEPD2_BW
  13. #include <GxEPD2_BW.h>
  14. #endif
  15. #ifdef USE_GXEPD2_3C
  16. #include <GxEPD2_3C.h>
  17. #endif
  18. #ifdef USE_GXEPD2_4C
  19. #include <GxEPD2_4C.h>
  20. #endif
  21. #ifdef USE_GXEPD2_7C
  22. #include <GxEPD2_7C.h>
  23. #endif
  24. namespace esphome::gxepd2 {
  25. static const char *const TAG = "gxepd2";
  26. // Selected by codegen from catalog.json's "wrapper" field.
  27. enum class GxEPD2ColorMode {
  28. BW,
  29. THREE_COLOR, // black/white plus one accent ink (red or yellow panels alike)
  30. FOUR_COLOR, // black/white/red/yellow(-ish) 4-ink panels
  31. SEVEN_COLOR, // full ACeP-style palette
  32. };
  33. // DriverT: a GxEPD2 panel driver class (e.g. GxEPD2_290_T94).
  34. // DisplayT: that driver wrapped in its GxEPD2 color-depth template (e.g.
  35. // GxEPD2_BW<GxEPD2_290_T94, GxEPD2_290_T94::HEIGHT>).
  36. // DualCS: whether DriverT takes 5 constructor args (cs, dc, rst, busy, cs2)
  37. // instead of 4. All four are chosen by codegen from catalog.json.
  38. //
  39. // cs_pin/dc_pin/reset_pin/busy_pin(/cs2_pin) are synthetic PinRegistry ids,
  40. // not real esphome::GPIOPin; digitalWrite()/digitalRead()/pinMode() in
  41. // arduino_compat/Arduino.h resolve them back to the real pin.
  42. //
  43. // display::DisplayBuffer already derives PollingComponent via Display - do
  44. // not add PollingComponent again here, it creates an ambiguous base.
  45. template<typename DriverT, typename DisplayT, GxEPD2ColorMode ColorMode, bool DualCS>
  46. class GxEPD2Display : public display::DisplayBuffer {
  47. public:
  48. GxEPD2Display(int16_t cs_pin, int16_t dc_pin, int16_t reset_pin, int16_t busy_pin)
  49. : cs_(cs_pin), dc_(dc_pin), rst_(reset_pin), busy_(busy_pin) {
  50. static_assert(!DualCS, "use the 5-pin constructor for a dual-CS model");
  51. }
  52. GxEPD2Display(int16_t cs_pin, int16_t dc_pin, int16_t reset_pin, int16_t busy_pin, int16_t cs2_pin)
  53. : cs_(cs_pin), dc_(dc_pin), rst_(reset_pin), busy_(busy_pin), cs2_(cs2_pin) {
  54. static_assert(DualCS, "the 5-pin constructor is only for a dual-CS model");
  55. }
  56. ~GxEPD2Display() {
  57. if (this->driver_ != nullptr) {
  58. this->driver_->~DisplayT();
  59. RAMAllocator<uint8_t>().deallocate(reinterpret_cast<uint8_t *>(this->driver_), 0);
  60. }
  61. }
  62. void set_pin(int16_t id, GPIOPin *pin) { PinRegistry::set(id, pin); }
  63. void set_spi_parent(spi::SPIComponent *parent) { this->spi_.set_spi_parent(parent); }
  64. // DisplayT (holds the page buffer, by far the biggest thing in this
  65. // component) is allocated here via RAMAllocator, not as a plain member -
  66. // ESPHome's Pvariable placement-news components into static storage, so
  67. // this is the one deliberate, explicitly-sized dynamic allocation that
  68. // lets the buffer prefer PSRAM when available, made once at boot and
  69. // never freed.
  70. void setup() override {
  71. RAMAllocator<uint8_t> allocator;
  72. void *mem = allocator.allocate(sizeof(DisplayT));
  73. if (mem == nullptr) {
  74. ESP_LOGE(TAG, "Failed to allocate %u bytes for display buffer", (unsigned) sizeof(DisplayT));
  75. this->mark_failed();
  76. return;
  77. }
  78. if constexpr (DualCS) {
  79. this->driver_ = new (mem) DisplayT(DriverT(this->cs_, this->dc_, this->rst_, this->busy_, this->cs2_));
  80. } else {
  81. this->driver_ = new (mem) DisplayT(DriverT(this->cs_, this->dc_, this->rst_, this->busy_));
  82. }
  83. this->spi_.spi_setup();
  84. this->driver_->epd2.selectSPI(this->spi_, SPISettings());
  85. this->driver_->init(0);
  86. }
  87. void update() override {
  88. this->driver_->setFullWindow();
  89. this->driver_->firstPage();
  90. do {
  91. this->do_update_();
  92. } while (this->driver_->nextPage());
  93. }
  94. void dump_config() override {
  95. ESP_LOGCONFIG(TAG, "GxEPD2 Display");
  96. LOG_UPDATE_INTERVAL(this);
  97. }
  98. display::DisplayType get_display_type() override {
  99. if constexpr (ColorMode == GxEPD2ColorMode::BW) {
  100. return display::DisplayType::DISPLAY_TYPE_BINARY;
  101. } else {
  102. return display::DisplayType::DISPLAY_TYPE_COLOR;
  103. }
  104. }
  105. protected:
  106. void draw_absolute_pixel_internal(int x, int y, Color color) override {
  107. if constexpr (ColorMode == GxEPD2ColorMode::BW) {
  108. this->driver_->drawPixel(x, y, color.is_on() ? GxEPD_BLACK : GxEPD_WHITE);
  109. } else if constexpr (ColorMode == GxEPD2ColorMode::THREE_COLOR) {
  110. if (color.red > color.green && color.red > color.blue && color.red > 0x7f) {
  111. this->driver_->drawPixel(x, y, GxEPD_RED);
  112. } else {
  113. // get_display_type() reports DISPLAY_TYPE_COLOR for this mode, so
  114. // callers draw with literal RGB colors (Color(255,255,255,...) for
  115. // white, Color(0,0,0,...) for black) - not the on/off convention
  116. // is_on() (raw_32 != 0) assumes, which is only correct for the BW
  117. // path above (DISPLAY_TYPE_BINARY). Using is_on() here inverted
  118. // black and white for any lambda drawing literal colors.
  119. bool bright = color.red > 0x7f || color.green > 0x7f || color.blue > 0x7f;
  120. this->driver_->drawPixel(x, y, bright ? GxEPD_WHITE : GxEPD_BLACK);
  121. }
  122. } else {
  123. uint16_t rgb565 = ((color.red & 0xF8) << 8) | ((color.green & 0xFC) << 3) | (color.blue >> 3);
  124. this->driver_->drawPixel(x, y, rgb565);
  125. }
  126. }
  127. int get_width_internal() override { return this->driver_->width(); }
  128. int get_height_internal() override { return this->driver_->height(); }
  129. int16_t cs_;
  130. int16_t dc_;
  131. int16_t rst_;
  132. int16_t busy_;
  133. int16_t cs2_{-1};
  134. DisplayT *driver_{nullptr};
  135. SPIClass spi_;
  136. };
  137. } // namespace esphome::gxepd2