My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Max7219_Debug_LEDs.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * This module is off by default, but can be enabled to facilitate the display of
  24. * extra debug information during code development.
  25. *
  26. * Just connect up 5V and GND to give it power, then connect up the pins assigned
  27. * in Configuration_adv.h. For example, on the Re-ARM you could use:
  28. *
  29. * #define MAX7219_CLK_PIN 77
  30. * #define MAX7219_DIN_PIN 78
  31. * #define MAX7219_LOAD_PIN 79
  32. *
  33. * Max7219_init() is called automatically at startup, and then there are a number of
  34. * support functions available to control the LEDs in the 8x8 grid.
  35. */
  36. #include "../inc/MarlinConfigPre.h"
  37. #if ENABLED(MAX7219_DEBUG)
  38. #define MAX7219_ERRORS // Disable to save 406 bytes of Program Memory
  39. #include "Max7219_Debug_LEDs.h"
  40. #include "../module/planner.h"
  41. #include "../module/stepper.h"
  42. #include "../Marlin.h"
  43. #include "../HAL/Delay.h"
  44. uint8_t LEDs[8 * (MAX7219_NUMBER_UNITS)] = { 0 };
  45. #ifndef MAX7219_ROTATE
  46. #define MAX7219_ROTATE 0
  47. #endif
  48. #ifdef CPU_32_BIT
  49. // Approximate a 1µs delay on 32-bit ARM
  50. #define SIG_DELAY() DELAY_US(1)
  51. #else
  52. // Delay for 0.1875µs (16MHz AVR) or 0.15µs (20MHz AVR)
  53. #define SIG_DELAY() DELAY_NS(188)
  54. #endif
  55. void Max7219_PutByte(uint8_t data) {
  56. #ifndef CPU_32_BIT
  57. CRITICAL_SECTION_START;
  58. #endif
  59. for (uint8_t i = 8; i--;) {
  60. SIG_DELAY();
  61. WRITE(MAX7219_CLK_PIN, LOW); // tick
  62. SIG_DELAY();
  63. WRITE(MAX7219_DIN_PIN, (data & 0x80) ? HIGH : LOW); // send 1 or 0 based on data bit
  64. SIG_DELAY();
  65. WRITE(MAX7219_CLK_PIN, HIGH); // tock
  66. SIG_DELAY();
  67. data <<= 1;
  68. }
  69. #ifndef CPU_32_BIT
  70. CRITICAL_SECTION_END;
  71. #endif
  72. }
  73. void Max7219_pulse_load() {
  74. SIG_DELAY();
  75. WRITE(MAX7219_LOAD_PIN, LOW); // tell the chip to load the data
  76. SIG_DELAY();
  77. WRITE(MAX7219_LOAD_PIN, HIGH);
  78. SIG_DELAY();
  79. }
  80. void Max7219(const uint8_t reg, const uint8_t data) {
  81. SIG_DELAY();
  82. #ifndef CPU_32_BIT
  83. CRITICAL_SECTION_START;
  84. #endif
  85. SIG_DELAY();
  86. Max7219_PutByte(reg); // specify register
  87. SIG_DELAY();
  88. Max7219_PutByte(data); // put data
  89. CRITICAL_SECTION_END;
  90. }
  91. #if ENABLED(MAX7219_NUMERIC)
  92. // Draw an integer with optional leading zeros and optional decimal point
  93. void Max7219_Print(const uint8_t start, int16_t value, uint8_t size, const bool leadzero=false, bool dec=false) {
  94. constexpr uint8_t led_numeral[10] = { 0x7E, 0x60, 0x6D, 0x79, 0x63, 0x5B, 0x5F, 0x70, 0x7F, 0x7A },
  95. led_decimal = 0x80, led_minus = 0x01;
  96. bool blank = false, neg = value < 0;
  97. if (neg) value *= -1;
  98. while (size--) {
  99. const bool minus = neg && blank;
  100. if (minus) neg = false;
  101. Max7219(
  102. max7219_reg_digit0 + start + size,
  103. minus ? led_minus : blank ? 0x00 : led_numeral[value % 10] | (dec ? led_decimal : 0x00)
  104. );
  105. Max7219_pulse_load(); // tell the chips to load the clocked out data
  106. value /= 10;
  107. if (!value && !leadzero) blank = true;
  108. dec = false;
  109. }
  110. }
  111. // Draw a float with a decimal point and optional digits
  112. void Max7219_Print(const uint8_t start, const float value, const uint8_t pre_size, const uint8_t post_size, const bool leadzero=false) {
  113. if (pre_size) Max7219_Print(start, value, pre_size, leadzero, !!post_size);
  114. if (post_size) {
  115. const int16_t after = ABS(value) * (10 ^ post_size);
  116. Max7219_Print(start + pre_size, after, post_size, true);
  117. }
  118. }
  119. #endif // MAX7219_NUMERIC
  120. inline void Max7219_Error(const char * const func, const int32_t v1, const int32_t v2=-1) {
  121. #if ENABLED(MAX7219_ERRORS)
  122. SERIAL_ECHOPGM("??? ");
  123. serialprintPGM(func);
  124. SERIAL_CHAR('(');
  125. SERIAL_ECHO(v1);
  126. if (v2 > 0) SERIAL_ECHOPAIR(", ", v2);
  127. SERIAL_CHAR(')');
  128. SERIAL_EOL();
  129. #else
  130. UNUSED(func); UNUSED(v1); UNUSED(v2);
  131. #endif
  132. }
  133. /**
  134. * uint32_t flipped(const uint32_t bits, const uint8_t n_bytes) operates on the number
  135. * of bytes specified in n_bytes. The lower order bits of the supplied bits are flipped.
  136. * flipped( x, 1) flips the low 8 bits of x.
  137. * flipped( x, 2) flips the low 16 bits of x.
  138. * flipped( x, 3) flips the low 24 bits of x.
  139. * flipped( x, 4) flips the low 32 bits of x.
  140. */
  141. inline uint32_t flipped(const uint32_t bits, const uint8_t n_bytes) {
  142. uint32_t mask = 1, outbits = 0;
  143. for (uint8_t b = 0; b < n_bytes * 8; b++) {
  144. outbits = (outbits << 1);
  145. if (bits & mask)
  146. outbits |= 1;
  147. mask = mask << 1;
  148. }
  149. return outbits;
  150. }
  151. // Modify a single LED bit and send the changed line
  152. void Max7219_LED_Set(const uint8_t x, const uint8_t y, const bool on) {
  153. if (x > (MAX7219_X_LEDS - 1) || y > (MAX7219_Y_LEDS - 1)) return Max7219_Error(PSTR("Max7219_LED_Set"), x, y);
  154. if (BIT_7219(x, y) == on) return;
  155. XOR_7219(x, y);
  156. SEND_7219(MAX7219_UPDATE_AXIS);
  157. }
  158. void Max7219_LED_On(const uint8_t x, const uint8_t y) {
  159. if (x > (MAX7219_X_LEDS - 1) || y > (MAX7219_Y_LEDS - 1)) return Max7219_Error(PSTR("Max7219_LED_On"), x, y);
  160. Max7219_LED_Set(x, y, true);
  161. }
  162. void Max7219_LED_Off(const uint8_t x, const uint8_t y) {
  163. if (x > (MAX7219_X_LEDS - 1) || y > (MAX7219_Y_LEDS - 1)) return Max7219_Error(PSTR("Max7219_LED_Off"), x, y);
  164. Max7219_LED_Set(x, y, false);
  165. }
  166. void Max7219_LED_Toggle(const uint8_t x, const uint8_t y) {
  167. if (x > (MAX7219_X_LEDS - 1) || y > (MAX7219_Y_LEDS - 1)) return Max7219_Error(PSTR("Max7219_LED_Toggle"), x, y);
  168. Max7219_LED_Set(x, y, !BIT_7219(x, y));
  169. }
  170. inline void _Max7219_Set_Digit_Segments(const uint8_t digit, const uint8_t val) {
  171. LEDs[digit] = val;
  172. SEND_7219(digit);
  173. }
  174. /**
  175. * void Max7219_Set_Row( const uint8_t col, const uint32_t val) plots the low order bits of
  176. * val to the specified row of the Max7219 matrix. With 4 Max7219 units in the chain, it
  177. * is possible to display an entire 32-bit number with one call to the function (if appropriately
  178. * orientated).
  179. */
  180. void Max7219_Set_Row(const uint8_t row, const uint32_t val) {
  181. if (row >= MAX7219_Y_LEDS) return Max7219_Error(PSTR("Max7219_Set_Row"), row);
  182. uint32_t mask = 0x0000001;
  183. for (uint8_t x = 0; x < MAX7219_X_LEDS; x++) {
  184. if (val & mask)
  185. SET_PIXEL_7219((MAX7219_X_LEDS-1-x), row);
  186. else
  187. CLEAR_PIXEL_7219((MAX7219_X_LEDS-1-x), row);
  188. mask = mask << 1;
  189. }
  190. #if _ROT == 90 || _ROT == 270
  191. for (uint8_t x = 0; x < 8; x++)
  192. SEND_7219(x); // force all columns out to the Max7219 chips and strobe them
  193. #else
  194. SEND_7219(row); // force the single column out to the Max7219 chips and strobe them
  195. #endif
  196. }
  197. void Max7219_Clear_Row(const uint8_t row) {
  198. if (row > 7) return Max7219_Error(PSTR("Max7219_Clear_Row"), row);
  199. #if _ROT == 90 || _ROT == 270
  200. for (uint8_t col = 0; col < 8; col++) Max7219_LED_Off(col, row);
  201. #else
  202. _Max7219_Set_Digit_Segments(row, 0);
  203. #endif
  204. }
  205. /**
  206. * void Max7219_Set_Column( const uint8_t col, const uint32_t val) plots the low order bits of
  207. * val to the specified column of the Max7219 matrix. With 4 Max7219 units in the chain, it
  208. * is possible to display an entire 32-bit number with one call to the function (if appropriately
  209. * orientated).
  210. */
  211. void Max7219_Set_Column(const uint8_t col, const uint32_t val) {
  212. if (col >= MAX7219_X_LEDS) return Max7219_Error(PSTR("Max7219_Set_Column"), col);
  213. uint32_t mask = 0x0000001;
  214. for (uint8_t y = 0; y < MAX7219_Y_LEDS; y++) {
  215. if (val & mask)
  216. SET_PIXEL_7219(col, MAX7219_Y_LEDS-1-y);
  217. else
  218. CLEAR_PIXEL_7219(col, MAX7219_Y_LEDS-1-y);
  219. mask = mask << 1;
  220. }
  221. #if _ROT == 90 || _ROT == 270
  222. SEND_7219(col); // force the column out to the Max7219 chips and strobe them
  223. #else
  224. for (uint8_t yy = 0; yy < 8; yy++)
  225. SEND_7219(yy); // force all columns out to the Max7219 chips and strobe them
  226. #endif
  227. }
  228. void Max7219_Clear_Column(const uint8_t col) {
  229. if (col >= MAX7219_X_LEDS) return Max7219_Error(PSTR("Max7219_Clear_Column"), col);
  230. for (uint8_t yy = 0; yy < MAX7219_Y_LEDS; yy++)
  231. CLEAR_PIXEL_7219(col, yy);
  232. #if _ROT == 90 || _ROT == 270
  233. SEND_7219(col); // force the column out to the Max7219 chips and strobe them
  234. #else
  235. for (uint8_t y = 0; y < 8; y++)
  236. SEND_7219(y); // force all columns out to the Max7219 chips and strobe them
  237. #endif
  238. }
  239. void Max7219_Clear() {
  240. for (uint8_t i = 0; i <= 7; i++) { // Clear LED bitmap
  241. for (uint8_t j = 0; j < MAX7219_NUMBER_UNITS; j++)
  242. LEDs[i + j * 8] = 0x00;
  243. SEND_7219(i);
  244. }
  245. }
  246. void Max7219_Set_Rows_16bits(const uint8_t y, uint32_t val) {
  247. #if MAX7219_X_LEDS == 8
  248. if (y > MAX7219_Y_LEDS - 2) return Max7219_Error(PSTR("Max7219_Set_Rows_16bits"), y, val);
  249. Max7219_Set_Row(y + 1, val); val >>= 8;
  250. Max7219_Set_Row(y + 0, val);
  251. #else // at least 16 bits on each row
  252. if (y > MAX7219_Y_LEDS - 1) return Max7219_Error(PSTR("Max7219_Set_Rows_16bits"), y, val);
  253. Max7219_Set_Row(y, val);
  254. #endif
  255. }
  256. void Max7219_Set_Rows_32bits(const uint8_t y, uint32_t val) {
  257. #if MAX7219_X_LEDS == 8
  258. if (y > MAX7219_Y_LEDS - 4) return Max7219_Error(PSTR("Max7219_Set_Rows_32bits"), y, val);
  259. Max7219_Set_Row(y + 3, val); val >>= 8;
  260. Max7219_Set_Row(y + 2, val); val >>= 8;
  261. Max7219_Set_Row(y + 1, val); val >>= 8;
  262. Max7219_Set_Row(y + 0, val);
  263. #elif MAX7219_X_LEDS == 16
  264. if (y > MAX7219_Y_LEDS - 2) return Max7219_Error(PSTR("Max7219_Set_Rows_32bits"), y, val);
  265. Max7219_Set_Row(y + 1, val); val >>= 16;
  266. Max7219_Set_Row(y + 0, val);
  267. #else // at least 24 bits on each row. In the 3 matrix case, just display the low 24 bits
  268. if (y > MAX7219_Y_LEDS - 1) return Max7219_Error(PSTR("Max7219_Set_Rows_32bits"), y, val);
  269. Max7219_Set_Row(y, val);
  270. #endif
  271. }
  272. void Max7219_Set_Columns_16bits(const uint8_t x, uint32_t val) {
  273. #if MAX7219_Y_LEDS == 8
  274. if (x > MAX7219_X_LEDS - 2) return Max7219_Error(PSTR("Max7219_Set_Columns_16bits"), x, val);
  275. Max7219_Set_Column(x + 0, val); val >>= 8;
  276. Max7219_Set_Column(x + 1, val);
  277. #else // at least 16 bits in each column
  278. if (x > MAX7219_X_LEDS - 1) return Max7219_Error(PSTR("Max7219_Set_Columns_16bits"), x, val);
  279. Max7219_Set_Column(x, val);
  280. #endif
  281. }
  282. void Max7219_Set_Columns_32bits(const uint8_t x, uint32_t val) {
  283. #if MAX7219_Y_LEDS == 8
  284. if (x > MAX7219_X_LEDS - 4) return Max7219_Error(PSTR("Max7219_Set_Rows_32bits"), x, val);
  285. Max7219_Set_Column(x + 3, val); val >>= 8;
  286. Max7219_Set_Column(x + 2, val); val >>= 8;
  287. Max7219_Set_Column(x + 1, val); val >>= 8;
  288. Max7219_Set_Column(x + 0, val);
  289. #elif MAX7219_Y_LEDS == 16
  290. if (x > MAX7219_X_LEDS - 2) return Max7219_Error(PSTR("Max7219_Set_Rows_32bits"), x, val);
  291. Max7219_Set_Column(x + 1, val); val >>= 16;
  292. Max7219_Set_Column(x + 0, val);
  293. #else // at least 24 bits on each row. In the 3 matrix case, just display the low 24 bits
  294. if (x > MAX7219_X_LEDS - 1) return Max7219_Error(PSTR("Max7219_Set_Rows_32bits"), x, val);
  295. Max7219_Set_Column(x, val);
  296. #endif
  297. }
  298. void Max7219_register_setup() {
  299. // Initialize the Max7219
  300. for (uint8_t i = 0; i < MAX7219_NUMBER_UNITS; i++)
  301. Max7219(max7219_reg_scanLimit, 0x07);
  302. Max7219_pulse_load(); // tell the chips to load the clocked out data
  303. for (uint8_t i = 0; i < MAX7219_NUMBER_UNITS; i++)
  304. Max7219(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
  305. Max7219_pulse_load(); // tell the chips to load the clocked out data
  306. for (uint8_t i = 0; i < MAX7219_NUMBER_UNITS; i++)
  307. Max7219(max7219_reg_shutdown, 0x01); // not in shutdown mode
  308. Max7219_pulse_load(); // tell the chips to load the clocked out data
  309. for (uint8_t i = 0; i < MAX7219_NUMBER_UNITS; i++)
  310. Max7219(max7219_reg_displayTest, 0x00); // no display test
  311. Max7219_pulse_load(); // tell the chips to load the clocked out data
  312. for (uint8_t i = 0; i < MAX7219_NUMBER_UNITS; i++)
  313. Max7219(max7219_reg_intensity, 0x01 & 0x0F); // the first 0x0F is the value you can set
  314. // range: 0x00 to 0x0F
  315. Max7219_pulse_load(); // tell the chips to load the clocked out data
  316. }
  317. #ifdef MAX7219_INIT_TEST
  318. #if (MAX7219_INIT_TEST + 0) == 2
  319. inline void Max7219_spiral(const bool on, const uint16_t del) {
  320. constexpr int8_t way[] = { 1, 0, 0, 1, -1, 0, 0, -1 };
  321. int8_t px = 0, py = 0, dir = 0;
  322. for (uint8_t i = MAX7219_X_LEDS * MAX7219_Y_LEDS; i--;) {
  323. Max7219_LED_Set(px, py, on);
  324. delay(del);
  325. const int8_t x = px + way[dir], y = py + way[dir + 1];
  326. if (!WITHIN(x, 0, MAX7219_X_LEDS-1) || !WITHIN(y, 0, MAX7219_Y_LEDS-1) || BIT_7219(x, y) == on) dir = (dir + 2) & 0x7;
  327. px += way[dir]; py += way[dir + 1];
  328. }
  329. }
  330. #else
  331. inline void Max7219_sweep(const int8_t dir, const uint16_t ms, const bool on) {
  332. uint8_t x = dir > 0 ? 0 : MAX7219_X_LEDS-1;
  333. for (uint8_t i = MAX7219_X_LEDS; i--; x += dir) {
  334. Max7219_Set_Column(x, on ? 0xFFFFFFFF : 0x00000000);
  335. delay(ms);
  336. }
  337. }
  338. #endif
  339. #endif // MAX7219_INIT_TEST
  340. void Max7219_init() {
  341. SET_OUTPUT(MAX7219_DIN_PIN);
  342. SET_OUTPUT(MAX7219_CLK_PIN);
  343. OUT_WRITE(MAX7219_LOAD_PIN, HIGH);
  344. delay(1);
  345. Max7219_register_setup();
  346. for (uint8_t i = 0; i <= 7; i++) { // Empty registers to turn all LEDs off
  347. LEDs[i] = 0x00;
  348. Max7219(max7219_reg_digit0 + i, 0);
  349. Max7219_pulse_load(); // tell the chips to load the clocked out data
  350. }
  351. #ifdef MAX7219_INIT_TEST
  352. #if (MAX7219_INIT_TEST + 0) == 2
  353. Max7219_spiral(true, 8);
  354. delay(150);
  355. Max7219_spiral(false, 8);
  356. #else
  357. // Do an aesthetically-pleasing pattern to fully test the Max7219 module and LEDs.
  358. // Light up and turn off columns, both forward and backward.
  359. Max7219_sweep(1, 20, true);
  360. Max7219_sweep(1, 20, false);
  361. delay(150);
  362. Max7219_sweep(-1, 20, true);
  363. Max7219_sweep(-1, 20, false);
  364. #endif
  365. #endif
  366. }
  367. /**
  368. * This code demonstrates some simple debugging using a single 8x8 LED Matrix. If your feature could
  369. * benefit from matrix display, add its code here. Very little processing is required, so the 7219 is
  370. * ideal for debugging when realtime feedback is important but serial output can't be used.
  371. */
  372. // Apply changes to update a marker
  373. inline void Max7219_Mark16(const uint8_t y, const uint8_t v1, const uint8_t v2) {
  374. #if MAX7219_X_LEDS == 8
  375. Max7219_LED_Off(v1 & 0x7, y + (v1 >= 8));
  376. Max7219_LED_On(v2 & 0x7, y + (v2 >= 8));
  377. #else // LED matrix has at least 16 LED's on the X-Axis. Use single line of LED's
  378. Max7219_LED_Off(v1 & 0xF, y);
  379. Max7219_LED_On(v2 & 0xF, y);
  380. #endif
  381. }
  382. // Apply changes to update a tail-to-head range
  383. inline void Max7219_Range16(const uint8_t y, const uint8_t ot, const uint8_t nt, const uint8_t oh, const uint8_t nh) {
  384. #if MAX7219_X_LEDS == 8
  385. if (ot != nt) for (uint8_t n = ot & 0xF; n != (nt & 0xF) && n != (nh & 0xF); n = (n + 1) & 0xF)
  386. Max7219_LED_Off(n & 0x7, y + (n >= 8));
  387. if (oh != nh) for (uint8_t n = (oh + 1) & 0xF; n != ((nh + 1) & 0xF); n = (n + 1) & 0xF)
  388. Max7219_LED_On(n & 0x7, y + (n >= 8));
  389. #else // LED matrix has at least 16 LED's on the X-Axis. Use single line of LED's
  390. if (ot != nt) for (uint8_t n = ot & 0xF; n != (nt & 0xF) && n != (nh & 0xF); n = (n + 1) & 0xF)
  391. Max7219_LED_Off(n & 0xF, y);
  392. if (oh != nh) for (uint8_t n = (oh + 1) & 0xF; n != ((nh + 1) & 0xF); n = (n + 1) & 0xF)
  393. Max7219_LED_On(n & 0xF, y);
  394. #endif
  395. }
  396. // Apply changes to update a quantity
  397. inline void Max7219_Quantity16(const uint8_t y, const uint8_t ov, const uint8_t nv) {
  398. for (uint8_t i = MIN(nv, ov); i < MAX(nv, ov); i++)
  399. #if MAX7219_X_LEDS == 8
  400. Max7219_LED_Set(i >> 1, y + (i & 1), nv >= ov); // single 8x8 LED matrix. Use two lines to get 16 LED's
  401. #else
  402. Max7219_LED_Set(i, y, nv >= ov); // LED matrix has at least 16 LED's on the X-Axis. Use single line of LED's
  403. #endif
  404. }
  405. void Max7219_idle_tasks() {
  406. #define MAX7219_USE_HEAD (defined(MAX7219_DEBUG_PLANNER_HEAD) || defined(MAX7219_DEBUG_PLANNER_QUEUE))
  407. #define MAX7219_USE_TAIL (defined(MAX7219_DEBUG_PLANNER_TAIL) || defined(MAX7219_DEBUG_PLANNER_QUEUE))
  408. #if MAX7219_USE_HEAD || MAX7219_USE_TAIL
  409. #ifndef CPU_32_BIT
  410. CRITICAL_SECTION_START;
  411. #endif
  412. #if MAX7219_USE_HEAD
  413. const uint8_t head = planner.block_buffer_head;
  414. #endif
  415. #if MAX7219_USE_TAIL
  416. const uint8_t tail = planner.block_buffer_tail;
  417. #endif
  418. #ifndef CPU_32_BIT
  419. CRITICAL_SECTION_END;
  420. #endif
  421. #endif
  422. #if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE)
  423. static uint8_t refresh_cnt; // = 0
  424. constexpr uint16_t refresh_limit = 5;
  425. static millis_t next_blink = 0;
  426. const millis_t ms = millis();
  427. const bool do_blink = ELAPSED(ms, next_blink);
  428. #else
  429. static uint16_t refresh_cnt; // = 0
  430. constexpr bool do_blink = true;
  431. constexpr uint16_t refresh_limit = 50000;
  432. #endif
  433. // Some Max7219 units are vulnerable to electrical noise, especially
  434. // with long wires next to high current wires. If the display becomes
  435. // corrupted, this will fix it within a couple seconds.
  436. if (do_blink && ++refresh_cnt >= refresh_limit) {
  437. refresh_cnt = 0;
  438. Max7219_register_setup();
  439. }
  440. #if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE)
  441. if (do_blink) {
  442. Max7219_LED_Toggle(MAX7219_X_LEDS - 1, MAX7219_Y_LEDS - 1);
  443. next_blink = ms + 1000;
  444. }
  445. #endif
  446. #if defined(MAX7219_DEBUG_PLANNER_HEAD) && defined(MAX7219_DEBUG_PLANNER_TAIL) && MAX7219_DEBUG_PLANNER_HEAD == MAX7219_DEBUG_PLANNER_TAIL
  447. static int16_t last_head_cnt = 0xF, last_tail_cnt = 0xF;
  448. if (last_head_cnt != head || last_tail_cnt != tail) {
  449. Max7219_Range16(MAX7219_DEBUG_PLANNER_HEAD, last_tail_cnt, tail, last_head_cnt, head);
  450. last_head_cnt = head;
  451. last_tail_cnt = tail;
  452. }
  453. #else
  454. #ifdef MAX7219_DEBUG_PLANNER_HEAD
  455. static int16_t last_head_cnt = 0x1;
  456. if (last_head_cnt != head) {
  457. Max7219_Mark16(MAX7219_DEBUG_PLANNER_HEAD, last_head_cnt, head);
  458. last_head_cnt = head;
  459. }
  460. #endif
  461. #ifdef MAX7219_DEBUG_PLANNER_TAIL
  462. static int16_t last_tail_cnt = 0x1;
  463. if (last_tail_cnt != tail) {
  464. Max7219_Mark16(MAX7219_DEBUG_PLANNER_TAIL, last_tail_cnt, tail);
  465. last_tail_cnt = tail;
  466. }
  467. #endif
  468. #endif
  469. #ifdef MAX7219_DEBUG_PLANNER_QUEUE
  470. static int16_t last_depth = 0;
  471. const int16_t current_depth = (head - tail + BLOCK_BUFFER_SIZE) & (BLOCK_BUFFER_SIZE - 1) & 0xF;
  472. if (current_depth != last_depth) {
  473. Max7219_Quantity16(MAX7219_DEBUG_PLANNER_QUEUE, last_depth, current_depth);
  474. last_depth = current_depth;
  475. }
  476. #endif
  477. }
  478. #endif // MAX7219_DEBUG