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

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