#pragma once // Prototype: emit a candidate panel_*.h EpdPanelDescriptor by *observing* a // real vendor GxEPD2 driver run, instead of hand-transcribing its C++. This // closes the hand-derivation gap for the straight-line parts of a descriptor // (init / window / refresh command sequences + the plane-write command) by // phase-segmenting the captured event stream at the public-API boundary - no // edits to the vendor source. // // Staging (the trick that separates the phases without instrumenting the // vendor's private methods): // writeScreenBuffer() -> lazy _InitDisplay() + a plane-write burst (clear). // Everything before that burst is the INIT sequence; // the command right before it is the PLANE-WRITE cmd. // writeImage(pattern) -> init already done, so this is just the WINDOW set // (commands before the pattern burst) + plane write. // refresh() -> re-sets the window then triggers; dropping the // duplicate window command leaves the REFRESH seq. // // Out of scope here (comes from the diag Pattern A/B passes, not one capture): // bits_per_pixel / palette / plane_invert / row_interleave. Those are emitted // as TODO placeholders so the diff against the hand file is about segmentation. #include #include #include #include #include #include "spy/Arduino.h" #include "spy/SPI.h" #include "trace.h" namespace emit { // A data burst at least this long is a pixel-plane write, not config data. constexpr size_t PLANE_BURST_MIN = 256; struct Cmd { uint8_t cmd; std::vector data; bool wait_busy = false; unsigned long delay_after = 0; }; // Expose GxEPD2_EPD's protected busy level without parsing (same trick as // run_harness.h's Peek). template struct Peek : Driver { using Driver::Driver; int busy_level() const { return this->_busy_level; } }; // Group a slice of the event stream into EpdCommand-shaped records. Events // before the first SPI *command* (the reset pulse + its delays) are skipped - // the engine owns the hardware reset, so it never belongs in a descriptor. inline std::vector group(size_t lo, size_t hi) { std::vector cmds; bool started = false; for (size_t i = lo; i < hi; i++) { const auto &e = Trace::events[i]; switch (e.kind) { case EventKind::SPI_TRANSFER: if (e.is_command) { cmds.push_back(Cmd{e.byte}); started = true; } else if (started) { cmds.back().data.push_back(e.byte); } break; case EventKind::DELAY: if (started) cmds.back().delay_after += e.ms; break; case EventKind::DIGITAL_READ: // the only reads are busy-pin polls if (started) { cmds.back().wait_busy = true; // The delay(1) GxEPD2_EPD::_waitWhileBusy() runs inside its poll // loop lands right before this read and is part of the busy-wait, // not an inter-command delay - our engine owns busy-waiting, so // fold it in rather than emit it. (This also absorbs a vendor's own // tiny settle delay right before a refresh trigger, e.g. 290c's // _refresh delay(1); immaterial, the panel asserts busy regardless.) cmds.back().delay_after = 0; } break; default: break; // DIGITAL_WRITE = reset/CS/DC toggles: engine's job, not data } } return cmds; } // Index of the first command whose data burst marks a plane write, or np // (cmds.size()) if there's none in this slice. inline size_t plane_index(const std::vector &cmds) { for (size_t i = 0; i < cmds.size(); i++) if (cmds[i].data.size() >= PLANE_BURST_MIN) return i; return cmds.size(); } inline void print_data_array(const char *macro, const char *suffix, const std::vector &d) { printf("inline const uint8_t %s_%s[] = {", macro, suffix); for (size_t i = 0; i < d.size(); i++) printf("%s0x%02X", i ? ", " : "", d[i]); printf("};\n"); } // Print one EpdCommand{} initializer line, with the optional fields only when // they're non-default (matches the hand files' style). inline void print_cmd(const char *macro, const char *suffix, const Cmd &c) { if (c.data.empty() && !c.wait_busy && c.delay_after == 0) { printf(" {0x%02X, nullptr, 0},\n", c.cmd); return; } const char *dataref = c.data.empty() ? "nullptr" : nullptr; if (dataref) printf(" {0x%02X, nullptr, 0", c.cmd); else printf(" {0x%02X, %s_%s, %u", c.cmd, macro, suffix, (unsigned) c.data.size()); if (c.wait_busy || c.delay_after) printf(", /*delay_before_ms=*/0, /*delay_after_ms=*/%lu, /*wait_busy_after=*/%s", c.delay_after, c.wait_busy ? "true" : "false"); printf("},\n"); } template void emit_descriptor(const char *class_name, const char *macro) { constexpr int16_t CS = 10, DC = 11, RST = 12, BUSY = 13; Trace::set_dc_pin(DC); Peek driver(CS, DC, RST, BUSY); const int bl = driver.busy_level(); Trace::last_busy_release_value = bl == HIGH ? LOW : HIGH; driver.selectSPI(SPI, SPISettings()); driver.init(0); const uint32_t plane_bytes = (static_cast(Driver::WIDTH) * Driver::HEIGHT) / 8; auto *black = new uint8_t[plane_bytes]; auto *color = new uint8_t[plane_bytes]; for (uint32_t i = 0; i < plane_bytes; i++) { black[i] = 0xA5; color[i] = 0xA5; } Trace::start_event_record(); const size_t s0 = 0; driver.writeScreenBuffer(); // init + clear burst const size_t s1 = Trace::events.size(); driver.writeImage(black, color, 0, 0, Driver::WIDTH, Driver::HEIGHT); // window + plane const size_t s2 = Trace::events.size(); driver.refresh(false); // window-again + trigger const size_t s3 = Trace::events.size(); Trace::stop_event_record(); delete[] black; delete[] color; // --- segment --- auto init_stage = group(s0, s1); auto image_stage = group(s1, s2); auto refresh_stage = group(s2, s3); auto cmds_equal = [](const std::vector &a, const std::vector &b) { if (a.size() != b.size()) return false; for (size_t i = 0; i < a.size(); i++) if (a[i].cmd != b[i].cmd || a[i].data != b[i].data) return false; return true; }; // Every pixel-plane burst in the image call. One => single-plane panel; // N => N planes (two colour planes sharing a region, or two physical // regions - told apart below by whether their window-sets match). std::vector bursts; for (size_t i = 0; i < image_stage.size(); i++) if (image_stage[i].data.size() >= PLANE_BURST_MIN) bursts.push_back(i); const size_t nplanes = bursts.empty() ? 1 : bursts.size(); if (getenv("EMIT_DEBUG")) { fprintf(stderr, "image_stage %zu cmds; bursts:", image_stage.size()); for (auto b : bursts) fprintf(stderr, " [%zu]=0x%02X", b, image_stage[b].cmd); fprintf(stderr, "\n"); for (size_t i = 0; i < image_stage.size(); i++) fprintf(stderr, " [%zu] 0x%02X data=%zu\n", i, image_stage[i].cmd, image_stage[i].data.size()); } // pre_block(k) = commands after burst k-1 (or the start) up to, but not // including, burst k's own plane-write command - i.e. the window set for // plane k, possibly with the lazily-run _InitDisplay in front for k==0. auto pre_block = [&](size_t k) { size_t lo = (k == 0) ? 0 : bursts[k - 1] + 1; return std::vector(image_stage.begin() + lo, image_stage.begin() + bursts[k]); }; std::vector init_seq, window0, window1; std::vector plane_cmds; bool per_plane = false; if (bursts.size() >= 2) { // Multi-plane. Two observed layouts: // interleaved: [init][W0][burst0][W1][burst1] - each plane's window // sits between its burst and the previous one (dual region); // batched: [init][W][W][burst0][burst1] - all windows precede all // bursts (SSD1680 3-colour: writeImage sets the black and // red RAM windows, then bursts both planes). for (size_t k = 0; k < bursts.size(); k++) plane_cmds.push_back(image_stage[bursts[k]].cmd); const size_t n = bursts.size(); std::vector pre = pre_block(0); std::vector> windows; bool interleaved = false; for (size_t k = 1; k < n; k++) if (bursts[k] - bursts[k - 1] > 1) { interleaved = true; break; } if (interleaved) { std::vector gap0(image_stage.begin() + bursts[0] + 1, image_stage.begin() + bursts[1]); const size_t wl = gap0.size(); if (wl <= pre.size()) { windows.push_back(std::vector(pre.end() - wl, pre.end())); init_seq.assign(pre.begin(), pre.end() - wl); } else { windows.push_back({}); init_seq = pre; } for (size_t k = 1; k < n; k++) windows.push_back(std::vector(image_stage.begin() + bursts[k - 1] + 1, image_stage.begin() + bursts[k])); } else { // Batched: pre = init + n copies of the window. Peel the smallest block // that repeats n times at the tail; the remainder is init. size_t L = 0; for (size_t cand = 1; cand * n <= pre.size(); cand++) { bool ok = true; for (size_t b = 0; b < n && ok; b++) for (size_t j = 0; j < cand && ok; j++) { const Cmd &a = pre[pre.size() - cand * n + b * cand + j]; const Cmd &c = pre[pre.size() - cand + j]; if (a.cmd != c.cmd || a.data != c.data) ok = false; } if (ok) { L = cand; break; } } if (L > 0) { for (size_t b = 0; b < n; b++) windows.push_back(std::vector(pre.end() - L * (n - b), pre.end() - L * (n - b - 1))); init_seq.assign(pre.begin(), pre.end() - L * n); } else { windows.push_back({}); init_seq = pre; } } window0 = windows.empty() ? std::vector{} : windows[0]; window1 = windows.size() > 1 ? windows[1] : std::vector{}; for (size_t k = 1; k < windows.size(); k++) if (!cmds_equal(windows[0], windows[k])) per_plane = true; // per-plane windows differ => separate regions } else { // Single plane: window = the image call's pre-burst block; init comes // from the writeScreenBuffer stage, cut at the first window-set command // (SSD16xx sets the window there too - that belongs in window_sequence). const size_t wp = plane_index(image_stage); window0.assign(image_stage.begin(), image_stage.begin() + wp); std::set wops; for (const auto &c : window0) wops.insert(c.cmd); const size_t ip = plane_index(init_stage); size_t cut = ip; for (size_t i = 0; i < ip; i++) if (wops.count(init_stage[i].cmd)) { cut = i; break; } init_seq.assign(init_stage.begin(), init_stage.begin() + cut); plane_cmds.push_back(wp < image_stage.size() ? image_stage[wp].cmd : 0x00); } // blank/clear fill byte, observed from the writeScreenBuffer clear burst. const size_t cip = plane_index(init_stage); const uint8_t clear_fill = (cip < init_stage.size() && !init_stage[cip].data.empty()) ? init_stage[cip].data[0] : 0x00; // Refresh = post-write commands after the LAST burst (e.g. UC8179's 0x92), // then the refresh call minus any window-set it repeats. std::set all_wops; for (const auto &c : window0) all_wops.insert(c.cmd); for (const auto &c : window1) all_wops.insert(c.cmd); std::vector refresh_seq; if (!bursts.empty()) for (size_t i = bursts.back() + 1; i < image_stage.size() && image_stage[i].data.size() < PLANE_BURST_MIN; i++) refresh_seq.push_back(image_stage[i]); for (const auto &c : refresh_stage) if (!all_wops.count(c.cmd)) refresh_seq.push_back(c); // --- emit --- printf("// GENERATED by emit_descriptor from %s - candidate, segmentation only.\n", class_name); printf("// bits_per_pixel/palette/plane_invert/plane_width/row_interleave still\n"); printf("// come from the diag passes.\n"); printf("#pragma once\n#include \"epd_command.h\"\n\nnamespace esphome::gxepd2 {\n\n"); auto emit_seq = [&](const char *seqsuffix, std::vector &cmds) { for (size_t i = 0; i < cmds.size(); i++) if (!cmds[i].data.empty()) { char suf[64]; snprintf(suf, sizeof(suf), "%s_%zu_%02X", seqsuffix, i, cmds[i].cmd); print_data_array(macro, suf, cmds[i].data); } printf("\ninline const EpdCommand %s_%s[] = {\n", macro, seqsuffix); for (size_t i = 0; i < cmds.size(); i++) { char suf[64]; snprintf(suf, sizeof(suf), "%s_%zu_%02X", seqsuffix, i, cmds[i].cmd); print_cmd(macro, suf, cmds[i]); } printf("};\n\n"); }; emit_seq("INIT", init_seq); emit_seq("WINDOW_SEQ0", window0); if (per_plane) emit_seq("WINDOW_SEQ1", window1); emit_seq("REFRESH", refresh_seq); printf("inline const EpdPanelDescriptor %s = {\n", macro); printf(" .width = %u,\n", (unsigned) Driver::WIDTH); printf(" .height = %u,\n", (unsigned) Driver::HEIGHT); printf(" .busy_active_level = %s,\n", bl == HIGH ? "true" : "false"); printf(" .busy_timeout_ms = %u,\n", (unsigned) Driver::full_refresh_time + 5000u); printf(" .init_sequence = %s_INIT,\n", macro); printf(" .init_sequence_len = sizeof(%s_INIT) / sizeof(%s_INIT[0]),\n", macro, macro); if (per_plane) { printf(" .window_sequence = {%s_WINDOW_SEQ0, %s_WINDOW_SEQ1},\n", macro, macro); printf(" .window_sequence_len = {sizeof(%s_WINDOW_SEQ0) / sizeof(%s_WINDOW_SEQ0[0]), " "sizeof(%s_WINDOW_SEQ1) / sizeof(%s_WINDOW_SEQ1[0])},\n", macro, macro, macro, macro); printf(" .window_per_plane = true, // per-plane windows differ => separate regions\n"); } else { printf(" .window_sequence = {%s_WINDOW_SEQ0, nullptr},\n", macro); printf(" .window_sequence_len = {sizeof(%s_WINDOW_SEQ0) / sizeof(%s_WINDOW_SEQ0[0]), 0},\n", macro, macro); } printf(" .plane_count = %zu,\n", nplanes); printf(" .plane_write_cmd = {0x%02X, 0x%02X},\n", plane_cmds.empty() ? 0x00 : plane_cmds[0], plane_cmds.size() > 1 ? plane_cmds[1] : 0x00); printf(" .plane_invert = {false, false}, // TODO: diag pass\n"); printf(" .bits_per_pixel = 1, // TODO: diag pass (clear fill byte observed: 0x%02X)\n", clear_fill); printf(" .palette = nullptr, // TODO: diag pass\n"); printf(" .palette_len = 0,\n"); if (per_plane) printf(" .plane_width = 0, // TODO: diag pass (separate-region width)\n"); printf(" .blank_code = 0x00, // TODO: derive from clear fill 0x%02X\n", clear_fill); printf(" .refresh_sequence = %s_REFRESH,\n", macro); printf(" .refresh_sequence_len = sizeof(%s_REFRESH) / sizeof(%s_REFRESH[0]),\n", macro, macro); printf("};\n\n} // namespace esphome::gxepd2\n"); } } // namespace emit