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

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