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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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 "../MarlinCore.h"
  42. #include "../HAL/shared/Delay.h"
  43. #if ENABLED(MAX7219_SIDE_BY_SIDE) && MAX7219_NUMBER_UNITS > 1
  44. #define HAS_SIDE_BY_SIDE 1
  45. #endif
  46. #define _ROT ((MAX7219_ROTATE + 360) % 360)
  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. #ifdef MAX7219_DEBUG_PROFILE
  57. CodeProfiler::Mode CodeProfiler::mode = ACCUMULATE_AVERAGE;
  58. uint8_t CodeProfiler::instance_count = 0;
  59. uint32_t CodeProfiler::last_calc_time = micros();
  60. uint8_t CodeProfiler::time_fraction = 0;
  61. uint32_t CodeProfiler::total_time = 0;
  62. uint16_t CodeProfiler::call_count = 0;
  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) == DISABLED(MAX7219_REVERSE_EACH)
  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(250)
  118. #endif
  119. void Max7219::error(FSTR_P const func, const int32_t v1, const int32_t v2/*=-1*/) {
  120. #if ENABLED(MAX7219_ERRORS)
  121. SERIAL_ECHOPGM("??? Max7219::");
  122. SERIAL_ECHOF(func, AS_CHAR('('));
  123. SERIAL_ECHO(v1);
  124. if (v2 > 0) SERIAL_ECHOPGM(", ", 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. LOOP_L_N(b, n_bytes * 8) {
  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 (suspended) return;
  194. #if MAX7219_NUMBER_UNITS == 1
  195. send(LINE_REG(line), led_line[line]);
  196. #else
  197. for (uint8_t u = MAX7219_NUMBER_UNITS; u--;)
  198. if (u == (line >> 3)) send(LINE_REG(line), led_line[line]); else noop();
  199. #endif
  200. pulse_load();
  201. }
  202. // Send out a single native row of bits to all units
  203. void Max7219::refresh_line(const uint8_t line) {
  204. if (suspended) return;
  205. #if MAX7219_NUMBER_UNITS == 1
  206. refresh_unit_line(line);
  207. #else
  208. for (uint8_t u = MAX7219_NUMBER_UNITS; u--;)
  209. send(LINE_REG(line), led_line[(u << 3) | (line & 0x7)]);
  210. #endif
  211. pulse_load();
  212. }
  213. void Max7219::set(const uint8_t line, const uint8_t bits) {
  214. led_line[line] = bits;
  215. refresh_unit_line(line);
  216. }
  217. #if ENABLED(MAX7219_NUMERIC)
  218. // Draw an integer with optional leading zeros and optional decimal point
  219. void Max7219::print(const uint8_t start, int16_t value, uint8_t size, const bool leadzero=false, bool dec=false) {
  220. if (suspended) return;
  221. constexpr uint8_t led_numeral[10] = { 0x7E, 0x60, 0x6D, 0x79, 0x63, 0x5B, 0x5F, 0x70, 0x7F, 0x7A },
  222. led_decimal = 0x80, led_minus = 0x01;
  223. bool blank = false, neg = value < 0;
  224. if (neg) value *= -1;
  225. while (size--) {
  226. const bool minus = neg && blank;
  227. if (minus) neg = false;
  228. send(
  229. max7219_reg_digit0 + start + size,
  230. minus ? led_minus : blank ? 0x00 : led_numeral[value % 10] | (dec ? led_decimal : 0x00)
  231. );
  232. pulse_load(); // tell the chips to load the clocked out data
  233. value /= 10;
  234. if (!value && !leadzero) blank = true;
  235. dec = false;
  236. }
  237. }
  238. // Draw a float with a decimal point and optional digits
  239. 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) {
  240. if (pre_size) print(start, value, pre_size, leadzero, !!post_size);
  241. if (post_size) {
  242. const int16_t after = ABS(value) * (10 ^ post_size);
  243. print(start + pre_size, after, post_size, true);
  244. }
  245. }
  246. #endif // MAX7219_NUMERIC
  247. // Modify a single LED bit and send the changed line
  248. void Max7219::led_set(const uint8_t x, const uint8_t y, const bool on, uint8_t * const rcm/*=nullptr*/) {
  249. if (x >= MAX7219_X_LEDS || y >= MAX7219_Y_LEDS) return error(F("led_set"), x, y);
  250. if (BIT_7219(x, y) == on) return;
  251. XOR_7219(x, y);
  252. refresh_unit_line(LED_IND(x, y));
  253. if (rcm != nullptr) *rcm |= _BV(LED_IND(x, y) & 0x07);
  254. }
  255. void Max7219::led_on(const uint8_t x, const uint8_t y, uint8_t * const rcm/*=nullptr*/) {
  256. if (x >= MAX7219_X_LEDS || y >= MAX7219_Y_LEDS) return error(F("led_on"), x, y);
  257. led_set(x, y, true, rcm);
  258. }
  259. void Max7219::led_off(const uint8_t x, const uint8_t y, uint8_t * const rcm/*=nullptr*/) {
  260. if (x >= MAX7219_X_LEDS || y >= MAX7219_Y_LEDS) return error(F("led_off"), x, y);
  261. led_set(x, y, false, rcm);
  262. }
  263. void Max7219::led_toggle(const uint8_t x, const uint8_t y, uint8_t * const rcm/*=nullptr*/) {
  264. if (x >= MAX7219_X_LEDS || y >= MAX7219_Y_LEDS) return error(F("led_toggle"), x, y);
  265. led_set(x, y, !BIT_7219(x, y), rcm);
  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(F("clear_row"), row);
  303. LOOP_L_N(x, MAX7219_X_LEDS) 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(F("set_column"), col);
  308. LOOP_L_N(y, MAX7219_Y_LEDS) 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(F("set_row"), row);
  318. uint32_t mask = _BV32(MAX7219_X_LEDS - 1);
  319. LOOP_L_N(x, MAX7219_X_LEDS) {
  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(F("set_column"), col);
  332. uint32_t mask = _BV32(MAX7219_Y_LEDS - 1);
  333. LOOP_L_N(y, MAX7219_Y_LEDS) {
  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(F("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(F("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(F("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(F("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(F("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(F("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(F("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(F("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(F("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(F("set_rows_32bits"), x, val);
  388. set_column(x, val);
  389. #endif
  390. }
  391. // Initialize the Max7219
  392. void Max7219::register_setup() {
  393. LOOP_L_N(i, MAX7219_NUMBER_UNITS)
  394. send(max7219_reg_scanLimit, 0x07);
  395. pulse_load(); // Tell the chips to load the clocked out data
  396. LOOP_L_N(i, MAX7219_NUMBER_UNITS)
  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. LOOP_L_N(i, MAX7219_NUMBER_UNITS)
  400. send(max7219_reg_shutdown, 0x01); // Not in shutdown mode
  401. pulse_load(); // Tell the chips to load the clocked out data
  402. LOOP_L_N(i, MAX7219_NUMBER_UNITS)
  403. send(max7219_reg_displayTest, 0x00); // No display test
  404. pulse_load(); // Tell the chips to load the clocked out data
  405. LOOP_L_N(i, MAX7219_NUMBER_UNITS)
  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. #if 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. clear();
  483. #if MAX7219_INIT_TEST
  484. start_test_pattern();
  485. #endif
  486. }
  487. /**
  488. * This code demonstrates some simple debugging using a single 8x8 LED Matrix. If your feature could
  489. * benefit from matrix display, add its code here. Very little processing is required, so the 7219 is
  490. * ideal for debugging when realtime feedback is important but serial output can't be used.
  491. */
  492. // Apply changes to update a marker
  493. void Max7219::mark16(const uint8_t pos, const uint8_t v1, const uint8_t v2, uint8_t * const rcm/*=nullptr*/) {
  494. #if MAX7219_X_LEDS > 8 // At least 16 LEDs on the X-Axis. Use single line.
  495. led_off(v1 & 0xF, pos, rcm);
  496. led_on(v2 & 0xF, pos, rcm);
  497. #elif MAX7219_Y_LEDS > 8 // At least 16 LEDs on the Y-Axis. Use a single column.
  498. led_off(pos, v1 & 0xF, rcm);
  499. led_on(pos, v2 & 0xF, rcm);
  500. #else // Single 8x8 LED matrix. Use two lines to get 16 LEDs.
  501. led_off(v1 & 0x7, pos + (v1 >= 8), rcm);
  502. led_on(v2 & 0x7, pos + (v2 >= 8), rcm);
  503. #endif
  504. }
  505. // Apply changes to update a tail-to-head range
  506. void Max7219::range16(const uint8_t y, const uint8_t ot, const uint8_t nt, const uint8_t oh,
  507. const uint8_t nh, uint8_t * const rcm/*=nullptr*/) {
  508. #if MAX7219_X_LEDS > 8 // At least 16 LEDs on the X-Axis. Use single line.
  509. if (ot != nt) for (uint8_t n = ot & 0xF; n != (nt & 0xF) && n != (nh & 0xF); n = (n + 1) & 0xF)
  510. led_off(n & 0xF, y, rcm);
  511. if (oh != nh) for (uint8_t n = (oh + 1) & 0xF; n != ((nh + 1) & 0xF); n = (n + 1) & 0xF)
  512. led_on(n & 0xF, y, rcm);
  513. #elif MAX7219_Y_LEDS > 8 // At least 16 LEDs on the Y-Axis. Use a single column.
  514. if (ot != nt) for (uint8_t n = ot & 0xF; n != (nt & 0xF) && n != (nh & 0xF); n = (n + 1) & 0xF)
  515. led_off(y, n & 0xF, rcm);
  516. if (oh != nh) for (uint8_t n = (oh + 1) & 0xF; n != ((nh + 1) & 0xF); n = (n + 1) & 0xF)
  517. led_on(y, n & 0xF, rcm);
  518. #else // Single 8x8 LED matrix. Use two lines to get 16 LEDs.
  519. if (ot != nt) for (uint8_t n = ot & 0xF; n != (nt & 0xF) && n != (nh & 0xF); n = (n + 1) & 0xF)
  520. led_off(n & 0x7, y + (n >= 8), rcm);
  521. if (oh != nh) for (uint8_t n = (oh + 1) & 0xF; n != ((nh + 1) & 0xF); n = (n + 1) & 0xF)
  522. led_on(n & 0x7, y + (n >= 8), rcm);
  523. #endif
  524. }
  525. // Apply changes to update a quantity
  526. void Max7219::quantity(const uint8_t pos, const uint8_t ov, const uint8_t nv, uint8_t * const rcm/*=nullptr*/) {
  527. for (uint8_t i = _MIN(nv, ov); i < _MAX(nv, ov); i++)
  528. led_set(
  529. #if MAX7219_X_LEDS >= MAX7219_Y_LEDS
  530. i, pos // Single matrix or multiple matrices in Landscape
  531. #else
  532. pos, i // Multiple matrices in Portrait
  533. #endif
  534. , nv >= ov
  535. , rcm
  536. );
  537. }
  538. void Max7219::quantity16(const uint8_t pos, const uint8_t ov, const uint8_t nv, uint8_t * const rcm/*=nullptr*/) {
  539. for (uint8_t i = _MIN(nv, ov); i < _MAX(nv, ov); i++)
  540. led_set(
  541. #if MAX7219_X_LEDS > 8 // At least 16 LEDs on the X-Axis. Use single line.
  542. i, pos
  543. #elif MAX7219_Y_LEDS > 8 // At least 16 LEDs on the Y-Axis. Use a single column.
  544. pos, i
  545. #else // Single 8x8 LED matrix. Use two lines to get 16 LEDs.
  546. i >> 1, pos + (i & 1)
  547. #endif
  548. , nv >= ov
  549. , rcm
  550. );
  551. }
  552. void Max7219::idle_tasks() {
  553. #define MAX7219_USE_HEAD (defined(MAX7219_DEBUG_PLANNER_HEAD) || defined(MAX7219_DEBUG_PLANNER_QUEUE))
  554. #define MAX7219_USE_TAIL (defined(MAX7219_DEBUG_PLANNER_TAIL) || defined(MAX7219_DEBUG_PLANNER_QUEUE))
  555. #if MAX7219_USE_HEAD || MAX7219_USE_TAIL
  556. CRITICAL_SECTION_START();
  557. #if MAX7219_USE_HEAD
  558. const uint8_t head = planner.block_buffer_head;
  559. #endif
  560. #if MAX7219_USE_TAIL
  561. const uint8_t tail = planner.block_buffer_tail;
  562. #endif
  563. CRITICAL_SECTION_END();
  564. #endif
  565. #if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE)
  566. static uint8_t refresh_cnt; // = 0
  567. constexpr uint16_t refresh_limit = 5;
  568. static millis_t next_blink = 0;
  569. const millis_t ms = millis();
  570. const bool do_blink = ELAPSED(ms, next_blink);
  571. #else
  572. static uint16_t refresh_cnt; // = 0
  573. constexpr bool do_blink = true;
  574. constexpr uint16_t refresh_limit = 50000;
  575. #endif
  576. // Some Max7219 units are vulnerable to electrical noise, especially
  577. // with long wires next to high current wires. If the display becomes
  578. // corrupted, this will fix it within a couple seconds.
  579. if (do_blink && ++refresh_cnt >= refresh_limit) {
  580. refresh_cnt = 0;
  581. register_setup();
  582. }
  583. #if MAX7219_INIT_TEST
  584. if (test_mode) {
  585. run_test_pattern();
  586. return;
  587. }
  588. #endif
  589. // suspend updates and record which lines have changed for batching later
  590. suspended++;
  591. uint8_t row_change_mask = 0x00;
  592. #if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE)
  593. if (do_blink) {
  594. led_toggle(MAX7219_X_LEDS - 1, MAX7219_Y_LEDS - 1, &row_change_mask);
  595. next_blink = ms + 1000;
  596. }
  597. #endif
  598. #if defined(MAX7219_DEBUG_PLANNER_HEAD) && defined(MAX7219_DEBUG_PLANNER_TAIL) && MAX7219_DEBUG_PLANNER_HEAD == MAX7219_DEBUG_PLANNER_TAIL
  599. static int16_t last_head_cnt = 0xF, last_tail_cnt = 0xF;
  600. if (last_head_cnt != head || last_tail_cnt != tail) {
  601. range16(MAX7219_DEBUG_PLANNER_HEAD, last_tail_cnt, tail, last_head_cnt, head, &row_change_mask);
  602. last_head_cnt = head;
  603. last_tail_cnt = tail;
  604. }
  605. #else
  606. #ifdef MAX7219_DEBUG_PLANNER_HEAD
  607. static int16_t last_head_cnt = 0x1;
  608. if (last_head_cnt != head) {
  609. mark16(MAX7219_DEBUG_PLANNER_HEAD, last_head_cnt, head, &row_change_mask);
  610. last_head_cnt = head;
  611. }
  612. #endif
  613. #ifdef MAX7219_DEBUG_PLANNER_TAIL
  614. static int16_t last_tail_cnt = 0x1;
  615. if (last_tail_cnt != tail) {
  616. mark16(MAX7219_DEBUG_PLANNER_TAIL, last_tail_cnt, tail, &row_change_mask);
  617. last_tail_cnt = tail;
  618. }
  619. #endif
  620. #endif
  621. #ifdef MAX7219_DEBUG_PLANNER_QUEUE
  622. static int16_t last_depth = 0;
  623. const int16_t current_depth = (head - tail + BLOCK_BUFFER_SIZE) & (BLOCK_BUFFER_SIZE - 1) & 0xF;
  624. if (current_depth != last_depth) {
  625. quantity16(MAX7219_DEBUG_PLANNER_QUEUE, last_depth, current_depth, &row_change_mask);
  626. last_depth = current_depth;
  627. }
  628. #endif
  629. #ifdef MAX7219_DEBUG_PROFILE
  630. static uint8_t last_time_fraction = 0;
  631. const uint8_t current_time_fraction = (uint16_t(CodeProfiler::get_time_fraction()) * MAX7219_NUMBER_UNITS + 8) / 16;
  632. if (current_time_fraction != last_time_fraction) {
  633. quantity(MAX7219_DEBUG_PROFILE, last_time_fraction, current_time_fraction, &row_change_mask);
  634. last_time_fraction = current_time_fraction;
  635. }
  636. #endif
  637. // batch line updates
  638. suspended--;
  639. if (!suspended)
  640. LOOP_L_N(i, 8) if (row_change_mask & _BV(i))
  641. refresh_line(i);
  642. // After resume() automatically do a refresh()
  643. if (suspended == 0x80) {
  644. suspended = 0;
  645. refresh();
  646. }
  647. }
  648. #endif // MAX7219_DEBUG