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

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