cycle_engine.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 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(). Called while a refresh is still
  29. // in flight (e.g. from on_powerdown()'s teardown path), they force a reset
  30. // pulse first to abort it rather than writing over a busy controller - see
  31. // begin_power_down_(). See epd_command.h.
  32. class CycleEngine {
  33. public:
  34. void begin(const EpdPanelDescriptor *desc, GPIOPin *cs, GPIOPin *dc, GPIOPin *rst, GPIOPin *busy,
  35. EpdSPIDevice *spi) {
  36. this->desc_ = desc;
  37. this->cs_ = cs;
  38. this->dc_ = dc;
  39. this->rst_ = rst;
  40. this->busy_ = busy;
  41. this->spi_ = spi;
  42. this->cs_->setup();
  43. this->dc_->setup();
  44. if (this->rst_ != nullptr)
  45. this->rst_->setup();
  46. if (this->busy_ != nullptr)
  47. this->busy_->setup();
  48. this->cs_->digital_write(true);
  49. this->dc_->digital_write(true);
  50. }
  51. // Arms the state machine; returns immediately. planes[1] is ignored when
  52. // desc->plane_count == 1. Undefined to call while !idle().
  53. void request_full_update(const uint8_t *plane0, const uint8_t *plane1) {
  54. this->arm_full_update_(plane0, plane1, this->rst_ != nullptr ? State::RESET_LOW : State::INIT_CMD);
  55. }
  56. #ifdef USE_GXEPD2_DIAGNOSTICS
  57. // Dev-time diagnostic, compiled out unless the display's `diagnostics:
  58. // true` is set (display.py adds -DUSE_GXEPD2_DIAGNOSTICS): arms a full
  59. // update starting at INIT_CMD, skipping the reset pulse entirely - the
  60. // same path a panel with no reset pin wired already takes every cycle.
  61. // Lets a bench test use the screen as the oracle for "is the panel
  62. // actually non-responsive right now" (e.g. right after hibernate()): if a
  63. // distinct pattern drawn this way does NOT appear, the prior power call
  64. // really did take the panel off-line; a normal request_full_update() right
  65. // after (reset pulse included) should then wake it and render correctly.
  66. void diag_request_update_no_reset(const uint8_t *plane0, const uint8_t *plane1) {
  67. this->arm_full_update_(plane0, plane1, State::INIT_CMD);
  68. }
  69. #endif
  70. // Arms a standalone powerOff() replay - no init/window/plane write, just
  71. // the captured power_off_sequence (if any) then DONE. Safe to call any
  72. // time, including mid-refresh: see begin_power_down_() below for why that
  73. // matters. Safe to call repeatedly: GxEPD2's own powerOff() is itself
  74. // guarded on "already off," which the capture already baked in (see
  75. // make_power_sequence_resolvers in emit_descriptor.h).
  76. void request_power_off() { this->begin_power_down_(false); }
  77. // Arms a standalone hibernate() replay: power_off_sequence then
  78. // hibernate_sequence (the deep-sleep trigger only - see epd_command.h).
  79. // See begin_power_down_() below for the mid-refresh case. No paired "wake"
  80. // call exists or is needed: request_full_update()'s RESET_LOW is an
  81. // unconditional hardware reset pulse every cycle, which is exactly how
  82. // GxEPD2 wakes a hibernating (RST-wakeable) panel.
  83. void request_hibernate() { this->begin_power_down_(true); }
  84. bool idle() const { return this->state_ == State::IDLE; }
  85. // Called once per loop() tick. Does one bounded unit of work.
  86. void step() {
  87. switch (this->state_) {
  88. case State::IDLE:
  89. return;
  90. case State::RESET_LOW:
  91. this->rst_->digital_write(true); // preset high, then pulse low - see GxEPD2_EPD::_reset()
  92. this->deadline_ = millis() + 10;
  93. this->set_state_(State::RESET_LOW_WAIT);
  94. return;
  95. case State::RESET_LOW_WAIT:
  96. if (millis() < this->deadline_)
  97. return;
  98. this->rst_->digital_write(false);
  99. this->deadline_ = millis() + 10;
  100. this->set_state_(State::RESET_HIGH_WAIT);
  101. return;
  102. case State::RESET_HIGH_WAIT:
  103. if (millis() < this->deadline_)
  104. return;
  105. this->rst_->digital_write(true);
  106. this->deadline_ = millis() + 10;
  107. this->set_state_(State::RESET_SETTLE);
  108. return;
  109. case State::RESET_SETTLE:
  110. if (millis() < this->deadline_)
  111. return;
  112. this->cursor_ = 0;
  113. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  114. // Post-reset busy wait, matching every vendor _InitDisplay()'s
  115. // _waitWhileBusy() right after the reset pulse. Some controllers hold
  116. // BUSY while they load settings/waveforms from OTP after a hardware
  117. // reset (notably JD79676 / GDEY0213F52, whose init is otherwise just
  118. // 0xE9 + power-on) - issuing any command before that clears gets it
  119. // silently dropped, so the panel never refreshes. Panels that don't
  120. // assert BUSY post-reset read "not busy" on the first poll and fall
  121. // straight through to INIT_CMD, so this is safe for all of them.
  122. // begin_power_down_()'s abort path sets reset_then_wait_busy_ to
  123. // false instead - see there for why.
  124. if (this->reset_then_wait_busy_) {
  125. this->start_busy_wait_(this->reset_done_next_);
  126. } else {
  127. this->set_state_(this->reset_done_next_);
  128. }
  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. // Normal reset path: post-reset busy wait (see RESET_SETTLE below), then
  233. // the real init sequence - this->reset_done_next_/reset_then_wait_busy_
  234. // are only ever overridden by begin_power_down_()'s abort path.
  235. this->reset_done_next_ = State::INIT_CMD;
  236. this->reset_then_wait_busy_ = true;
  237. this->set_state_(start_state);
  238. }
  239. // Shared by request_power_off()/request_hibernate(). Most controllers
  240. // ignore new commands outright while BUSY is asserted, so writing
  241. // power_off_sequence/hibernate_sequence straight over a cycle that's still
  242. // mid-refresh would silently do nothing - the panel never actually powers
  243. // down, it just keeps running the update it was already doing. If a reset
  244. // pin is wired, force a hardware reset pulse first (the same one
  245. // request_full_update() issues every normal cycle) to abort whatever's in
  246. // flight and bring the controller back to a known, command-accepting
  247. // state before sending the power-down sequence - skipping the post-reset
  248. // busy wait that a real update takes, since this is an emergency path
  249. // (e.g. on_powerdown()'s tight teardown budget) that can't afford to
  250. // stall on busy_timeout_ms. Without a reset pin there's no way to
  251. // interrupt a busy controller, so this is best-effort in that case:
  252. // jamming the state machine straight to POWER_OFF_CMD, same as calling
  253. // this while idle.
  254. void begin_power_down_(bool hibernating) {
  255. this->cursor_ = 0;
  256. this->plane_ = 0;
  257. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  258. this->hibernating_ = hibernating;
  259. if (this->state_ != State::IDLE && this->rst_ != nullptr) {
  260. this->reset_done_next_ = State::POWER_OFF_CMD;
  261. this->reset_then_wait_busy_ = false;
  262. this->set_state_(State::RESET_LOW);
  263. } else {
  264. this->set_state_(State::POWER_OFF_CMD);
  265. }
  266. }
  267. // Every command byte and every data byte is its own SPI transaction, CS
  268. // toggled around each one - this looks wasteful, but it's mechanically
  269. // what GxEPD2_EPD::_writeCommand()/_writeData(uint8_t) do (see
  270. // third_party/GxEPD2/src/GxEPD2_EPD.cpp), and matching the vendor cycle byte-for-byte
  271. // is the point of this engine.
  272. void write_byte_(uint8_t b, bool dc_high) {
  273. this->dc_->digital_write(dc_high);
  274. this->spi_->enable();
  275. this->cs_->digital_write(false);
  276. this->spi_->write_byte(b);
  277. this->cs_->digital_write(true);
  278. this->spi_->disable();
  279. }
  280. void write_command_(uint8_t cmd, const uint8_t *data, uint8_t data_len) {
  281. #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
  282. // Command-level skeleton trace - deliberately left in (not gated to
  283. // VERY_VERBOSE) while diagnosing a real-hardware content bug: this is
  284. // the exact byte-for-byte counterpart to compare against a captured
  285. // epaper_spi trace on the same panel. Bulk plane data isn't logged
  286. // here (see step_write_plane_) - only the command skeleton matters for
  287. // that comparison.
  288. char buf[32] = {0};
  289. int n = 0;
  290. for (uint8_t i = 0; i < data_len && n < (int) sizeof(buf) - 3; i++)
  291. n += snprintf(buf + n, sizeof(buf) - n, "%02X ", data[i]);
  292. ESP_LOGD("gxepd2", "cmd 0x%02X data[%u]: %s", cmd, data_len, buf);
  293. #endif
  294. this->write_byte_(cmd, false);
  295. for (uint8_t i = 0; i < data_len; i++)
  296. this->write_byte_(data[i], true);
  297. }
  298. // Steps through one EpdCommand table one entry (and one delay/busy-wait,
  299. // if that entry has one) at a time, resuming in self_state after any
  300. // wait so the same call re-enters here for the next entry. Shared by
  301. // INIT_CMD and REFRESH_CMD - which table is being stepped is entirely
  302. // determined by which state called this.
  303. void step_sequence_(const EpdCommand *seq, uint8_t len, State self_state, State done_state) {
  304. if (this->cursor_ >= len) {
  305. this->cursor_ = 0;
  306. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  307. this->set_state_(done_state);
  308. return;
  309. }
  310. const EpdCommand &c = seq[this->cursor_];
  311. if (this->cmd_phase_ == CmdPhase::BEFORE_DELAY) {
  312. this->cmd_phase_ = CmdPhase::ISSUE;
  313. if (c.delay_before_ms > 0) {
  314. this->start_delay_(c.delay_before_ms, self_state);
  315. return;
  316. }
  317. }
  318. this->write_command_(c.cmd, c.data, c.data_len);
  319. this->cursor_++;
  320. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  321. if (c.wait_busy_after) {
  322. this->start_busy_wait_(self_state);
  323. } else if (c.delay_after_ms > 0) {
  324. this->start_delay_(c.delay_after_ms, self_state);
  325. }
  326. // else: stays in self_state; the next step() call processes the next entry.
  327. }
  328. // Writes up to BYTE_BUDGET bytes of the current plane per call, then
  329. // returns - this bound is what keeps a full-frame write (thousands of
  330. // bytes) from ever making one loop() tick run long, replacing the old
  331. // per-256-byte esp_task_wdt_reset()+delay(1) throttle with simply not
  332. // doing enough work in one call to need it.
  333. //
  334. // 64 was overly conservative: at 20MHz SPI that's ~25us of actual bus
  335. // time, so a 426T82 full-plane write (48000 bytes) was bottlenecked
  336. // almost entirely on loop() scheduling overhead (~17ms/step observed),
  337. // not SPI - ~12.7s for one plane. Raised to 16384 (up to ~25ms of
  338. // blocking per step is fine here), cutting that to 3 steps instead of
  339. // 750 - measured ~1.07s for the plane write on hardware. The remaining
  340. // time is dominated by write_byte_()'s per-byte SPI enable()/disable()
  341. // (ESP-IDF transaction setup/teardown, ~21.7us/byte vs ~0.4us raw
  342. // transfer), which is intentionally NOT batched away: it lets other
  343. // devices on a shared spi: bus interleave between our bytes instead of
  344. // being locked out for the whole burst. 1s is well under half this
  345. // panel's own multi-second refresh time anyway, so it wasn't worth
  346. // trading that fairness for more throughput.
  347. static constexpr uint32_t BYTE_BUDGET = 16384;
  348. void step_write_plane_() {
  349. if (this->cursor_ == 0) {
  350. const uint8_t *buf = this->planes_[this->plane_];
  351. uint32_t histo[3] = {0, 0, 0}; // [0]=0x00, [1]=0xFF, [2]=other
  352. if (buf != nullptr) {
  353. for (uint32_t i = 0; i < this->plane_bytes_; i++) {
  354. if (buf[i] == 0x00)
  355. histo[0]++;
  356. else if (buf[i] == 0xFF)
  357. histo[1]++;
  358. else
  359. histo[2]++;
  360. }
  361. }
  362. 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",
  363. this->plane_, this->desc_->plane_write_cmd[this->plane_], (unsigned) this->wire_bytes_,
  364. (unsigned) this->plane_bytes_, this->desc_->plane_invert[this->plane_], buf != nullptr ? buf[0] : 0,
  365. buf != nullptr ? buf[this->plane_bytes_ - 1] : 0, (unsigned) histo[0], (unsigned) histo[1],
  366. (unsigned) histo[2]);
  367. this->write_byte_(this->desc_->plane_write_cmd[this->plane_], false);
  368. }
  369. const uint8_t *buf = this->planes_[this->plane_];
  370. bool invert = this->desc_->plane_invert[this->plane_];
  371. uint32_t end = this->cursor_ + BYTE_BUDGET;
  372. if (end > this->wire_bytes_)
  373. end = this->wire_bytes_;
  374. for (; this->cursor_ < end; this->cursor_++) {
  375. uint32_t src = this->line_src_index_(this->cursor_);
  376. uint8_t b = buf != nullptr ? buf[src] : 0xFF;
  377. this->write_byte_(invert ? static_cast<uint8_t>(~b) : b, true);
  378. }
  379. if (this->cursor_ >= this->wire_bytes_) {
  380. this->cursor_ = 0;
  381. this->plane_++;
  382. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  383. if (this->plane_ >= this->desc_->plane_count) {
  384. this->plane_ = 0;
  385. this->set_state_(State::REFRESH_CMD);
  386. } else if (this->desc_->window_per_plane) {
  387. // Another plane to go and it needs its own window-set first (a
  388. // genuinely separate physical RAM region, not just another color
  389. // plane sharing the window already set) - re-enter SET_WINDOW,
  390. // which will pick window_sequence[this->plane_] now that plane_
  391. // has already advanced.
  392. this->set_state_(State::SET_WINDOW);
  393. }
  394. // else: more planes, sharing one window already set - stay in
  395. // WRITE_PLANE, next step() call starts the next plane's byte burst.
  396. }
  397. }
  398. // Maps a wire-transmission-order byte position (0..wire_bytes_-1) to the
  399. // buffer byte it reads. The wire is a sequence of lines, each bytes_per_row_
  400. // bytes from one buffer row; line_order picks which buffer row each wire
  401. // line carries (and, for PAIRED_EDGES, there are 2x as many wire lines as
  402. // buffer rows). SEQUENTIAL is the identity (wire line i -> row i).
  403. uint32_t line_src_index_(uint32_t wire_pos) const {
  404. uint32_t wire_line = wire_pos / this->bytes_per_row_;
  405. uint32_t byte_in_line = wire_pos % this->bytes_per_row_;
  406. uint32_t buf_row;
  407. switch (this->desc_->line_order) {
  408. case LineOrder::PAIRED_EDGES: {
  409. // wire lines come in pairs: for i in [0,H), line 2i = row (H-1-i),
  410. // line 2i+1 = row i. Matches GxEPD2 writeNative's emission exactly.
  411. uint32_t i = wire_line / 2;
  412. buf_row = (wire_line % 2 == 0) ? (this->desc_->height - 1 - i) : i;
  413. break;
  414. }
  415. case LineOrder::SEQUENTIAL:
  416. default:
  417. buf_row = wire_line;
  418. break;
  419. }
  420. return buf_row * this->bytes_per_row_ + byte_in_line;
  421. }
  422. void start_delay_(uint16_t delay_ms, State return_to) {
  423. this->deadline_ = millis() + delay_ms;
  424. this->delay_return_state_ = return_to;
  425. this->set_state_(State::POST_DELAY);
  426. }
  427. void start_busy_wait_(State next) {
  428. this->busy_wait_next_ = next;
  429. this->busy_wait_deadline_ = millis() + this->desc_->busy_timeout_ms;
  430. this->set_state_(State::WAIT_BUSY);
  431. }
  432. void step_wait_busy_() {
  433. if (this->busy_ == nullptr) {
  434. this->set_state_(this->busy_wait_next_);
  435. return;
  436. }
  437. bool busy = this->busy_->digital_read() == this->desc_->busy_active_level;
  438. if (!busy) {
  439. this->set_state_(this->busy_wait_next_);
  440. return;
  441. }
  442. if (millis() >= this->busy_wait_deadline_) {
  443. ESP_LOGW("gxepd2", "Busy timeout waiting for panel");
  444. this->set_state_(State::ABORTED);
  445. }
  446. }
  447. // Logs every state transition (only when the state actually changes -
  448. // WAIT_BUSY and POST_DELAY re-enter their own case on every step() call
  449. // while polling, which would otherwise spam this at loop() rate) so a
  450. // first hardware run can be checked against the vendor cycle it's meant
  451. // to replicate before this gets dialed back to WARN-only.
  452. void set_state_(State s) {
  453. if (s != this->state_)
  454. ESP_LOGD("gxepd2", "state: %s -> %s", state_name_(this->state_), state_name_(s));
  455. this->state_ = s;
  456. }
  457. static const char *state_name_(State s) {
  458. switch (s) {
  459. case State::IDLE:
  460. return "IDLE";
  461. case State::RESET_LOW:
  462. return "RESET_LOW";
  463. case State::RESET_LOW_WAIT:
  464. return "RESET_LOW_WAIT";
  465. case State::RESET_HIGH_WAIT:
  466. return "RESET_HIGH_WAIT";
  467. case State::RESET_SETTLE:
  468. return "RESET_SETTLE";
  469. case State::INIT_CMD:
  470. return "INIT_CMD";
  471. case State::SET_WINDOW:
  472. return "SET_WINDOW";
  473. case State::WRITE_PLANE:
  474. return "WRITE_PLANE";
  475. case State::REFRESH_CMD:
  476. return "REFRESH_CMD";
  477. case State::POWER_OFF_CMD:
  478. return "POWER_OFF_CMD";
  479. case State::HIBERNATE_CMD:
  480. return "HIBERNATE_CMD";
  481. case State::POST_DELAY:
  482. return "POST_DELAY";
  483. case State::WAIT_BUSY:
  484. return "WAIT_BUSY";
  485. case State::DONE:
  486. return "DONE";
  487. case State::ABORTED:
  488. return "ABORTED";
  489. }
  490. return "?";
  491. }
  492. const EpdPanelDescriptor *desc_{nullptr};
  493. GPIOPin *cs_{nullptr};
  494. GPIOPin *dc_{nullptr};
  495. GPIOPin *rst_{nullptr};
  496. GPIOPin *busy_{nullptr};
  497. EpdSPIDevice *spi_{nullptr};
  498. State state_{State::IDLE};
  499. uint32_t cursor_{0};
  500. CmdPhase cmd_phase_{CmdPhase::BEFORE_DELAY};
  501. uint8_t plane_{0};
  502. const uint8_t *planes_[2]{nullptr, nullptr};
  503. uint32_t plane_bytes_{0}; // real buffer size per plane
  504. uint32_t wire_bytes_{0}; // bytes actually sent per plane (>= plane_bytes_)
  505. uint32_t bytes_per_row_{0};
  506. uint32_t deadline_{0};
  507. State reset_done_next_{State::INIT_CMD}; // RESET_SETTLE's continuation - see arm_full_update_()/begin_power_down_()
  508. bool reset_then_wait_busy_{true}; // false only for begin_power_down_()'s abort path
  509. State delay_return_state_{State::DONE};
  510. uint32_t busy_wait_deadline_{0};
  511. State busy_wait_next_{State::DONE};
  512. bool hibernating_{false}; // POWER_OFF_CMD's done_state: HIBERNATE_CMD if set, else DONE
  513. };
  514. } // namespace esphome::gxepd2