Sfoglia il codice sorgente

Non-blocking Max7219 test pattern

Scott Lahteine 4 anni fa
parent
commit
1da49d0ac5

+ 79
- 36
Marlin/src/feature/Max7219_Debug_LEDs.cpp Vedi File

@@ -453,35 +453,83 @@ void Max7219::register_setup() {
453 453
 }
454 454
 
455 455
 #ifdef MAX7219_INIT_TEST
456
-#if MAX7219_INIT_TEST == 2
457
-
458
-  #define MAX7219_LEDS (MAX7219_X_LEDS * MAX7219_Y_LEDS)
459
-
460
-  void Max7219::spiral(const bool on, const uint16_t del) {
461
-    constexpr int8_t way[][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
462
-    int8_t px = 0, py = 0, dir = 0;
463
-    for (IF<(MAX7219_LEDS > 255), uint16_t, uint8_t>::type i = MAX7219_LEDS; i--;) {
464
-      led_set(px, py, on);
465
-      delay(del);
466
-      const int8_t x = px + way[dir][0], y = py + way[dir][1];
467
-      if (!WITHIN(x, 0, MAX7219_X_LEDS - 1) || !WITHIN(y, 0, MAX7219_Y_LEDS - 1) || BIT_7219(x, y) == on)
468
-        dir = (dir + 1) & 0x3;
469
-      px += way[dir][0];
470
-      py += way[dir][1];
456
+
457
+  uint8_t test_mode = 0;
458
+  millis_t next_patt_ms;
459
+  bool patt_on;
460
+
461
+  #if MAX7219_INIT_TEST == 2
462
+
463
+    #define MAX7219_LEDS (MAX7219_X_LEDS * MAX7219_Y_LEDS)
464
+
465
+    constexpr millis_t pattern_delay = 4;
466
+
467
+    int8_t spiralx, spiraly, spiral_dir;
468
+    IF<(MAX7219_LEDS > 255), uint16_t, uint8_t>::type spiral_count;
469
+
470
+    void Max7219::test_pattern() {
471
+      constexpr int8_t way[][2] = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
472
+      led_set(spiralx, spiraly, patt_on);
473
+      const int8_t x = spiralx + way[spiral_dir][0], y = spiraly + way[spiral_dir][1];
474
+      if (!WITHIN(x, 0, MAX7219_X_LEDS - 1) || !WITHIN(y, 0, MAX7219_Y_LEDS - 1) || BIT_7219(x, y) == patt_on)
475
+        spiral_dir = (spiral_dir + 1) & 0x3;
476
+      spiralx += way[spiral_dir][0];
477
+      spiraly += way[spiral_dir][1];
478
+      if (!spiral_count--) {
479
+        if (!patt_on)
480
+          test_mode = 0;
481
+        else {
482
+          spiral_count = MAX7219_LEDS;
483
+          spiralx = spiraly = spiral_dir = 0;
484
+          patt_on = false;
485
+        }
486
+      }
471 487
     }
472
-  }
473 488
 
474
-#else
489
+  #else
475 490
 
476
-  void Max7219::sweep(const int8_t dir, const uint16_t ms, const bool on) {
477
-    uint8_t x = dir > 0 ? 0 : MAX7219_X_LEDS - 1;
478
-    for (uint8_t i = MAX7219_X_LEDS; i--; x += dir) {
479
-      set_column(x, on ? 0xFFFFFFFF : 0x00000000);
480
-      delay(ms);
491
+    constexpr millis_t pattern_delay = 20;
492
+    int8_t sweep_count, sweepx, sweep_dir;
493
+
494
+    void Max7219::test_pattern() {
495
+      set_column(sweepx, patt_on ? 0xFFFFFFFF : 0x00000000);
496
+      sweepx += sweep_dir;
497
+      if (!WITHIN(sweepx, 0, MAX7219_X_LEDS - 1)) {
498
+        if (!patt_on) {
499
+          sweep_dir *= -1;
500
+          sweepx += sweep_dir;
501
+        }
502
+        else
503
+          sweepx -= MAX7219_X_LEDS * sweep_dir;
504
+        patt_on ^= true;
505
+        next_patt_ms += 100;
506
+        if (++test_mode > 4) test_mode = 0;
507
+      }
481 508
     }
509
+
510
+  #endif
511
+
512
+  void Max7219::run_test_pattern() {
513
+    const millis_t ms = millis();
514
+    if (PENDING(ms, next_patt_ms)) return;
515
+    next_patt_ms = ms + pattern_delay;
516
+    test_pattern();
517
+  }
518
+
519
+  void Max7219::start_test_pattern() {
520
+    clear();
521
+    test_mode = 1;
522
+    patt_on = true;
523
+    #if MAX7219_INIT_TEST == 2
524
+      spiralx = spiraly = spiral_dir = 0;
525
+      spiral_count = MAX7219_LEDS;
526
+    #else
527
+      sweep_dir = 1;
528
+      sweepx = 0;
529
+      sweep_count = MAX7219_X_LEDS;
530
+    #endif
482 531
   }
483 532
 
484
-#endif
485 533
 #endif // MAX7219_INIT_TEST
486 534
 
487 535
 void Max7219::init() {
@@ -499,19 +547,7 @@ void Max7219::init() {
499 547
   }
500 548
 
501 549
   #ifdef MAX7219_INIT_TEST
502
-    #if MAX7219_INIT_TEST == 2
503
-      spiral(true, 8);
504
-      delay(150);
505
-      spiral(false, 8);
506
-    #else
507
-      // Do an aesthetically-pleasing pattern to fully test the Max7219 module and LEDs.
508
-      // Light up and turn off columns, both forward and backward.
509
-      sweep(1, 20, true);
510
-      sweep(1, 20, false);
511
-      delay(150);
512
-      sweep(-1, 20, true);
513
-      sweep(-1, 20, false);
514
-    #endif
550
+    start_test_pattern();
515 551
   #endif
516 552
 }
517 553
 
@@ -604,6 +640,13 @@ void Max7219::idle_tasks() {
604 640
     register_setup();
605 641
   }
606 642
 
643
+  #ifdef MAX7219_INIT_TEST
644
+    if (test_mode) {
645
+      run_test_pattern();
646
+      return;
647
+    }
648
+  #endif
649
+
607 650
   #if ENABLED(MAX7219_DEBUG_PRINTER_ALIVE)
608 651
     if (do_blink) {
609 652
       led_toggle(MAX7219_X_LEDS - 1, MAX7219_Y_LEDS - 1);

+ 3
- 5
Marlin/src/feature/Max7219_Debug_LEDs.h Vedi File

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

Loading…
Annulla
Salva