Max7219_Debug_LEDs.cpp 19 KB

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