Pārlūkot izejas kodu

Reduce all stepper_indirection init code with macros

Scott Lahteine 7 gadus atpakaļ
vecāks
revīzija
644fac5d34
1 mainītis faili ar 114 papildinājumiem un 147 dzēšanām
  1. 114
    147
      Marlin/stepper_indirection.cpp

+ 114
- 147
Marlin/stepper_indirection.cpp Parādīt failu

@@ -45,88 +45,88 @@
45 45
 
46 46
 #include "MarlinConfig.h"
47 47
 
48
+//
49
+// TMC26X Driver objects and inits
50
+//
48 51
 #if ENABLED(HAVE_TMCDRIVER)
52
+
49 53
   #include <SPI.h>
50 54
   #include <TMC26XStepper.h>
51
-#endif
52
-
53
-// Stepper objects of TMC steppers used
54
-#if ENABLED(X_IS_TMC)
55
-  TMC26XStepper stepperX(200, X_ENABLE_PIN, X_STEP_PIN, X_DIR_PIN, X_MAX_CURRENT, X_SENSE_RESISTOR);
56
-#endif
57
-#if ENABLED(X2_IS_TMC)
58
-  TMC26XStepper stepperX2(200, X2_ENABLE_PIN, X2_STEP_PIN, X2_DIR_PIN, X2_MAX_CURRENT, X2_SENSE_RESISTOR);
59
-#endif
60
-#if ENABLED(Y_IS_TMC)
61
-  TMC26XStepper stepperY(200, Y_ENABLE_PIN, Y_STEP_PIN, Y_DIR_PIN, Y_MAX_CURRENT, Y_SENSE_RESISTOR);
62
-#endif
63
-#if ENABLED(Y2_IS_TMC)
64
-  TMC26XStepper stepperY2(200, Y2_ENABLE_PIN, Y2_STEP_PIN, Y2_DIR_PIN, Y2_MAX_CURRENT, Y2_SENSE_RESISTOR);
65
-#endif
66
-#if ENABLED(Z_IS_TMC)
67
-  TMC26XStepper stepperZ(200, Z_ENABLE_PIN, Z_STEP_PIN, Z_DIR_PIN, Z_MAX_CURRENT, Z_SENSE_RESISTOR);
68
-#endif
69
-#if ENABLED(Z2_IS_TMC)
70
-  TMC26XStepper stepperZ2(200, Z2_ENABLE_PIN, Z2_STEP_PIN, Z2_DIR_PIN, Z2_MAX_CURRENT, Z2_SENSE_RESISTOR);
71
-#endif
72
-#if ENABLED(E0_IS_TMC)
73
-  TMC26XStepper stepperE0(200, E0_ENABLE_PIN, E0_STEP_PIN, E0_DIR_PIN, E0_MAX_CURRENT, E0_SENSE_RESISTOR);
74
-#endif
75
-#if ENABLED(E1_IS_TMC)
76
-  TMC26XStepper stepperE1(200, E1_ENABLE_PIN, E1_STEP_PIN, E1_DIR_PIN, E1_MAX_CURRENT, E1_SENSE_RESISTOR);
77
-#endif
78
-#if ENABLED(E2_IS_TMC)
79
-  TMC26XStepper stepperE2(200, E2_ENABLE_PIN, E2_STEP_PIN, E2_DIR_PIN, E2_MAX_CURRENT, E2_SENSE_RESISTOR);
80
-#endif
81
-#if ENABLED(E3_IS_TMC)
82
-  TMC26XStepper stepperE3(200, E3_ENABLE_PIN, E3_STEP_PIN, E3_DIR_PIN, E3_MAX_CURRENT, E3_SENSE_RESISTOR);
83
-#endif
84 55
 
85
-#if ENABLED(HAVE_TMCDRIVER)
86
-void tmc_init() {
87 56
   #if ENABLED(X_IS_TMC)
88
-    stepperX.setMicrosteps(X_MICROSTEPS);
89
-    stepperX.start();
57
+    TMC26XStepper stepperX(200, X_ENABLE_PIN, X_STEP_PIN, X_DIR_PIN, X_MAX_CURRENT, X_SENSE_RESISTOR);
90 58
   #endif
91 59
   #if ENABLED(X2_IS_TMC)
92
-    stepperX2.setMicrosteps(X2_MICROSTEPS);
93
-    stepperX2.start();
60
+    TMC26XStepper stepperX2(200, X2_ENABLE_PIN, X2_STEP_PIN, X2_DIR_PIN, X2_MAX_CURRENT, X2_SENSE_RESISTOR);
94 61
   #endif
95 62
   #if ENABLED(Y_IS_TMC)
96
-    stepperY.setMicrosteps(Y_MICROSTEPS);
97
-    stepperY.start();
63
+    TMC26XStepper stepperY(200, Y_ENABLE_PIN, Y_STEP_PIN, Y_DIR_PIN, Y_MAX_CURRENT, Y_SENSE_RESISTOR);
98 64
   #endif
99 65
   #if ENABLED(Y2_IS_TMC)
100
-    stepperY2.setMicrosteps(Y2_MICROSTEPS);
101
-    stepperY2.start();
66
+    TMC26XStepper stepperY2(200, Y2_ENABLE_PIN, Y2_STEP_PIN, Y2_DIR_PIN, Y2_MAX_CURRENT, Y2_SENSE_RESISTOR);
102 67
   #endif
103 68
   #if ENABLED(Z_IS_TMC)
104
-    stepperZ.setMicrosteps(Z_MICROSTEPS);
105
-    stepperZ.start();
69
+    TMC26XStepper stepperZ(200, Z_ENABLE_PIN, Z_STEP_PIN, Z_DIR_PIN, Z_MAX_CURRENT, Z_SENSE_RESISTOR);
106 70
   #endif
107 71
   #if ENABLED(Z2_IS_TMC)
108
-    stepperZ2.setMicrosteps(Z2_MICROSTEPS);
109
-    stepperZ2.start();
72
+    TMC26XStepper stepperZ2(200, Z2_ENABLE_PIN, Z2_STEP_PIN, Z2_DIR_PIN, Z2_MAX_CURRENT, Z2_SENSE_RESISTOR);
110 73
   #endif
111 74
   #if ENABLED(E0_IS_TMC)
112
-    stepperE0.setMicrosteps(E0_MICROSTEPS);
113
-    stepperE0.start();
75
+    TMC26XStepper stepperE0(200, E0_ENABLE_PIN, E0_STEP_PIN, E0_DIR_PIN, E0_MAX_CURRENT, E0_SENSE_RESISTOR);
114 76
   #endif
115 77
   #if ENABLED(E1_IS_TMC)
116
-    stepperE1.setMicrosteps(E1_MICROSTEPS);
117
-    stepperE1.start();
78
+    TMC26XStepper stepperE1(200, E1_ENABLE_PIN, E1_STEP_PIN, E1_DIR_PIN, E1_MAX_CURRENT, E1_SENSE_RESISTOR);
118 79
   #endif
119 80
   #if ENABLED(E2_IS_TMC)
120
-    stepperE2.setMicrosteps(E2_MICROSTEPS);
121
-    stepperE2.start();
81
+    TMC26XStepper stepperE2(200, E2_ENABLE_PIN, E2_STEP_PIN, E2_DIR_PIN, E2_MAX_CURRENT, E2_SENSE_RESISTOR);
122 82
   #endif
123 83
   #if ENABLED(E3_IS_TMC)
124
-    stepperE3.setMicrosteps(E3_MICROSTEPS);
125
-    stepperE3.start();
84
+    TMC26XStepper stepperE3(200, E3_ENABLE_PIN, E3_STEP_PIN, E3_DIR_PIN, E3_MAX_CURRENT, E3_SENSE_RESISTOR);
126 85
   #endif
127
-}
128
-#endif
129 86
 
87
+  #define _TMC_INIT(A) do{ \
88
+    stepper##A.setMicrosteps(A##_MICROSTEPS);
89
+    stepper##A.start();
90
+  } while(0)
91
+
92
+  void tmc_init() {
93
+    #if ENABLED(X_IS_TMC)
94
+      _TMC_INIT(X);
95
+    #endif
96
+    #if ENABLED(X2_IS_TMC)
97
+      _TMC_INIT(X2);
98
+    #endif
99
+    #if ENABLED(Y_IS_TMC)
100
+      _TMC_INIT(Y);
101
+    #endif
102
+    #if ENABLED(Y2_IS_TMC)
103
+      _TMC_INIT(Y2);
104
+    #endif
105
+    #if ENABLED(Z_IS_TMC)
106
+      _TMC_INIT(Z);
107
+    #endif
108
+    #if ENABLED(Z2_IS_TMC)
109
+      _TMC_INIT(Z2);
110
+    #endif
111
+    #if ENABLED(E0_IS_TMC)
112
+      _TMC_INIT(E0);
113
+    #endif
114
+    #if ENABLED(E1_IS_TMC)
115
+      _TMC_INIT(E1);
116
+    #endif
117
+    #if ENABLED(E2_IS_TMC)
118
+      _TMC_INIT(E2);
119
+    #endif
120
+    #if ENABLED(E3_IS_TMC)
121
+      _TMC_INIT(E3);
122
+    #endif
123
+  }
124
+
125
+#endif // HAVE_TMCDRIVER
126
+
127
+//
128
+// TMC2130 Driver objects and inits
129
+//
130 130
 #if ENABLED(HAVE_TMC2130DRIVER)
131 131
 
132 132
   #include <SPI.h>
@@ -549,119 +549,86 @@ void tmc_init() {
549 549
 
550 550
 #endif // HAVE_TMC2130DRIVER
551 551
 
552
+//
552 553
 // L6470 Driver objects and inits
553
-
554
+//
554 555
 #if ENABLED(HAVE_L6470DRIVER)
556
+
555 557
   #include <SPI.h>
556 558
   #include <L6470.h>
557
-#endif
558
-
559
-// L6470 Stepper objects
560
-#if ENABLED(X_IS_L6470)
561
-  L6470 stepperX(X_ENABLE_PIN);
562
-#endif
563
-#if ENABLED(X2_IS_L6470)
564
-  L6470 stepperX2(X2_ENABLE_PIN);
565
-#endif
566
-#if ENABLED(Y_IS_L6470)
567
-  L6470 stepperY(Y_ENABLE_PIN);
568
-#endif
569
-#if ENABLED(Y2_IS_L6470)
570
-  L6470 stepperY2(Y2_ENABLE_PIN);
571
-#endif
572
-#if ENABLED(Z_IS_L6470)
573
-  L6470 stepperZ(Z_ENABLE_PIN);
574
-#endif
575
-#if ENABLED(Z2_IS_L6470)
576
-  L6470 stepperZ2(Z2_ENABLE_PIN);
577
-#endif
578
-#if ENABLED(E0_IS_L6470)
579
-  L6470 stepperE0(E0_ENABLE_PIN);
580
-#endif
581
-#if ENABLED(E1_IS_L6470)
582
-  L6470 stepperE1(E1_ENABLE_PIN);
583
-#endif
584
-#if ENABLED(E2_IS_L6470)
585
-  L6470 stepperE2(E2_ENABLE_PIN);
586
-#endif
587
-#if ENABLED(E3_IS_L6470)
588
-  L6470 stepperE3(E3_ENABLE_PIN);
589
-#endif
590
-
591 559
 
592
-// init routine
593
-#if ENABLED(HAVE_L6470DRIVER)
594
-void L6470_init() {
560
+  // L6470 Stepper objects
595 561
   #if ENABLED(X_IS_L6470)
596
-    stepperX.init(X_K_VAL);
597
-    stepperX.softFree();
598
-    stepperX.setMicroSteps(X_MICROSTEPS);
599
-    stepperX.setOverCurrent(X_OVERCURRENT); //set overcurrent protection
600
-    stepperX.setStallCurrent(X_STALLCURRENT);
562
+    L6470 stepperX(X_ENABLE_PIN);
601 563
   #endif
602 564
   #if ENABLED(X2_IS_L6470)
603
-    stepperX2.init(X2_K_VAL);
604
-    stepperX2.softFree();
605
-    stepperX2.setMicroSteps(X2_MICROSTEPS);
606
-    stepperX2.setOverCurrent(X2_OVERCURRENT); //set overcurrent protection
607
-    stepperX2.setStallCurrent(X2_STALLCURRENT);
565
+    L6470 stepperX2(X2_ENABLE_PIN);
608 566
   #endif
609 567
   #if ENABLED(Y_IS_L6470)
610
-    stepperY.init(Y_K_VAL);
611
-    stepperY.softFree();
612
-    stepperY.setMicroSteps(Y_MICROSTEPS);
613
-    stepperY.setOverCurrent(Y_OVERCURRENT); //set overcurrent protection
614
-    stepperY.setStallCurrent(Y_STALLCURRENT);
568
+    L6470 stepperY(Y_ENABLE_PIN);
615 569
   #endif
616 570
   #if ENABLED(Y2_IS_L6470)
617
-    stepperY2.init(Y2_K_VAL);
618
-    stepperY2.softFree();
619
-    stepperY2.setMicroSteps(Y2_MICROSTEPS);
620
-    stepperY2.setOverCurrent(Y2_OVERCURRENT); //set overcurrent protection
621
-    stepperY2.setStallCurrent(Y2_STALLCURRENT);
571
+    L6470 stepperY2(Y2_ENABLE_PIN);
622 572
   #endif
623 573
   #if ENABLED(Z_IS_L6470)
624
-    stepperZ.init(Z_K_VAL);
625
-    stepperZ.softFree();
626
-    stepperZ.setMicroSteps(Z_MICROSTEPS);
627
-    stepperZ.setOverCurrent(Z_OVERCURRENT); //set overcurrent protection
628
-    stepperZ.setStallCurrent(Z_STALLCURRENT);
574
+    L6470 stepperZ(Z_ENABLE_PIN);
629 575
   #endif
630 576
   #if ENABLED(Z2_IS_L6470)
631
-    stepperZ2.init(Z2_K_VAL);
632
-    stepperZ2.softFree();
633
-    stepperZ2.setMicroSteps(Z2_MICROSTEPS);
634
-    stepperZ2.setOverCurrent(Z2_OVERCURRENT); //set overcurrent protection
635
-    stepperZ2.setStallCurrent(Z2_STALLCURRENT);
577
+    L6470 stepperZ2(Z2_ENABLE_PIN);
636 578
   #endif
637 579
   #if ENABLED(E0_IS_L6470)
638
-    stepperE0.init(E0_K_VAL);
639
-    stepperE0.softFree();
640
-    stepperE0.setMicroSteps(E0_MICROSTEPS);
641
-    stepperE0.setOverCurrent(E0_OVERCURRENT); //set overcurrent protection
642
-    stepperE0.setStallCurrent(E0_STALLCURRENT);
580
+    L6470 stepperE0(E0_ENABLE_PIN);
643 581
   #endif
644 582
   #if ENABLED(E1_IS_L6470)
645
-    stepperE1.init(E1_K_VAL);
646
-    stepperE1.softFree();
647
-    stepperE1.setMicroSteps(E1_MICROSTEPS);
648
-    stepperE1.setOverCurrent(E1_OVERCURRENT); //set overcurrent protection
649
-    stepperE1.setStallCurrent(E1_STALLCURRENT);
583
+    L6470 stepperE1(E1_ENABLE_PIN);
650 584
   #endif
651 585
   #if ENABLED(E2_IS_L6470)
652
-    stepperE2.init(E2_K_VAL);
653
-    stepperE2.softFree();
654
-    stepperE2.setMicroSteps(E2_MICROSTEPS);
655
-    stepperE2.setOverCurrent(E2_OVERCURRENT); //set overcurrent protection
656
-    stepperE2.setStallCurrent(E2_STALLCURRENT);
586
+    L6470 stepperE2(E2_ENABLE_PIN);
657 587
   #endif
658 588
   #if ENABLED(E3_IS_L6470)
659
-    stepperE3.init(E3_K_VAL);
660
-    stepperE3.softFree();
661
-    stepperE3.setMicroSteps(E3_MICROSTEPS);
662
-    stepperE3.setOverCurrent(E3_OVERCURRENT); //set overcurrent protection
663
-    stepperE3.setStallCurrent(E3_STALLCURRENT);
589
+    L6470 stepperE3(E3_ENABLE_PIN);
664 590
   #endif
665
-}
666
-#endif
591
+
592
+  #define _L6470_INIT(A) do{ \
593
+    stepper##A.init(A##_K_VAL); \
594
+    stepper##A.softFree(); \
595
+    stepper##A.setMicroSteps(A##_MICROSTEPS); \
596
+    stepper##A.setOverCurrent(A##_OVERCURRENT); \
597
+    stepper##A.setStallCurrent(A##_STALLCURRENT); \
598
+  } while(0)
599
+
600
+  void L6470_init() {
601
+    #if ENABLED(X_IS_L6470)
602
+      _L6470_INIT(X);
603
+    #endif
604
+    #if ENABLED(X2_IS_L6470)
605
+      _L6470_INIT(X2);
606
+    #endif
607
+    #if ENABLED(Y_IS_L6470)
608
+      _L6470_INIT(Y);
609
+    #endif
610
+    #if ENABLED(Y2_IS_L6470)
611
+      _L6470_INIT(Y2);
612
+    #endif
613
+    #if ENABLED(Z_IS_L6470)
614
+      _L6470_INIT(Z);
615
+    #endif
616
+    #if ENABLED(Z2_IS_L6470)
617
+      _L6470_INIT(Z2);
618
+    #endif
619
+    #if ENABLED(E0_IS_L6470)
620
+      _L6470_INIT(E0);
621
+    #endif
622
+    #if ENABLED(E1_IS_L6470)
623
+      _L6470_INIT(E1);
624
+    #endif
625
+    #if ENABLED(E2_IS_L6470)
626
+      _L6470_INIT(E2);
627
+    #endif
628
+    #if ENABLED(E3_IS_L6470)
629
+      _L6470_INIT(E3);
630
+    #endif
631
+  }
632
+
633
+#endif // HAVE_L6470DRIVER
667 634
 

Notiek ielāde…
Atcelt
Saglabāt