epd_command.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #pragma once
  2. #include <cstdint>
  3. namespace esphome::gxepd2 {
  4. // One command byte plus its (fixed, compile-time) data bytes, as sent by
  5. // GxEPD2_EPD::_writeCommand()/_writeData(). delay_before_ms/delay_after_ms
  6. // and wait_busy_after mirror the fixed delay()s and _waitWhileBusy() calls
  7. // bracketing specific commands in the vendor source (which command gets
  8. // which varies per panel - e.g. GxEPD2_213_Z98c delays 10ms on both sides
  9. // of its SWRESET, while GxEPD2_154_Z90c busy-waits after its SWRESET
  10. // instead). At most one of delay_after_ms/wait_busy_after is expected to be
  11. // set on any one command.
  12. struct EpdCommand {
  13. uint8_t cmd;
  14. const uint8_t *data; // nullptr if the command takes no data bytes
  15. uint8_t data_len;
  16. uint16_t delay_before_ms{0};
  17. uint16_t delay_after_ms{0};
  18. bool wait_busy_after{false};
  19. };
  20. // One palette entry for a packed (bits_per_pixel > 1) panel: a reference
  21. // RGB color (ESPHome Color space, 0-255 per channel) and the wire code the
  22. // controller uses for it. Classification at draw time picks the entry
  23. // nearest the drawn color, with exact matches winning - so named colors map
  24. // deterministically, the way GxEPD2's own color4()/color7() switch does.
  25. // Several entries may share a code (GxEPD2 maps green and orange both onto
  26. // the 4-color yellow code, and blue onto black).
  27. struct EpdPaletteEntry {
  28. uint8_t r, g, b;
  29. uint8_t code;
  30. };
  31. // Standard GxEPD2 4-color (BWRY) palette - codes per GxEPD2_4C.h color4():
  32. // black=0x00, white=0x01, yellow/green/orange=0x02, red=0x03, blue->black.
  33. inline const EpdPaletteEntry GXEPD2_PALETTE_4C[] = {
  34. {0x00, 0x00, 0x00, 0x00}, // black
  35. {0xFF, 0xFF, 0xFF, 0x01}, // white
  36. {0xFF, 0xFF, 0x00, 0x02}, // yellow
  37. {0x00, 0xFF, 0x00, 0x02}, // green -> yellow code
  38. {0xFF, 0xA4, 0x00, 0x02}, // orange -> yellow code
  39. {0xFF, 0x00, 0x00, 0x03}, // red
  40. {0x00, 0x00, 0xFF, 0x00}, // blue -> black code
  41. };
  42. inline const uint8_t GXEPD2_PALETTE_4C_LEN = sizeof(GXEPD2_PALETTE_4C) / sizeof(GXEPD2_PALETTE_4C[0]);
  43. // Standard ACeP 7-color palette - codes per GxEPD2_7C.h color7(): black=0x00,
  44. // white=0x01, green=0x02, blue=0x03, red=0x04, yellow=0x05, orange=0x06.
  45. // Ready for the first 4bpp panel (GxEPD2_565c/730c); no shipped descriptor
  46. // consumes it yet - see the gxepd2 panel work.
  47. inline const EpdPaletteEntry GXEPD2_PALETTE_7C[] = {
  48. {0x00, 0x00, 0x00, 0x00}, // black
  49. {0xFF, 0xFF, 0xFF, 0x01}, // white
  50. {0x00, 0xFF, 0x00, 0x02}, // green
  51. {0x00, 0x00, 0xFF, 0x03}, // blue
  52. {0xFF, 0x00, 0x00, 0x04}, // red
  53. {0xFF, 0xFF, 0x00, 0x05}, // yellow
  54. {0xFF, 0xA4, 0x00, 0x06}, // orange
  55. };
  56. inline const uint8_t GXEPD2_PALETTE_7C_LEN = sizeof(GXEPD2_PALETTE_7C) / sizeof(GXEPD2_PALETTE_7C[0]);
  57. // Everything CycleEngine needs to drive one panel, hand-derived from a
  58. // GxEPD2 vendor driver's .cpp/.h (see panel_154_z90c.h). Only what this
  59. // first slice's engine actually uses - the RAM-window-set command layout
  60. // (0x44/0x45/0x4E/0x4F) is common to every panel inspected so far and is
  61. // hardcoded in CycleEngine itself rather than made part of this struct;
  62. // revisit if/when the extraction-tooling phase finds a controller family
  63. // that lays it out differently.
  64. struct EpdPanelDescriptor {
  65. uint16_t width;
  66. uint16_t height;
  67. bool busy_active_level; // digitalRead() level meaning "still busy"
  68. uint32_t busy_timeout_ms; // ceiling for every busy wait (init and refresh alike)
  69. const EpdCommand *init_sequence; // run once, after the reset pulse
  70. uint8_t init_sequence_len;
  71. // Sets the (always full-frame, since this engine has no partial-window
  72. // support) RAM addressing window. window_sequence[0] runs once after
  73. // init_sequence and before the first plane's byte burst; index 1 is only
  74. // used when window_per_plane is true (see below). Was hardcoded into
  75. // CycleEngine assuming every controller shares SSD1680's
  76. // 0x44/0x45/0x4E/0x4F byte-column layout - GxEPD2_426_GDEQ0426T82
  77. // (SSD1677) turned out to address in raw pixels with reversed Y and
  78. // re-issues its entry-mode command here, and GxEPD2_583_GDEQ0583T31
  79. // (UC8179) uses one 0x90 command bracketed by 0x91/0x92 markers instead.
  80. // Made per-panel data, same as init/refresh, rather than special-cased
  81. // in the engine.
  82. const EpdCommand *window_sequence[2];
  83. uint8_t window_sequence_len[2];
  84. // true if each plane needs window_sequence[plane index] re-run before
  85. // its own byte burst, instead of one window_sequence[0] covering every
  86. // plane's burst (the default, e.g. a BW+red panel's shared full-frame
  87. // window). This is how GxEPD2_579c_GDEY0579F51's real master/slave
  88. // regions are handled: two genuinely separate physical RAM windows, not
  89. // just two color planes sharing one - "plane" here just means "one more
  90. // buffer with its own byte burst," which happens to fit a two-physical-
  91. // region panel as well as a two-color-plane one. See
  92. // panel_579c_gdey0579f51.h.
  93. bool window_per_plane{false};
  94. uint8_t plane_count; // 1 (BW), 2 (BW + accent color), or 2 physical regions
  95. uint8_t plane_write_cmd[2]; // e.g. {0x24, 0x26}
  96. bool plane_invert[2]; // true if the byte must be bit-inverted on write
  97. // When window_per_plane is true, each plane's own buffer is sized for
  98. // this width, not the descriptor's overall `width` (which is the full
  99. // logical canvas ESPHome draws into and get_width_internal() reports) -
  100. // e.g. 396 for GDEY0579F51's master/slave halves, each 396x272 on its
  101. // own, not the full 792x272. Unused otherwise.
  102. uint16_t plane_width{0};
  103. // true if rows must be sent in interleaved top/bottom-pair order (row 0,
  104. // row H-1, row 1, row H-2, ...) instead of plain sequential top-to-
  105. // bottom - GDEY0579F51's HX8717 pair apparently scans each half from
  106. // both edges toward the middle, per Waveshare's own epd5in79g.cpp
  107. // reference driver (confirmed 2026-09-19: a bar/ladder/diagonal test
  108. // pattern came out vertically scrambled with a broken diagonal - the
  109. // documented signature of a row-reordering bug - sent in plain
  110. // sequential order). Not assumed for any other packed (bits_per_pixel > 1)
  111. // panel (e.g. GDEY029F51, a single-chip panel with no master/slave split
  112. // at all) without the same kind of hardware confirmation.
  113. bool row_interleave{false};
  114. // Pixel bit-depth, which also selects the buffer model:
  115. // 1 = one-bit-per-pixel plane model (mono, or BW + accent as two
  116. // separate 1bpp planes). plane_invert applies; palette is unused.
  117. // 2 = packed 4-color: 2 bits/pixel, 4 pixels/byte, MSB-first.
  118. // 4 = packed 7-color (ACeP): 4 bits/pixel, 2 pixels/byte, MSB-first.
  119. // For bits_per_pixel > 1 the buffer is a single packed code-per-pixel
  120. // stream (per region when window_per_plane), sized width*height*bpp/8,
  121. // and each pixel's wire code comes from `palette` (nearest-color
  122. // classification at draw time) rather than the 1bpp plane bitmasks;
  123. // plane_invert is unused. This replaces the old packed_2bpp_plane bool -
  124. // "is it packed" is now bits_per_pixel > 1, "which codes" is data, so a
  125. // 4bpp 7-color panel is a palette + bits_per_pixel=4, no new engine flag.
  126. uint8_t bits_per_pixel{1};
  127. // Packed-mode (bits_per_pixel > 1) palette and the wire code the buffer is
  128. // cleared to (the panel's white/blank). Unused when bits_per_pixel == 1.
  129. // See GXEPD2_PALETTE_4C / GXEPD2_PALETTE_7C.
  130. const EpdPaletteEntry *palette{nullptr};
  131. uint8_t palette_len{0};
  132. uint8_t blank_code{0};
  133. const EpdCommand *refresh_sequence; // e.g. 0x22/0xF7 then trigger 0x20
  134. uint8_t refresh_sequence_len;
  135. };
  136. } // namespace esphome::gxepd2