engine_model.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #pragma once
  2. #include <cstdint>
  3. namespace esphome::zepd {
  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 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 PALETTE_4C_LEN = sizeof(PALETTE_4C) / sizeof(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. inline const EpdPaletteEntry PALETTE_7C[] = {
  46. {0x00, 0x00, 0x00, 0x00}, // black
  47. {0xFF, 0xFF, 0xFF, 0x01}, // white
  48. {0x00, 0xFF, 0x00, 0x02}, // green
  49. {0x00, 0x00, 0xFF, 0x03}, // blue
  50. {0xFF, 0x00, 0x00, 0x04}, // red
  51. {0xFF, 0xFF, 0x00, 0x05}, // yellow
  52. {0xFF, 0xA4, 0x00, 0x06}, // orange
  53. };
  54. inline const uint8_t PALETTE_7C_LEN = sizeof(PALETTE_7C) / sizeof(PALETTE_7C[0]);
  55. // Everything the engine needs to drive one panel - this is the codegen'd
  56. // "engine model" produced from a model's drivers/<model>.json (see
  57. // ../../../../claude/DESCRIPTOR_REGISTRY_PLAN.md), hand-derived today from a
  58. // GxEPD2 vendor driver's .cpp/.h the same way the legacy
  59. // components/gxepd2/panel_*.h files were. Only what the engine actually
  60. // uses - the RAM-window-set command layout (0x44/0x45/0x4E/0x4F) is common
  61. // to every panel inspected so far and is hardcoded in Engine itself rather
  62. // than made part of this struct; revisit if/when a controller family turns
  63. // up that lays it out differently.
  64. //
  65. // Order in which the engine emits a plane's wire lines, derived by the diag
  66. // native-path tool. The buffer always holds `height` rows of real pixels;
  67. // this controls only which buffer row each wire line carries and how many
  68. // wire lines there are, so wire length can exceed buffer size.
  69. // SEQUENTIAL - wire line i carries buffer row i; line count == height.
  70. // Every single-region panel captured so far.
  71. // PAIRED_EDGES - the controller fills a 2x-height RAM scanned from both
  72. // edges toward the middle: for i in [0,height) it wants
  73. // buffer row (height-1-i) then row i, so line count ==
  74. // 2*height and each row is sent twice. GDEY0579F51 / HX8717.
  75. // This is exactly what GxEPD2's writeNative emits for that
  76. // panel (99 bytes/line x 544 lines/region), so a faithful
  77. // replay needs it - nothing panel-specific in the engine.
  78. enum class LineOrder : uint8_t { SEQUENTIAL, PAIRED_EDGES };
  79. struct EngineModel {
  80. uint16_t width;
  81. uint16_t height;
  82. bool busy_active_level; // digitalRead() level meaning "still busy"
  83. uint32_t busy_timeout_ms; // ceiling for every busy wait (init and refresh alike)
  84. const EpdCommand *init_sequence; // run once, after the reset pulse
  85. uint8_t init_sequence_len;
  86. // Sets the (always full-frame, since this engine has no partial-window
  87. // support) RAM addressing window. window_sequence[0] runs once after
  88. // init_sequence and before the first plane's byte burst; index 1 is only
  89. // used when window_per_plane is true (see below). Was hardcoded assuming
  90. // every controller shares SSD1680's 0x44/0x45/0x4E/0x4F byte-column
  91. // layout - GxEPD2_426_GDEQ0426T82 (SSD1677) turned out to address in raw
  92. // pixels with reversed Y and re-issues its entry-mode command here, and
  93. // GxEPD2_583_GDEQ0583T31 (UC8179) uses one 0x90 command bracketed by
  94. // 0x91/0x92 markers instead. Made per-model data, same as init/refresh,
  95. // rather than special-cased in the engine.
  96. const EpdCommand *window_sequence[2];
  97. uint8_t window_sequence_len[2];
  98. // true if each plane needs window_sequence[plane index] re-run before
  99. // its own byte burst, instead of one window_sequence[0] covering every
  100. // plane's burst (the default, e.g. a BW+red panel's shared full-frame
  101. // window). This is how a panel with real master/slave regions (two
  102. // genuinely separate physical RAM windows, not just two color planes
  103. // sharing one) is handled - "plane" here just means "one more buffer
  104. // with its own byte burst," which happens to fit a two-physical-region
  105. // panel as well as a two-color-plane one.
  106. bool window_per_plane{false};
  107. uint8_t plane_count; // 1 (BW), 2 (BW + accent color), or 2 physical regions
  108. uint8_t plane_write_cmd[2]; // e.g. {0x24, 0x26}
  109. // The extra "previous RAM bank" write GxEPD2's writeImageForFullRefresh()
  110. // makes that plain writeImage() doesn't, on controllers where the two
  111. // diverge (SSD1680/SSD1681/SSD1619-family: 0x26 then 0x24, or 0x10 then
  112. // 0x13 - same bitmap written twice, to two different commands). 0x00 (the
  113. // default) means no divergence was observed: writeImageForFullRefresh
  114. // falls back to writeImage for that controller, the common case. NOT read
  115. // by Engine today - this engine always does a plain single write per
  116. // plane, correct for full-refresh-only operation, since the full-refresh
  117. // waveform doesn't depend on the "previous" bank's content. It matters
  118. // once partial refresh reads that bank to compute its diff, so it's
  119. // captured now rather than needing a second capture pass later.
  120. uint8_t plane_write_cmd_for_full_refresh[2]{0, 0};
  121. bool plane_invert[2]; // true if the byte must be bit-inverted on write
  122. // When window_per_plane is true, each plane's own buffer is sized for
  123. // this width, not the descriptor's overall `width` (which is the full
  124. // logical canvas the adapter draws into) - e.g. 396 for GDEY0579F51's
  125. // master/slave halves, each 396x272 on its own, not the full 792x272.
  126. // Unused otherwise.
  127. uint16_t plane_width{0};
  128. // Wire-line emission order for this panel's plane(s) - see LineOrder
  129. // above. Default SEQUENTIAL; PAIRED_EDGES is not assumed for any panel
  130. // without the diag native-path tool deriving it.
  131. LineOrder line_order{LineOrder::SEQUENTIAL};
  132. // Pixel bit-depth, which also selects the buffer model:
  133. // 1 = one-bit-per-pixel plane model (mono, or BW + accent as two
  134. // separate 1bpp planes). plane_invert applies; palette is unused.
  135. // 2 = packed 4-color: 2 bits/pixel, 4 pixels/byte, MSB-first.
  136. // 4 = packed 7-color (ACeP): 4 bits/pixel, 2 pixels/byte, MSB-first.
  137. // For bits_per_pixel > 1 the buffer is a single packed code-per-pixel
  138. // stream (per region when window_per_plane), sized width*height*bpp/8,
  139. // and each pixel's wire code comes from `palette` (nearest-color
  140. // classification at draw time) rather than the 1bpp plane bitmasks;
  141. // plane_invert is unused.
  142. uint8_t bits_per_pixel{1};
  143. // Packed-mode (bits_per_pixel > 1) palette and the wire code the buffer is
  144. // cleared to (the panel's white/blank). Unused when bits_per_pixel == 1.
  145. // See PALETTE_4C / PALETTE_7C.
  146. const EpdPaletteEntry *palette{nullptr};
  147. uint8_t palette_len{0};
  148. uint8_t blank_code{0};
  149. const EpdCommand *refresh_sequence; // e.g. 0x22/0xF7 then trigger 0x20
  150. uint8_t refresh_sequence_len;
  151. // powerOff()/hibernate(), captured the same way as refresh_sequence.
  152. // Exposed for a user's own lambda (Engine::request_power_off/
  153. // request_hibernate), not run automatically by update(): this engine does
  154. // a full hardware reset on every cycle regardless of prior power state,
  155. // so correctness never depends on calling either. hibernate_sequence is
  156. // the deep-sleep trigger ONLY (every GxEPD2 driver's hibernate() is
  157. // powerOff() + one more command when rst is wired) - Engine runs
  158. // power_off_sequence first, then hibernate_sequence, to replay the same
  159. // two-step effect. Both nullptr/0 (not yet captured) is a safe no-op:
  160. // Engine treats a zero-length sequence as already done.
  161. const EpdCommand *power_off_sequence{nullptr};
  162. uint8_t power_off_sequence_len{0};
  163. const EpdCommand *hibernate_sequence{nullptr}; // deep-sleep trigger only, after power_off_sequence
  164. uint8_t hibernate_sequence_len{0};
  165. };
  166. } // namespace esphome::zepd