My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Max7219_Debug_LEDs.cpp 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. * send() 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/shared/Delay.h"
  44. Max7219 max7219;
  45. uint8_t Max7219::led_line[MAX7219_LINES]; // = { 0 };
  46. #define LINE_REG(Q) (max7219_reg_digit0 + ((Q) & 0x7))
  47. #if _ROT == 0 || _ROT == 270
  48. #define _LED_BIT(Q) (7 - ((Q) & 0x7))
  49. #define _LED_UNIT(Q) ((Q) & ~0x7)
  50. //#define _LED_UNIT(Q) ((MAX7219_NUMBER_UNITS - 1 - ((Q) >> 3)) << 3) // some Max7219 boards have rotated the matrix
  51. // this line can be substituted to correct orientation
  52. #else
  53. #define _LED_BIT(Q) ((Q) & 0x7)
  54. #define _LED_UNIT(Q) ((MAX7219_NUMBER_UNITS - 1 - ((Q) >> 3)) << 3)
  55. #endif
  56. #if _ROT < 180
  57. #define _LED_IND(P,Q) (_LED_UNIT(P) + (Q))
  58. #else
  59. #define _LED_IND(P,Q) (_LED_UNIT(P) + (7 - ((Q) & 0x7)))
  60. #endif
  61. #if _ROT == 0 || _ROT == 180
  62. #define LED_IND(X,Y) _LED_IND(X,Y)
  63. #define LED_BIT(X,Y) _LED_BIT(X)
  64. #elif _ROT == 90 || _ROT == 270
  65. #define LED_IND(X,Y) _LED_IND(Y,X)
  66. #define LED_BIT(X,Y) _LED_BIT(Y)
  67. #endif
  68. #define XOR_7219(X,Y) do{ led_line[LED_IND(X,Y)] ^= _BV(LED_BIT(X,Y)); }while(0)
  69. #define SET_7219(X,Y) do{ led_line[LED_IND(X,Y)] |= _BV(LED_BIT(X,Y)); }while(0)
  70. #define CLR_7219(X,Y) do{ led_line[LED_IND(X,Y)] &= ~_BV(LED_BIT(X,Y)); }while(0)
  71. #define BIT_7219(X,Y) TEST(led_line[LED_IND(X,Y)], LED_BIT(X,Y))
  72. #ifdef CPU_32_BIT
  73. #define SIG_DELAY() DELAY_US(1) // Approximate a 1µs delay on 32-bit ARM
  74. #undef CRITICAL_SECTION_START
  75. #undef CRITICAL_SECTION_END
  76. #define CRITICAL_SECTION_START NOOP
  77. #define CRITICAL_SECTION_END NOOP
  78. #else
  79. #define SIG_DELAY() DELAY_NS(188) // Delay for 0.1875µs (16MHz AVR) or 0.15µs (20MHz AVR)
  80. #endif
  81. void Max7219::error(const char * const func, const int32_t v1, const int32_t v2/*=-1*/) {
  82. #if ENABLED(MAX7219_ERRORS)
  83. SERIAL_ECHOPGM("??? Max7219::");
  84. serialprintPGM(func);
  85. SERIAL_CHAR('(');
  86. SERIAL_ECHO(v1);
  87. if (v2 > 0) SERIAL_ECHOPAIR(", ", v2);
  88. SERIAL_CHAR(')');
  89. SERIAL_EOL();
  90. #else
  91. UNUSED(func); UNUSED(v1); UNUSED(v2);
  92. #endif
  93. }
  94. /**
  95. * Flip the lowest n_bytes of the supplied bits:
  96. * flipped(x, 1) flips the low 8 bits of x.
  97. * flipped(x, 2) flips the low 16 bits of x.
  98. * flipped(x, 3) flips the low 24 bits of x.
  99. * flipped(x, 4) flips the low 32 bits of x.
  100. */
  101. inline uint32_t flipped(const uint32_t bits, const uint8_t n_bytes) {
  102. uint32_t mask = 1, outbits = 0;
  103. for (uint8_t b = 0; b < n_bytes * 8; b++) {
  104. outbits <<= 1;
  105. if (bits & mask) outbits |= 1;
  106. mask <<= 1;
  107. }
  108. return outbits;
  109. }
  110. void Max7219::noop() {
  111. CRITICAL_SECTION_START;
  112. SIG_DELAY();
  113. WRITE(MAX7219_DIN_PIN, LOW);
  114. for (uint8_t i = 16; i--;) {
  115. SIG_DELAY();
  116. WRITE(MAX7219_CLK_PIN, LOW);
  117. SIG_DELAY();
  118. SIG_DELAY();
  119. WRITE(MAX7219_CLK_PIN, HIGH);
  120. SIG_DELAY();
  121. }
  122. CRITICAL_SECTION_END;
  123. }
  124. void Max7219::putbyte(uint8_t data) {
  125. CRITICAL_SECTION_START;
  126. for (uint8_t i = 8; i--;) {
  127. SIG_DELAY();
  128. WRITE(MAX7219_CLK_PIN, LOW); // tick
  129. SIG_DELAY();
  130. WRITE(MAX7219_DIN_PIN, (data & 0x80) ? HIGH : LOW); // send 1 or 0 based on data bit
  131. SIG_DELAY();
  132. WRITE(MAX7219_CLK_PIN, HIGH); // tock
  133. SIG_DELAY();
  134. data <<= 1;
  135. }
  136. CRITICAL_SECTION_END;
  137. }
  138. void Max7219::pulse_load() {
  139. SIG_DELAY();
  140. WRITE(MAX7219_LOAD_PIN, LOW); // tell the chip to load the data
  141. SIG_DELAY();
  142. WRITE(MAX7219_LOAD_PIN, HIGH);
  143. SIG_DELAY();
  144. }
  145. void Max7219::send(const uint8_t reg, const uint8_t data) {
  146. SIG_DELAY();
  147. CRITICAL_SECTION_START;
  148. SIG_DELAY();
  149. putbyte(reg); // specify register
  150. SIG_DELAY();
  151. putbyte(data); // put data
  152. CRITICAL_SECTION_END;
  153. }
  154. // Send out a single native row of bits to all units
  155. void Max7219::refresh_line(const uint8_t line) {
  156. for (uint8_t u = MAX7219_NUMBER_UNITS; u--;)
  157. send(LINE_REG(line), led_line[(u << 3) | (line & 0x7)]);
  158. pulse_load();
  159. }
  160. // Send out a single native row of bits to just one unit
  161. void Max7219::refresh_unit_line(const uint8_t line) {
  162. for (uint8_t u = MAX7219_NUMBER_UNITS; u--;)
  163. if (u == (line >> 3)) send(LINE_REG(line), led_line[line]); else noop();
  164. pulse_load();
  165. }
  166. void Max7219::set(const uint8_t line, const uint8_t bits) {
  167. led_line[line] = bits;
  168. refresh_line(line);
  169. }
  170. #if ENABLED(MAX7219_NUMERIC)
  171. // Draw an integer with optional leading zeros and optional decimal point
  172. void Max7219::print(const uint8_t start, int16_t value, uint8_t size, const bool leadzero=false, bool dec=false) {
  173. constexpr uint8_t led_numeral[10] = { 0x7E, 0x60, 0x6D, 0x79, 0x63, 0x5B, 0x5F, 0x70, 0x7F, 0x7A },
  174. led_decimal = 0x80, led_minus = 0x01;
  175. bool blank = false, neg = value < 0;
  176. if (neg) value *= -1;
  177. while (size--) {
  178. const bool minus = neg && blank;
  179. if (minus) neg = false;
  180. send(
  181. max7219_reg_digit0 + start + size,
  182. minus ? led_minus : blank ? 0x00 : led_numeral[value % 10] | (dec ? led_decimal : 0x00)
  183. );
  184. pulse_load(); // tell the chips to load the clocked out data
  185. value /= 10;
  186. if (!value && !leadzero) blank = true;
  187. dec = false;
  188. }
  189. }
  190. // Draw a float with a decimal point and optional digits
  191. void Max7219::print(const uint8_t start, const float value, const uint8_t pre_size, const uint8_t post_size, const bool leadzero=false) {
  192. if (pre_size) print(start, value, pre_size, leadzero, !!post_size);
  193. if (post_size) {
  194. const int16_t after = ABS(value) * (10 ^ post_size);
  195. print(start + pre_size, after, post_size, true);
  196. }
  197. }
  198. #endif // MAX7219_NUMERIC
  199. // Modify a single LED bit and send the changed line
  200. void Max7219::led_set(const uint8_t x, const uint8_t y, const bool on) {
  201. if (x > MAX7219_X_LEDS - 1 || y > MAX7219_Y_LEDS - 1) return error(PSTR("led_set"), x, y);
  202. if (BIT_7219(x, y) == on) return;
  203. XOR_7219(x, y);
  204. refresh_line(LED_IND(x, y));
  205. }
  206. void Max7219::led_on(const uint8_t x, const uint8_t y) {
  207. if (x > MAX7219_X_LEDS - 1 || y > MAX7219_Y_LEDS - 1) return error(PSTR("led_on"), x, y);
  208. led_set(x, y, true);
  209. }
  210. void Max7219::led_off(const uint8_t x, const uint8_t y) {
  211. if (x > MAX7219_X_LEDS - 1 || y > MAX7219_Y_LEDS - 1) return error(PSTR("led_off"), x, y);
  212. led_set(x, y, false);
  213. }
  214. void Max7219::led_toggle(const uint8_t x, const uint8_t y) {
  215. if (x > MAX7219_X_LEDS - 1 || y > MAX7219_Y_LEDS - 1) return error(PSTR("led_toggle"), x, y);
  216. led_set(x, y, !BIT_7219(x, y));
  217. }
  218. void Max7219::send_row(const uint8_t row) {
  219. #if _ROT == 0 || _ROT == 180
  220. refresh_line(LED_IND(0, row));
  221. #else
  222. UNUSED(row);
  223. refresh();
  224. #endif
  225. }
  226. void Max7219::send_column(const uint8_t col) {
  227. #if _ROT == 90 || _ROT == 270
  228. refresh_line(LED_IND(col, 0));
  229. #else
  230. UNUSED(col);
  231. refresh();
  232. #endif
  233. }
  234. void Max7219::clear() {
  235. ZERO(led_line);
  236. refresh();
  237. }
  238. void Max7219::fill() {
  239. memset(led_line, 0xFF, sizeof(led_line));
  240. refresh();
  241. }
  242. void Max7219::clear_row(const uint8_t row) {
  243. if (row >= MAX7219_Y_LEDS) return error(PSTR("clear_row"), row);
  244. for (uint8_t x = 0; x < MAX7219_X_LEDS; x++) CLR_7219(x, row);
  245. send_row(row);
  246. }
  247. void Max7219::clear_column(const uint8_t col) {
  248. if (col >= MAX7219_X_LEDS) return error(PSTR("set_column"), col);
  249. for (uint8_t y = 0; y < MAX7219_Y_LEDS; y++) CLR_7219(col, y);
  250. send_column(col);
  251. }
  252. /**
  253. * Plot the low order bits of val to the specified row of the matrix.
  254. * With 4 Max7219 units in the chain, it's possible to set 32 bits at once with
  255. * one call to the function (if rotated 90° or 180°).
  256. */
  257. void Max7219::set_row(const uint8_t row, const uint32_t val) {
  258. if (row >= MAX7219_Y_LEDS) return error(PSTR("set_row"), row);
  259. uint32_t mask = _BV32(MAX7219_X_LEDS - 1);
  260. for (uint8_t x = 0; x < MAX7219_X_LEDS; x++) {
  261. if (val & mask) SET_7219(x, row); else CLR_7219(x, row);
  262. mask >>= 1;
  263. }
  264. send_row(row);
  265. }
  266. /**
  267. * Plot the low order bits of val to the specified column of the matrix.
  268. * With 4 Max7219 units in the chain, it's possible to set 32 bits at once with
  269. * one call to the function (if rotated 90° or 180°).
  270. */
  271. void Max7219::set_column(const uint8_t col, const uint32_t val) {
  272. if (col >= MAX7219_X_LEDS) return error(PSTR("set_column"), col);
  273. uint32_t mask = _BV32(MAX7219_Y_LEDS - 1);
  274. for (uint8_t y = 0; y < MAX7219_Y_LEDS; y++) {
  275. if (val & mask) SET_7219(col, y); else CLR_7219(col, y);
  276. mask >>= 1;
  277. }
  278. send_column(col);
  279. }
  280. void Max7219::set_rows_16bits(const uint8_t y, uint32_t val) {
  281. #if MAX7219_X_LEDS == 8
  282. if (y > MAX7219_Y_LEDS - 2) return error(PSTR("set_rows_16bits"), y, val);
  283. set_row(y + 1, val); val >>= 8;
  284. set_row(y + 0, val);
  285. #else // at least 16 bits on each row
  286. if (y > MAX7219_Y_LEDS - 1) return error(PSTR("set_rows_16bits"), y, val);
  287. set_row(y, val);
  288. #endif
  289. }
  290. void Max7219::set_rows_32bits(const uint8_t y, uint32_t val) {
  291. #if MAX7219_X_LEDS == 8
  292. if (y > MAX7219_Y_LEDS - 4) return error(PSTR("set_rows_32bits"), y, val);
  293. set_row(y + 3, val); val >>= 8;
  294. set_row(y + 2, val); val >>= 8;
  295. set_row(y + 1, val); val >>= 8;
  296. set_row(y + 0, val);
  297. #elif MAX7219_X_LEDS == 16
  298. if (y > MAX7219_Y_LEDS - 2) return error(PSTR("set_rows_32bits"), y, val);
  299. set_row(y + 1, val); val >>= 16;
  300. set_row(y + 0, val);
  301. #else // at least 24 bits on each row. In the 3 matrix case, just display the low 24 bits
  302. if (y > MAX7219_Y_LEDS - 1) return error(PSTR("set_rows_32bits"), y, val);
  303. set_row(y, val);
  304. #endif
  305. }
  306. void Max7219::set_columns_16bits(const uint8_t x, uint32_t val) {
  307. #if MAX7219_Y_LEDS == 8
  308. if (x > MAX7219_X_LEDS - 2) return error(PSTR("set_columns_16bits"), x, val);
  309. set_column(x + 0, val); val >>= 8;
  310. set_column(x + 1, val);
  311. #else // at least 16 bits in each column
  312. if (x > MAX7219_X_LEDS - 1) return error(PSTR("set_columns_16bits"), x, val);
  313. set_column(x, val);
  314. #endif
  315. }
  316. void Max7219::set_columns_32bits(const uint8_t x, uint32_t val) {
  317. #if MAX7219_Y_LEDS == 8
  318. if (x > MAX7219_X_LEDS - 4) return error(PSTR("set_rows_32bits"), x, val);
  319. set_column(x + 3, val); val >>= 8;
  320. set_column(x + 2, val); val >>= 8;
  321. set_column(x + 1, val); val >>= 8;
  322. set_column(x + 0, val);
  323. #elif MAX7219_Y_LEDS == 16
  324. if (x > MAX7219_X_LEDS - 2) return error(PSTR("set_rows_32bits"), x, val);
  325. set_column(x + 1, val); val >>= 16;
  326. set_column(x + 0, val);
  327. #else // at least 24 bits on each row. In the 3 matrix case, just display the low 24 bits
  328. if (x > MAX7219_X_LEDS - 1) return error(PSTR("set_rows_32bits"), x, val);
  329. set_column(x, val);
  330. #endif
  331. }
  332. // Initialize the Max7219
  333. void Max7219::register_setup() {
  334. for (uint8_t i = 0; i < MAX7219_NUMBER_UNITS; i++)
  335. send(max7219_reg_scanLimit, 0x07);
  336. pulse_load(); // tell the chips to load the clocked out data
  337. for (uint8_t i = 0; i < MAX7219_NUMBER_UNITS; i++)
  338. send(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits)
  339. pulse_load(); // tell the chips to load the clocked out data
  340. for (uint8_t i = 0; i < MAX7219_NUMBER_UNITS; i++)
  341. send(max7219_reg_shutdown, 0x01); // not in shutdown mode
  342. pulse_load(); // tell the chips to load the clocked out data
  343. for (uint8_t i = 0; i < MAX7219_NUMBER_UNITS; i++)
  344. send(max7219_reg_displayTest, 0x00); // no display test
  345. pulse_load(); // tell the chips to load the clocked out data
  346. for (uint8_t i = 0; i < MAX7219_NUMBER_UNITS; i++)
  347. send(max7219_reg_intensity, 0x01 & 0x0F); // the first 0x0F is the value you can set
  348. // range: 0x00 to 0x0F
  349. pulse_load(); // tell the chips to load the clocked out data
  350. }
  351. #ifdef MAX7219_INIT_TEST
  352. #if MAX7219_INIT_TEST == 2
  353. void Max7219::spiral(const bool on, const uint16_t del) {
  354. constexpr int8_t way[] = { 1, 0, 0, 1, -1, 0, 0, -1 };
  355. int8_t px = 0, py = 0, dir = 0;
  356. for (uint8_t i = MAX7219_X_LEDS * MAX7219_Y_LEDS; i--;) {
  357. led_set(px, py, on);
  358. delay(del);
  359. const int8_t x = px + way[dir], y = py + way[dir + 1];
  360. if (!WITHIN(x, 0, MAX7219_X_LEDS-1) || !WITHIN(y, 0, MAX7219_Y_LEDS-1) || BIT_7219(x, y) == on) dir = (dir + 2) & 0x7;
  361. px += way[dir]; py += way[dir + 1];
  362. }
  363. }
  364. #else
  365. void Max7219::sweep(const int8_t dir, const uint16_t ms, const bool on) {
  366. uint8_t x = dir > 0 ? 0 : MAX7219_X_LEDS-1;
  367. for (uint8_t i = MAX7219_X_LEDS; i--; x += dir) {
  368. set_column(x, on ? 0xFFFFFFFF : 0x00000000);
  369. delay(ms);
  370. }
  371. }
  372. #endif
  373. #endif // MAX7219_INIT_TEST
  374. void Max7219::init() {
  375. SET_OUTPUT(MAX7219_DIN_PIN);
  376. SET_OUTPUT(MAX7219_CLK_PIN);
  377. OUT_WRITE(MAX7219_LOAD_PIN, HIGH);
  378. delay(1);
  379. register_setup();
  380. for (uint8_t i = 0; i <= 7; i++) { // Empty registers to turn all LEDs off
  381. led_line[i] = 0x00;
  382. send(max7219_reg_digit0 + i, 0);
  383. pulse_load(); // tell the chips to load the clocked out data
  384. }
  385. #ifdef MAX7219_INIT_TEST
  386. #if MAX7219_INIT_TEST == 2
  387. spiral(true, 8);
  388. delay(150);
  389. spiral(false, 8);
  390. #else
  391. // Do an aesthetically-pleasing pattern to fully test the Max7219 module and LEDs.
  392. // Light up and turn off columns, both forward and backward.
  393. sweep(1, 20, true);
  394. sweep(1, 20, false);
  395. delay(150);
  396. sweep(-1, 20, true);
  397. sweep(-1, 20, false);
  398. #endif
  399. #endif
  400. }
  401. /**
  402. * This code demonstrates some simple debugging using a single 8x8 LED Matrix. If your feature could
  403. * benefit from matrix display, add its code here. Very little processing is required, so the 7219 is
  404. * ideal for debugging when realtime feedback is important but serial output can't be used.
  405. */
  406. // Apply changes to update a marker
  407. void Max7219::mark16(const uint8_t y, const uint8_t v1, const uint8_t v2) {
  408. #if MAX7219_X_LEDS == 8
  409. #if MAX7219_Y_LEDS == 8
  410. led_off(v1 & 0x7, y + (v1 >= 8));
  411. led_on(v2 & 0x7, y + (v2 >= 8));
  412. #else
  413. led_off(y, v1 & 0xF); // At least 16 LEDs down. Use a single column.
  414. led_on(y, v2 & 0xF);
  415. #endif
  416. #else
  417. led_off(v1 & 0xF, y); // At least 16 LEDs across. Use a single row.
  418. led_on(v2 & 0xF, y);
  419. #endif
  420. }
  421. // Apply changes to update a tail-to-head range
  422. void Max7219::range16(const uint8_t y, const uint8_t ot, const uint8_t nt, const uint8_t oh, const uint8_t nh) {
  423. #if MAX7219_X_LEDS == 8
  424. #if MAX7219_Y_LEDS == 8
  425. if (ot != nt) for (uint8_t n = ot & 0xF; n != (nt & 0xF) && n != (nh & 0xF); n = (n + 1) & 0xF)
  426. led_off(n & 0x7, y + (n >= 8));
  427. if (oh != nh) for (uint8_t n = (oh + 1) & 0xF; n != ((nh + 1) & 0xF); n = (n + 1) & 0xF)
  428. led_on(n & 0x7, y + (n >= 8));
  429. #else // The Max7219 Y-Axis has at least 16 LED's. So use a single column
  430. if (ot != nt) for (uint8_t n = ot & 0xF; n != (nt & 0xF) && n != (nh & 0xF); n = (n + 1) & 0xF)
  431. led_off(y, n & 0xF);
  432. if (oh != nh) for (uint8_t n = (oh + 1) & 0xF; n != ((nh + 1) & 0xF); n = (n + 1) & 0xF)
  433. led_on(y, n & 0xF);
  434. #endif
  435. #else // LED matrix has at least 16 LED's on the X-Axis. Use single line of LED's
  436. if (ot != nt) for (uint8_t n = ot & 0xF; n != (nt & 0xF) && n != (nh & 0xF); n = (n + 1) & 0xF)
  437. led_off(n & 0xF, y);
  438. if (oh != nh) for (uint8_t n = (oh + 1) & 0xF; n != ((nh + 1) & 0xF); n = (n + 1) & 0xF)
  439. led_on(n & 0xF, y);
  440. #endif
  441. }
  442. // Apply changes to update a quantity
  443. void Max7219::quantity16(const uint8_t y, const uint8_t ov, const uint8_t nv) {
  444. for (uint8_t i = MIN(nv, ov); i < MAX(nv, ov); i++)
  445. #if MAX7219_X_LEDS == 8
  446. #if MAX7219_Y_LEDS == 8
  447. led_set(i >> 1, y + (i & 1), nv >= ov); // single 8x8 LED matrix. Use two lines to get 16 LED's
  448. #else
  449. led_set(y, i, nv >= ov); // The Max7219 Y-Axis has at least 16 LED's. So use a single column
  450. #endif
  451. #else
  452. led_set(i, y, nv >= ov); // LED matrix has at least 16 LED's on the X-Axis. Use single line of LED's
  453. #endif
  454. }
  455. void Max7219::idle_tasks() {
  456. #define MAX7219_USE_HEAD (defined(MAX7219_DEBUG_PLANNER_HEAD) || defined(MAX7219_DEBUG_PLANNER_QUEUE))
  457. #define MAX7219_USE_TAIL (defined(MAX7219_DEBUG_PLANNER_TAIL) || defined(MAX7219_DEBUG_PLANNER_QUEUE))
  458. #if MAX7219_USE_HEAD || MAX7219_USE_TAIL
  459. CRITICAL_SECTION_START;
  460. #if MAX7219_USE_HEAD
  461. const uint8_t head = planner.block_buffer_head;
  462. #endif
  463. #if MAX7219_USE_TAIL
  464. const uint8_t tail = planner.block_buffer_tail;
  465. #endif
  466. CRITICAL_SECTION_END;
  467. #endif
  468. #if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE)
  469. static uint8_t refresh_cnt; // = 0
  470. constexpr uint16_t refresh_limit = 5;
  471. static millis_t next_blink = 0;
  472. const millis_t ms = millis();
  473. const bool do_blink = ELAPSED(ms, next_blink);
  474. #else
  475. static uint16_t refresh_cnt; // = 0
  476. constexpr bool do_blink = true;
  477. constexpr uint16_t refresh_limit = 50000;
  478. #endif
  479. // Some Max7219 units are vulnerable to electrical noise, especially
  480. // with long wires next to high current wires. If the display becomes
  481. // corrupted, this will fix it within a couple seconds.
  482. if (do_blink && ++refresh_cnt >= refresh_limit) {
  483. refresh_cnt = 0;
  484. register_setup();
  485. }
  486. #if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE)
  487. if (do_blink) {
  488. led_toggle(MAX7219_X_LEDS - 1, MAX7219_Y_LEDS - 1);
  489. next_blink = ms + 1000;
  490. }
  491. #endif
  492. #if defined(MAX7219_DEBUG_PLANNER_HEAD) && defined(MAX7219_DEBUG_PLANNER_TAIL) && MAX7219_DEBUG_PLANNER_HEAD == MAX7219_DEBUG_PLANNER_TAIL
  493. static int16_t last_head_cnt = 0xF, last_tail_cnt = 0xF;
  494. if (last_head_cnt != head || last_tail_cnt != tail) {
  495. range16(MAX7219_DEBUG_PLANNER_HEAD, last_tail_cnt, tail, last_head_cnt, head);
  496. last_head_cnt = head;
  497. last_tail_cnt = tail;
  498. }
  499. #else
  500. #ifdef MAX7219_DEBUG_PLANNER_HEAD
  501. static int16_t last_head_cnt = 0x1;
  502. if (last_head_cnt != head) {
  503. mark16(MAX7219_DEBUG_PLANNER_HEAD, last_head_cnt, head);
  504. last_head_cnt = head;
  505. }
  506. #endif
  507. #ifdef MAX7219_DEBUG_PLANNER_TAIL
  508. static int16_t last_tail_cnt = 0x1;
  509. if (last_tail_cnt != tail) {
  510. mark16(MAX7219_DEBUG_PLANNER_TAIL, last_tail_cnt, tail);
  511. last_tail_cnt = tail;
  512. }
  513. #endif
  514. #endif
  515. #ifdef MAX7219_DEBUG_PLANNER_QUEUE
  516. static int16_t last_depth = 0;
  517. const int16_t current_depth = (head - tail + BLOCK_BUFFER_SIZE) & (BLOCK_BUFFER_SIZE - 1) & 0xF;
  518. if (current_depth != last_depth) {
  519. quantity16(MAX7219_DEBUG_PLANNER_QUEUE, last_depth, current_depth);
  520. last_depth = current_depth;
  521. }
  522. #endif
  523. }
  524. #endif // MAX7219_DEBUG