run_diagN_harness.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #pragma once
  2. // Diagnostic-fill harness, native-path row ordering - Pattern A for PACKED
  3. // panels. Pattern A's writeImage tag doesn't survive the 1bpp->2bpp/4bpp
  4. // re-pack, so instead we tag the NATIVE (already-packed) buffer at byte
  5. // granularity and send it via the driver's byte-preserving writeNative(),
  6. // then recover the buffer->wire row permutation per emitted burst.
  7. //
  8. // Tag: each native row r carries (r>>8),(r&0xFF) at the start of BOTH half-
  9. // width regions (byte offsets 0/1 and halfbpr/halfbpr+1), so a single-region
  10. // panel's one burst and a dual-region panel's master+slave bursts each find
  11. // the tag at their row start. We analyse each plane-sized/length burst with
  12. // its own bytes-per-row = burst_len/H.
  13. //
  14. // Reads out the real wire row order, including any interleave/flip the
  15. // driver's native send applies. Host-only.
  16. #include <cstdint>
  17. #include <cstdio>
  18. #include <vector>
  19. #include "spy/Arduino.h"
  20. #include "spy/SPI.h"
  21. #include "trace.h"
  22. template<typename Driver> struct DiagNPeek : Driver {
  23. using Driver::Driver;
  24. int busy_level() const { return this->_busy_level; }
  25. };
  26. // All data-byte bursts (maximal runs between command bytes) whose length is
  27. // a whole number of rows and >= one row - the native plane/region sends.
  28. inline std::vector<std::vector<uint8_t>> diagN_bursts(uint16_t H, uint32_t min_len) {
  29. std::vector<std::vector<uint8_t>> out;
  30. std::vector<uint8_t> run;
  31. auto flush = [&]() {
  32. if (run.size() >= min_len && run.size() % H == 0)
  33. out.push_back(run);
  34. run.clear();
  35. };
  36. for (const auto &ev : Trace::record) {
  37. if (ev.first)
  38. flush();
  39. else
  40. run.push_back(ev.second);
  41. }
  42. flush();
  43. return out;
  44. }
  45. template<typename WrapperDriver> void run_diagN(const char *name, uint16_t W, uint16_t H, int bpp) {
  46. const uint32_t bpr = (uint32_t) W * bpp / 8; // full-width native row bytes
  47. const uint32_t halfbpr = (uint32_t) (W / 2) * bpp / 8;
  48. const uint32_t buf_bytes = bpr * H;
  49. printf("=== %s: DIAGNOSTIC FILL (native-path row ordering) W=%u H=%u bpp=%d bpr=%u ===\n", name, W, H, bpp,
  50. (unsigned) bpr);
  51. auto *buf = new uint8_t[buf_bytes]();
  52. for (uint16_t r = 0; r < H; r++) {
  53. uint8_t hi = (r >> 8) & 0xFF, lo = r & 0xFF;
  54. buf[(uint32_t) r * bpr + 0] = hi;
  55. buf[(uint32_t) r * bpr + 1] = lo;
  56. if (halfbpr + 1 < bpr) { // dual-region: also tag the second half's row start
  57. buf[(uint32_t) r * bpr + halfbpr] = hi;
  58. buf[(uint32_t) r * bpr + halfbpr + 1] = lo;
  59. }
  60. }
  61. WrapperDriver d(10, 11, 12, 13);
  62. Trace::set_dc_pin(11);
  63. Trace::last_busy_release_value = d.busy_level() == HIGH ? LOW : HIGH;
  64. d.selectSPI(SPI, SPISettings());
  65. d.init(0);
  66. d.writeNative(buf, buf, 0, 0, W, H); // prime (untracked): clears _initial_write
  67. Trace::start_record();
  68. d.writeNative(buf, buf, 0, 0, W, H); // tagged native send
  69. Trace::stop_record();
  70. delete[] buf;
  71. auto bursts = diagN_bursts(H, halfbpr > 0 ? halfbpr : bpr);
  72. // One burst => full frame (stride = full bpr); >=2 => per-region (stride =
  73. // half bpr). A driver may emit more lines than H (e.g. the 579 writes two
  74. // lines per source row into a 2x-height RAM); nlines = burst_len / stride.
  75. const uint32_t stride = (bursts.size() >= 2 && halfbpr > 0) ? halfbpr : bpr;
  76. printf(" native bursts found=%zu, region row stride=%u bytes\n", bursts.size(), (unsigned) stride);
  77. for (size_t p = 0; p < bursts.size(); p++) {
  78. const auto &data = bursts[p];
  79. uint32_t nlines = (uint32_t) data.size() / stride;
  80. std::vector<int> rec(nlines);
  81. for (uint32_t g = 0; g < nlines; g++)
  82. rec[g] = (data[g * stride] << 8) | data[g * stride + 1];
  83. bool identity = (nlines == H), reversed = (nlines == H), paired = (nlines == 2u * H);
  84. for (uint32_t g = 0; g < nlines; g++) {
  85. if (identity && rec[g] != (int) g)
  86. identity = false;
  87. if (reversed && rec[g] != (int) (H - 1 - g))
  88. reversed = false;
  89. // paired, top-reversed: H-1, 0, H-2, 1, H-3, 2, ... (two lines per row)
  90. int expect = (g % 2 == 0) ? (int) (H - 1 - g / 2) : (int) ((g - 1) / 2);
  91. if (paired && rec[g] != expect)
  92. paired = false;
  93. }
  94. const char *verdict = identity ? "SEQUENTIAL (identity)"
  95. : reversed ? "REVERSED rows"
  96. : paired ? "ROW_INTERLEAVE paired top-reversed (H-1,0,H-2,1,...; 2 lines/row)"
  97. : "NON-SEQUENTIAL (see rows)";
  98. printf(" burst %zu: len=%u, nlines=%u (H=%u) -> %s\n", p, (unsigned) data.size(), (unsigned) nlines, H,
  99. verdict);
  100. printf(" first lines:");
  101. for (uint32_t g = 0; g < 8 && g < nlines; g++)
  102. printf(" %d", rec[g]);
  103. printf(" | last lines:");
  104. for (uint32_t g = (nlines > 8 ? nlines - 8 : 0); g < nlines; g++)
  105. printf(" %d", rec[g]);
  106. printf("\n");
  107. }
  108. }