emit_descriptor.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. #pragma once
  2. // Prototype: emit a candidate panel_*.h EpdPanelDescriptor by *observing* a
  3. // real vendor GxEPD2 driver run, instead of hand-transcribing its C++. This
  4. // closes the hand-derivation gap for the straight-line parts of a descriptor
  5. // (init / window / refresh command sequences + the plane-write command) by
  6. // phase-segmenting the captured event stream at the public-API boundary - no
  7. // edits to the vendor source.
  8. //
  9. // Staging (the trick that separates the phases without instrumenting the
  10. // vendor's private methods):
  11. // writeScreenBuffer() -> lazy _InitDisplay() + a plane-write burst (clear).
  12. // Everything before that burst is the INIT sequence;
  13. // the command right before it is the PLANE-WRITE cmd.
  14. // writeImage(pattern) -> init already done, so this is just the WINDOW set
  15. // (commands before the pattern burst) + plane write.
  16. // refresh() -> re-sets the window then triggers; dropping the
  17. // duplicate window command leaves the REFRESH seq.
  18. //
  19. // Out of scope here (comes from the diag Pattern A/B passes, not one capture):
  20. // bits_per_pixel / palette / plane_invert / line_order. Those are emitted
  21. // as TODO placeholders so the diff against the hand file is about segmentation.
  22. #include <cstdint>
  23. #include <cstdio>
  24. #include <cstdlib>
  25. #include <functional>
  26. #include <set>
  27. #include <string>
  28. #include <type_traits>
  29. #include <utility>
  30. #include <vector>
  31. #include "spy/Arduino.h"
  32. #include "spy/SPI.h"
  33. #include "trace.h"
  34. namespace emit {
  35. // A data burst at least this long is a pixel-plane write, not config data.
  36. constexpr size_t PLANE_BURST_MIN = 256;
  37. struct Cmd {
  38. uint8_t cmd;
  39. std::vector<uint8_t> data;
  40. bool wait_busy = false;
  41. unsigned long delay_after = 0;
  42. };
  43. // Expose GxEPD2_EPD's protected busy level without parsing (same trick as
  44. // run_harness.h's Peek).
  45. template<typename Driver> struct Peek : Driver {
  46. using Driver::Driver;
  47. int busy_level() const { return this->_busy_level; }
  48. };
  49. // Group a slice of the event stream into EpdCommand-shaped records. Events
  50. // before the first SPI *command* (the reset pulse + its delays) are skipped -
  51. // the engine owns the hardware reset, so it never belongs in a descriptor.
  52. inline std::vector<Cmd> group(size_t lo, size_t hi) {
  53. std::vector<Cmd> cmds;
  54. bool started = false;
  55. for (size_t i = lo; i < hi; i++) {
  56. const auto &e = Trace::events[i];
  57. switch (e.kind) {
  58. case EventKind::SPI_TRANSFER:
  59. if (e.is_command) {
  60. cmds.push_back(Cmd{e.byte});
  61. started = true;
  62. } else if (started) {
  63. cmds.back().data.push_back(e.byte);
  64. }
  65. break;
  66. case EventKind::DELAY:
  67. if (started)
  68. cmds.back().delay_after += e.ms;
  69. break;
  70. case EventKind::DIGITAL_READ: // the only reads are busy-pin polls
  71. if (started) {
  72. cmds.back().wait_busy = true;
  73. // The delay(1) GxEPD2_EPD::_waitWhileBusy() runs inside its poll
  74. // loop lands right before this read and is part of the busy-wait,
  75. // not an inter-command delay - our engine owns busy-waiting, so
  76. // fold it in rather than emit it. (This also absorbs a vendor's own
  77. // tiny settle delay right before a refresh trigger, e.g. 290c's
  78. // _refresh delay(1); immaterial, the panel asserts busy regardless.)
  79. cmds.back().delay_after = 0;
  80. }
  81. break;
  82. default:
  83. break; // DIGITAL_WRITE = reset/CS/DC toggles: engine's job, not data
  84. }
  85. }
  86. return cmds;
  87. }
  88. // Index of the first command whose data burst marks a plane write, or np
  89. // (cmds.size()) if there's none in this slice.
  90. inline size_t plane_index(const std::vector<Cmd> &cmds) {
  91. for (size_t i = 0; i < cmds.size(); i++)
  92. if (cmds[i].data.size() >= PLANE_BURST_MIN)
  93. return i;
  94. return cmds.size();
  95. }
  96. inline void print_data_array(const char *macro, const char *suffix, const std::vector<uint8_t> &d) {
  97. printf("inline const uint8_t %s_%s[] = {", macro, suffix);
  98. for (size_t i = 0; i < d.size(); i++)
  99. printf("%s0x%02X", i ? ", " : "", d[i]);
  100. printf("};\n");
  101. }
  102. // Print one EpdCommand{} initializer line, with the optional fields only when
  103. // they're non-default (matches the hand files' style).
  104. inline void print_cmd(const char *macro, const char *suffix, const Cmd &c) {
  105. if (c.data.empty() && !c.wait_busy && c.delay_after == 0) {
  106. printf(" {0x%02X, nullptr, 0},\n", c.cmd);
  107. return;
  108. }
  109. const char *dataref = c.data.empty() ? "nullptr" : nullptr;
  110. if (dataref)
  111. printf(" {0x%02X, nullptr, 0", c.cmd);
  112. else
  113. printf(" {0x%02X, %s_%s, %u", c.cmd, macro, suffix, (unsigned) c.data.size());
  114. if (c.wait_busy || c.delay_after)
  115. printf(", /*delay_before_ms=*/0, /*delay_after_ms=*/%lu, /*wait_busy_after=*/%s", c.delay_after,
  116. c.wait_busy ? "true" : "false");
  117. printf("},\n");
  118. }
  119. // First data byte of the burst written right after command `op`, or -1.
  120. inline int burst_byte_after(const std::vector<Cmd> &cmds, uint8_t op) {
  121. for (const auto &c : cmds)
  122. if (c.cmd == op && c.data.size() >= PLANE_BURST_MIN)
  123. return c.data[0];
  124. return -1;
  125. }
  126. // The full data burst written right after command `op`, or empty.
  127. inline std::vector<uint8_t> burst_data_after(const std::vector<Cmd> &cmds, uint8_t op) {
  128. for (const auto &c : cmds)
  129. if (c.cmd == op && c.data.size() >= PLANE_BURST_MIN)
  130. return c.data;
  131. return {};
  132. }
  133. // Detects the two-arg writeScreenBuffer(black_value, color_value) that colour
  134. // (plane_count > 1) drivers have and mono drivers don't.
  135. template<typename T, typename = void> struct has_wsb2 : std::false_type {};
  136. template<typename T>
  137. struct has_wsb2<T, std::void_t<decltype(std::declval<T &>().writeScreenBuffer(uint8_t{}, uint8_t{}))>>
  138. : std::true_type {};
  139. struct PlaneInvert {
  140. bool inv0 = false, inv1 = false;
  141. bool plane1_valid = false;
  142. bool is_1bpp = false; // white fill => an all-bits-same wire byte (not packed)
  143. int w0_white = -1, w1_red = -1;
  144. };
  145. // A packed panel's depth / palette / blank code, resolved by observation for
  146. // the panel families the emitter knows how to probe (currently 4-colour, via
  147. // emit_palette_4c.h). `emit_descriptor_impl` calls a resolver of this shape
  148. // only for a packed panel (the plane-invert probe said white fill isn't an
  149. // all-bits wire byte); a null or unresolved result keeps the old TODO fields,
  150. // so an unknown packed family still emits a reviewable placeholder rather than
  151. // a wrong palette.
  152. struct PackedResolved {
  153. bool resolved = false; // false => emit TODO placeholders
  154. int bits_per_pixel = 2; // 2 (4-colour) or 4 (7-colour)
  155. const char *palette_expr = "nullptr"; // e.g. "GXEPD2_PALETTE_4C"
  156. const char *palette_len_expr = "0"; // e.g. "GXEPD2_PALETTE_4C_LEN"
  157. int blank_code = 0; // per-pixel clear code (observed white)
  158. int color_count = -1; // actual distinct colours probed; -1 => assume 1<<bits_per_pixel
  159. std::string note; // appended as a comment (observed table, or why unresolved)
  160. // Set by make_resolver_bw_packed (below): a still-GxEPD2_BW-wrapped (binary)
  161. // driver that nonetheless packs >1 bit/pixel on the wire internally, with no
  162. // colour wrapper to drive a la emit_palette_4c.h/7c.h - e.g. GxEPD2_583's
  163. // _send8pixel. Only two codes are ever observed (black_code, blank_code=
  164. // white code), so the emitter prints a local 2-entry palette array instead
  165. // of referencing a shared GXEPD2_PALETTE_* constant.
  166. bool two_code_bw = false;
  167. int black_code = -1;
  168. };
  169. using PackedResolverFn = std::function<PackedResolved(uint8_t clear_fill, const PlaneInvert &pv)>;
  170. // A generous, class-aware default for busy_timeout_ms. The timeout is only a
  171. // diagnostic ceiling - the engine hard-aborts a busy-wait that runs past it and
  172. // logs a failure, where GxEPD2 itself just keeps polling - so erring long costs
  173. // nothing on a healthy panel and the exact number is meant to be dev-tuned. The
  174. // vendor full_refresh_time is a soft estimate, so we take 2x it, then clamp up
  175. // to a per-depth floor so a panel that under-reports (or whose colour pass is
  176. // genuinely slow) still gets room: mono/3-colour fail fast, 4-colour and
  177. // 7-colour (ACeP) get progressively more headroom.
  178. inline uint32_t default_busy_timeout_ms(unsigned full_refresh_time, int bits_per_pixel) {
  179. uint32_t headroom = static_cast<uint32_t>(full_refresh_time) * 2;
  180. uint32_t floor = bits_per_pixel >= 4 ? 60000u : bits_per_pixel == 2 ? 45000u : 15000u;
  181. return headroom > floor ? headroom : floor;
  182. }
  183. // plane_invert by capture (see probe_plane_invert.h for the rationale): drive
  184. // known full-screen fills and read each plane's wire byte, reconciling against
  185. // the engine's fixed buffer convention (plane0 bit=white, plane1 bit=red). A
  186. // white-fill plane0 wire that isn't 0x00/0xFF means a packed (>1bpp) panel,
  187. // where plane_invert is unused and bits_per_pixel/palette come from the diag
  188. // pass instead.
  189. template<typename Driver> PlaneInvert probe_plane_invert_values(uint8_t p0cmd, uint8_t p1cmd, size_t plane_count) {
  190. constexpr int16_t CS = 10, DC = 11, RST = 12, BUSY = 13;
  191. Trace::set_dc_pin(DC);
  192. Peek<Driver> d(CS, DC, RST, BUSY);
  193. Trace::last_busy_release_value = d.busy_level() == HIGH ? LOW : HIGH;
  194. d.selectSPI(SPI, SPISettings());
  195. d.init(0);
  196. // plane_bytes sized for the 1bpp hypothesis - same assumption
  197. // emit_descriptor_impl's own main capture makes (see its `black`/`color`
  198. // buffers). A genuinely packed driver still re-packs this buffer through
  199. // its real plane-write path (e.g. GxEPD2_583's _send8pixel), so the wire
  200. // byte it reports is the true packed code, not a false 1bpp reading.
  201. const uint32_t plane_bytes = (static_cast<uint32_t>(Driver::WIDTH) * Driver::HEIGHT) / 8;
  202. auto *fillbuf = new uint8_t[plane_bytes];
  203. PlaneInvert r;
  204. // Drive the SAME call path (writeImage) that produced p0cmd/p1cmd, not
  205. // writeScreenBuffer: some drivers (e.g. GxEPD2_270, "current" buffer 0x13
  206. // vs image-plane 0x15) use a different command for the screen-buffer clear
  207. // than for writeImage's own plane write. Probing via writeScreenBuffer then
  208. // searches the trace for a command (p0cmd) that call never emits, silently
  209. // returning "not found" (-1) and misreporting a true 1bpp panel as packed.
  210. auto cap0 = [&](uint8_t value) -> int {
  211. memset(fillbuf, value, plane_bytes);
  212. Trace::start_event_record();
  213. d.writeImage(fillbuf, fillbuf, 0, 0, Driver::WIDTH, Driver::HEIGHT);
  214. Trace::stop_event_record();
  215. return burst_byte_after(group(0, Trace::events.size()), p0cmd);
  216. };
  217. r.w0_white = cap0(0xFF);
  218. delete[] fillbuf;
  219. r.is_1bpp = (r.w0_white == 0x00 || r.w0_white == 0xFF);
  220. r.inv0 = (r.w0_white == 0x00); // engine plane0 bit=white; white wire 0x00 => invert
  221. if (plane_count > 1) {
  222. if constexpr (has_wsb2<Driver>::value) {
  223. Trace::start_event_record();
  224. d.writeScreenBuffer((uint8_t) 0xFF, (uint8_t) 0x00); // all-red fill
  225. Trace::stop_event_record();
  226. r.w1_red = burst_byte_after(group(0, Trace::events.size()), p1cmd);
  227. r.plane1_valid = (r.w1_red == 0x00 || r.w1_red == 0xFF);
  228. r.inv1 = (r.w1_red == 0x00); // engine plane1 bit=red; red wire 0x00 => invert
  229. }
  230. }
  231. return r;
  232. }
  233. // Generic packed resolver for a driver with NO colour wrapper to drive (unlike
  234. // emit_palette_4c.h/7c.h, which observe a GxEPD2_4C/7C wrapper's named-colour
  235. // fills) - the gap GxEPD2_583 (UC8159c) exposed: it's wrapped GxEPD2_BW
  236. // (binary), but `_send8pixel` expands each 1bpp buffer byte into a wider wire
  237. // format using only two of its codes. Drives the plain Driver's own
  238. // writeImage(black,color,...) - the SAME call the main capture already makes
  239. // - with a uniform all-white (0xFF) and all-black (0x00) 1bpp buffer. If both
  240. // wire bursts are internally uniform (one repeated byte - true for a per-bit
  241. // code lookup with no cross-pixel dependency) and the byte count divides the
  242. // pixel count evenly, that ratio directly gives bits_per_pixel; the MSB field
  243. // of each uniform byte gives that colour's code. A driver whose packing
  244. // depends on more than one bit at a time (dithering, error diffusion) fails
  245. // the uniformity check and stays an honest TODO rather than a wrong guess.
  246. template<typename Driver> PackedResolverFn make_resolver_bw_packed(uint8_t p0cmd) {
  247. return [p0cmd](uint8_t /*clear_fill*/, const PlaneInvert & /*pv*/) -> PackedResolved {
  248. PackedResolved r;
  249. auto fail = [&](const char *why) {
  250. r.resolved = false;
  251. r.note = why;
  252. return r;
  253. };
  254. constexpr int16_t CS = 10, DC = 11, RST = 12, BUSY = 13;
  255. Trace::set_dc_pin(DC);
  256. Peek<Driver> d(CS, DC, RST, BUSY);
  257. Trace::last_busy_release_value = d.busy_level() == HIGH ? LOW : HIGH;
  258. d.selectSPI(SPI, SPISettings());
  259. d.init(0);
  260. const uint16_t W = Driver::WIDTH, H = Driver::HEIGHT;
  261. const uint32_t plane_bytes = (uint32_t) W * H / 8; // 1bpp-sized source buffer
  262. auto *buf = new uint8_t[plane_bytes];
  263. auto burst_for = [&](uint8_t value) -> std::vector<uint8_t> {
  264. memset(buf, value, plane_bytes);
  265. Trace::start_event_record();
  266. d.writeImage(buf, buf, 0, 0, W, H);
  267. Trace::stop_event_record();
  268. return burst_data_after(group(0, Trace::events.size()), p0cmd);
  269. };
  270. const std::vector<uint8_t> white = burst_for(0xFF); // engine convention: bit=1 => white
  271. const std::vector<uint8_t> black = burst_for(0x00);
  272. delete[] buf;
  273. if (white.empty() || black.empty())
  274. return fail("bw-packed probe: no plane-write burst found");
  275. if (white.size() != black.size())
  276. return fail("bw-packed probe: white/black burst lengths differ");
  277. const uint32_t total_pixels = (uint32_t) W * H;
  278. if (white.size() == 0 || total_pixels % white.size() != 0)
  279. return fail("bw-packed probe: pixel count not a multiple of the wire-byte count");
  280. const int ppb = (int) (total_pixels / white.size()); // pixels per wire byte
  281. if (ppb <= 0 || ppb > 8 || (8 % ppb) != 0)
  282. return fail("bw-packed probe: implied pixels/byte doesn't divide 8 evenly");
  283. const int bpp = 8 / ppb;
  284. const uint8_t wbyte = white[0], bbyte = black[0];
  285. for (uint8_t v : white)
  286. if (v != wbyte)
  287. return fail("bw-packed probe: white-fill wire byte not uniform (packing depends on more than one bit)");
  288. for (uint8_t v : black)
  289. if (v != bbyte)
  290. return fail("bw-packed probe: black-fill wire byte not uniform (packing depends on more than one bit)");
  291. if (wbyte == bbyte)
  292. return fail("bw-packed probe: white and black fills produced the same wire byte");
  293. const uint8_t mask = (uint8_t) ((1u << bpp) - 1);
  294. const uint8_t white_code = (wbyte >> (bpp * (ppb - 1))) & mask; // MSB field (every field equal, uniform byte)
  295. const uint8_t black_code = (bbyte >> (bpp * (ppb - 1))) & mask;
  296. r.resolved = true;
  297. r.bits_per_pixel = bpp;
  298. r.color_count = 2;
  299. r.two_code_bw = true;
  300. r.blank_code = white_code;
  301. r.black_code = black_code;
  302. char note[96];
  303. snprintf(note, sizeof(note), "derived from raw 1bpp fills, no colour wrapper: black=0x%02X white=0x%02X, %d px/wire-byte",
  304. black_code, white_code, ppb);
  305. r.note = note;
  306. return r;
  307. };
  308. }
  309. // Captures powerOff() or hibernate() as its own standalone command sequence -
  310. // NOT folded into the refresh_seq phase-segmentation above, and NOT captured
  311. // from the same live session as each other. Every GxEPD2 driver's powerOff()
  312. // guards on `_power_is_on` (set true by the refresh() a normal update ends
  313. // with), and hibernate() calls powerOff() again internally - so capturing
  314. // both from one session would have the second call's powerOff() portion
  315. // silently emit nothing (already off from the first). Each call here gets its
  316. // own fresh instance, replayed through the exact init/clear/write/refresh
  317. // setup the main capture above uses, so `call` always runs with
  318. // _power_is_on true, matching a real update-then-power-down.
  319. template<typename Driver, typename MethodCall> std::vector<Cmd> capture_power_sequence(MethodCall call) {
  320. constexpr int16_t CS = 10, DC = 11, RST = 12, BUSY = 13;
  321. Trace::set_dc_pin(DC);
  322. Peek<Driver> d(CS, DC, RST, BUSY);
  323. Trace::last_busy_release_value = d.busy_level() == HIGH ? LOW : HIGH;
  324. d.selectSPI(SPI, SPISettings());
  325. d.init(0);
  326. const uint32_t plane_bytes = (uint32_t) Driver::WIDTH * Driver::HEIGHT / 8;
  327. auto *buf = new uint8_t[plane_bytes];
  328. memset(buf, 0xA5, plane_bytes);
  329. d.writeScreenBuffer();
  330. d.writeImage(buf, buf, 0, 0, Driver::WIDTH, Driver::HEIGHT);
  331. d.refresh(false);
  332. delete[] buf;
  333. Trace::start_event_record();
  334. call(d);
  335. Trace::stop_event_record();
  336. return group(0, Trace::events.size());
  337. }
  338. // (power_off_sequence, hibernate_sequence) for Driver, each captured via
  339. // capture_power_sequence above. hibernate_sequence is hibernate()'s tail
  340. // PAST whatever it shares with powerOff() - every driver observed so far
  341. // calls powerOff() internally then adds exactly one more command (the
  342. // deep-sleep trigger) - since power_off_sequence already has that shared
  343. // part and CycleEngine runs the two back to back (see epd_command.h); a
  344. // prefix mismatch (seen nowhere so far) falls back to the full hibernate
  345. // capture rather than guess.
  346. template<typename Driver> std::pair<std::vector<Cmd>, std::vector<Cmd>> capture_power_off_and_hibernate() {
  347. std::vector<Cmd> power_off_seq = capture_power_sequence<Driver>([](Peek<Driver> &d) { d.powerOff(); });
  348. std::vector<Cmd> hibernate_full_seq = capture_power_sequence<Driver>([](Peek<Driver> &d) { d.hibernate(); });
  349. std::vector<Cmd> hibernate_seq;
  350. if (hibernate_full_seq.size() > power_off_seq.size()) {
  351. bool prefix_matches = true;
  352. for (size_t i = 0; i < power_off_seq.size() && prefix_matches; i++)
  353. if (hibernate_full_seq[i].cmd != power_off_seq[i].cmd || hibernate_full_seq[i].data != power_off_seq[i].data)
  354. prefix_matches = false;
  355. if (prefix_matches)
  356. hibernate_seq.assign(hibernate_full_seq.begin() + power_off_seq.size(), hibernate_full_seq.end());
  357. }
  358. if (hibernate_seq.empty())
  359. hibernate_seq = hibernate_full_seq;
  360. return {power_off_seq, hibernate_seq};
  361. }
  362. // Prints just the POWER_OFF/HIBERNATE arrays + EpdCommand tables (plus the
  363. // descriptor-field lines to paste) for Driver - for splicing
  364. // power_off_sequence/hibernate_sequence into an EXISTING, hand-maintained
  365. // panel_*.h (one shipped before these fields existed) without regenerating,
  366. // and losing, everything else already in it. tools/capture.py --power.
  367. template<typename Driver> void print_power_sequences(const char *macro) {
  368. auto captured = capture_power_off_and_hibernate<Driver>();
  369. const std::vector<Cmd> &power_off_seq = captured.first;
  370. const std::vector<Cmd> &hibernate_seq = captured.second;
  371. auto emit_seq = [&](const char *seqsuffix, const std::vector<Cmd> &cmds) {
  372. for (size_t i = 0; i < cmds.size(); i++)
  373. if (!cmds[i].data.empty()) {
  374. char suf[64];
  375. snprintf(suf, sizeof(suf), "%s_%zu_%02X", seqsuffix, i, cmds[i].cmd);
  376. print_data_array(macro, suf, cmds[i].data);
  377. }
  378. printf("\ninline const EpdCommand %s_%s[] = {\n", macro, seqsuffix);
  379. for (size_t i = 0; i < cmds.size(); i++) {
  380. char suf[64];
  381. snprintf(suf, sizeof(suf), "%s_%zu_%02X", seqsuffix, i, cmds[i].cmd);
  382. print_cmd(macro, suf, cmds[i]);
  383. }
  384. printf("};\n");
  385. };
  386. if (!power_off_seq.empty()) {
  387. emit_seq("POWER_OFF", power_off_seq);
  388. printf("// splice into the descriptor:\n");
  389. printf("// .power_off_sequence = %s_POWER_OFF,\n", macro);
  390. printf("// .power_off_sequence_len = sizeof(%s_POWER_OFF) / sizeof(%s_POWER_OFF[0]),\n", macro, macro);
  391. } else {
  392. printf("// powerOff(): empty capture - this driver's full refresh already leaves\n"
  393. "// _power_is_on false (its _Update_Full() powers itself down), so powerOff()\n"
  394. "// has nothing left to do. No power_off_sequence to add.\n");
  395. }
  396. printf("\n");
  397. if (!hibernate_seq.empty()) {
  398. emit_seq("HIBERNATE", hibernate_seq);
  399. printf("// splice into the descriptor:\n");
  400. printf("// .hibernate_sequence = %s_HIBERNATE,\n", macro);
  401. printf("// .hibernate_sequence_len = sizeof(%s_HIBERNATE) / sizeof(%s_HIBERNATE[0]),\n", macro, macro);
  402. } else {
  403. printf("// hibernate(): empty capture (unexpected - every driver seen so far adds at\n"
  404. "// least the deep-sleep trigger when rst is wired). Review by hand.\n");
  405. }
  406. }
  407. template<typename Driver>
  408. void emit_descriptor_impl(const char *class_name, const char *macro, const PackedResolverFn &resolve_packed) {
  409. constexpr int16_t CS = 10, DC = 11, RST = 12, BUSY = 13;
  410. Trace::set_dc_pin(DC);
  411. Peek<Driver> driver(CS, DC, RST, BUSY);
  412. const int bl = driver.busy_level();
  413. Trace::last_busy_release_value = bl == HIGH ? LOW : HIGH;
  414. driver.selectSPI(SPI, SPISettings());
  415. driver.init(0);
  416. const uint32_t plane_bytes = (static_cast<uint32_t>(Driver::WIDTH) * Driver::HEIGHT) / 8;
  417. auto *black = new uint8_t[plane_bytes];
  418. auto *color = new uint8_t[plane_bytes];
  419. for (uint32_t i = 0; i < plane_bytes; i++) {
  420. black[i] = 0xA5;
  421. color[i] = 0xA5;
  422. }
  423. Trace::start_event_record();
  424. const size_t s0 = 0;
  425. driver.writeScreenBuffer(); // init + clear burst
  426. const size_t s1 = Trace::events.size();
  427. driver.writeImage(black, color, 0, 0, Driver::WIDTH, Driver::HEIGHT); // window + plane
  428. const size_t s2 = Trace::events.size();
  429. driver.refresh(false); // window-again + trigger
  430. const size_t s3 = Trace::events.size();
  431. Trace::stop_event_record();
  432. delete[] black;
  433. delete[] color;
  434. // --- segment ---
  435. auto init_stage = group(s0, s1);
  436. auto image_stage = group(s1, s2);
  437. auto refresh_stage = group(s2, s3);
  438. auto cmds_equal = [](const std::vector<Cmd> &a, const std::vector<Cmd> &b) {
  439. if (a.size() != b.size())
  440. return false;
  441. for (size_t i = 0; i < a.size(); i++)
  442. if (a[i].cmd != b[i].cmd || a[i].data != b[i].data)
  443. return false;
  444. return true;
  445. };
  446. // Every pixel-plane burst in the image call. One => single-plane panel;
  447. // N => N planes (two colour planes sharing a region, or two physical
  448. // regions - told apart below by whether their window-sets match).
  449. std::vector<size_t> bursts;
  450. for (size_t i = 0; i < image_stage.size(); i++)
  451. if (image_stage[i].data.size() >= PLANE_BURST_MIN)
  452. bursts.push_back(i);
  453. const size_t nplanes = bursts.empty() ? 1 : bursts.size();
  454. if (getenv("EMIT_DEBUG")) {
  455. fprintf(stderr, "image_stage %zu cmds; bursts:", image_stage.size());
  456. for (auto b : bursts)
  457. fprintf(stderr, " [%zu]=0x%02X", b, image_stage[b].cmd);
  458. fprintf(stderr, "\n");
  459. for (size_t i = 0; i < image_stage.size(); i++)
  460. fprintf(stderr, " [%zu] 0x%02X data=%zu\n", i, image_stage[i].cmd, image_stage[i].data.size());
  461. }
  462. // pre_block(k) = commands after burst k-1 (or the start) up to, but not
  463. // including, burst k's own plane-write command - i.e. the window set for
  464. // plane k, possibly with the lazily-run _InitDisplay in front for k==0.
  465. auto pre_block = [&](size_t k) {
  466. size_t lo = (k == 0) ? 0 : bursts[k - 1] + 1;
  467. return std::vector<Cmd>(image_stage.begin() + lo, image_stage.begin() + bursts[k]);
  468. };
  469. std::vector<Cmd> init_seq, window0, window1;
  470. std::vector<uint8_t> plane_cmds;
  471. bool per_plane = false;
  472. bool dual_region = false; // two physical regions (per-region select + window)
  473. int plane_width_val = 0; // decoded per-region width (dual_region only)
  474. uint32_t region0_burst = 0; // region 0's image burst length, for line-order detection
  475. if (bursts.size() >= 2) {
  476. // Multi-plane. Two observed layouts:
  477. // interleaved: [init][W0][burst0][W1][burst1] - each plane's window
  478. // sits between its burst and the previous one (dual region);
  479. // batched: [init][W][W][burst0][burst1] - all windows precede all
  480. // bursts (SSD1680 3-colour: writeImage sets the black and
  481. // red RAM windows, then bursts both planes).
  482. for (size_t k = 0; k < bursts.size(); k++)
  483. plane_cmds.push_back(image_stage[bursts[k]].cmd);
  484. const size_t n = bursts.size();
  485. std::vector<Cmd> pre = pre_block(0);
  486. std::vector<std::vector<Cmd>> windows;
  487. bool interleaved = false;
  488. for (size_t k = 1; k < n; k++)
  489. if (bursts[k] - bursts[k - 1] > 1) {
  490. interleaved = true;
  491. break;
  492. }
  493. if (interleaved) {
  494. std::vector<Cmd> gap0(image_stage.begin() + bursts[0] + 1, image_stage.begin() + bursts[1]);
  495. const size_t wl = gap0.size();
  496. if (wl <= pre.size()) {
  497. windows.push_back(std::vector<Cmd>(pre.end() - wl, pre.end()));
  498. init_seq.assign(pre.begin(), pre.end() - wl);
  499. } else {
  500. windows.push_back({});
  501. init_seq = pre;
  502. }
  503. for (size_t k = 1; k < n; k++)
  504. windows.push_back(std::vector<Cmd>(image_stage.begin() + bursts[k - 1] + 1, image_stage.begin() + bursts[k]));
  505. } else {
  506. // Batched: pre = init + n copies of the window. Peel the smallest block
  507. // that repeats n times at the tail; the remainder is init.
  508. size_t L = 0;
  509. for (size_t cand = 1; cand * n <= pre.size(); cand++) {
  510. bool ok = true;
  511. for (size_t b = 0; b < n && ok; b++)
  512. for (size_t j = 0; j < cand && ok; j++) {
  513. const Cmd &a = pre[pre.size() - cand * n + b * cand + j];
  514. const Cmd &c = pre[pre.size() - cand + j];
  515. if (a.cmd != c.cmd || a.data != c.data)
  516. ok = false;
  517. }
  518. if (ok) {
  519. L = cand;
  520. break;
  521. }
  522. }
  523. if (L > 0) {
  524. for (size_t b = 0; b < n; b++)
  525. windows.push_back(std::vector<Cmd>(pre.end() - L * (n - b), pre.end() - L * (n - b - 1)));
  526. init_seq.assign(pre.begin(), pre.end() - L * n);
  527. } else {
  528. windows.push_back({});
  529. init_seq = pre;
  530. }
  531. }
  532. window0 = windows.empty() ? std::vector<Cmd>{} : windows[0];
  533. window1 = windows.size() > 1 ? windows[1] : std::vector<Cmd>{};
  534. for (size_t k = 1; k < windows.size(); k++)
  535. if (!cmds_equal(windows[0], windows[k]))
  536. per_plane = true; // per-plane windows differ => separate regions
  537. // Dual-region refinement (e.g. GDEY0579F51 / HX8717: a two-chip panel whose
  538. // "planes" are two physical half-width regions, each gated by a region-
  539. // select command before its own window + burst). The generic interleaved
  540. // path above already recovers the per-region windows and plane commands;
  541. // three things it can't are filled here.
  542. if (interleaved && per_plane) {
  543. dual_region = true;
  544. std::set<uint8_t> wops; // region-select + window-set opcodes
  545. for (const auto &c : window0)
  546. wops.insert(c.cmd);
  547. for (const auto &c : window1)
  548. wops.insert(c.cmd);
  549. // (1) INIT. A dual-region panel runs its whole _InitDisplay during the
  550. // earlier writeScreenBuffer clear, and that init REUSES the region-select
  551. // opcode (select master/slave for each region's PSR), so the single-plane
  552. // "cut at the first window opcode" rule would cut at the very first init
  553. // command. Instead take init from the clear stage and drop only the
  554. // CONTIGUOUS trailing run of window-set commands before the first clear
  555. // burst (the both-/first-region window-sets); the select/PSR pairs earlier
  556. // in init survive because a non-window command (BTST, power-on, ...)
  557. // separates them from that trailing run.
  558. const size_t cb = plane_index(init_stage); // first clear burst
  559. size_t cut = cb;
  560. while (cut > 0 && wops.count(init_stage[cut - 1].cmd))
  561. cut--;
  562. init_seq.assign(init_stage.begin(), init_stage.begin() + cut);
  563. // (2) plane_width: each region addresses its own half. Decode it from the
  564. // window-set command's payload - first two big-endian 16-bit fields are
  565. // x and x_end, as every window encoder observed so far lays them out.
  566. for (const auto &c : window0)
  567. if (c.data.size() >= 4) {
  568. int x = (c.data[0] << 8) | c.data[1];
  569. int xe = (c.data[2] << 8) | c.data[3];
  570. plane_width_val = xe - x + 1;
  571. break;
  572. }
  573. // (3) line doubling: region 0's burst length, compared in the emit stage
  574. // against plane_width*height*bpp/8 to flag PAIRED_EDGES.
  575. region0_burst = (uint32_t) image_stage[bursts[0]].data.size();
  576. }
  577. } else {
  578. // Single plane: window = the image call's pre-burst block; init comes
  579. // from the writeScreenBuffer stage, cut at the first window-set command
  580. // (SSD16xx sets the window there too - that belongs in window_sequence).
  581. const size_t wp = plane_index(image_stage);
  582. window0.assign(image_stage.begin(), image_stage.begin() + wp);
  583. std::set<uint8_t> wops;
  584. for (const auto &c : window0)
  585. wops.insert(c.cmd);
  586. const size_t ip = plane_index(init_stage);
  587. size_t cut = ip;
  588. for (size_t i = 0; i < ip; i++)
  589. if (wops.count(init_stage[i].cmd)) {
  590. cut = i;
  591. break;
  592. }
  593. init_seq.assign(init_stage.begin(), init_stage.begin() + cut);
  594. plane_cmds.push_back(wp < image_stage.size() ? image_stage[wp].cmd : 0x00);
  595. }
  596. // blank/clear fill byte, observed from the writeScreenBuffer clear burst.
  597. const size_t cip = plane_index(init_stage);
  598. const uint8_t clear_fill = (cip < init_stage.size() && !init_stage[cip].data.empty()) ? init_stage[cip].data[0] : 0x00;
  599. // plane_invert + 1bpp/packed detection, derived by the fill-capture probe.
  600. const uint8_t p0cmd = plane_cmds.empty() ? 0x00 : plane_cmds[0];
  601. const uint8_t p1cmd = plane_cmds.size() > 1 ? plane_cmds[1] : 0x00;
  602. const PlaneInvert pv = probe_plane_invert_values<Driver>(p0cmd, p1cmd, nplanes);
  603. // Packed-family resolution (4-colour palette etc.), run once here so both the
  604. // busy-timeout class and the packed fields below use the same answer. 1bpp
  605. // panels skip it. A specific colour-wrapper resolver (4-colour/7-colour,
  606. // passed in by emit_descriptor_4c/_7c) takes priority; absent one (the
  607. // default emit_descriptor<Driver> entry - a plain, possibly still-mono-
  608. // wrapped driver), fall back to the generic no-wrapper probe, which catches
  609. // a GxEPD2_BW driver that packs internally (see make_resolver_bw_packed).
  610. // An unresolved packed panel (neither resolver recognised it) is treated as
  611. // the slow class for the timeout floor.
  612. const PackedResolverFn effective_resolver = resolve_packed ? resolve_packed : make_resolver_bw_packed<Driver>(p0cmd);
  613. const PackedResolved pr = (!pv.is_1bpp && effective_resolver) ? effective_resolver(clear_fill, pv) : PackedResolved{};
  614. const int depth_bpp = pv.is_1bpp ? 1 : (pr.resolved ? pr.bits_per_pixel : 4);
  615. // Refresh = post-write commands after the LAST burst (e.g. UC8179's 0x92),
  616. // then the refresh call minus any window-set it repeats.
  617. std::set<uint8_t> all_wops;
  618. for (const auto &c : window0)
  619. all_wops.insert(c.cmd);
  620. for (const auto &c : window1)
  621. all_wops.insert(c.cmd);
  622. std::vector<Cmd> refresh_seq;
  623. if (!bursts.empty())
  624. for (size_t i = bursts.back() + 1; i < image_stage.size() && image_stage[i].data.size() < PLANE_BURST_MIN; i++)
  625. refresh_seq.push_back(image_stage[i]);
  626. // A shared-region panel re-sets its one window in refresh() and we drop that
  627. // duplicate; a dual-region panel's refresh instead sets a DISTINCT both-region
  628. // window (its own select payload), which is part of the refresh - keep it.
  629. for (const auto &c : refresh_stage)
  630. if (dual_region || !all_wops.count(c.cmd))
  631. refresh_seq.push_back(c);
  632. // The refresh ends at its trigger (the command we busy-wait on). A dual-region
  633. // driver re-sets the window AFTER the trigger to prime its own next writeImage;
  634. // our engine always sets its own window per cycle, so that trailing window is
  635. // dead in replay - trim anything past the last busy-wait.
  636. if (dual_region) {
  637. size_t last_wait = refresh_seq.size();
  638. for (size_t i = 0; i < refresh_seq.size(); i++)
  639. if (refresh_seq[i].wait_busy)
  640. last_wait = i;
  641. if (last_wait < refresh_seq.size())
  642. refresh_seq.resize(last_wait + 1);
  643. }
  644. // powerOff()/hibernate(), each its own fresh-session capture - see
  645. // capture_power_off_and_hibernate's doc comment.
  646. auto power_captured = capture_power_off_and_hibernate<Driver>();
  647. std::vector<Cmd> &power_off_seq = power_captured.first;
  648. std::vector<Cmd> &hibernate_seq = power_captured.second;
  649. // --- emit ---
  650. printf("// GENERATED by emit_descriptor from %s - candidate.\n", class_name);
  651. printf("// Command sequences, plane layout and plane_invert are observed here;\n");
  652. printf("// for a packed panel bits_per_pixel/palette/blank_code are probe-filled\n");
  653. printf("// when the colour family is recognised (4-colour/7-colour), else left TODO. A\n");
  654. printf("// dual-region panel also gets plane_width and a candidate line_order; the\n");
  655. printf("// exact PAIRED_EDGES row order still wants diag/driver confirmation.\n");
  656. printf("#pragma once\n#include \"epd_command.h\"\n\nnamespace esphome::gxepd2 {\n\n");
  657. if (pr.resolved && pr.two_code_bw) {
  658. printf("// 2-code packed mono (see .bits_per_pixel below): derived purely by observing\n");
  659. printf("// raw white/black fills through the plane-write path, no colour wrapper to\n");
  660. printf("// drive (see make_resolver_bw_packed in emit_descriptor.h).\n");
  661. printf("inline const EpdPaletteEntry %s_PALETTE[] = {\n", macro);
  662. printf(" {0x00, 0x00, 0x00, 0x%02X}, // black\n", pr.black_code & 0xFF);
  663. printf(" {0xFF, 0xFF, 0xFF, 0x%02X}, // white\n", pr.blank_code & 0xFF);
  664. printf("};\n");
  665. printf("inline const uint8_t %s_PALETTE_LEN = sizeof(%s_PALETTE) / sizeof(%s_PALETTE[0]);\n\n", macro, macro, macro);
  666. }
  667. auto emit_seq = [&](const char *seqsuffix, std::vector<Cmd> &cmds) {
  668. for (size_t i = 0; i < cmds.size(); i++)
  669. if (!cmds[i].data.empty()) {
  670. char suf[64];
  671. snprintf(suf, sizeof(suf), "%s_%zu_%02X", seqsuffix, i, cmds[i].cmd);
  672. print_data_array(macro, suf, cmds[i].data);
  673. }
  674. printf("\ninline const EpdCommand %s_%s[] = {\n", macro, seqsuffix);
  675. for (size_t i = 0; i < cmds.size(); i++) {
  676. char suf[64];
  677. snprintf(suf, sizeof(suf), "%s_%zu_%02X", seqsuffix, i, cmds[i].cmd);
  678. print_cmd(macro, suf, cmds[i]);
  679. }
  680. printf("};\n\n");
  681. };
  682. const bool w0_empty = window0.empty(); // e.g. OTP-default panels (F52) set no window
  683. emit_seq("INIT", init_seq);
  684. if (!w0_empty)
  685. emit_seq("WINDOW_SEQ0", window0);
  686. if (per_plane)
  687. emit_seq("WINDOW_SEQ1", window1);
  688. emit_seq("REFRESH", refresh_seq);
  689. const bool has_power_off = !power_off_seq.empty();
  690. const bool has_hibernate = !hibernate_seq.empty();
  691. if (has_power_off)
  692. emit_seq("POWER_OFF", power_off_seq);
  693. if (has_hibernate)
  694. emit_seq("HIBERNATE", hibernate_seq);
  695. printf("inline const EpdPanelDescriptor %s = {\n", macro);
  696. printf(" .width = %u,\n", (unsigned) Driver::WIDTH);
  697. printf(" .height = %u,\n", (unsigned) Driver::HEIGHT);
  698. printf(" .busy_active_level = %s,\n", bl == HIGH ? "true" : "false");
  699. const char *depth_class = depth_bpp >= 4 ? "4bpp 7-colour" : depth_bpp == 2 ? "2bpp 4-colour" : "1bpp mono/3-colour";
  700. printf(" .busy_timeout_ms = %u, // generous diagnostic ceiling (vendor full_refresh_time %ums, "
  701. "%s class); dev-tunable\n",
  702. (unsigned) default_busy_timeout_ms(Driver::full_refresh_time, depth_bpp),
  703. (unsigned) Driver::full_refresh_time, depth_class);
  704. printf(" .init_sequence = %s_INIT,\n", macro);
  705. printf(" .init_sequence_len = sizeof(%s_INIT) / sizeof(%s_INIT[0]),\n", macro, macro);
  706. if (per_plane) {
  707. printf(" .window_sequence = {%s_WINDOW_SEQ0, %s_WINDOW_SEQ1},\n", macro, macro);
  708. printf(" .window_sequence_len = {sizeof(%s_WINDOW_SEQ0) / sizeof(%s_WINDOW_SEQ0[0]), "
  709. "sizeof(%s_WINDOW_SEQ1) / sizeof(%s_WINDOW_SEQ1[0])},\n",
  710. macro, macro, macro, macro);
  711. printf(" .window_per_plane = true, // per-plane windows differ => separate regions\n");
  712. } else if (w0_empty) {
  713. printf(" .window_sequence = {nullptr, nullptr}, // panel sets no window (OTP-default frame)\n");
  714. printf(" .window_sequence_len = {0, 0},\n");
  715. } else {
  716. printf(" .window_sequence = {%s_WINDOW_SEQ0, nullptr},\n", macro);
  717. printf(" .window_sequence_len = {sizeof(%s_WINDOW_SEQ0) / sizeof(%s_WINDOW_SEQ0[0]), 0},\n", macro, macro);
  718. }
  719. printf(" .plane_count = %zu,\n", nplanes);
  720. printf(" .plane_write_cmd = {0x%02X, 0x%02X},\n", p0cmd, p1cmd);
  721. // Fields below are emitted in struct-declaration order (plane_invert,
  722. // plane_width, line_order, bits_per_pixel, palette, palette_len,
  723. // blank_code) so the designated initializer stays well-formed.
  724. if (pv.is_1bpp) {
  725. const bool i1 = (nplanes > 1 && pv.plane1_valid) ? pv.inv1 : false;
  726. printf(" .plane_invert = {%s, %s}, // probe-derived\n", pv.inv0 ? "true" : "false", i1 ? "true" : "false");
  727. } else {
  728. printf(" .plane_invert = {false, false}, // unused: packed panel\n");
  729. }
  730. if (per_plane) {
  731. if (dual_region && plane_width_val > 0)
  732. printf(" .plane_width = %d, // decoded from the region window-set (x_end-x+1); sanity-check\n",
  733. plane_width_val);
  734. else
  735. printf(" .plane_width = 0, // TODO: diag pass (separate-region width)\n");
  736. }
  737. // line_order: a dual-region panel whose per-region burst is twice the region's
  738. // own pixel bytes is sending two wire lines per row (GxEPD2 writeNative's
  739. // doubled, both-edges-to-middle emission). The doubling is observable here;
  740. // the exact row order that PAIRED_EDGES encodes is NOT (a uniform fill can't
  741. // show which buffer row each line carries), so emit it as a candidate the
  742. // diag native-path pass / driver source confirms.
  743. if (dual_region && plane_width_val > 0 && depth_bpp > 0) {
  744. uint32_t expect = (uint32_t) plane_width_val * Driver::HEIGHT * depth_bpp / 8;
  745. if (expect && region0_burst == 2u * expect)
  746. printf(" .line_order = LineOrder::PAIRED_EDGES, // region burst %u B = 2x %u B (2 lines/row); "
  747. "confirm edges-to-middle order via the diag native-path pass\n",
  748. region0_burst, expect);
  749. else if (expect && region0_burst != expect)
  750. printf(" // NOTE: region burst %u B vs expected %u B (1 line/row) - line order unclear, "
  751. "check with the diag native-path pass\n",
  752. region0_burst, expect);
  753. }
  754. if (pv.is_1bpp) {
  755. // 1bpp plane model: depth confirmed by the probe; palette/blank_code unused.
  756. printf(" .bits_per_pixel = 1, // probe: white fill => all-bits wire byte 0x%02X\n", pv.w0_white & 0xFF);
  757. printf(" .palette = nullptr,\n");
  758. printf(" .palette_len = 0,\n");
  759. } else {
  760. // Packed panel. If a family resolver recognised it (4-colour, via
  761. // emit_palette_4c.h), it derived depth/palette/blank_code by observing the
  762. // driver's own colour fills - emit those (computed once, above). Otherwise
  763. // fall back to a TODO the diag pass fills in by hand (an unknown packed
  764. // family, e.g. a packed panel outside the 4-colour/7-colour resolvers).
  765. if (pr.resolved && pr.two_code_bw) {
  766. printf(" .bits_per_pixel = %d, // probe: %s\n", pr.bits_per_pixel, pr.note.c_str());
  767. printf(" .palette = %s_PALETTE,\n", macro);
  768. printf(" .palette_len = %s_PALETTE_LEN,\n", macro);
  769. printf(" .blank_code = 0x%02X, // observed white-fill code\n", pr.blank_code & 0xFF);
  770. } else if (pr.resolved) {
  771. const int shown_colors = pr.color_count >= 0 ? pr.color_count : (1 << pr.bits_per_pixel);
  772. printf(" .bits_per_pixel = %d, // probe: %d-colour packed%s%s\n", pr.bits_per_pixel,
  773. shown_colors, pr.note.empty() ? "" : " - ", pr.note.c_str());
  774. printf(" .palette = %s,\n", pr.palette_expr);
  775. printf(" .palette_len = %s,\n", pr.palette_len_expr);
  776. printf(" .blank_code = 0x%02X, // observed white fill code\n", pr.blank_code & 0xFF);
  777. } else {
  778. printf(" .bits_per_pixel = 2, // TODO: diag pass - PACKED (white fill wire 0x%02X, not 1bpp)%s%s\n",
  779. pv.w0_white & 0xFF, pr.note.empty() ? "" : " - ", pr.note.c_str());
  780. printf(" .palette = nullptr, // TODO: diag pass\n");
  781. printf(" .palette_len = 0,\n");
  782. printf(" .blank_code = 0x00, // TODO: diag pass (clear fill observed 0x%02X)\n", clear_fill);
  783. }
  784. }
  785. printf(" .refresh_sequence = %s_REFRESH,\n", macro);
  786. printf(" .refresh_sequence_len = sizeof(%s_REFRESH) / sizeof(%s_REFRESH[0]),\n", macro, macro);
  787. if (has_power_off) {
  788. printf(" .power_off_sequence = %s_POWER_OFF, // probe: powerOff(), fresh session after a normal refresh\n", macro);
  789. printf(" .power_off_sequence_len = sizeof(%s_POWER_OFF) / sizeof(%s_POWER_OFF[0]),\n", macro, macro);
  790. }
  791. if (has_hibernate) {
  792. printf(" .hibernate_sequence = %s_HIBERNATE, // probe: hibernate()'s tail past powerOff() (see file header)\n",
  793. macro);
  794. printf(" .hibernate_sequence_len = sizeof(%s_HIBERNATE) / sizeof(%s_HIBERNATE[0]),\n", macro, macro);
  795. }
  796. printf("};\n\n} // namespace esphome::gxepd2\n");
  797. }
  798. // Generic entry: no packed-family resolver, so a packed panel's
  799. // depth/palette/blank_code emit as TODO placeholders (the pre-resolver
  800. // behaviour). The 4-colour entry is emit_descriptor_4c() in emit_palette_4c.h.
  801. template<typename Driver> void emit_descriptor(const char *class_name, const char *macro) {
  802. emit_descriptor_impl<Driver>(class_name, macro, nullptr);
  803. }
  804. } // namespace emit