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