cycle_engine.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. #pragma once
  2. #include <cstdint>
  3. #include <cstdio>
  4. #include "esphome/core/gpio.h"
  5. #include "esphome/core/hal.h"
  6. #include "esphome/core/log.h"
  7. #include "esphome/components/spi/spi.h"
  8. #include "epd_command.h"
  9. namespace esphome::gxepd2 {
  10. // Stock GxEPD2 vendor value (20MHz) for this panel. Was dropped to 10MHz
  11. // (matching epaper_spi) while chasing the solid-black/solid-red symptom,
  12. // which turned out to be unrelated - display.py never wired the configured
  13. // lambda to set_writer() at all, so nothing but auto_clear's default fill
  14. // ever rendered, regardless of SPI clock. Rolled back to test the stock
  15. // GxEPD2 clock now that the real bug is fixed.
  16. using EpdSPIDevice = spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
  17. spi::DATA_RATE_20MHZ>;
  18. // Replays one GxEPD2 driver's refresh cycle - reset pulse, init command table,
  19. // RAM-window set, per-plane byte burst, refresh trigger, busy-pin wait - as a
  20. // state machine stepped from loop(), instead of GxEPD2's own synchronous
  21. // top-to-bottom call stack. step() never blocks: every state does a small
  22. // bounded unit of work and returns, so nothing here can starve another task or
  23. // need a watchdog subscription.
  24. //
  25. // request_power_off()/request_hibernate() are a separate, standalone entry
  26. // point (no reset pulse, no init/window/plane-write) replaying just
  27. // power_off_sequence/hibernate_sequence - for a user's own lambda, not run
  28. // automatically by request_full_update(). See epd_command.h.
  29. class CycleEngine {
  30. public:
  31. void begin(const EpdPanelDescriptor *desc, GPIOPin *cs, GPIOPin *dc, GPIOPin *rst, GPIOPin *busy,
  32. EpdSPIDevice *spi) {
  33. this->desc_ = desc;
  34. this->cs_ = cs;
  35. this->dc_ = dc;
  36. this->rst_ = rst;
  37. this->busy_ = busy;
  38. this->spi_ = spi;
  39. this->cs_->setup();
  40. this->dc_->setup();
  41. if (this->rst_ != nullptr)
  42. this->rst_->setup();
  43. if (this->busy_ != nullptr)
  44. this->busy_->setup();
  45. this->cs_->digital_write(true);
  46. this->dc_->digital_write(true);
  47. }
  48. // Arms the state machine; returns immediately. planes[1] is ignored when
  49. // desc->plane_count == 1. Undefined to call while !idle().
  50. void request_full_update(const uint8_t *plane0, const uint8_t *plane1) {
  51. this->arm_full_update_(plane0, plane1, this->rst_ != nullptr ? State::RESET_LOW : State::INIT_CMD);
  52. }
  53. #ifdef USE_GXEPD2_DIAGNOSTICS
  54. // Dev-time diagnostic, compiled out unless the display's `diagnostics:
  55. // true` is set (display.py adds -DUSE_GXEPD2_DIAGNOSTICS): arms a full
  56. // update starting at INIT_CMD, skipping the reset pulse entirely - the
  57. // same path a panel with no reset pin wired already takes every cycle.
  58. // Lets a bench test use the screen as the oracle for "is the panel
  59. // actually non-responsive right now" (e.g. right after hibernate()): if a
  60. // distinct pattern drawn this way does NOT appear, the prior power call
  61. // really did take the panel off-line; a normal request_full_update() right
  62. // after (reset pulse included) should then wake it and render correctly.
  63. void diag_request_update_no_reset(const uint8_t *plane0, const uint8_t *plane1) {
  64. this->arm_full_update_(plane0, plane1, State::INIT_CMD);
  65. }
  66. #endif
  67. // Arms a standalone powerOff() replay - no reset pulse, no init/window/
  68. // plane write, just the captured power_off_sequence (if any) then DONE.
  69. // Undefined to call while !idle(). Safe to call repeatedly: GxEPD2's own
  70. // powerOff() is itself guarded on "already off," which the capture already
  71. // baked in (see make_power_sequence_resolvers in emit_descriptor.h).
  72. void request_power_off() {
  73. this->cursor_ = 0;
  74. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  75. this->hibernating_ = false;
  76. this->set_state_(State::POWER_OFF_CMD);
  77. }
  78. // Arms a standalone hibernate() replay: power_off_sequence then
  79. // hibernate_sequence (the deep-sleep trigger only - see epd_command.h).
  80. // Undefined to call while !idle(). No paired "wake" call exists or is
  81. // needed: request_full_update()'s RESET_LOW is an unconditional hardware
  82. // reset pulse every cycle, which is exactly how GxEPD2 wakes a hibernating
  83. // (RST-wakeable) panel.
  84. void request_hibernate() {
  85. this->cursor_ = 0;
  86. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  87. this->hibernating_ = true;
  88. this->set_state_(State::POWER_OFF_CMD);
  89. }
  90. bool idle() const { return this->state_ == State::IDLE; }
  91. // Called once per loop() tick. Does one bounded unit of work.
  92. void step() {
  93. switch (this->state_) {
  94. case State::IDLE:
  95. return;
  96. case State::RESET_LOW:
  97. this->rst_->digital_write(true); // preset high, then pulse low - see GxEPD2_EPD::_reset()
  98. this->deadline_ = millis() + 10;
  99. this->set_state_(State::RESET_LOW_WAIT);
  100. return;
  101. case State::RESET_LOW_WAIT:
  102. if (millis() < this->deadline_)
  103. return;
  104. this->rst_->digital_write(false);
  105. this->deadline_ = millis() + 10;
  106. this->set_state_(State::RESET_HIGH_WAIT);
  107. return;
  108. case State::RESET_HIGH_WAIT:
  109. if (millis() < this->deadline_)
  110. return;
  111. this->rst_->digital_write(true);
  112. this->deadline_ = millis() + 10;
  113. this->set_state_(State::RESET_SETTLE);
  114. return;
  115. case State::RESET_SETTLE:
  116. if (millis() < this->deadline_)
  117. return;
  118. this->cursor_ = 0;
  119. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  120. // Post-reset busy wait, matching every vendor _InitDisplay()'s
  121. // _waitWhileBusy() right after the reset pulse. Some controllers hold
  122. // BUSY while they load settings/waveforms from OTP after a hardware
  123. // reset (notably JD79676 / GDEY0213F52, whose init is otherwise just
  124. // 0xE9 + power-on) - issuing any command before that clears gets it
  125. // silently dropped, so the panel never refreshes. Panels that don't
  126. // assert BUSY post-reset read "not busy" on the first poll and fall
  127. // straight through to INIT_CMD, so this is safe for all of them.
  128. this->start_busy_wait_(State::INIT_CMD);
  129. return;
  130. case State::INIT_CMD:
  131. this->step_sequence_(this->desc_->init_sequence, this->desc_->init_sequence_len, State::INIT_CMD,
  132. State::SET_WINDOW);
  133. return;
  134. case State::SET_WINDOW:
  135. // window_sequence[this->plane_]: index 0 on the first entry
  136. // (plane_ reset to 0 in request_full_update()); when
  137. // window_per_plane is true, step_write_plane_() re-enters this
  138. // state with plane_ already advanced to 1 before the next plane's
  139. // byte burst, picking window_sequence[1] instead - see
  140. // panel_579c_gdey0579f51.h for why a panel needs that.
  141. this->step_sequence_(this->desc_->window_sequence[this->plane_], this->desc_->window_sequence_len[this->plane_],
  142. State::SET_WINDOW, State::WRITE_PLANE);
  143. return;
  144. case State::WRITE_PLANE:
  145. this->step_write_plane_();
  146. return;
  147. case State::REFRESH_CMD:
  148. this->step_sequence_(this->desc_->refresh_sequence, this->desc_->refresh_sequence_len, State::REFRESH_CMD,
  149. State::DONE);
  150. return;
  151. case State::POWER_OFF_CMD:
  152. // request_hibernate() routes through here first (hibernate() is
  153. // itself powerOff() + one more command on every GxEPD2 driver - see
  154. // epd_command.h) - done_state branches to HIBERNATE_CMD only then.
  155. this->step_sequence_(this->desc_->power_off_sequence, this->desc_->power_off_sequence_len,
  156. State::POWER_OFF_CMD, this->hibernating_ ? State::HIBERNATE_CMD : State::DONE);
  157. return;
  158. case State::HIBERNATE_CMD:
  159. this->step_sequence_(this->desc_->hibernate_sequence, this->desc_->hibernate_sequence_len,
  160. State::HIBERNATE_CMD, State::DONE);
  161. return;
  162. case State::POST_DELAY:
  163. if (millis() < this->deadline_)
  164. return;
  165. this->set_state_(this->delay_return_state_);
  166. return;
  167. case State::WAIT_BUSY:
  168. this->step_wait_busy_();
  169. return;
  170. case State::DONE:
  171. ESP_LOGD("gxepd2", "Display update finished");
  172. this->set_state_(State::IDLE);
  173. return;
  174. case State::ABORTED:
  175. // A busy timeout means the panel isn't behaving as wired/expected
  176. // (unplugged, dead, wrong busy polarity, ...) - pressing ahead
  177. // with the rest of the sequence (more command bytes, another
  178. // busy-wait) is very unlikely to "just work" and only obscures
  179. // which step actually failed. Bail the whole cycle here instead of
  180. // continuing to whatever state the timed-out wait was headed to.
  181. ESP_LOGE("gxepd2", "Aborting display update: busy timeout");
  182. this->cursor_ = 0;
  183. this->plane_ = 0;
  184. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  185. this->set_state_(State::IDLE);
  186. return;
  187. }
  188. }
  189. private:
  190. // State/CmdPhase must come before arm_full_update_() below: a member
  191. // function's PARAMETER types (unlike its body) are resolved at the point
  192. // of declaration, not deferred to complete-class context, so `State` has
  193. // to already be a known type by the time arm_full_update_()'s signature is
  194. // parsed.
  195. enum class State {
  196. IDLE,
  197. RESET_LOW,
  198. RESET_LOW_WAIT,
  199. RESET_HIGH_WAIT,
  200. RESET_SETTLE,
  201. INIT_CMD,
  202. SET_WINDOW,
  203. WRITE_PLANE,
  204. REFRESH_CMD,
  205. POWER_OFF_CMD,
  206. HIBERNATE_CMD,
  207. POST_DELAY,
  208. WAIT_BUSY,
  209. DONE,
  210. ABORTED,
  211. };
  212. enum class CmdPhase { BEFORE_DELAY, ISSUE };
  213. // Shared by request_full_update() and (dev builds only) the no-reset
  214. // diagnostic path above - identical setup, different starting state.
  215. void arm_full_update_(const uint8_t *plane0, const uint8_t *plane1, State start_state) {
  216. this->planes_[0] = plane0;
  217. this->planes_[1] = plane1;
  218. uint32_t w = this->desc_->window_per_plane ? this->desc_->plane_width : this->desc_->width;
  219. // width*height*bpp/8: /8 at 1bpp, /4 at 2bpp, /2 at 4bpp.
  220. this->plane_bytes_ = (uint32_t) w * this->desc_->height * this->desc_->bits_per_pixel / 8;
  221. this->bytes_per_row_ = this->plane_bytes_ / this->desc_->height;
  222. // Wire length can exceed the buffer: PAIRED_EDGES sends each row twice
  223. // (2x-height RAM). plane_bytes_ stays the real buffer size; wire_bytes_
  224. // is what actually goes out on 0x10/0x24/... per plane.
  225. uint32_t wire_lines = this->desc_->height;
  226. if (this->desc_->line_order == LineOrder::PAIRED_EDGES)
  227. wire_lines *= 2;
  228. this->wire_bytes_ = wire_lines * this->bytes_per_row_;
  229. this->cursor_ = 0;
  230. this->plane_ = 0;
  231. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  232. this->set_state_(start_state);
  233. }
  234. // Every command byte and every data byte is its own SPI transaction, CS
  235. // toggled around each one - this looks wasteful, but it's mechanically
  236. // what GxEPD2_EPD::_writeCommand()/_writeData(uint8_t) do (see
  237. // third_party/GxEPD2/src/GxEPD2_EPD.cpp), and matching the vendor cycle byte-for-byte
  238. // is the point of this engine.
  239. void write_byte_(uint8_t b, bool dc_high) {
  240. this->dc_->digital_write(dc_high);
  241. this->spi_->enable();
  242. this->cs_->digital_write(false);
  243. this->spi_->write_byte(b);
  244. this->cs_->digital_write(true);
  245. this->spi_->disable();
  246. }
  247. void write_command_(uint8_t cmd, const uint8_t *data, uint8_t data_len) {
  248. #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
  249. // Command-level skeleton trace - deliberately left in (not gated to
  250. // VERY_VERBOSE) while diagnosing a real-hardware content bug: this is
  251. // the exact byte-for-byte counterpart to compare against a captured
  252. // epaper_spi trace on the same panel. Bulk plane data isn't logged
  253. // here (see step_write_plane_) - only the command skeleton matters for
  254. // that comparison.
  255. char buf[32] = {0};
  256. int n = 0;
  257. for (uint8_t i = 0; i < data_len && n < (int) sizeof(buf) - 3; i++)
  258. n += snprintf(buf + n, sizeof(buf) - n, "%02X ", data[i]);
  259. ESP_LOGD("gxepd2", "cmd 0x%02X data[%u]: %s", cmd, data_len, buf);
  260. #endif
  261. this->write_byte_(cmd, false);
  262. for (uint8_t i = 0; i < data_len; i++)
  263. this->write_byte_(data[i], true);
  264. }
  265. // Steps through one EpdCommand table one entry (and one delay/busy-wait,
  266. // if that entry has one) at a time, resuming in self_state after any
  267. // wait so the same call re-enters here for the next entry. Shared by
  268. // INIT_CMD and REFRESH_CMD - which table is being stepped is entirely
  269. // determined by which state called this.
  270. void step_sequence_(const EpdCommand *seq, uint8_t len, State self_state, State done_state) {
  271. if (this->cursor_ >= len) {
  272. this->cursor_ = 0;
  273. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  274. this->set_state_(done_state);
  275. return;
  276. }
  277. const EpdCommand &c = seq[this->cursor_];
  278. if (this->cmd_phase_ == CmdPhase::BEFORE_DELAY) {
  279. this->cmd_phase_ = CmdPhase::ISSUE;
  280. if (c.delay_before_ms > 0) {
  281. this->start_delay_(c.delay_before_ms, self_state);
  282. return;
  283. }
  284. }
  285. this->write_command_(c.cmd, c.data, c.data_len);
  286. this->cursor_++;
  287. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  288. if (c.wait_busy_after) {
  289. this->start_busy_wait_(self_state);
  290. } else if (c.delay_after_ms > 0) {
  291. this->start_delay_(c.delay_after_ms, self_state);
  292. }
  293. // else: stays in self_state; the next step() call processes the next entry.
  294. }
  295. // Writes up to BYTE_BUDGET bytes of the current plane per call, then
  296. // returns - this bound is what keeps a full-frame write (thousands of
  297. // bytes) from ever making one loop() tick run long, replacing the old
  298. // per-256-byte esp_task_wdt_reset()+delay(1) throttle with simply not
  299. // doing enough work in one call to need it.
  300. //
  301. // 64 was overly conservative: at 20MHz SPI that's ~25us of actual bus
  302. // time, so a 426T82 full-plane write (48000 bytes) was bottlenecked
  303. // almost entirely on loop() scheduling overhead (~17ms/step observed),
  304. // not SPI - ~12.7s for one plane. Raised to 16384 (up to ~25ms of
  305. // blocking per step is fine here), cutting that to 3 steps instead of
  306. // 750 - measured ~1.07s for the plane write on hardware. The remaining
  307. // time is dominated by write_byte_()'s per-byte SPI enable()/disable()
  308. // (ESP-IDF transaction setup/teardown, ~21.7us/byte vs ~0.4us raw
  309. // transfer), which is intentionally NOT batched away: it lets other
  310. // devices on a shared spi: bus interleave between our bytes instead of
  311. // being locked out for the whole burst. 1s is well under half this
  312. // panel's own multi-second refresh time anyway, so it wasn't worth
  313. // trading that fairness for more throughput.
  314. static constexpr uint32_t BYTE_BUDGET = 16384;
  315. void step_write_plane_() {
  316. if (this->cursor_ == 0) {
  317. const uint8_t *buf = this->planes_[this->plane_];
  318. uint32_t histo[3] = {0, 0, 0}; // [0]=0x00, [1]=0xFF, [2]=other
  319. if (buf != nullptr) {
  320. for (uint32_t i = 0; i < this->plane_bytes_; i++) {
  321. if (buf[i] == 0x00)
  322. histo[0]++;
  323. else if (buf[i] == 0xFF)
  324. histo[1]++;
  325. else
  326. histo[2]++;
  327. }
  328. }
  329. ESP_LOGD("gxepd2", "plane %u: cmd 0x%02X, %u wire bytes (buf %u), invert=%d, buf[0]=0x%02X buf[last]=0x%02X, histo(00/FF/other)=%u/%u/%u",
  330. this->plane_, this->desc_->plane_write_cmd[this->plane_], (unsigned) this->wire_bytes_,
  331. (unsigned) this->plane_bytes_, this->desc_->plane_invert[this->plane_], buf != nullptr ? buf[0] : 0,
  332. buf != nullptr ? buf[this->plane_bytes_ - 1] : 0, (unsigned) histo[0], (unsigned) histo[1],
  333. (unsigned) histo[2]);
  334. this->write_byte_(this->desc_->plane_write_cmd[this->plane_], false);
  335. }
  336. const uint8_t *buf = this->planes_[this->plane_];
  337. bool invert = this->desc_->plane_invert[this->plane_];
  338. uint32_t end = this->cursor_ + BYTE_BUDGET;
  339. if (end > this->wire_bytes_)
  340. end = this->wire_bytes_;
  341. for (; this->cursor_ < end; this->cursor_++) {
  342. uint32_t src = this->line_src_index_(this->cursor_);
  343. uint8_t b = buf != nullptr ? buf[src] : 0xFF;
  344. this->write_byte_(invert ? static_cast<uint8_t>(~b) : b, true);
  345. }
  346. if (this->cursor_ >= this->wire_bytes_) {
  347. this->cursor_ = 0;
  348. this->plane_++;
  349. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  350. if (this->plane_ >= this->desc_->plane_count) {
  351. this->plane_ = 0;
  352. this->set_state_(State::REFRESH_CMD);
  353. } else if (this->desc_->window_per_plane) {
  354. // Another plane to go and it needs its own window-set first (a
  355. // genuinely separate physical RAM region, not just another color
  356. // plane sharing the window already set) - re-enter SET_WINDOW,
  357. // which will pick window_sequence[this->plane_] now that plane_
  358. // has already advanced.
  359. this->set_state_(State::SET_WINDOW);
  360. }
  361. // else: more planes, sharing one window already set - stay in
  362. // WRITE_PLANE, next step() call starts the next plane's byte burst.
  363. }
  364. }
  365. // Maps a wire-transmission-order byte position (0..wire_bytes_-1) to the
  366. // buffer byte it reads. The wire is a sequence of lines, each bytes_per_row_
  367. // bytes from one buffer row; line_order picks which buffer row each wire
  368. // line carries (and, for PAIRED_EDGES, there are 2x as many wire lines as
  369. // buffer rows). SEQUENTIAL is the identity (wire line i -> row i).
  370. uint32_t line_src_index_(uint32_t wire_pos) const {
  371. uint32_t wire_line = wire_pos / this->bytes_per_row_;
  372. uint32_t byte_in_line = wire_pos % this->bytes_per_row_;
  373. uint32_t buf_row;
  374. switch (this->desc_->line_order) {
  375. case LineOrder::PAIRED_EDGES: {
  376. // wire lines come in pairs: for i in [0,H), line 2i = row (H-1-i),
  377. // line 2i+1 = row i. Matches GxEPD2 writeNative's emission exactly.
  378. uint32_t i = wire_line / 2;
  379. buf_row = (wire_line % 2 == 0) ? (this->desc_->height - 1 - i) : i;
  380. break;
  381. }
  382. case LineOrder::SEQUENTIAL:
  383. default:
  384. buf_row = wire_line;
  385. break;
  386. }
  387. return buf_row * this->bytes_per_row_ + byte_in_line;
  388. }
  389. void start_delay_(uint16_t delay_ms, State return_to) {
  390. this->deadline_ = millis() + delay_ms;
  391. this->delay_return_state_ = return_to;
  392. this->set_state_(State::POST_DELAY);
  393. }
  394. void start_busy_wait_(State next) {
  395. this->busy_wait_next_ = next;
  396. this->busy_wait_deadline_ = millis() + this->desc_->busy_timeout_ms;
  397. this->set_state_(State::WAIT_BUSY);
  398. }
  399. void step_wait_busy_() {
  400. if (this->busy_ == nullptr) {
  401. this->set_state_(this->busy_wait_next_);
  402. return;
  403. }
  404. bool busy = this->busy_->digital_read() == this->desc_->busy_active_level;
  405. if (!busy) {
  406. this->set_state_(this->busy_wait_next_);
  407. return;
  408. }
  409. if (millis() >= this->busy_wait_deadline_) {
  410. ESP_LOGW("gxepd2", "Busy timeout waiting for panel");
  411. this->set_state_(State::ABORTED);
  412. }
  413. }
  414. // Logs every state transition (only when the state actually changes -
  415. // WAIT_BUSY and POST_DELAY re-enter their own case on every step() call
  416. // while polling, which would otherwise spam this at loop() rate) so a
  417. // first hardware run can be checked against the vendor cycle it's meant
  418. // to replicate before this gets dialed back to WARN-only.
  419. void set_state_(State s) {
  420. if (s != this->state_)
  421. ESP_LOGD("gxepd2", "state: %s -> %s", state_name_(this->state_), state_name_(s));
  422. this->state_ = s;
  423. }
  424. static const char *state_name_(State s) {
  425. switch (s) {
  426. case State::IDLE:
  427. return "IDLE";
  428. case State::RESET_LOW:
  429. return "RESET_LOW";
  430. case State::RESET_LOW_WAIT:
  431. return "RESET_LOW_WAIT";
  432. case State::RESET_HIGH_WAIT:
  433. return "RESET_HIGH_WAIT";
  434. case State::RESET_SETTLE:
  435. return "RESET_SETTLE";
  436. case State::INIT_CMD:
  437. return "INIT_CMD";
  438. case State::SET_WINDOW:
  439. return "SET_WINDOW";
  440. case State::WRITE_PLANE:
  441. return "WRITE_PLANE";
  442. case State::REFRESH_CMD:
  443. return "REFRESH_CMD";
  444. case State::POWER_OFF_CMD:
  445. return "POWER_OFF_CMD";
  446. case State::HIBERNATE_CMD:
  447. return "HIBERNATE_CMD";
  448. case State::POST_DELAY:
  449. return "POST_DELAY";
  450. case State::WAIT_BUSY:
  451. return "WAIT_BUSY";
  452. case State::DONE:
  453. return "DONE";
  454. case State::ABORTED:
  455. return "ABORTED";
  456. }
  457. return "?";
  458. }
  459. const EpdPanelDescriptor *desc_{nullptr};
  460. GPIOPin *cs_{nullptr};
  461. GPIOPin *dc_{nullptr};
  462. GPIOPin *rst_{nullptr};
  463. GPIOPin *busy_{nullptr};
  464. EpdSPIDevice *spi_{nullptr};
  465. State state_{State::IDLE};
  466. uint32_t cursor_{0};
  467. CmdPhase cmd_phase_{CmdPhase::BEFORE_DELAY};
  468. uint8_t plane_{0};
  469. const uint8_t *planes_[2]{nullptr, nullptr};
  470. uint32_t plane_bytes_{0}; // real buffer size per plane
  471. uint32_t wire_bytes_{0}; // bytes actually sent per plane (>= plane_bytes_)
  472. uint32_t bytes_per_row_{0};
  473. uint32_t deadline_{0};
  474. State delay_return_state_{State::DONE};
  475. uint32_t busy_wait_deadline_{0};
  476. State busy_wait_next_{State::DONE};
  477. bool hibernating_{false}; // POWER_OFF_CMD's done_state: HIBERNATE_CMD if set, else DONE
  478. };
  479. } // namespace esphome::gxepd2