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 22KB

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