Non-blocking Max7219 test pattern

This commit is contained in:
Scott Lahteine 2020-03-06 13:16:30 -06:00
parent ca4423ed2a
commit 1da49d0ac5
2 changed files with 81 additions and 40 deletions

View file

@ -453,35 +453,83 @@ void Max7219::register_setup() {
} }
#ifdef MAX7219_INIT_TEST #ifdef MAX7219_INIT_TEST
#if MAX7219_INIT_TEST == 2
uint8_t test_mode = 0;
millis_t next_patt_ms;
bool patt_on;
#if MAX7219_INIT_TEST == 2
#define MAX7219_LEDS (MAX7219_X_LEDS * MAX7219_Y_LEDS) #define MAX7219_LEDS (MAX7219_X_LEDS * MAX7219_Y_LEDS)
void Max7219::spiral(const bool on, const uint16_t del) { constexpr millis_t pattern_delay = 4;
int8_t spiralx, spiraly, spiral_dir;
IF<(MAX7219_LEDS > 255), uint16_t, uint8_t>::type spiral_count;
void Max7219::test_pattern() {
constexpr int8_t way[][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } }; constexpr int8_t way[][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
int8_t px = 0, py = 0, dir = 0; led_set(spiralx, spiraly, patt_on);
for (IF<(MAX7219_LEDS > 255), uint16_t, uint8_t>::type i = MAX7219_LEDS; i--;) { const int8_t x = spiralx + way[spiral_dir][0], y = spiraly + way[spiral_dir][1];
led_set(px, py, on); if (!WITHIN(x, 0, MAX7219_X_LEDS - 1) || !WITHIN(y, 0, MAX7219_Y_LEDS - 1) || BIT_7219(x, y) == patt_on)
delay(del); spiral_dir = (spiral_dir + 1) & 0x3;
const int8_t x = px + way[dir][0], y = py + way[dir][1]; spiralx += way[spiral_dir][0];
if (!WITHIN(x, 0, MAX7219_X_LEDS - 1) || !WITHIN(y, 0, MAX7219_Y_LEDS - 1) || BIT_7219(x, y) == on) spiraly += way[spiral_dir][1];
dir = (dir + 1) & 0x3; if (!spiral_count--) {
px += way[dir][0]; if (!patt_on)
py += way[dir][1]; test_mode = 0;
else {
spiral_count = MAX7219_LEDS;
spiralx = spiraly = spiral_dir = 0;
patt_on = false;
}
} }
} }
#else #else
void Max7219::sweep(const int8_t dir, const uint16_t ms, const bool on) { constexpr millis_t pattern_delay = 20;
uint8_t x = dir > 0 ? 0 : MAX7219_X_LEDS - 1; int8_t sweep_count, sweepx, sweep_dir;
for (uint8_t i = MAX7219_X_LEDS; i--; x += dir) {
set_column(x, on ? 0xFFFFFFFF : 0x00000000); void Max7219::test_pattern() {
delay(ms); set_column(sweepx, patt_on ? 0xFFFFFFFF : 0x00000000);
sweepx += sweep_dir;
if (!WITHIN(sweepx, 0, MAX7219_X_LEDS - 1)) {
if (!patt_on) {
sweep_dir *= -1;
sweepx += sweep_dir;
}
else
sweepx -= MAX7219_X_LEDS * sweep_dir;
patt_on ^= true;
next_patt_ms += 100;
if (++test_mode > 4) test_mode = 0;
} }
} }
#endif #endif
void Max7219::run_test_pattern() {
const millis_t ms = millis();
if (PENDING(ms, next_patt_ms)) return;
next_patt_ms = ms + pattern_delay;
test_pattern();
}
void Max7219::start_test_pattern() {
clear();
test_mode = 1;
patt_on = true;
#if MAX7219_INIT_TEST == 2
spiralx = spiraly = spiral_dir = 0;
spiral_count = MAX7219_LEDS;
#else
sweep_dir = 1;
sweepx = 0;
sweep_count = MAX7219_X_LEDS;
#endif
}
#endif // MAX7219_INIT_TEST #endif // MAX7219_INIT_TEST
void Max7219::init() { void Max7219::init() {
@ -499,19 +547,7 @@ void Max7219::init() {
} }
#ifdef MAX7219_INIT_TEST #ifdef MAX7219_INIT_TEST
#if MAX7219_INIT_TEST == 2 start_test_pattern();
spiral(true, 8);
delay(150);
spiral(false, 8);
#else
// Do an aesthetically-pleasing pattern to fully test the Max7219 module and LEDs.
// Light up and turn off columns, both forward and backward.
sweep(1, 20, true);
sweep(1, 20, false);
delay(150);
sweep(-1, 20, true);
sweep(-1, 20, false);
#endif
#endif #endif
} }
@ -604,6 +640,13 @@ void Max7219::idle_tasks() {
register_setup(); register_setup();
} }
#ifdef MAX7219_INIT_TEST
if (test_mode) {
run_test_pattern();
return;
}
#endif
#if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE) #if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE)
if (do_blink) { if (do_blink) {
led_toggle(MAX7219_X_LEDS - 1, MAX7219_Y_LEDS - 1); led_toggle(MAX7219_X_LEDS - 1, MAX7219_Y_LEDS - 1);

View file

@ -136,11 +136,9 @@ private:
static void quantity16(const uint8_t y, const uint8_t ov, const uint8_t nv); static void quantity16(const uint8_t y, const uint8_t ov, const uint8_t nv);
#ifdef MAX7219_INIT_TEST #ifdef MAX7219_INIT_TEST
#if MAX7219_INIT_TEST == 2 static void test_pattern();
static void spiral(const bool on, const uint16_t del); static void run_test_pattern();
#else static void start_test_pattern();
static void sweep(const int8_t dir, const uint16_t ms, const bool on);
#endif
#endif #endif
}; };