run_diagB_harness.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #pragma once
  2. // Diagnostic-fill harness, Pattern B (pixel packing + codes) - see the
  3. // gxepd2-diag-fill-spec memory. Packing geometry and the colour->code table
  4. // live in the vendor wrapper's draw path (color4()/color7() and _pixel_buffer
  5. // are private), so we derive them by observation: drive the wrapper's
  6. // fillScreen/drawPixel and read the emitted wire bytes (writeNative output).
  7. // Host-only, no hardware.
  8. //
  9. // B2 colour->code: fillScreen(colour) -> every byte is that colour's code
  10. // replicated across its pixel fields; read byte0, all fields equal = the
  11. // code. The set of codes across the named colours IS the palette.
  12. // B1 intra-byte order: fill background=black, set pixels 0..ppb-1 of row 0
  13. // to distinct-code colours; byte0's fields (read MSB-first) reveal which
  14. // pixel lands in which field -> MSB-first vs LSB-first, and confirms ppb.
  15. #include <cstdint>
  16. #include <cstdio>
  17. #include <vector>
  18. #include "spy/Arduino.h"
  19. #include "spy/SPI.h"
  20. #include "trace.h"
  21. template<typename Driver> struct DiagBPeek : Driver {
  22. using Driver::Driver;
  23. int busy_level() const { return this->_busy_level; }
  24. };
  25. // First plane-sized burst from the record (any content - unlike Pattern A we
  26. // do NOT skip uniform bursts, since a fillScreen burst is what B2 reads).
  27. inline std::vector<uint8_t> diagB_first_burst(uint32_t plane_bytes) {
  28. std::vector<uint8_t> run, best;
  29. auto flush = [&]() {
  30. if (run.size() == plane_bytes && best.empty())
  31. best = run;
  32. run.clear();
  33. };
  34. for (const auto &ev : Trace::record) {
  35. if (ev.first)
  36. flush();
  37. else
  38. run.push_back(ev.second);
  39. }
  40. flush();
  41. return best;
  42. }
  43. // Decode a byte's pixel fields, MSB field first (field 0 = highest bits).
  44. inline void diagB_fields(uint8_t byte, int bpp, int ppb, uint8_t out[]) {
  45. uint8_t mask = (uint8_t) ((1u << bpp) - 1);
  46. for (int j = 0; j < ppb; j++)
  47. out[j] = (byte >> ((ppb - 1 - j) * bpp)) & mask;
  48. }
  49. template<typename WrapperT, typename DriverT> void run_diagB(const char *name, uint16_t W, uint16_t H, int bpp) {
  50. const int ppb = 8 / bpp;
  51. const uint32_t plane_bytes = (uint32_t) W * H * bpp / 8; // per region/page
  52. printf("=== %s: DIAGNOSTIC FILL (Pattern B: packing + codes) bpp=%d ppb=%d ===\n", name, bpp, ppb);
  53. WrapperT display(DriverT(10, 11, 12, 13));
  54. Trace::set_dc_pin(11);
  55. Trace::last_busy_release_value = display.epd2.busy_level() == HIGH ? LOW : HIGH;
  56. display.epd2.selectSPI(SPI, SPISettings());
  57. display.init(0);
  58. display.setFullWindow();
  59. // Prime (untracked) so the first-page screen-buffer prime doesn't land in a
  60. // recorded burst.
  61. display.firstPage();
  62. do {
  63. display.fillScreen(GxEPD_WHITE);
  64. } while (display.nextPage());
  65. struct NC {
  66. uint16_t color;
  67. const char *label;
  68. };
  69. const NC colors[] = {{GxEPD_BLACK, "black"}, {GxEPD_WHITE, "white"}, {GxEPD_RED, "red"}, {GxEPD_YELLOW, "yellow"},
  70. {GxEPD_GREEN, "green"}, {GxEPD_BLUE, "blue"}, {GxEPD_ORANGE, "orange"}};
  71. const int NCOL = (int) (sizeof(colors) / sizeof(colors[0]));
  72. uint8_t code_of[16] = {0};
  73. printf(" [B2] colour -> code (fillScreen, byte0 replicated field):\n");
  74. for (int k = 0; k < NCOL; k++) {
  75. Trace::start_record();
  76. display.firstPage();
  77. do {
  78. display.fillScreen(colors[k].color);
  79. } while (display.nextPage());
  80. Trace::stop_record();
  81. auto b = diagB_first_burst(plane_bytes);
  82. uint8_t byte0 = b.empty() ? 0 : b[0];
  83. uint8_t f[8];
  84. diagB_fields(byte0, bpp, ppb, f);
  85. bool uniform = true;
  86. for (int j = 1; j < ppb; j++)
  87. if (f[j] != f[0])
  88. uniform = false;
  89. code_of[k] = f[0];
  90. printf(" %-7s byte0=0x%02X code=%u%s\n", colors[k].label, byte0, f[0],
  91. uniform ? "" : " (WARN fields not uniform: bpp/ppb mismatch)");
  92. }
  93. auto code_for = [&](uint16_t col) -> int {
  94. for (int k = 0; k < NCOL; k++)
  95. if (colors[k].color == col)
  96. return code_of[k];
  97. return -1;
  98. };
  99. printf(" [B1] intra-byte order (bg=black; px 0..%d = distinct-code colours):\n", ppb - 1);
  100. const uint16_t probe[4] = {GxEPD_RED, GxEPD_YELLOW, GxEPD_WHITE, GxEPD_BLACK}; // codes 3,2,1,0 on 4-colour
  101. Trace::start_record();
  102. display.firstPage();
  103. do {
  104. display.fillScreen(GxEPD_BLACK);
  105. for (int i = 0; i < ppb; i++)
  106. display.drawPixel(i, 0, probe[i % 4]);
  107. } while (display.nextPage());
  108. Trace::stop_record();
  109. auto b = diagB_first_burst(plane_bytes);
  110. uint8_t byte0 = b.empty() ? 0 : b[0];
  111. uint8_t f[8];
  112. diagB_fields(byte0, bpp, ppb, f);
  113. printf(" byte0=0x%02X fields MSB..LSB:", byte0);
  114. for (int j = 0; j < ppb; j++)
  115. printf(" %u", f[j]);
  116. printf("\n");
  117. bool msb_first = true, lsb_first = true;
  118. for (int i = 0; i < ppb; i++) {
  119. int want = code_for(probe[i % 4]);
  120. if ((int) f[i] != want)
  121. msb_first = false; // pixel i in field i (MSB-first)
  122. if ((int) f[ppb - 1 - i] != want)
  123. lsb_first = false; // pixel i in field (ppb-1-i)
  124. }
  125. printf(" => %s\n", msb_first ? "MSB-first: pixel i in field i (shift=(ppb-1-i)*bpp)"
  126. : lsb_first ? "LSB-first: pixel i in low fields"
  127. : "UNKNOWN packing order");
  128. }