Browse Source

Merge branch 'Development' into marlin_configurator

Latest upstream changes
Scott Lahteine 10 years ago
parent
commit
fad14ae7eb

+ 1
- 8
Marlin/BlinkM.cpp View File

5
 #include "Marlin.h"
5
 #include "Marlin.h"
6
 #ifdef BLINKM
6
 #ifdef BLINKM
7
 
7
 
8
-#if (ARDUINO >= 100)
9
-  # include "Arduino.h"
10
-#else
11
-  # include "WProgram.h"
12
-#endif
13
-
14
 #include "BlinkM.h"
8
 #include "BlinkM.h"
15
 
9
 
16
-void SendColors(byte red, byte grn, byte blu)
17
-{
10
+void SendColors(byte red, byte grn, byte blu) {
18
   Wire.begin(); 
11
   Wire.begin(); 
19
   Wire.beginTransmission(0x09);
12
   Wire.beginTransmission(0x09);
20
   Wire.write('o');                    //to disable ongoing script, only needs to be used once
13
   Wire.write('o');                    //to disable ongoing script, only needs to be used once

+ 3
- 4
Marlin/BlinkM.h View File

2
   BlinkM.h
2
   BlinkM.h
3
   Library header file for BlinkM library
3
   Library header file for BlinkM library
4
  */
4
  */
5
-#if (ARDUINO >= 100)
6
-  # include "Arduino.h"
5
+#if ARDUINO >= 100
6
+  #include "Arduino.h"
7
 #else
7
 #else
8
-  # include "WProgram.h"
8
+  #include "WProgram.h"
9
 #endif
9
 #endif
10
 
10
 
11
 #include "Wire.h"
11
 #include "Wire.h"
12
 
12
 
13
 void SendColors(byte red, byte grn, byte blu);
13
 void SendColors(byte red, byte grn, byte blu);
14
-

+ 3
- 3
Marlin/Configuration.h View File

428
 
428
 
429
   // these are the offsets to the probe relative to the extruder tip (Hotend - Probe)
429
   // these are the offsets to the probe relative to the extruder tip (Hotend - Probe)
430
   // X and Y offsets must be integers
430
   // X and Y offsets must be integers
431
-  #define X_PROBE_OFFSET_FROM_EXTRUDER -25
432
-  #define Y_PROBE_OFFSET_FROM_EXTRUDER -29
433
-  #define Z_PROBE_OFFSET_FROM_EXTRUDER -12.35
431
+  #define X_PROBE_OFFSET_FROM_EXTRUDER -25     // -left  +right
432
+  #define Y_PROBE_OFFSET_FROM_EXTRUDER -29     // -front +behind
433
+  #define Z_PROBE_OFFSET_FROM_EXTRUDER -12.35  // -below (always!)
434
 
434
 
435
   #define Z_RAISE_BEFORE_HOMING 4       // (in mm) Raise Z before homing (G28) for Probe Clearance.
435
   #define Z_RAISE_BEFORE_HOMING 4       // (in mm) Raise Z before homing (G28) for Probe Clearance.
436
                                         // Be sure you have this distance over your Z_MAX_POS in case
436
                                         // Be sure you have this distance over your Z_MAX_POS in case

+ 3
- 3
Marlin/ConfigurationStore.h View File

1
-#ifndef CONFIG_STORE_H
2
-#define CONFIG_STORE_H
1
+#ifndef CONFIGURATIONSTORE_H
2
+#define CONFIGURATIONSTORE_H
3
 
3
 
4
 #include "Configuration.h"
4
 #include "Configuration.h"
5
 
5
 
19
   FORCE_INLINE void Config_RetrieveSettings() { Config_ResetDefault(); Config_PrintSettings(); }
19
   FORCE_INLINE void Config_RetrieveSettings() { Config_ResetDefault(); Config_PrintSettings(); }
20
 #endif
20
 #endif
21
 
21
 
22
-#endif // __CONFIG_STORE_H
22
+#endif //CONFIGURATIONSTORE_H

+ 2
- 2
Marlin/Marlin.h View File

180
   #define disable_e3() /* nothing */
180
   #define disable_e3() /* nothing */
181
 #endif
181
 #endif
182
 
182
 
183
-enum AxisEnum {X_AXIS=0, Y_AXIS=1, Z_AXIS=2, E_AXIS=3, X_HEAD=4, Y_HEAD=5};
184
-
183
+enum AxisEnum {X_AXIS=0, Y_AXIS=1, Z_AXIS=2, E_AXIS=3, X_HEAD=4, Y_HEAD=5}; 
184
+//X_HEAD and Y_HEAD is used for systems that don't have a 1:1 relationship between X_AXIS and X Head movement, like CoreXY bots.
185
 
185
 
186
 void FlushSerialRequestResend();
186
 void FlushSerialRequestResend();
187
 void ClearToSend();
187
 void ClearToSend();

+ 396
- 199
Marlin/Marlin_main.cpp View File

154
 // M302 - Allow cold extrudes, or set the minimum extrude S<temperature>.
154
 // M302 - Allow cold extrudes, or set the minimum extrude S<temperature>.
155
 // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
155
 // M303 - PID relay autotune S<temperature> sets the target temperature. (default target temperature = 150C)
156
 // M304 - Set bed PID parameters P I and D
156
 // M304 - Set bed PID parameters P I and D
157
+// M380 - Activate solenoid on active extruder
158
+// M381 - Disable all solenoids
157
 // M400 - Finish all moves
159
 // M400 - Finish all moves
158
 // M401 - Lower z-probe if present
160
 // M401 - Lower z-probe if present
159
 // M402 - Raise z-probe if present
161
 // M402 - Raise z-probe if present
529
 void setup_photpin()
531
 void setup_photpin()
530
 {
532
 {
531
   #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
533
   #if defined(PHOTOGRAPH_PIN) && PHOTOGRAPH_PIN > -1
532
-    SET_OUTPUT(PHOTOGRAPH_PIN);
533
-    WRITE(PHOTOGRAPH_PIN, LOW);
534
+    OUT_WRITE(PHOTOGRAPH_PIN, LOW);
534
   #endif
535
   #endif
535
 }
536
 }
536
 
537
 
537
 void setup_powerhold()
538
 void setup_powerhold()
538
 {
539
 {
539
   #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
540
   #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
540
-    SET_OUTPUT(SUICIDE_PIN);
541
-    WRITE(SUICIDE_PIN, HIGH);
541
+    OUT_WRITE(SUICIDE_PIN, HIGH);
542
   #endif
542
   #endif
543
   #if defined(PS_ON_PIN) && PS_ON_PIN > -1
543
   #if defined(PS_ON_PIN) && PS_ON_PIN > -1
544
-    SET_OUTPUT(PS_ON_PIN);
545
-	#if defined(PS_DEFAULT_OFF)
546
-	  WRITE(PS_ON_PIN, PS_ON_ASLEEP);
547
-    #else
548
-	  WRITE(PS_ON_PIN, PS_ON_AWAKE);
549
-	#endif
544
+    #if defined(PS_DEFAULT_OFF)
545
+      OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP);
546
+      #else
547
+      OUT_WRITE(PS_ON_PIN, PS_ON_AWAKE);
548
+    #endif
550
   #endif
549
   #endif
551
 }
550
 }
552
 
551
 
553
 void suicide()
552
 void suicide()
554
 {
553
 {
555
   #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
554
   #if defined(SUICIDE_PIN) && SUICIDE_PIN > -1
556
-    SET_OUTPUT(SUICIDE_PIN);
557
-    WRITE(SUICIDE_PIN, LOW);
555
+    OUT_WRITE(SUICIDE_PIN, LOW);
558
   #endif
556
   #endif
559
 }
557
 }
560
 
558
 
1200
     #endif
1198
     #endif
1201
 }
1199
 }
1202
 
1200
 
1201
+enum ProbeAction { ProbeStay, ProbeEngage, ProbeRetract, ProbeEngageRetract };
1202
+
1203
 /// Probe bed height at position (x,y), returns the measured z value
1203
 /// Probe bed height at position (x,y), returns the measured z value
1204
-static float probe_pt(float x, float y, float z_before, int retract_action=0) {
1204
+static float probe_pt(float x, float y, float z_before, ProbeAction retract_action=ProbeEngageRetract) {
1205
   // move to right place
1205
   // move to right place
1206
   do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z_before);
1206
   do_blocking_move_to(current_position[X_AXIS], current_position[Y_AXIS], z_before);
1207
   do_blocking_move_to(x - X_PROBE_OFFSET_FROM_EXTRUDER, y - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
1207
   do_blocking_move_to(x - X_PROBE_OFFSET_FROM_EXTRUDER, y - Y_PROBE_OFFSET_FROM_EXTRUDER, current_position[Z_AXIS]);
1208
 
1208
 
1209
-#ifndef Z_PROBE_SLED
1210
-   if ((retract_action==0) || (retract_action==1)) 
1211
-     engage_z_probe();   // Engage Z Servo endstop if available
1212
-#endif // Z_PROBE_SLED
1209
+  #ifndef Z_PROBE_SLED
1210
+    if (retract_action & ProbeEngage) engage_z_probe();
1211
+  #endif
1212
+
1213
   run_z_probe();
1213
   run_z_probe();
1214
   float measured_z = current_position[Z_AXIS];
1214
   float measured_z = current_position[Z_AXIS];
1215
-#ifndef Z_PROBE_SLED
1216
-  if ((retract_action==0) || (retract_action==3)) 
1217
-     retract_z_probe();
1218
-#endif // Z_PROBE_SLED
1215
+
1216
+  #ifndef Z_PROBE_SLED
1217
+    if (retract_action & ProbeRetract) retract_z_probe();
1218
+  #endif
1219
 
1219
 
1220
   SERIAL_PROTOCOLPGM(MSG_BED);
1220
   SERIAL_PROTOCOLPGM(MSG_BED);
1221
   SERIAL_PROTOCOLPGM(" x: ");
1221
   SERIAL_PROTOCOLPGM(" x: ");
1376
 #endif //FWRETRACT
1376
 #endif //FWRETRACT
1377
 
1377
 
1378
 #ifdef Z_PROBE_SLED
1378
 #ifdef Z_PROBE_SLED
1379
+
1380
+  #ifndef SLED_DOCKING_OFFSET
1381
+    #define SLED_DOCKING_OFFSET 0
1382
+  #endif
1383
+
1379
 //
1384
 //
1380
 // Method to dock/undock a sled designed by Charles Bell.
1385
 // Method to dock/undock a sled designed by Charles Bell.
1381
 //
1386
 //
1430
             if(autoretract_enabled)
1435
             if(autoretract_enabled)
1431
             if( !(code_seen('X') || code_seen('Y') || code_seen('Z')) && code_seen('E')) {
1436
             if( !(code_seen('X') || code_seen('Y') || code_seen('Z')) && code_seen('E')) {
1432
               float echange=destination[E_AXIS]-current_position[E_AXIS];
1437
               float echange=destination[E_AXIS]-current_position[E_AXIS];
1433
-              if((echange<-MIN_RETRACT && !retracted) || (echange>MIN_RETRACT && retracted)) { //move appears to be an attempt to retract or recover
1438
+              if((echange<-MIN_RETRACT && !retracted[active_extruder]) || (echange>MIN_RETRACT && retracted[active_extruder])) { //move appears to be an attempt to retract or recover
1434
                   current_position[E_AXIS] = destination[E_AXIS]; //hide the slicer-generated retract/recover from calculations
1439
                   current_position[E_AXIS] = destination[E_AXIS]; //hide the slicer-generated retract/recover from calculations
1435
                   plan_set_e_position(current_position[E_AXIS]); //AND from the planner
1440
                   plan_set_e_position(current_position[E_AXIS]); //AND from the planner
1436
-                  retract(!retracted);
1441
+                  retract(!retracted[active_extruder]);
1437
                   return;
1442
                   return;
1438
               }
1443
               }
1439
             }
1444
             }
1662
                                                 // Let's see if X and Y are homed and probe is inside bed area.
1667
                                                 // Let's see if X and Y are homed and probe is inside bed area.
1663
           if(code_seen(axis_codes[Z_AXIS])) {
1668
           if(code_seen(axis_codes[Z_AXIS])) {
1664
             if ( (axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]) \
1669
             if ( (axis_known_position[X_AXIS]) && (axis_known_position[Y_AXIS]) \
1665
-              && (current_position[X_AXIS]+X_PROBE_OFFSET_FROM_EXTRUDER >= X_MIN_POS) \
1666
-              && (current_position[X_AXIS]+X_PROBE_OFFSET_FROM_EXTRUDER <= X_MAX_POS) \
1667
-              && (current_position[Y_AXIS]+Y_PROBE_OFFSET_FROM_EXTRUDER >= Y_MIN_POS) \
1668
-              && (current_position[Y_AXIS]+Y_PROBE_OFFSET_FROM_EXTRUDER <= Y_MAX_POS)) {
1670
+              && (current_position[X_AXIS] >= X_MIN_POS - X_PROBE_OFFSET_FROM_EXTRUDER) \
1671
+              && (current_position[X_AXIS] <= X_MAX_POS - X_PROBE_OFFSET_FROM_EXTRUDER) \
1672
+              && (current_position[Y_AXIS] >= Y_MIN_POS - Y_PROBE_OFFSET_FROM_EXTRUDER) \
1673
+              && (current_position[Y_AXIS] <= Y_MAX_POS - Y_PROBE_OFFSET_FROM_EXTRUDER)) {
1669
 
1674
 
1670
               current_position[Z_AXIS] = 0;
1675
               current_position[Z_AXIS] = 0;
1671
               plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
1676
               plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
1719
       break;
1724
       break;
1720
 
1725
 
1721
 #ifdef ENABLE_AUTO_BED_LEVELING
1726
 #ifdef ENABLE_AUTO_BED_LEVELING
1727
+
1728
+    #if Z_MIN_PIN == -1
1729
+      #error "You must have a Z_MIN endstop in order to enable Auto Bed Leveling!!! Z_MIN_PIN must point to a valid hardware pin."
1730
+    #endif
1731
+
1732
+   /**
1733
+    * Enhanced G29 Auto Bed Leveling Probe Routine
1734
+    * 
1735
+    * Parameters With AUTO_BED_LEVELING_GRID:
1736
+    *
1737
+    *  P  Set the size of the grid that will be probed (P x P points).
1738
+    *     Example: "G29 P4"
1739
+    *
1740
+    *  V  Set the verbose level (0-4). Example: "G29 V3"
1741
+    *
1742
+    *  T  Generate a Bed Topology Report. Example: "G29 P5 T" for a detailed report.
1743
+    *     This is useful for manual bed leveling and finding flaws in the bed (to
1744
+    *     assist with part placement).
1745
+    *
1746
+    *  F  Set the Front limit of the probing grid
1747
+    *  B  Set the Back limit of the probing grid
1748
+    *  L  Set the Left limit of the probing grid
1749
+    *  R  Set the Right limit of the probing grid
1750
+    *
1751
+    * Global Parameters:
1752
+    *
1753
+    * E/e By default G29 engages / disengages the probe for each point.
1754
+    *     Include "E" to engage and disengage the probe just once.
1755
+    *     There's no extra effect if you have a fixed probe.
1756
+    *     Usage: "G29 E" or "G29 e"
1757
+    *
1758
+    */
1759
+
1722
     case 29: // G29 Detailed Z-Probe, probes the bed at 3 or more points.
1760
     case 29: // G29 Detailed Z-Probe, probes the bed at 3 or more points.
1723
-    	     // Override probing area by providing [F]ront [B]ack [L]eft [R]ight Grid[P]oints values
1724
-        {
1725
-            #if Z_MIN_PIN == -1
1726
-            #error "You must have a Z_MIN endstop in order to enable Auto Bed Leveling feature!!! Z_MIN_PIN must point to a valid hardware pin."
1727
-            #endif
1761
+    {
1762
+      // Use one of these defines to specify the origin
1763
+      // for a topographical map to be printed for your bed.
1764
+      #define ORIGIN_BACK_LEFT   1
1765
+      #define ORIGIN_FRONT_RIGHT 2
1766
+      #define ORIGIN_BACK_RIGHT  3
1767
+      #define ORIGIN_FRONT_LEFT  4
1768
+      #define TOPO_ORIGIN        ORIGIN_FRONT_LEFT
1769
+
1770
+      // Prevent user from running a G29 without first homing in X and Y
1771
+      if (!(axis_known_position[X_AXIS] && axis_known_position[Y_AXIS])) {
1772
+        LCD_MESSAGEPGM(MSG_POSITION_UNKNOWN);
1773
+        SERIAL_ECHO_START;
1774
+        SERIAL_ECHOLNPGM(MSG_POSITION_UNKNOWN);
1775
+        break; // abort G29, since we don't know where we are
1776
+      }
1728
 
1777
 
1729
-            // Prevent user from running a G29 without first homing in X and Y
1730
-            if (! (axis_known_position[X_AXIS] && axis_known_position[Y_AXIS]) )
1731
-            {
1732
-                LCD_MESSAGEPGM(MSG_POSITION_UNKNOWN);
1733
-                SERIAL_ECHO_START;
1734
-                SERIAL_ECHOLNPGM(MSG_POSITION_UNKNOWN);
1735
-                break; // abort G29, since we don't know where we are
1736
-            }
1778
+      bool enhanced_g29 = code_seen('E') || code_seen('e');
1737
 
1779
 
1738
-#ifdef Z_PROBE_SLED
1739
-            dock_sled(false);
1740
-#endif // Z_PROBE_SLED
1741
-            st_synchronize();
1742
-            // make sure the bed_level_rotation_matrix is identity or the planner will get it incorectly
1743
-            //vector_3 corrected_position = plan_get_position_mm();
1744
-            //corrected_position.debug("position before G29");
1745
-            plan_bed_level_matrix.set_to_identity();
1746
-            vector_3 uncorrected_position = plan_get_position();
1747
-            //uncorrected_position.debug("position durring G29");
1748
-            current_position[X_AXIS] = uncorrected_position.x;
1749
-            current_position[Y_AXIS] = uncorrected_position.y;
1750
-            current_position[Z_AXIS] = uncorrected_position.z;
1751
-            plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
1752
-            setup_for_endstop_move();
1780
+      #ifdef AUTO_BED_LEVELING_GRID
1753
 
1781
 
1754
-            feedrate = homing_feedrate[Z_AXIS];
1755
-#ifdef AUTO_BED_LEVELING_GRID
1756
-            // probe at the points of a lattice grid
1757
-            int left_probe_bed_position=LEFT_PROBE_BED_POSITION;
1758
-            int right_probe_bed_position=RIGHT_PROBE_BED_POSITION;
1759
-            int back_probe_bed_position=BACK_PROBE_BED_POSITION;
1760
-            int front_probe_bed_position=FRONT_PROBE_BED_POSITION;
1761
-            int auto_bed_leveling_grid_points=AUTO_BED_LEVELING_GRID_POINTS;
1762
-            if (code_seen('L')) left_probe_bed_position=(int)code_value();
1763
-            if (code_seen('R')) right_probe_bed_position=(int)code_value();
1764
-            if (code_seen('B')) back_probe_bed_position=(int)code_value();
1765
-            if (code_seen('F')) front_probe_bed_position=(int)code_value();
1766
-            if (code_seen('P')) auto_bed_leveling_grid_points=(int)code_value();
1782
+        // Example Syntax:  G29 N4 V2 E T
1783
+        int verbose_level = 1;
1767
 
1784
 
1768
-            int xGridSpacing = (right_probe_bed_position - left_probe_bed_position) / (auto_bed_leveling_grid_points-1);
1769
-            int yGridSpacing = (back_probe_bed_position - front_probe_bed_position) / (auto_bed_leveling_grid_points-1);
1785
+        bool topo_flag = code_seen('T') || code_seen('t');
1770
 
1786
 
1787
+        if (code_seen('V') || code_seen('v')) {
1788
+          verbose_level = code_value();
1789
+          if (verbose_level < 0 || verbose_level > 4) {
1790
+            SERIAL_PROTOCOLPGM("?(V)erbose Level is implausible (0-4).\n");
1791
+            break;
1792
+          }
1793
+          if (verbose_level > 0) {
1794
+            SERIAL_PROTOCOLPGM("G29 Enhanced Auto Bed Leveling Code V1.25:\n");
1795
+            SERIAL_PROTOCOLPGM("Full support at: http://3dprintboard.com/forum.php\n");
1796
+            if (verbose_level > 2) topo_flag = true;
1797
+          }
1798
+        }
1771
 
1799
 
1772
-            // solve the plane equation ax + by + d = z
1773
-            // A is the matrix with rows [x y 1] for all the probed points
1774
-            // B is the vector of the Z positions
1775
-            // the normal vector to the plane is formed by the coefficients of the plane equation in the standard form, which is Vx*x+Vy*y+Vz*z+d = 0
1776
-            // so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
1800
+        int auto_bed_leveling_grid_points = code_seen('P') ? code_value_long() : AUTO_BED_LEVELING_GRID_POINTS;
1801
+        if (auto_bed_leveling_grid_points < 2 || auto_bed_leveling_grid_points > AUTO_BED_LEVELING_GRID_POINTS) {
1802
+          SERIAL_PROTOCOLPGM("?Number of probed (P)oints is implausible (2 minimum).\n");
1803
+          break;
1804
+        }
1777
 
1805
 
1778
-            // "A" matrix of the linear system of equations
1779
-            double eqnAMatrix[auto_bed_leveling_grid_points*auto_bed_leveling_grid_points*3];
1806
+        // Define the possible boundaries for probing based on the set limits.
1807
+        // Code above (in G28) might have these limits wrong, or I am wrong here.
1808
+        #define MIN_PROBE_EDGE 10 // Edges of the probe square can be no less
1809
+        const int min_probe_x = max(X_MIN_POS, X_MIN_POS + X_PROBE_OFFSET_FROM_EXTRUDER),
1810
+                  max_probe_x = min(X_MAX_POS, X_MAX_POS + X_PROBE_OFFSET_FROM_EXTRUDER),
1811
+                  min_probe_y = max(Y_MIN_POS, Y_MIN_POS + Y_PROBE_OFFSET_FROM_EXTRUDER),
1812
+                  max_probe_y = min(Y_MAX_POS, Y_MAX_POS + Y_PROBE_OFFSET_FROM_EXTRUDER);
1813
+
1814
+        int left_probe_bed_position = code_seen('L') ? code_value_long() : LEFT_PROBE_BED_POSITION,
1815
+            right_probe_bed_position = code_seen('R') ? code_value_long() : RIGHT_PROBE_BED_POSITION,
1816
+            front_probe_bed_position = code_seen('F') ? code_value_long() : FRONT_PROBE_BED_POSITION,
1817
+            back_probe_bed_position = code_seen('B') ? code_value_long() : BACK_PROBE_BED_POSITION;
1818
+
1819
+        bool left_out_l = left_probe_bed_position < min_probe_x,
1820
+             left_out_r = left_probe_bed_position > right_probe_bed_position - MIN_PROBE_EDGE,
1821
+             left_out = left_out_l || left_out_r,
1822
+             right_out_r = right_probe_bed_position > max_probe_x,
1823
+             right_out_l =right_probe_bed_position < left_probe_bed_position + MIN_PROBE_EDGE,
1824
+             right_out = right_out_l || right_out_r,
1825
+             front_out_f = front_probe_bed_position < min_probe_y,
1826
+             front_out_b = front_probe_bed_position > back_probe_bed_position - MIN_PROBE_EDGE,
1827
+             front_out = front_out_f || front_out_b,
1828
+             back_out_b = back_probe_bed_position > max_probe_y,
1829
+             back_out_f = back_probe_bed_position < front_probe_bed_position + MIN_PROBE_EDGE,
1830
+             back_out = back_out_f || back_out_b;
1831
+
1832
+        if (left_out || right_out || front_out || back_out) {
1833
+          if (left_out) {
1834
+            SERIAL_PROTOCOLPGM("?Probe (L)eft position out of range.\n");
1835
+            left_probe_bed_position = left_out_l ? min_probe_x : right_probe_bed_position - MIN_PROBE_EDGE;
1836
+          }
1837
+          if (right_out) {
1838
+            SERIAL_PROTOCOLPGM("?Probe (R)ight position out of range.\n");
1839
+            right_probe_bed_position = right_out_r ? max_probe_x : left_probe_bed_position + MIN_PROBE_EDGE;
1840
+          }
1841
+          if (front_out) {
1842
+            SERIAL_PROTOCOLPGM("?Probe (F)ront position out of range.\n");
1843
+            front_probe_bed_position = front_out_f ? min_probe_y : back_probe_bed_position - MIN_PROBE_EDGE;
1844
+          }
1845
+          if (back_out) {
1846
+            SERIAL_PROTOCOLPGM("?Probe (B)ack position out of range.\n");
1847
+            back_probe_bed_position = back_out_b ? max_probe_y : front_probe_bed_position + MIN_PROBE_EDGE;
1848
+          }
1849
+          break;
1850
+        }
1780
 
1851
 
1781
-            // "B" vector of Z points
1782
-            double eqnBVector[auto_bed_leveling_grid_points*auto_bed_leveling_grid_points];
1852
+      #endif
1783
 
1853
 
1854
+      #ifdef Z_PROBE_SLED
1855
+        dock_sled(false); // engage (un-dock) the probe
1856
+      #endif
1784
 
1857
 
1858
+      st_synchronize();
1859
+      // make sure the bed_level_rotation_matrix is identity or the planner will get it incorectly
1860
+      //vector_3 corrected_position = plan_get_position_mm();
1861
+      //corrected_position.debug("position before G29");
1862
+      plan_bed_level_matrix.set_to_identity();
1863
+      vector_3 uncorrected_position = plan_get_position();
1864
+      //uncorrected_position.debug("position durring G29");
1865
+      current_position[X_AXIS] = uncorrected_position.x;
1866
+      current_position[Y_AXIS] = uncorrected_position.y;
1867
+      current_position[Z_AXIS] = uncorrected_position.z;
1868
+      plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
1869
+      setup_for_endstop_move();
1785
 
1870
 
1786
-            int probePointCounter = 0;
1787
-            bool zig = true;
1871
+      feedrate = homing_feedrate[Z_AXIS];
1788
 
1872
 
1789
-            for (int yProbe=front_probe_bed_position; yProbe <= back_probe_bed_position; yProbe += yGridSpacing)
1873
+      #ifdef AUTO_BED_LEVELING_GRID
1874
+        // probe at the points of a lattice grid
1790
 
1875
 
1791
-            {
1792
-              int xProbe, xInc;
1793
-              if (zig)
1794
-              {
1795
-                xProbe = left_probe_bed_position;
1796
-                //xEnd = right_probe_bed_position;
1797
-                xInc = xGridSpacing;
1798
-                zig = false;
1799
-              } else // zag
1800
-              {
1801
-                xProbe = right_probe_bed_position;
1802
-                //xEnd = left_probe_bed_position;
1803
-                xInc = -xGridSpacing;
1804
-                zig = true;
1805
-              }
1876
+        int xGridSpacing = (right_probe_bed_position - left_probe_bed_position) / (auto_bed_leveling_grid_points - 1);
1877
+        int yGridSpacing = (back_probe_bed_position - front_probe_bed_position) / (auto_bed_leveling_grid_points - 1);
1806
 
1878
 
1807
-              for (int xCount=0; xCount < auto_bed_leveling_grid_points; xCount++)
1808
-              {
1809
-                float z_before;
1810
-                if (probePointCounter == 0)
1811
-                {
1812
-                  // raise before probing
1813
-                  z_before = Z_RAISE_BEFORE_PROBING;
1814
-                } else
1815
-                {
1816
-                  // raise extruder
1817
-                  z_before = current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS;
1818
-                }
1819
-
1820
-                float measured_z;
1821
-                //Enhanced G29 - Do not retract servo between probes
1822
-                if (code_seen('E') || code_seen('e') )
1823
-                   {
1824
-                   if ((yProbe==FRONT_PROBE_BED_POSITION) && (xCount==0))
1825
-                       {
1826
-                        measured_z = probe_pt(xProbe, yProbe, z_before,1);
1827
-                       } else if ((yProbe==FRONT_PROBE_BED_POSITION + (yGridSpacing * (AUTO_BED_LEVELING_GRID_POINTS-1))) && (xCount == AUTO_BED_LEVELING_GRID_POINTS-1))
1828
-                         {
1829
-                         measured_z = probe_pt(xProbe, yProbe, z_before,3);
1830
-                         } else {
1831
-                           measured_z = probe_pt(xProbe, yProbe, z_before,2);
1832
-                         }
1833
-                    } else {
1834
-                    measured_z = probe_pt(xProbe, yProbe, z_before);
1835
-                    }
1836
-
1837
-                eqnBVector[probePointCounter] = measured_z;
1838
-
1839
-                eqnAMatrix[probePointCounter + 0*auto_bed_leveling_grid_points*auto_bed_leveling_grid_points] = xProbe;
1840
-                eqnAMatrix[probePointCounter + 1*auto_bed_leveling_grid_points*auto_bed_leveling_grid_points] = yProbe;
1841
-                eqnAMatrix[probePointCounter + 2*auto_bed_leveling_grid_points*auto_bed_leveling_grid_points] = 1;
1842
-                probePointCounter++;
1843
-                xProbe += xInc;
1844
-              }
1879
+        // solve the plane equation ax + by + d = z
1880
+        // A is the matrix with rows [x y 1] for all the probed points
1881
+        // B is the vector of the Z positions
1882
+        // the normal vector to the plane is formed by the coefficients of the plane equation in the standard form, which is Vx*x+Vy*y+Vz*z+d = 0
1883
+        // so Vx = -a Vy = -b Vz = 1 (we want the vector facing towards positive Z
1884
+
1885
+        int abl2 = auto_bed_leveling_grid_points * auto_bed_leveling_grid_points;
1886
+
1887
+        double eqnAMatrix[abl2 * 3], // "A" matrix of the linear system of equations
1888
+               eqnBVector[abl2],     // "B" vector of Z points
1889
+               mean = 0.0;
1890
+
1891
+        int probePointCounter = 0;
1892
+        bool zig = true;
1893
+
1894
+        for (int yProbe = front_probe_bed_position; yProbe <= back_probe_bed_position; yProbe += yGridSpacing) {
1895
+          int xProbe, xInc;
1896
+
1897
+          if (zig)
1898
+            xProbe = left_probe_bed_position, xInc = xGridSpacing;
1899
+          else
1900
+            xProbe = right_probe_bed_position, xInc = -xGridSpacing;
1901
+
1902
+          // If topo_flag is set then don't zig-zag. Just scan in one direction.
1903
+          // This gets the probe points in more readable order.
1904
+          if (!topo_flag) zig = !zig;
1905
+
1906
+          for (int xCount = 0; xCount < auto_bed_leveling_grid_points; xCount++) {
1907
+            // raise extruder
1908
+            float z_before = probePointCounter == 0 ? Z_RAISE_BEFORE_PROBING : current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS,
1909
+                  measured_z;
1910
+
1911
+            // Enhanced G29 - Do not retract servo between probes
1912
+            ProbeAction act;
1913
+            if (enhanced_g29) {
1914
+              if (yProbe == front_probe_bed_position && xCount == 0)
1915
+                act = ProbeEngage;
1916
+              else if (yProbe == front_probe_bed_position + (yGridSpacing * (auto_bed_leveling_grid_points - 1)) && xCount == auto_bed_leveling_grid_points - 1)
1917
+                act = ProbeRetract;
1918
+              else
1919
+                act = ProbeStay;
1845
             }
1920
             }
1846
-            clean_up_after_endstop_move();
1921
+            else
1922
+              act = ProbeEngageRetract;
1847
 
1923
 
1848
-            // solve lsq problem
1849
-            double *plane_equation_coefficients = qr_solve(auto_bed_leveling_grid_points*auto_bed_leveling_grid_points, 3, eqnAMatrix, eqnBVector);
1924
+            measured_z = probe_pt(xProbe, yProbe, z_before, act);
1850
 
1925
 
1851
-            SERIAL_PROTOCOLPGM("Eqn coefficients: a: ");
1852
-            SERIAL_PROTOCOL(plane_equation_coefficients[0]);
1853
-            SERIAL_PROTOCOLPGM(" b: ");
1854
-            SERIAL_PROTOCOL(plane_equation_coefficients[1]);
1855
-            SERIAL_PROTOCOLPGM(" d: ");
1856
-            SERIAL_PROTOCOLLN(plane_equation_coefficients[2]);
1926
+            mean += measured_z;
1857
 
1927
 
1928
+            eqnBVector[probePointCounter] = measured_z;
1929
+            eqnAMatrix[probePointCounter + 0 * abl2] = xProbe;
1930
+            eqnAMatrix[probePointCounter + 1 * abl2] = yProbe;
1931
+            eqnAMatrix[probePointCounter + 2 * abl2] = 1;
1858
 
1932
 
1859
-            set_bed_level_equation_lsq(plane_equation_coefficients);
1933
+            probePointCounter++;
1934
+            xProbe += xInc;
1860
 
1935
 
1861
-            free(plane_equation_coefficients);
1936
+          } //xProbe
1862
 
1937
 
1863
-#else // AUTO_BED_LEVELING_GRID not defined
1938
+        } //yProbe
1864
 
1939
 
1865
-            // Probe at 3 arbitrary points
1866
-            // Enhanced G29
1867
-            
1868
-            float z_at_pt_1, z_at_pt_2, z_at_pt_3;
1869
-            
1870
-            if (code_seen('E') || code_seen('e')) {
1871
-              // probe 1               
1872
-              z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING,1);
1873
-              // probe 2
1874
-              z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS,2);
1875
-              // probe 3
1876
-              z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS,3); 
1877
-            }
1878
-            else {
1879
-              // probe 1
1880
-              z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING);
1881
-              // probe 2
1882
-              z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
1883
-              // probe 3
1884
-              z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
1885
-            }
1886
-            clean_up_after_endstop_move();
1887
-            set_bed_level_equation_3pts(z_at_pt_1, z_at_pt_2, z_at_pt_3);
1940
+        clean_up_after_endstop_move();
1888
 
1941
 
1942
+        // solve lsq problem
1943
+        double *plane_equation_coefficients = qr_solve(abl2, 3, eqnAMatrix, eqnBVector);
1944
+
1945
+        mean /= abl2;
1946
+
1947
+        if (verbose_level) {
1948
+          SERIAL_PROTOCOLPGM("Eqn coefficients: a: ");
1949
+          SERIAL_PROTOCOL(plane_equation_coefficients[0]);
1950
+          SERIAL_PROTOCOLPGM(" b: ");
1951
+          SERIAL_PROTOCOL(plane_equation_coefficients[1]);
1952
+          SERIAL_PROTOCOLPGM(" d: ");
1953
+          SERIAL_PROTOCOLLN(plane_equation_coefficients[2]);
1954
+          if (verbose_level > 2) {
1955
+            SERIAL_PROTOCOLPGM("Mean of sampled points: ");
1956
+            SERIAL_PROTOCOL_F(mean, 6);
1957
+            SERIAL_PROTOCOLPGM(" \n");
1958
+          }
1959
+        }
1889
 
1960
 
1890
-#endif // AUTO_BED_LEVELING_GRID
1891
-            st_synchronize();
1961
+        if (topo_flag) {
1892
 
1962
 
1893
-            // The following code correct the Z height difference from z-probe position and hotend tip position.
1894
-            // The Z height on homing is measured by Z-Probe, but the probe is quite far from the hotend.
1895
-            // When the bed is uneven, this height must be corrected.
1896
-            real_z = float(st_get_position(Z_AXIS))/axis_steps_per_unit[Z_AXIS];  //get the real Z (since the auto bed leveling is already correcting the plane)
1897
-            x_tmp = current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER;
1898
-            y_tmp = current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER;
1899
-            z_tmp = current_position[Z_AXIS];
1963
+          int xx, yy;
1900
 
1964
 
1901
-            apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp);         //Apply the correction sending the probe offset
1902
-            current_position[Z_AXIS] = z_tmp - real_z + current_position[Z_AXIS];   //The difference is added to current position and sent to planner.
1903
-            plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
1904
-#ifdef Z_PROBE_SLED
1905
-            dock_sled(true, -SLED_DOCKING_OFFSET); // correct for over travel.
1906
-#endif // Z_PROBE_SLED
1965
+          SERIAL_PROTOCOLPGM(" \nBed Height Topography: \n");
1966
+          #if TOPO_ORIGIN == ORIGIN_FRONT_LEFT
1967
+            for (yy = auto_bed_leveling_grid_points - 1; yy >= 0; yy--)
1968
+          #else
1969
+            for (yy = 0; yy < auto_bed_leveling_grid_points; yy++)
1970
+          #endif
1971
+            {
1972
+              #if TOPO_ORIGIN == ORIGIN_BACK_RIGHT
1973
+                for (xx = auto_bed_leveling_grid_points - 1; xx >= 0; xx--)
1974
+              #else
1975
+                for (xx = 0; xx < auto_bed_leveling_grid_points; xx++)
1976
+              #endif
1977
+                {
1978
+                  int ind =
1979
+                    #if TOPO_ORIGIN == ORIGIN_BACK_RIGHT || TOPO_ORIGIN == ORIGIN_FRONT_LEFT
1980
+                      yy * auto_bed_leveling_grid_points + xx
1981
+                    #elif TOPO_ORIGIN == ORIGIN_BACK_LEFT
1982
+                      xx * auto_bed_leveling_grid_points + yy
1983
+                    #elif TOPO_ORIGIN == ORIGIN_FRONT_RIGHT
1984
+                      abl2 - xx * auto_bed_leveling_grid_points - yy - 1
1985
+                    #endif
1986
+                  ;
1987
+                  float diff = eqnBVector[ind] - mean;
1988
+                  if (diff >= 0.0)
1989
+                    SERIAL_PROTOCOLPGM(" +");   // Watch column alignment in Pronterface
1990
+                  else
1991
+                    SERIAL_PROTOCOLPGM(" -");
1992
+                  SERIAL_PROTOCOL_F(diff, 5);
1993
+                } // xx
1994
+                SERIAL_PROTOCOLPGM("\n");
1995
+            } // yy
1996
+            SERIAL_PROTOCOLPGM("\n");
1997
+
1998
+        } //topo_flag
1999
+
2000
+
2001
+        set_bed_level_equation_lsq(plane_equation_coefficients);
2002
+        free(plane_equation_coefficients);
2003
+
2004
+      #else // !AUTO_BED_LEVELING_GRID
2005
+
2006
+        // Probe at 3 arbitrary points
2007
+        float z_at_pt_1, z_at_pt_2, z_at_pt_3;
2008
+
2009
+        if (enhanced_g29) {
2010
+          // Basic Enhanced G29
2011
+          z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING, ProbeEngage);
2012
+          z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS, ProbeStay);
2013
+          z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS, ProbeRetract);
1907
         }
2014
         }
1908
-        break;
2015
+        else {
2016
+          z_at_pt_1 = probe_pt(ABL_PROBE_PT_1_X, ABL_PROBE_PT_1_Y, Z_RAISE_BEFORE_PROBING);
2017
+          z_at_pt_2 = probe_pt(ABL_PROBE_PT_2_X, ABL_PROBE_PT_2_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
2018
+          z_at_pt_3 = probe_pt(ABL_PROBE_PT_3_X, ABL_PROBE_PT_3_Y, current_position[Z_AXIS] + Z_RAISE_BETWEEN_PROBINGS);
2019
+        }
2020
+        clean_up_after_endstop_move();
2021
+        set_bed_level_equation_3pts(z_at_pt_1, z_at_pt_2, z_at_pt_3);
2022
+
2023
+      #endif // !AUTO_BED_LEVELING_GRID
2024
+
2025
+      st_synchronize();
2026
+
2027
+      if (verbose_level > 0)
2028
+        plan_bed_level_matrix.debug(" \n\nBed Level Correction Matrix:");
2029
+
2030
+      // The following code correct the Z height difference from z-probe position and hotend tip position.
2031
+      // The Z height on homing is measured by Z-Probe, but the probe is quite far from the hotend.
2032
+      // When the bed is uneven, this height must be corrected.
2033
+      real_z = float(st_get_position(Z_AXIS)) / axis_steps_per_unit[Z_AXIS];  //get the real Z (since the auto bed leveling is already correcting the plane)
2034
+      x_tmp = current_position[X_AXIS] + X_PROBE_OFFSET_FROM_EXTRUDER;
2035
+      y_tmp = current_position[Y_AXIS] + Y_PROBE_OFFSET_FROM_EXTRUDER;
2036
+      z_tmp = current_position[Z_AXIS];
2037
+
2038
+      apply_rotation_xyz(plan_bed_level_matrix, x_tmp, y_tmp, z_tmp);         //Apply the correction sending the probe offset
2039
+      current_position[Z_AXIS] = z_tmp - real_z + current_position[Z_AXIS];   //The difference is added to current position and sent to planner.
2040
+      plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);
2041
+
2042
+      #ifdef Z_PROBE_SLED
2043
+        dock_sled(true, -SLED_DOCKING_OFFSET); // dock the probe, correcting for over-travel
2044
+      #endif
2045
+    }
2046
+    break;
2047
+
1909
 #ifndef Z_PROBE_SLED
2048
 #ifndef Z_PROBE_SLED
1910
     case 30: // G30 Single Z Probe
2049
     case 30: // G30 Single Z Probe
1911
         {
2050
         {
2038
         enable_e0();
2177
         enable_e0();
2039
         enable_e1();
2178
         enable_e1();
2040
         enable_e2();
2179
         enable_e2();
2180
+        enable_e3();
2041
       break;
2181
       break;
2042
 
2182
 
2043
 #ifdef SDSUPPORT
2183
 #ifdef SDSUPPORT
2723
 
2863
 
2724
     #if defined(PS_ON_PIN) && PS_ON_PIN > -1
2864
     #if defined(PS_ON_PIN) && PS_ON_PIN > -1
2725
       case 80: // M80 - Turn on Power Supply
2865
       case 80: // M80 - Turn on Power Supply
2726
-        SET_OUTPUT(PS_ON_PIN); //GND
2727
-        WRITE(PS_ON_PIN, PS_ON_AWAKE);
2866
+        OUT_WRITE(PS_ON_PIN, PS_ON_AWAKE); // GND
2728
 
2867
 
2729
         // If you have a switch on suicide pin, this is useful
2868
         // If you have a switch on suicide pin, this is useful
2730
         // if you want to start another print with suicide feature after
2869
         // if you want to start another print with suicide feature after
2731
         // a print without suicide...
2870
         // a print without suicide...
2732
         #if defined SUICIDE_PIN && SUICIDE_PIN > -1
2871
         #if defined SUICIDE_PIN && SUICIDE_PIN > -1
2733
-            SET_OUTPUT(SUICIDE_PIN);
2734
-            WRITE(SUICIDE_PIN, HIGH);
2872
+            OUT_WRITE(SUICIDE_PIN, HIGH);
2735
         #endif
2873
         #endif
2736
 
2874
 
2737
         #ifdef ULTIPANEL
2875
         #ifdef ULTIPANEL
2748
         disable_e0();
2886
         disable_e0();
2749
         disable_e1();
2887
         disable_e1();
2750
         disable_e2();
2888
         disable_e2();
2889
+        disable_e3();
2751
         finishAndDisableSteppers();
2890
         finishAndDisableSteppers();
2752
         fanSpeed = 0;
2891
         fanSpeed = 0;
2753
         delay(1000); // Wait a little before to switch off
2892
         delay(1000); // Wait a little before to switch off
2755
         st_synchronize();
2894
         st_synchronize();
2756
         suicide();
2895
         suicide();
2757
       #elif defined(PS_ON_PIN) && PS_ON_PIN > -1
2896
       #elif defined(PS_ON_PIN) && PS_ON_PIN > -1
2758
-        SET_OUTPUT(PS_ON_PIN);
2759
-        WRITE(PS_ON_PIN, PS_ON_ASLEEP);
2897
+        OUT_WRITE(PS_ON_PIN, PS_ON_ASLEEP);
2760
       #endif
2898
       #endif
2761
       #ifdef ULTIPANEL
2899
       #ifdef ULTIPANEL
2762
         powersupply = false;
2900
         powersupply = false;
2785
           disable_e0();
2923
           disable_e0();
2786
           disable_e1();
2924
           disable_e1();
2787
           disable_e2();
2925
           disable_e2();
2926
+          disable_e3();
2788
           finishAndDisableSteppers();
2927
           finishAndDisableSteppers();
2789
         }
2928
         }
2790
         else
2929
         else
2798
               disable_e0();
2937
               disable_e0();
2799
               disable_e1();
2938
               disable_e1();
2800
               disable_e2();
2939
               disable_e2();
2940
+              disable_e3();
2801
             }
2941
             }
2802
           #endif
2942
           #endif
2803
         }
2943
         }
3118
          SERIAL_ECHO(extruder_offset[Z_AXIS][tmp_extruder]);
3258
          SERIAL_ECHO(extruder_offset[Z_AXIS][tmp_extruder]);
3119
       #endif
3259
       #endif
3120
       }
3260
       }
3121
-      SERIAL_ECHOLN("");
3261
+      SERIAL_EOL;
3122
     }break;
3262
     }break;
3123
     #endif
3263
     #endif
3124
     case 220: // M220 S<factor in percent>- set speed factor override percentage
3264
     case 220: // M220 S<factor in percent>- set speed factor override percentage
3337
      {
3477
      {
3338
      	#ifdef CHDK
3478
      	#ifdef CHDK
3339
        
3479
        
3340
-         SET_OUTPUT(CHDK);
3341
-         WRITE(CHDK, HIGH);
3480
+         OUT_WRITE(CHDK, HIGH);
3342
          chdkHigh = millis();
3481
          chdkHigh = millis();
3343
          chdkActive = true;
3482
          chdkActive = true;
3344
        
3483
        
3497
       }
3636
       }
3498
       break;
3637
       break;
3499
 	#endif
3638
 	#endif
3639
+    
3640
+#ifdef EXT_SOLENOID
3641
+    case 380:
3642
+        enable_solenoid_on_active_extruder();
3643
+        break;
3644
+
3645
+    case 381:
3646
+        disable_all_solenoids();
3647
+        break;
3648
+#endif //EXT_SOLENOID
3649
+
3500
     case 400: // M400 finish all moves
3650
     case 400: // M400 finish all moves
3501
     {
3651
     {
3502
       st_synchronize();
3652
       st_synchronize();
3726
         disable_e0();
3876
         disable_e0();
3727
         disable_e1();
3877
         disable_e1();
3728
         disable_e2();
3878
         disable_e2();
3879
+        disable_e3();
3729
         delay(100);
3880
         delay(100);
3730
         LCD_ALERTMESSAGEPGM(MSG_FILAMENTCHANGE);
3881
         LCD_ALERTMESSAGEPGM(MSG_FILAMENTCHANGE);
3731
         uint8_t cnt=0;
3882
         uint8_t cnt=0;
3737
           if(cnt==0)
3888
           if(cnt==0)
3738
           {
3889
           {
3739
           #if BEEPER > 0
3890
           #if BEEPER > 0
3740
-            SET_OUTPUT(BEEPER);
3741
-
3742
-            WRITE(BEEPER,HIGH);
3891
+            OUT_WRITE(BEEPER,HIGH);
3743
             delay(3);
3892
             delay(3);
3744
             WRITE(BEEPER,LOW);
3893
             WRITE(BEEPER,LOW);
3745
             delay(3);
3894
             delay(3);
4000
            prepare_move();
4149
            prepare_move();
4001
         }
4150
         }
4002
       }
4151
       }
4152
+
4153
+#ifdef EXT_SOLENOID
4154
+      st_synchronize();
4155
+      disable_all_solenoids();
4156
+      enable_solenoid_on_active_extruder();
4157
+#endif //EXT_SOLENOID
4158
+
4003
       #endif
4159
       #endif
4004
       SERIAL_ECHO_START;
4160
       SERIAL_ECHO_START;
4005
       SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
4161
       SERIAL_ECHO(MSG_ACTIVE_EXTRUDER);
4469
         disable_e0();
4625
         disable_e0();
4470
         disable_e1();
4626
         disable_e1();
4471
         disable_e2();
4627
         disable_e2();
4628
+        disable_e3();
4472
       }
4629
       }
4473
     }
4630
     }
4474
   }
4631
   }
4574
   disable_e0();
4731
   disable_e0();
4575
   disable_e1();
4732
   disable_e1();
4576
   disable_e2();
4733
   disable_e2();
4734
+  disable_e3();
4577
 
4735
 
4578
 #if defined(PS_ON_PIN) && PS_ON_PIN > -1
4736
 #if defined(PS_ON_PIN) && PS_ON_PIN > -1
4579
   pinMode(PS_ON_PIN,INPUT);
4737
   pinMode(PS_ON_PIN,INPUT);
4707
   return false;
4865
   return false;
4708
 }
4866
 }
4709
 
4867
 
4710
-
4711
 float calculate_volumetric_multiplier(float diameter) {
4868
 float calculate_volumetric_multiplier(float diameter) {
4712
   if (!volumetric_enabled || diameter == 0) return 1.0;
4869
   if (!volumetric_enabled || diameter == 0) return 1.0;
4713
   float d2 = diameter * 0.5;
4870
   float d2 = diameter * 0.5;
4718
   for (int i=0; i<EXTRUDERS; i++)
4875
   for (int i=0; i<EXTRUDERS; i++)
4719
     volumetric_multiplier[i] = calculate_volumetric_multiplier(filament_size[i]);
4876
     volumetric_multiplier[i] = calculate_volumetric_multiplier(filament_size[i]);
4720
 }
4877
 }
4878
+
4879
+#ifdef EXT_SOLENOID
4880
+
4881
+void enable_solenoid(uint8_t num) {
4882
+  switch(num) {
4883
+    case 0:
4884
+      OUT_WRITE(SOL0_PIN, HIGH);
4885
+      break;
4886
+      #if defined(SOL1_PIN) && SOL1_PIN > -1
4887
+        case 1:
4888
+          OUT_WRITE(SOL1_PIN, HIGH);
4889
+          break;
4890
+      #endif
4891
+      #if defined(SOL2_PIN) && SOL2_PIN > -1
4892
+        case 2:
4893
+          OUT_WRITE(SOL2_PIN, HIGH);
4894
+          break;
4895
+      #endif
4896
+      #if defined(SOL3_PIN) && SOL3_PIN > -1
4897
+        case 3:
4898
+          OUT_WRITE(SOL3_PIN, HIGH);
4899
+          break;
4900
+      #endif
4901
+    default:
4902
+      SERIAL_ECHO_START;
4903
+      SERIAL_ECHOLNPGM(MSG_INVALID_SOLENOID);
4904
+      break;
4905
+  }
4906
+}
4907
+
4908
+void enable_solenoid_on_active_extruder() { enable_solenoid(active_extruder); }
4909
+
4910
+void disable_all_solenoids() {
4911
+  OUT_WRITE(SOL0_PIN, LOW);
4912
+  OUT_WRITE(SOL1_PIN, LOW);
4913
+  OUT_WRITE(SOL2_PIN, LOW);
4914
+  OUT_WRITE(SOL3_PIN, LOW);
4915
+}
4916
+
4917
+#endif //EXT_SOLENOID

+ 251
- 378
Marlin/cardreader.cpp View File

7
 
7
 
8
 #ifdef SDSUPPORT
8
 #ifdef SDSUPPORT
9
 
9
 
10
+CardReader::CardReader() {
11
+  filesize = 0;
12
+  sdpos = 0;
13
+  sdprinting = false;
14
+  cardOK = false;
15
+  saving = false;
16
+  logging = false;
17
+  workDirDepth = 0;
18
+  file_subcall_ctr = 0;
19
+  memset(workDirParents, 0, sizeof(workDirParents));
10
 
20
 
11
-
12
-CardReader::CardReader()
13
-{
14
-   filesize = 0;
15
-   sdpos = 0;
16
-   sdprinting = false;
17
-   cardOK = false;
18
-   saving = false;
19
-   logging = false;
20
-   autostart_atmillis=0;
21
-   workDirDepth = 0;
22
-   file_subcall_ctr=0;
23
-   memset(workDirParents, 0, sizeof(workDirParents));
24
-
25
-   autostart_stilltocheck=true; //the SD start is delayed, because otherwise the serial cannot answer fast enough to make contact with the host software.
26
-   autostart_index=0;
21
+  autostart_stilltocheck = true; //the SD start is delayed, because otherwise the serial cannot answer fast enough to make contact with the host software.
22
+  autostart_index = 0;
27
   //power to SD reader
23
   //power to SD reader
28
   #if SDPOWER > -1
24
   #if SDPOWER > -1
29
-    SET_OUTPUT(SDPOWER); 
30
-    WRITE(SDPOWER,HIGH);
25
+    OUT_WRITE(SDPOWER, HIGH);
31
   #endif //SDPOWER
26
   #endif //SDPOWER
32
-  
33
-  autostart_atmillis=millis()+5000;
27
+
28
+  autostart_atmillis = millis() + 5000;
34
 }
29
 }
35
 
30
 
36
-char *createFilename(char *buffer,const dir_t &p) //buffer>12characters
37
-{
38
-  char *pos=buffer;
39
-  for (uint8_t i = 0; i < 11; i++) 
40
-  {
41
-    if (p.name[i] == ' ')continue;
42
-    if (i == 8) 
43
-    {
44
-      *pos++='.';
45
-    }
46
-    *pos++=p.name[i];
31
+char *createFilename(char *buffer, const dir_t &p) { //buffer > 12characters
32
+  char *pos = buffer;
33
+  for (uint8_t i = 0; i < 11; i++) {
34
+    if (p.name[i] == ' ') continue;
35
+    if (i == 8) *pos++ = '.';
36
+    *pos++ = p.name[i];
47
   }
37
   }
48
-  *pos++=0;
38
+  *pos++ = 0;
49
   return buffer;
39
   return buffer;
50
 }
40
 }
51
 
41
 
52
-
53
-void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/)
54
-{
42
+void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) {
55
   dir_t p;
43
   dir_t p;
56
- uint8_t cnt=0;
57
- 
58
-  while (parent.readDir(p, longFilename) > 0)
59
-  {
60
-    if( DIR_IS_SUBDIR(&p) && lsAction!=LS_Count && lsAction!=LS_GetFilename) // hence LS_SerialPrint
61
-    {
44
+  uint8_t cnt = 0;
62
 
45
 
46
+  while (parent.readDir(p, longFilename) > 0) {
47
+    if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) { // hence LS_SerialPrint
63
       char path[FILENAME_LENGTH*2];
48
       char path[FILENAME_LENGTH*2];
64
       char lfilename[FILENAME_LENGTH];
49
       char lfilename[FILENAME_LENGTH];
65
-      createFilename(lfilename,p);
66
-      
67
-      path[0]=0;
68
-      if(prepend[0]==0) //avoid leading / if already in prepend
69
-      {
70
-       strcat(path,"/");
71
-      }
72
-      strcat(path,prepend);
73
-      strcat(path,lfilename);
74
-      strcat(path,"/");
75
-      
50
+      createFilename(lfilename, p);
51
+
52
+      path[0] = 0;
53
+      if (prepend[0] == 0) strcat(path, "/"); //avoid leading / if already in prepend
54
+      strcat(path, prepend);
55
+      strcat(path, lfilename);
56
+      strcat(path, "/");
57
+
76
       //Serial.print(path);
58
       //Serial.print(path);
77
-      
59
+
78
       SdFile dir;
60
       SdFile dir;
79
-      if(!dir.open(parent,lfilename, O_READ))
80
-      {
81
-        if(lsAction==LS_SerialPrint)
82
-        {
61
+      if (!dir.open(parent, lfilename, O_READ)) {
62
+        if (lsAction == LS_SerialPrint) {
83
           SERIAL_ECHO_START;
63
           SERIAL_ECHO_START;
84
           SERIAL_ECHOLN(MSG_SD_CANT_OPEN_SUBDIR);
64
           SERIAL_ECHOLN(MSG_SD_CANT_OPEN_SUBDIR);
85
           SERIAL_ECHOLN(lfilename);
65
           SERIAL_ECHOLN(lfilename);
86
         }
66
         }
87
       }
67
       }
88
-      lsDive(path,dir);
68
+      lsDive(path, dir);
89
       //close done automatically by destructor of SdFile
69
       //close done automatically by destructor of SdFile
90
-
91
-      
92
     }
70
     }
93
-    else
94
-    {
71
+    else {
95
       char pn0 = p.name[0];
72
       char pn0 = p.name[0];
96
       if (pn0 == DIR_NAME_FREE) break;
73
       if (pn0 == DIR_NAME_FREE) break;
97
-      if (pn0 == DIR_NAME_DELETED || pn0 == '.' || pn0 == '_') continue;
74
+      if (pn0 == DIR_NAME_DELETED || pn0 == '.') continue;
98
       char lf0 = longFilename[0];
75
       char lf0 = longFilename[0];
99
-      if (lf0 == '.' || lf0 == '_') continue;
76
+      if (lf0 == '.') continue;
100
 
77
 
101
       if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
78
       if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;
102
-      filenameIsDir=DIR_IS_SUBDIR(&p);
103
-      
104
-      
105
-      if(!filenameIsDir)
106
-      {
107
-        if(p.name[8]!='G') continue;
108
-        if(p.name[9]=='~') continue;
109
-      }
110
-      //if(cnt++!=nr) continue;
111
-      createFilename(filename,p);
112
-      if(lsAction==LS_SerialPrint)
113
-      {
79
+
80
+      filenameIsDir = DIR_IS_SUBDIR(&p);
81
+
82
+      if (!filenameIsDir && (p.name[8] != 'G' || p.name[9] == '~')) continue;
83
+
84
+      //if (cnt++ != nr) continue;
85
+      createFilename(filename, p);
86
+      if (lsAction == LS_SerialPrint) {
114
         SERIAL_PROTOCOL(prepend);
87
         SERIAL_PROTOCOL(prepend);
115
         SERIAL_PROTOCOLLN(filename);
88
         SERIAL_PROTOCOLLN(filename);
116
       }
89
       }
117
-      else if(lsAction==LS_Count)
118
-      {
90
+      else if (lsAction == LS_Count) {
119
         nrFiles++;
91
         nrFiles++;
120
-      } 
121
-      else if(lsAction==LS_GetFilename)
122
-      {
92
+      }
93
+      else if (lsAction == LS_GetFilename) {
123
         if (match != NULL) {
94
         if (match != NULL) {
124
           if (strcasecmp(match, filename) == 0) return;
95
           if (strcasecmp(match, filename) == 0) return;
125
         }
96
         }
126
         else if (cnt == nrFiles) return;
97
         else if (cnt == nrFiles) return;
127
         cnt++;
98
         cnt++;
128
-        
129
       }
99
       }
130
     }
100
     }
131
   }
101
   }
132
 }
102
 }
133
 
103
 
134
-void CardReader::ls() 
135
-{
136
-  lsAction=LS_SerialPrint;
137
-  if(lsAction==LS_Count)
138
-  nrFiles=0;
139
-
104
+void CardReader::ls()  {
105
+  lsAction = LS_SerialPrint;
140
   root.rewind();
106
   root.rewind();
141
-  lsDive("",root);
107
+  lsDive("", root);
142
 }
108
 }
143
 
109
 
144
-
145
-void CardReader::initsd()
146
-{
110
+void CardReader::initsd() {
147
   cardOK = false;
111
   cardOK = false;
148
-  if(root.isOpen())
149
-    root.close();
150
-#ifdef SDSLOW
151
-  if (!card.init(SPI_HALF_SPEED,SDSS)
152
-  #if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
153
-    && !card.init(SPI_HALF_SPEED,LCD_SDSS)
154
-  #endif
155
-    )
156
-#else
157
-  if (!card.init(SPI_FULL_SPEED,SDSS)
158
-  #if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
159
-    && !card.init(SPI_FULL_SPEED,LCD_SDSS)
112
+  if (root.isOpen()) root.close();
113
+
114
+  #ifdef SDSLOW
115
+    #define SPI_SPEED SPI_HALF_SPEED
116
+  #else
117
+    #define SPI_SPEED SPI_FULL_SPEED
160
   #endif
118
   #endif
161
-    )
162
-#endif
163
-  {
119
+
120
+  if (!card.init(SPI_SPEED,SDSS)
121
+    #if defined(LCD_SDSS) && (LCD_SDSS != SDSS)
122
+      && !card.init(SPI_SPEED, LCD_SDSS)
123
+    #endif
124
+  ) {
164
     //if (!card.init(SPI_HALF_SPEED,SDSS))
125
     //if (!card.init(SPI_HALF_SPEED,SDSS))
165
     SERIAL_ECHO_START;
126
     SERIAL_ECHO_START;
166
     SERIAL_ECHOLNPGM(MSG_SD_INIT_FAIL);
127
     SERIAL_ECHOLNPGM(MSG_SD_INIT_FAIL);
167
   }
128
   }
168
-  else if (!volume.init(&card))
169
-  {
129
+  else if (!volume.init(&card)) {
170
     SERIAL_ERROR_START;
130
     SERIAL_ERROR_START;
171
     SERIAL_ERRORLNPGM(MSG_SD_VOL_INIT_FAIL);
131
     SERIAL_ERRORLNPGM(MSG_SD_VOL_INIT_FAIL);
172
   }
132
   }
173
-  else if (!root.openRoot(&volume)) 
174
-  {
133
+  else if (!root.openRoot(&volume)) {
175
     SERIAL_ERROR_START;
134
     SERIAL_ERROR_START;
176
     SERIAL_ERRORLNPGM(MSG_SD_OPENROOT_FAIL);
135
     SERIAL_ERRORLNPGM(MSG_SD_OPENROOT_FAIL);
177
   }
136
   }
178
-  else 
179
-  {
137
+  else {
180
     cardOK = true;
138
     cardOK = true;
181
     SERIAL_ECHO_START;
139
     SERIAL_ECHO_START;
182
     SERIAL_ECHOLNPGM(MSG_SD_CARD_OK);
140
     SERIAL_ECHOLNPGM(MSG_SD_CARD_OK);
183
   }
141
   }
184
-  workDir=root;
185
-  curDir=&root;
142
+  workDir = root;
143
+  curDir = &root;
186
   /*
144
   /*
187
-  if(!workDir.openRoot(&volume))
188
-  {
145
+  if (!workDir.openRoot(&volume)) {
189
     SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
146
     SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
190
   }
147
   }
191
   */
148
   */
192
-  
193
 }
149
 }
194
 
150
 
195
-void CardReader::setroot()
196
-{
197
-  /*if(!workDir.openRoot(&volume))
198
-  {
151
+void CardReader::setroot() {
152
+  /*if (!workDir.openRoot(&volume)) {
199
     SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
153
     SERIAL_ECHOLNPGM(MSG_SD_WORKDIR_FAIL);
200
   }*/
154
   }*/
201
-  workDir=root;
202
-  
203
-  curDir=&workDir;
155
+  workDir = root;
156
+  curDir = &workDir;
204
 }
157
 }
205
-void CardReader::release()
206
-{
158
+
159
+void CardReader::release() {
207
   sdprinting = false;
160
   sdprinting = false;
208
   cardOK = false;
161
   cardOK = false;
209
 }
162
 }
210
 
163
 
211
-void CardReader::startFileprint()
212
-{
213
-  if(cardOK)
214
-  {
164
+void CardReader::startFileprint() {
165
+  if (cardOK) {
215
     sdprinting = true;
166
     sdprinting = true;
216
   }
167
   }
217
 }
168
 }
218
 
169
 
219
-void CardReader::pauseSDPrint()
220
-{
221
-  if(sdprinting)
222
-  {
223
-    sdprinting = false;
224
-  }
170
+void CardReader::pauseSDPrint() {
171
+  if (sdprinting) sdprinting = false;
225
 }
172
 }
226
 
173
 
227
-
228
-void CardReader::openLogFile(char* name)
229
-{
174
+void CardReader::openLogFile(char* name) {
230
   logging = true;
175
   logging = true;
231
   openFile(name, false);
176
   openFile(name, false);
232
 }
177
 }
233
 
178
 
234
-void CardReader::getAbsFilename(char *t)
235
-{
236
-  uint8_t cnt=0;
237
-  *t='/';t++;cnt++;
238
-  for(uint8_t i=0;i<workDirDepth;i++)
239
-  {
179
+void CardReader::getAbsFilename(char *t) {
180
+  uint8_t cnt = 0;
181
+  *t = '/'; t++; cnt++;
182
+  for (uint8_t i = 0; i < workDirDepth; i++) {
240
     workDirParents[i].getFilename(t); //SDBaseFile.getfilename!
183
     workDirParents[i].getFilename(t); //SDBaseFile.getfilename!
241
-    while(*t!=0 && cnt< MAXPATHNAMELENGTH) 
242
-    {t++;cnt++;}  //crawl counter forward.
184
+    while(*t && cnt < MAXPATHNAMELENGTH) { t++; cnt++; } //crawl counter forward.
243
   }
185
   }
244
-  if(cnt<MAXPATHNAMELENGTH-FILENAME_LENGTH)
186
+  if (cnt < MAXPATHNAMELENGTH - FILENAME_LENGTH)
245
     file.getFilename(t);
187
     file.getFilename(t);
246
   else
188
   else
247
-    t[0]=0;
189
+    t[0] = 0;
248
 }
190
 }
249
 
191
 
250
-void CardReader::openFile(char* name,bool read, bool replace_current/*=true*/)
251
-{
252
-  if(!cardOK)
253
-    return;
254
-  if(file.isOpen())  //replacing current file by new file, or subfile call
255
-  {
256
-    if(!replace_current)
257
-    {
258
-     if((int)file_subcall_ctr>(int)SD_PROCEDURE_DEPTH-1)
259
-     {
192
+void CardReader::openFile(char* name, bool read, bool replace_current/*=true*/) {
193
+  if (!cardOK) return;
194
+  if (file.isOpen()) { //replacing current file by new file, or subfile call
195
+    if (!replace_current) {
196
+     if (file_subcall_ctr > SD_PROCEDURE_DEPTH - 1) {
260
        SERIAL_ERROR_START;
197
        SERIAL_ERROR_START;
261
        SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:");
198
        SERIAL_ERRORPGM("trying to call sub-gcode files with too many levels. MAX level is:");
262
        SERIAL_ERRORLN(SD_PROCEDURE_DEPTH);
199
        SERIAL_ERRORLN(SD_PROCEDURE_DEPTH);
263
        kill();
200
        kill();
264
        return;
201
        return;
265
      }
202
      }
266
-     
203
+
267
      SERIAL_ECHO_START;
204
      SERIAL_ECHO_START;
268
      SERIAL_ECHOPGM("SUBROUTINE CALL target:\"");
205
      SERIAL_ECHOPGM("SUBROUTINE CALL target:\"");
269
      SERIAL_ECHO(name);
206
      SERIAL_ECHO(name);
270
      SERIAL_ECHOPGM("\" parent:\"");
207
      SERIAL_ECHOPGM("\" parent:\"");
271
-     
208
+
272
      //store current filename and position
209
      //store current filename and position
273
      getAbsFilename(filenames[file_subcall_ctr]);
210
      getAbsFilename(filenames[file_subcall_ctr]);
274
-     
211
+
275
      SERIAL_ECHO(filenames[file_subcall_ctr]);
212
      SERIAL_ECHO(filenames[file_subcall_ctr]);
276
      SERIAL_ECHOPGM("\" pos");
213
      SERIAL_ECHOPGM("\" pos");
277
      SERIAL_ECHOLN(sdpos);
214
      SERIAL_ECHOLN(sdpos);
278
-     filespos[file_subcall_ctr]=sdpos;
215
+     filespos[file_subcall_ctr] = sdpos;
279
      file_subcall_ctr++;
216
      file_subcall_ctr++;
280
     }
217
     }
281
-    else
282
-    {
218
+    else {
283
      SERIAL_ECHO_START;
219
      SERIAL_ECHO_START;
284
      SERIAL_ECHOPGM("Now doing file: ");
220
      SERIAL_ECHOPGM("Now doing file: ");
285
      SERIAL_ECHOLN(name);
221
      SERIAL_ECHOLN(name);
286
     }
222
     }
287
     file.close();
223
     file.close();
288
   }
224
   }
289
-  else //opening fresh file
290
-  {
291
-    file_subcall_ctr=0; //resetting procedure depth in case user cancels print while in procedure
225
+  else { //opening fresh file
226
+    file_subcall_ctr = 0; //resetting procedure depth in case user cancels print while in procedure
292
     SERIAL_ECHO_START;
227
     SERIAL_ECHO_START;
293
     SERIAL_ECHOPGM("Now fresh file: ");
228
     SERIAL_ECHOPGM("Now fresh file: ");
294
     SERIAL_ECHOLN(name);
229
     SERIAL_ECHOLN(name);
295
   }
230
   }
296
   sdprinting = false;
231
   sdprinting = false;
297
-  
298
- 
232
+
299
   SdFile myDir;
233
   SdFile myDir;
300
-  curDir=&root;
301
-  char *fname=name;
302
-  
303
-  char *dirname_start,*dirname_end;
304
-  if(name[0]=='/')
305
-  {
306
-    dirname_start=strchr(name,'/')+1;
307
-    while(dirname_start>0)
308
-    {
309
-      dirname_end=strchr(dirname_start,'/');
310
-      //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
311
-      //SERIAL_ECHO("end  :");SERIAL_ECHOLN((int)(dirname_end-name));
312
-      if(dirname_end>0 && dirname_end>dirname_start)
313
-      {
234
+  curDir = &root;
235
+  char *fname = name;
236
+
237
+  char *dirname_start, *dirname_end;
238
+  if (name[0] == '/') {
239
+    dirname_start = &name[1];
240
+    while(dirname_start > 0) {
241
+      dirname_end = strchr(dirname_start, '/');
242
+      //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start - name));
243
+      //SERIAL_ECHO("end  :");SERIAL_ECHOLN((int)(dirname_end - name));
244
+      if (dirname_end > 0 && dirname_end > dirname_start) {
314
         char subdirname[FILENAME_LENGTH];
245
         char subdirname[FILENAME_LENGTH];
315
-        strncpy(subdirname, dirname_start, dirname_end-dirname_start);
316
-        subdirname[dirname_end-dirname_start]=0;
246
+        strncpy(subdirname, dirname_start, dirname_end - dirname_start);
247
+        subdirname[dirname_end - dirname_start] = 0;
317
         SERIAL_ECHOLN(subdirname);
248
         SERIAL_ECHOLN(subdirname);
318
-        if(!myDir.open(curDir,subdirname,O_READ))
319
-        {
249
+        if (!myDir.open(curDir, subdirname, O_READ)) {
320
           SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
250
           SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
321
           SERIAL_PROTOCOL(subdirname);
251
           SERIAL_PROTOCOL(subdirname);
322
           SERIAL_PROTOCOLLNPGM(".");
252
           SERIAL_PROTOCOLLNPGM(".");
323
           return;
253
           return;
324
         }
254
         }
325
-        else
326
-        {
255
+        else {
327
           //SERIAL_ECHOLN("dive ok");
256
           //SERIAL_ECHOLN("dive ok");
328
         }
257
         }
329
-          
330
-        curDir=&myDir; 
331
-        dirname_start=dirname_end+1;
258
+
259
+        curDir = &myDir;
260
+        dirname_start = dirname_end + 1;
332
       }
261
       }
333
-      else // the reminder after all /fsa/fdsa/ is the filename
334
-      {
335
-        fname=dirname_start;
336
-        //SERIAL_ECHOLN("remaider");
262
+      else { // the remainder after all /fsa/fdsa/ is the filename
263
+        fname = dirname_start;
264
+        //SERIAL_ECHOLN("remainder");
337
         //SERIAL_ECHOLN(fname);
265
         //SERIAL_ECHOLN(fname);
338
         break;
266
         break;
339
       }
267
       }
340
-      
341
     }
268
     }
342
   }
269
   }
343
-  else //relative path
344
-  {
345
-    curDir=&workDir;
270
+  else { //relative path
271
+    curDir = &workDir;
346
   }
272
   }
347
-  if(read)
348
-  {
349
-    if (file.open(curDir, fname, O_READ)) 
350
-    {
273
+
274
+  if (read) {
275
+    if (file.open(curDir, fname, O_READ)) {
351
       filesize = file.fileSize();
276
       filesize = file.fileSize();
352
       SERIAL_PROTOCOLPGM(MSG_SD_FILE_OPENED);
277
       SERIAL_PROTOCOLPGM(MSG_SD_FILE_OPENED);
353
       SERIAL_PROTOCOL(fname);
278
       SERIAL_PROTOCOL(fname);
354
       SERIAL_PROTOCOLPGM(MSG_SD_SIZE);
279
       SERIAL_PROTOCOLPGM(MSG_SD_SIZE);
355
       SERIAL_PROTOCOLLN(filesize);
280
       SERIAL_PROTOCOLLN(filesize);
356
       sdpos = 0;
281
       sdpos = 0;
357
-      
282
+
358
       SERIAL_PROTOCOLLNPGM(MSG_SD_FILE_SELECTED);
283
       SERIAL_PROTOCOLLNPGM(MSG_SD_FILE_SELECTED);
359
       getfilename(0, fname);
284
       getfilename(0, fname);
360
       lcd_setstatus(longFilename[0] ? longFilename : fname);
285
       lcd_setstatus(longFilename[0] ? longFilename : fname);
361
     }
286
     }
362
-    else
363
-    {
287
+    else {
364
       SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
288
       SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
365
       SERIAL_PROTOCOL(fname);
289
       SERIAL_PROTOCOL(fname);
366
       SERIAL_PROTOCOLLNPGM(".");
290
       SERIAL_PROTOCOLLNPGM(".");
367
     }
291
     }
368
   }
292
   }
369
-  else 
370
-  { //write
371
-    if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC))
372
-    {
293
+  else { //write
294
+    if (!file.open(curDir, fname, O_CREAT | O_APPEND | O_WRITE | O_TRUNC)) {
373
       SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
295
       SERIAL_PROTOCOLPGM(MSG_SD_OPEN_FILE_FAIL);
374
       SERIAL_PROTOCOL(fname);
296
       SERIAL_PROTOCOL(fname);
375
       SERIAL_PROTOCOLLNPGM(".");
297
       SERIAL_PROTOCOLLNPGM(".");
376
     }
298
     }
377
-    else
378
-    {
299
+    else {
379
       saving = true;
300
       saving = true;
380
       SERIAL_PROTOCOLPGM(MSG_SD_WRITE_TO_FILE);
301
       SERIAL_PROTOCOLPGM(MSG_SD_WRITE_TO_FILE);
381
       SERIAL_PROTOCOLLN(name);
302
       SERIAL_PROTOCOLLN(name);
382
       lcd_setstatus(fname);
303
       lcd_setstatus(fname);
383
     }
304
     }
384
   }
305
   }
385
-  
386
 }
306
 }
387
 
307
 
388
-void CardReader::removeFile(char* name)
389
-{
390
-  if(!cardOK)
391
-    return;
308
+void CardReader::removeFile(char* name) {
309
+  if (!cardOK) return;
310
+
392
   file.close();
311
   file.close();
393
   sdprinting = false;
312
   sdprinting = false;
394
-  
395
-  
313
+
396
   SdFile myDir;
314
   SdFile myDir;
397
-  curDir=&root;
398
-  char *fname=name;
399
-  
400
-  char *dirname_start,*dirname_end;
401
-  if(name[0]=='/')
402
-  {
403
-    dirname_start=strchr(name,'/')+1;
404
-    while(dirname_start>0)
405
-    {
406
-      dirname_end=strchr(dirname_start,'/');
407
-      //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start-name));
408
-      //SERIAL_ECHO("end  :");SERIAL_ECHOLN((int)(dirname_end-name));
409
-      if(dirname_end>0 && dirname_end>dirname_start)
410
-      {
315
+  curDir = &root;
316
+  char *fname = name;
317
+
318
+  char *dirname_start, *dirname_end;
319
+  if (name[0] == '/') {
320
+    dirname_start = strchr(name, '/') + 1;
321
+    while (dirname_start > 0) {
322
+      dirname_end = strchr(dirname_start, '/');
323
+      //SERIAL_ECHO("start:");SERIAL_ECHOLN((int)(dirname_start - name));
324
+      //SERIAL_ECHO("end  :");SERIAL_ECHOLN((int)(dirname_end - name));
325
+      if (dirname_end > 0 && dirname_end > dirname_start) {
411
         char subdirname[FILENAME_LENGTH];
326
         char subdirname[FILENAME_LENGTH];
412
-        strncpy(subdirname, dirname_start, dirname_end-dirname_start);
413
-        subdirname[dirname_end-dirname_start]=0;
327
+        strncpy(subdirname, dirname_start, dirname_end - dirname_start);
328
+        subdirname[dirname_end - dirname_start] = 0;
414
         SERIAL_ECHOLN(subdirname);
329
         SERIAL_ECHOLN(subdirname);
415
-        if(!myDir.open(curDir,subdirname,O_READ))
416
-        {
330
+        if (!myDir.open(curDir, subdirname, O_READ)) {
417
           SERIAL_PROTOCOLPGM("open failed, File: ");
331
           SERIAL_PROTOCOLPGM("open failed, File: ");
418
           SERIAL_PROTOCOL(subdirname);
332
           SERIAL_PROTOCOL(subdirname);
419
           SERIAL_PROTOCOLLNPGM(".");
333
           SERIAL_PROTOCOLLNPGM(".");
420
           return;
334
           return;
421
         }
335
         }
422
-        else
423
-        {
336
+        else {
424
           //SERIAL_ECHOLN("dive ok");
337
           //SERIAL_ECHOLN("dive ok");
425
         }
338
         }
426
-          
427
-        curDir=&myDir; 
428
-        dirname_start=dirname_end+1;
339
+
340
+        curDir = &myDir;
341
+        dirname_start = dirname_end + 1;
429
       }
342
       }
430
-      else // the reminder after all /fsa/fdsa/ is the filename
431
-      {
432
-        fname=dirname_start;
433
-        //SERIAL_ECHOLN("remaider");
343
+      else { // the remainder after all /fsa/fdsa/ is the filename
344
+        fname = dirname_start;
345
+        //SERIAL_ECHOLN("remainder");
434
         //SERIAL_ECHOLN(fname);
346
         //SERIAL_ECHOLN(fname);
435
         break;
347
         break;
436
       }
348
       }
437
-      
438
     }
349
     }
439
   }
350
   }
440
-  else //relative path
441
-  {
442
-    curDir=&workDir;
351
+  else { // relative path
352
+    curDir = &workDir;
353
+  }
354
+
355
+  if (file.remove(curDir, fname)) {
356
+    SERIAL_PROTOCOLPGM("File deleted:");
357
+    SERIAL_PROTOCOLLN(fname);
358
+    sdpos = 0;
359
+  }
360
+  else {
361
+    SERIAL_PROTOCOLPGM("Deletion failed, File: ");
362
+    SERIAL_PROTOCOL(fname);
363
+    SERIAL_PROTOCOLLNPGM(".");
443
   }
364
   }
444
-    if (file.remove(curDir, fname)) 
445
-    {
446
-      SERIAL_PROTOCOLPGM("File deleted:");
447
-      SERIAL_PROTOCOLLN(fname);
448
-      sdpos = 0;
449
-    }
450
-    else
451
-    {
452
-      SERIAL_PROTOCOLPGM("Deletion failed, File: ");
453
-      SERIAL_PROTOCOL(fname);
454
-      SERIAL_PROTOCOLLNPGM(".");
455
-    }
456
-  
457
 }
365
 }
458
 
366
 
459
-void CardReader::getStatus()
460
-{
461
-  if(cardOK){
367
+void CardReader::getStatus() {
368
+  if (cardOK) {
462
     SERIAL_PROTOCOLPGM(MSG_SD_PRINTING_BYTE);
369
     SERIAL_PROTOCOLPGM(MSG_SD_PRINTING_BYTE);
463
     SERIAL_PROTOCOL(sdpos);
370
     SERIAL_PROTOCOL(sdpos);
464
     SERIAL_PROTOCOLPGM("/");
371
     SERIAL_PROTOCOLPGM("/");
465
     SERIAL_PROTOCOLLN(filesize);
372
     SERIAL_PROTOCOLLN(filesize);
466
   }
373
   }
467
-  else{
374
+  else {
468
     SERIAL_PROTOCOLLNPGM(MSG_SD_NOT_PRINTING);
375
     SERIAL_PROTOCOLLNPGM(MSG_SD_NOT_PRINTING);
469
   }
376
   }
470
 }
377
 }
471
-void CardReader::write_command(char *buf)
472
-{
378
+
379
+void CardReader::write_command(char *buf) {
473
   char* begin = buf;
380
   char* begin = buf;
474
   char* npos = 0;
381
   char* npos = 0;
475
   char* end = buf + strlen(buf) - 1;
382
   char* end = buf + strlen(buf) - 1;
476
 
383
 
477
   file.writeError = false;
384
   file.writeError = false;
478
-  if((npos = strchr(buf, 'N')) != NULL)
479
-  {
385
+  if ((npos = strchr(buf, 'N')) != NULL) {
480
     begin = strchr(npos, ' ') + 1;
386
     begin = strchr(npos, ' ') + 1;
481
     end = strchr(npos, '*') - 1;
387
     end = strchr(npos, '*') - 1;
482
   }
388
   }
484
   end[2] = '\n';
390
   end[2] = '\n';
485
   end[3] = '\0';
391
   end[3] = '\0';
486
   file.write(begin);
392
   file.write(begin);
487
-  if (file.writeError)
488
-  {
393
+  if (file.writeError) {
489
     SERIAL_ERROR_START;
394
     SERIAL_ERROR_START;
490
     SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
395
     SERIAL_ERRORLNPGM(MSG_SD_ERR_WRITE_TO_FILE);
491
   }
396
   }
492
 }
397
 }
493
 
398
 
399
+void CardReader::checkautostart(bool force) {
400
+  if (!force && (!autostart_stilltocheck || autostart_atmillis < millis()))
401
+    return;
494
 
402
 
495
-void CardReader::checkautostart(bool force)
496
-{
497
-  if(!force)
498
-  {
499
-    if(!autostart_stilltocheck)
500
-      return;
501
-    if(autostart_atmillis<millis())
502
-      return;
503
-  }
504
-  autostart_stilltocheck=false;
505
-  if(!cardOK)
506
-  {
403
+  autostart_stilltocheck = false;
404
+
405
+  if (!cardOK) {
507
     initsd();
406
     initsd();
508
-    if(!cardOK) //fail
509
-      return;
407
+    if (!cardOK) return; // fail
510
   }
408
   }
511
-  
409
+
512
   char autoname[30];
410
   char autoname[30];
513
   sprintf_P(autoname, PSTR("auto%i.g"), autostart_index);
411
   sprintf_P(autoname, PSTR("auto%i.g"), autostart_index);
514
-  for(int8_t i=0;i<(int8_t)strlen(autoname);i++)
515
-    autoname[i]=tolower(autoname[i]);
412
+  for (int8_t i = 0; i < (int8_t)strlen(autoname); i++) autoname[i] = tolower(autoname[i]);
413
+
516
   dir_t p;
414
   dir_t p;
517
 
415
 
518
   root.rewind();
416
   root.rewind();
519
-  
520
-  bool found=false;
521
-  while (root.readDir(p, NULL) > 0) 
522
-  {
523
-    for(int8_t i=0;i<(int8_t)strlen((char*)p.name);i++)
524
-    p.name[i]=tolower(p.name[i]);
525
-    //Serial.print((char*)p.name);
526
-    //Serial.print(" ");
527
-    //Serial.println(autoname);
528
-    if(p.name[9]!='~') //skip safety copies
529
-    if(strncmp((char*)p.name,autoname,5)==0)
530
-    {
531
-      char cmd[30];
532
 
417
 
418
+  bool found = false;
419
+  while (root.readDir(p, NULL) > 0) {
420
+    for (int8_t i = 0; i < (int8_t)strlen((char*)p.name); i++) p.name[i] = tolower(p.name[i]);
421
+    if (p.name[9] != '~' && strncmp((char*)p.name, autoname, 5) == 0) {
422
+      char cmd[30];
533
       sprintf_P(cmd, PSTR("M23 %s"), autoname);
423
       sprintf_P(cmd, PSTR("M23 %s"), autoname);
534
       enquecommand(cmd);
424
       enquecommand(cmd);
535
       enquecommands_P(PSTR("M24"));
425
       enquecommands_P(PSTR("M24"));
536
-      found=true;
426
+      found = true;
537
     }
427
     }
538
   }
428
   }
539
-  if(!found)
540
-    autostart_index=-1;
429
+  if (!found)
430
+    autostart_index = -1;
541
   else
431
   else
542
     autostart_index++;
432
     autostart_index++;
543
 }
433
 }
544
 
434
 
545
-void CardReader::closefile(bool store_location)
546
-{
435
+void CardReader::closefile(bool store_location) {
547
   file.sync();
436
   file.sync();
548
   file.close();
437
   file.close();
549
-  saving = false; 
550
-  logging = false;
551
-  
552
-  if(store_location)
553
-  {
438
+  saving = logging = false;
439
+
440
+  if (store_location) {
554
     //future: store printer state, filename and position for continuing a stopped print
441
     //future: store printer state, filename and position for continuing a stopped print
555
     // so one can unplug the printer and continue printing the next day.
442
     // so one can unplug the printer and continue printing the next day.
556
-    
557
   }
443
   }
558
-
559
-  
560
 }
444
 }
561
 
445
 
562
-void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/)
563
-{
564
-  curDir=&workDir;
565
-  lsAction=LS_GetFilename;
566
-  nrFiles=nr;
446
+/**
447
+ * Get the name of a file in the current directory by index
448
+ */
449
+void CardReader::getfilename(uint16_t nr, const char * const match/*=NULL*/) {
450
+  curDir = &workDir;
451
+  lsAction = LS_GetFilename;
452
+  nrFiles = nr;
567
   curDir->rewind();
453
   curDir->rewind();
568
-  lsDive("",*curDir,match);
569
-  
454
+  lsDive("", *curDir, match);
570
 }
455
 }
571
 
456
 
572
-uint16_t CardReader::getnrfilenames()
573
-{
574
-  curDir=&workDir;
575
-  lsAction=LS_Count;
576
-  nrFiles=0;
457
+uint16_t CardReader::getnrfilenames() {
458
+  curDir = &workDir;
459
+  lsAction = LS_Count;
460
+  nrFiles = 0;
577
   curDir->rewind();
461
   curDir->rewind();
578
-  lsDive("",*curDir);
462
+  lsDive("", *curDir);
579
   //SERIAL_ECHOLN(nrFiles);
463
   //SERIAL_ECHOLN(nrFiles);
580
   return nrFiles;
464
   return nrFiles;
581
 }
465
 }
582
 
466
 
583
-void CardReader::chdir(const char * relpath)
584
-{
467
+void CardReader::chdir(const char * relpath) {
585
   SdFile newfile;
468
   SdFile newfile;
586
-  SdFile *parent=&root;
587
-  
588
-  if(workDir.isOpen())
589
-    parent=&workDir;
590
-  
591
-  if(!newfile.open(*parent,relpath, O_READ))
592
-  {
593
-   SERIAL_ECHO_START;
594
-   SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
595
-   SERIAL_ECHOLN(relpath);
469
+  SdFile *parent = &root;
470
+
471
+  if (workDir.isOpen()) parent = &workDir;
472
+
473
+  if (!newfile.open(*parent, relpath, O_READ)) {
474
+    SERIAL_ECHO_START;
475
+    SERIAL_ECHOPGM(MSG_SD_CANT_ENTER_SUBDIR);
476
+    SERIAL_ECHOLN(relpath);
596
   }
477
   }
597
-  else
598
-  {
478
+  else {
599
     if (workDirDepth < MAX_DIR_DEPTH) {
479
     if (workDirDepth < MAX_DIR_DEPTH) {
600
-      for (int d = ++workDirDepth; d--;)
601
-        workDirParents[d+1] = workDirParents[d];
602
-      workDirParents[0]=*parent;
480
+      ++workDirDepth;
481
+      for (int d = workDirDepth; d--;) workDirParents[d + 1] = workDirParents[d];
482
+      workDirParents[0] = *parent;
603
     }
483
     }
604
-    workDir=newfile;
484
+    workDir = newfile;
605
   }
485
   }
606
 }
486
 }
607
 
487
 
608
-void CardReader::updir()
609
-{
610
-  if(workDirDepth > 0)
611
-  {
488
+void CardReader::updir() {
489
+  if (workDirDepth > 0) {
612
     --workDirDepth;
490
     --workDirDepth;
613
     workDir = workDirParents[0];
491
     workDir = workDirParents[0];
614
-    int d;
615
     for (int d = 0; d < workDirDepth; d++)
492
     for (int d = 0; d < workDirDepth; d++)
616
       workDirParents[d] = workDirParents[d+1];
493
       workDirParents[d] = workDirParents[d+1];
617
   }
494
   }
618
 }
495
 }
619
 
496
 
620
-
621
-void CardReader::printingHasFinished()
622
-{
623
-    st_synchronize();
624
-    if(file_subcall_ctr>0) //heading up to a parent file that called current as a procedure.
625
-    {
626
-      file.close();
627
-      file_subcall_ctr--;
628
-      openFile(filenames[file_subcall_ctr],true,true);
629
-      setIndex(filespos[file_subcall_ctr]);
630
-      startFileprint();
631
-    }
632
-    else
633
-    {
634
-      quickStop();
635
-      file.close();
636
-      sdprinting = false;
637
-      if(SD_FINISHED_STEPPERRELEASE)
638
-      {
639
-          //finishAndDisableSteppers();
640
-          enquecommands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
641
-      }
642
-      autotempShutdown();
497
+void CardReader::printingHasFinished() {
498
+  st_synchronize();
499
+  if (file_subcall_ctr > 0) { // Heading up to a parent file that called current as a procedure.
500
+    file.close();
501
+    file_subcall_ctr--;
502
+    openFile(filenames[file_subcall_ctr], true, true);
503
+    setIndex(filespos[file_subcall_ctr]);
504
+    startFileprint();
505
+  }
506
+  else {
507
+    quickStop();
508
+    file.close();
509
+    sdprinting = false;
510
+    if (SD_FINISHED_STEPPERRELEASE) {
511
+      //finishAndDisableSteppers();
512
+      enquecommands_P(PSTR(SD_FINISHED_RELEASECOMMAND));
643
     }
513
     }
514
+    autotempShutdown();
515
+  }
644
 }
516
 }
517
+
645
 #endif //SDSUPPORT
518
 #endif //SDSUPPORT

+ 30
- 34
Marlin/cardreader.h View File

3
 
3
 
4
 #ifdef SDSUPPORT
4
 #ifdef SDSUPPORT
5
 
5
 
6
-#define MAX_DIR_DEPTH 10
6
+#define MAX_DIR_DEPTH 10          // Maximum folder depth
7
 
7
 
8
 #include "SdFile.h"
8
 #include "SdFile.h"
9
-enum LsAction {LS_SerialPrint,LS_Count,LS_GetFilename};
10
-class CardReader
11
-{
9
+enum LsAction { LS_SerialPrint, LS_Count, LS_GetFilename };
10
+
11
+class CardReader {
12
 public:
12
 public:
13
   CardReader();
13
   CardReader();
14
-  
14
+
15
   void initsd();
15
   void initsd();
16
   void write_command(char *buf);
16
   void write_command(char *buf);
17
   //files auto[0-9].g on the sd card are performed in a row
17
   //files auto[0-9].g on the sd card are performed in a row
18
   //this is to delay autostart and hence the initialisaiton of the sd card to some seconds after the normal init, so the device is available quick after a reset
18
   //this is to delay autostart and hence the initialisaiton of the sd card to some seconds after the normal init, so the device is available quick after a reset
19
 
19
 
20
-  void checkautostart(bool x); 
20
+  void checkautostart(bool x);
21
   void openFile(char* name,bool read,bool replace_current=true);
21
   void openFile(char* name,bool read,bool replace_current=true);
22
   void openLogFile(char* name);
22
   void openLogFile(char* name);
23
   void removeFile(char* name);
23
   void removeFile(char* name);
30
 
30
 
31
   void getfilename(uint16_t nr, const char* const match=NULL);
31
   void getfilename(uint16_t nr, const char* const match=NULL);
32
   uint16_t getnrfilenames();
32
   uint16_t getnrfilenames();
33
-  
33
+
34
   void getAbsFilename(char *t);
34
   void getAbsFilename(char *t);
35
-  
36
 
35
 
37
   void ls();
36
   void ls();
38
   void chdir(const char * relpath);
37
   void chdir(const char * relpath);
41
 
40
 
42
 
41
 
43
   FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
42
   FORCE_INLINE bool isFileOpen() { return file.isOpen(); }
44
-  FORCE_INLINE bool eof() { return sdpos>=filesize ;};
45
-  FORCE_INLINE int16_t get() {  sdpos = file.curPosition();return (int16_t)file.read();};
46
-  FORCE_INLINE void setIndex(long index) {sdpos = index;file.seekSet(index);};
47
-  FORCE_INLINE uint8_t percentDone(){if(!isFileOpen()) return 0; if(filesize) return sdpos/((filesize+99)/100); else return 0;};
48
-  FORCE_INLINE char* getWorkDirName(){workDir.getFilename(filename);return filename;};
43
+  FORCE_INLINE bool eof() { return sdpos >= filesize; }
44
+  FORCE_INLINE int16_t get() { sdpos = file.curPosition(); return (int16_t)file.read(); }
45
+  FORCE_INLINE void setIndex(long index) { sdpos = index; file.seekSet(index); }
46
+  FORCE_INLINE uint8_t percentDone() { return (isFileOpen() && filesize) ? sdpos / ((filesize + 99) / 100) : 0; }
47
+  FORCE_INLINE char* getWorkDirName() { workDir.getFilename(filename); return filename; }
49
 
48
 
50
 public:
49
 public:
51
-  bool saving;
52
-  bool logging;
53
-  bool sdprinting;  
54
-  bool cardOK;
55
-  char filename[FILENAME_LENGTH];
56
-  char longFilename[LONG_FILENAME_LENGTH];
57
-  bool filenameIsDir;
50
+  bool saving, logging, sdprinting, cardOK, filenameIsDir;
51
+  char filename[FILENAME_LENGTH], longFilename[LONG_FILENAME_LENGTH];
58
   int autostart_index;
52
   int autostart_index;
59
 private:
53
 private:
60
-  SdFile root,*curDir,workDir,workDirParents[MAX_DIR_DEPTH];
54
+  SdFile root, *curDir, workDir, workDirParents[MAX_DIR_DEPTH];
61
   uint16_t workDirDepth;
55
   uint16_t workDirDepth;
62
   Sd2Card card;
56
   Sd2Card card;
63
   SdVolume volume;
57
   SdVolume volume;
64
   SdFile file;
58
   SdFile file;
65
   #define SD_PROCEDURE_DEPTH 1
59
   #define SD_PROCEDURE_DEPTH 1
66
-  #define MAXPATHNAMELENGTH (FILENAME_LENGTH*MAX_DIR_DEPTH+MAX_DIR_DEPTH+1)
60
+  #define MAXPATHNAMELENGTH (FILENAME_LENGTH*MAX_DIR_DEPTH + MAX_DIR_DEPTH + 1)
67
   uint8_t file_subcall_ctr;
61
   uint8_t file_subcall_ctr;
68
   uint32_t filespos[SD_PROCEDURE_DEPTH];
62
   uint32_t filespos[SD_PROCEDURE_DEPTH];
69
   char filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
63
   char filenames[SD_PROCEDURE_DEPTH][MAXPATHNAMELENGTH];
70
   uint32_t filesize;
64
   uint32_t filesize;
71
-  //int16_t n;
72
   unsigned long autostart_atmillis;
65
   unsigned long autostart_atmillis;
73
-  uint32_t sdpos ;
66
+  uint32_t sdpos;
74
 
67
 
75
   bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
68
   bool autostart_stilltocheck; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
76
-  
69
+
77
   LsAction lsAction; //stored for recursion.
70
   LsAction lsAction; //stored for recursion.
78
-  int16_t nrFiles; //counter for the files in the current directory and recycled as position counter for getting the nrFiles'th name in the directory.
71
+  uint16_t nrFiles; //counter for the files in the current directory and recycled as position counter for getting the nrFiles'th name in the directory.
79
   char* diveDirName;
72
   char* diveDirName;
80
   void lsDive(const char *prepend, SdFile parent, const char * const match=NULL);
73
   void lsDive(const char *prepend, SdFile parent, const char * const match=NULL);
81
 };
74
 };
75
+
82
 extern CardReader card;
76
 extern CardReader card;
77
+
83
 #define IS_SD_PRINTING (card.sdprinting)
78
 #define IS_SD_PRINTING (card.sdprinting)
84
 
79
 
85
 #if (SDCARDDETECT > -1)
80
 #if (SDCARDDETECT > -1)
86
-# ifdef SDCARDDETECTINVERTED 
87
-#  define IS_SD_INSERTED (READ(SDCARDDETECT)!=0)
88
-# else
89
-#  define IS_SD_INSERTED (READ(SDCARDDETECT)==0)
90
-# endif //SDCARDTETECTINVERTED
81
+  #ifdef SDCARDDETECTINVERTED
82
+    #define IS_SD_INSERTED (READ(SDCARDDETECT) != 0)
83
+  #else
84
+    #define IS_SD_INSERTED (READ(SDCARDDETECT) == 0)
85
+  #endif
91
 #else
86
 #else
92
-//If we don't have a card detect line, aways asume the card is inserted
93
-# define IS_SD_INSERTED true
87
+  //No card detect line? Assume the card is inserted.
88
+  #define IS_SD_INSERTED true
94
 #endif
89
 #endif
95
 
90
 
96
 #else
91
 #else
98
 #define IS_SD_PRINTING (false)
93
 #define IS_SD_PRINTING (false)
99
 
94
 
100
 #endif //SDSUPPORT
95
 #endif //SDSUPPORT
101
-#endif
96
+
97
+#endif //__CARDREADER_H

+ 38
- 39
Marlin/digipot_mcp4451.cpp View File

1
 #include "Configuration.h"
1
 #include "Configuration.h"
2
 
2
 
3
 #ifdef DIGIPOT_I2C
3
 #ifdef DIGIPOT_I2C
4
+
4
 #include "Stream.h"
5
 #include "Stream.h"
5
 #include "utility/twi.h"
6
 #include "utility/twi.h"
6
 #include "Wire.h"
7
 #include "Wire.h"
7
 
8
 
8
 // Settings for the I2C based DIGIPOT (MCP4451) on Azteeg X3 Pro
9
 // Settings for the I2C based DIGIPOT (MCP4451) on Azteeg X3 Pro
9
 #if MB(5DPRINT)
10
 #if MB(5DPRINT)
10
-#define DIGIPOT_I2C_FACTOR 117.96
11
-#define DIGIPOT_I2C_MAX_CURRENT 1.736
11
+  #define DIGIPOT_I2C_FACTOR 117.96
12
+  #define DIGIPOT_I2C_MAX_CURRENT 1.736
12
 #else
13
 #else
13
-#define DIGIPOT_I2C_FACTOR 106.7
14
-#define DIGIPOT_I2C_MAX_CURRENT 2.5
14
+  #define DIGIPOT_I2C_FACTOR 106.7
15
+  #define DIGIPOT_I2C_MAX_CURRENT 2.5
15
 #endif
16
 #endif
16
 
17
 
17
-static byte current_to_wiper( float current ){
18
-    return byte(ceil(float((DIGIPOT_I2C_FACTOR*current))));
18
+static byte current_to_wiper(float current) {
19
+  return byte(ceil(float((DIGIPOT_I2C_FACTOR*current))));
19
 }
20
 }
20
 
21
 
21
-static void i2c_send(byte addr, byte a, byte b)
22
-{
23
-	Wire.beginTransmission(addr);
24
-    Wire.write(a);
25
-    Wire.write(b);
26
-    Wire.endTransmission();
22
+static void i2c_send(byte addr, byte a, byte b) {
23
+  Wire.beginTransmission(addr);
24
+  Wire.write(a);
25
+  Wire.write(b);
26
+  Wire.endTransmission();
27
 }
27
 }
28
 
28
 
29
 // This is for the MCP4451 I2C based digipot
29
 // This is for the MCP4451 I2C based digipot
30
-void digipot_i2c_set_current( int channel, float current )
31
-{
32
-    current = min( (float) max( current, 0.0f ), DIGIPOT_I2C_MAX_CURRENT);
33
-    // these addresses are specific to Azteeg X3 Pro, can be set to others,
34
-    // In this case first digipot is at address A0=0, A1= 0, second one is at A0=0, A1= 1
35
-    byte addr= 0x2C; // channel 0-3
36
-    if(channel >= 4) {
37
-    	addr= 0x2E; // channel 4-7
38
-    	channel-= 4;
39
-    }
40
-
41
-    // Initial setup
42
-    i2c_send( addr, 0x40, 0xff );
43
-    i2c_send( addr, 0xA0, 0xff );
44
-
45
-    // Set actual wiper value
46
-    byte addresses[4] = { 0x00, 0x10, 0x60, 0x70 };
47
-    i2c_send( addr, addresses[channel], current_to_wiper(current) );
30
+void digipot_i2c_set_current(int channel, float current) {
31
+  current = min( (float) max( current, 0.0f ), DIGIPOT_I2C_MAX_CURRENT);
32
+  // these addresses are specific to Azteeg X3 Pro, can be set to others,
33
+  // In this case first digipot is at address A0=0, A1= 0, second one is at A0=0, A1= 1
34
+  byte addr = 0x2C; // channel 0-3
35
+  if (channel >= 4) {
36
+  	addr = 0x2E; // channel 4-7
37
+  	channel -= 4;
38
+  }
39
+
40
+  // Initial setup
41
+  i2c_send(addr, 0x40, 0xff);
42
+  i2c_send(addr, 0xA0, 0xff);
43
+
44
+  // Set actual wiper value
45
+  byte addresses[4] = { 0x00, 0x10, 0x60, 0x70 };
46
+  i2c_send(addr, addresses[channel], current_to_wiper(current));
48
 }
47
 }
49
 
48
 
50
-void digipot_i2c_init()
51
-{
52
-    const float digipot_motor_current[] = DIGIPOT_I2C_MOTOR_CURRENTS;
53
-    Wire.begin();
54
-    // setup initial currents as defined in Configuration_adv.h
55
-    for(int i=0;i<=sizeof(digipot_motor_current)/sizeof(float);i++) {
56
-        digipot_i2c_set_current(i, digipot_motor_current[i]);
57
-    }
49
+void digipot_i2c_init() {
50
+  const float digipot_motor_current[] = DIGIPOT_I2C_MOTOR_CURRENTS;
51
+  Wire.begin();
52
+  // setup initial currents as defined in Configuration_adv.h
53
+  for(int i = 0; i <= sizeof(digipot_motor_current) / sizeof(float); i++) {
54
+    digipot_i2c_set_current(i, digipot_motor_current[i]);
55
+  }
58
 }
56
 }
59
-#endif
57
+
58
+#endif //DIGIPOT_I2C

+ 7
- 11
Marlin/dogm_lcd_implementation.h View File

21
 **/
21
 **/
22
 
22
 
23
 #ifdef ULTIPANEL
23
 #ifdef ULTIPANEL
24
-#define BLEN_A 0
25
-#define BLEN_B 1
26
-#define BLEN_C 2
27
-#define EN_A (1<<BLEN_A)
28
-#define EN_B (1<<BLEN_B)
29
-#define EN_C (1<<BLEN_C)
30
-#define encrot0 0
31
-#define encrot1 2
32
-#define encrot2 3
33
-#define encrot3 1
34
-#define LCD_CLICKED (buttons&EN_C)
24
+  #define BLEN_A 0
25
+  #define BLEN_B 1
26
+  #define BLEN_C 2
27
+  #define EN_A (1<<BLEN_A)
28
+  #define EN_B (1<<BLEN_B)
29
+  #define EN_C (1<<BLEN_C)
30
+  #define LCD_CLICKED (buttons&EN_C)
35
 #endif
31
 #endif
36
 
32
 
37
 #include <U8glib.h>
33
 #include <U8glib.h>

+ 3
- 0
Marlin/fastio.h View File

83
 /// check if pin is an timer wrapper
83
 /// check if pin is an timer wrapper
84
 #define GET_TIMER(IO)  _GET_TIMER(IO)
84
 #define GET_TIMER(IO)  _GET_TIMER(IO)
85
 
85
 
86
+// Shorthand
87
+#define OUT_WRITE(IO, v) { SET_OUTPUT(IO); WRITE(IO, v); }
88
+
86
 /*
89
 /*
87
 	ports and functions
90
 	ports and functions
88
 
91
 

+ 1
- 0
Marlin/language.h View File

121
 #define MSG_UNKNOWN_COMMAND                 "Unknown command: \""
121
 #define MSG_UNKNOWN_COMMAND                 "Unknown command: \""
122
 #define MSG_ACTIVE_EXTRUDER                 "Active Extruder: "
122
 #define MSG_ACTIVE_EXTRUDER                 "Active Extruder: "
123
 #define MSG_INVALID_EXTRUDER                "Invalid extruder"
123
 #define MSG_INVALID_EXTRUDER                "Invalid extruder"
124
+#define MSG_INVALID_SOLENOID                "Invalid solenoid"
124
 #define MSG_X_MIN                           "x_min: "
125
 #define MSG_X_MIN                           "x_min: "
125
 #define MSG_X_MAX                           "x_max: "
126
 #define MSG_X_MAX                           "x_max: "
126
 #define MSG_Y_MIN                           "y_min: "
127
 #define MSG_Y_MIN                           "y_min: "

+ 0
- 6
Marlin/pins_CHEAPTRONIC.h View File

87
 
87
 
88
 // Cheaptronic v1.0 does not use this port
88
 // Cheaptronic v1.0 does not use this port
89
 #define SDCARDDETECT -1
89
 #define SDCARDDETECT -1
90
-
91
-// Encoder rotation values
92
-#define encrot0 0
93
-#define encrot1 2
94
-#define encrot2 3
95
-#define encrot3 1

+ 0
- 6
Marlin/pins_ELEFU_3.h View File

74
   #define BLEN_B           1
74
   #define BLEN_B           1
75
   #define BLEN_A           0
75
   #define BLEN_A           0
76
 
76
 
77
-  //encoder rotation values
78
-  #define encrot0          0
79
-  #define encrot1          2
80
-  #define encrot2          3
81
-  #define encrot3          1
82
-
83
 #endif // RA_CONTROL_PANEL
77
 #endif // RA_CONTROL_PANEL
84
 
78
 
85
 #ifdef RA_DISCO
79
 #ifdef RA_DISCO

+ 0
- 6
Marlin/pins_MEGATRONICS.h View File

83
 
83
 
84
   #define SDCARDDETECT -1   // Ramps does not use this port
84
   #define SDCARDDETECT -1   // Ramps does not use this port
85
 
85
 
86
-    //encoder rotation values
87
-  #define encrot0 0
88
-  #define encrot1 2
89
-  #define encrot2 3
90
-  #define encrot3 1
91
-
92
 #endif // ULTRA_LCD && NEWPANEL
86
 #endif // ULTRA_LCD && NEWPANEL

+ 0
- 6
Marlin/pins_MEGATRONICS_1.h View File

80
 #define BLEN_A 0
80
 #define BLEN_A 0
81
 
81
 
82
 #define SDCARDDETECT -1  // Megatronics does not use this port
82
 #define SDCARDDETECT -1  // Megatronics does not use this port
83
-
84
-// Encoder rotation values
85
-#define encrot0 0
86
-#define encrot1 2
87
-#define encrot2 3
88
-#define encrot3 1

+ 0
- 6
Marlin/pins_MEGATRONICS_2.h View File

95
 #define BLEN_A 0
95
 #define BLEN_A 0
96
 
96
 
97
 #define SDCARDDETECT -1  // Megatronics does not use this port
97
 #define SDCARDDETECT -1  // Megatronics does not use this port
98
-
99
-// Encoder rotation values
100
-#define encrot0 0
101
-#define encrot1 2
102
-#define encrot2 3
103
-#define encrot3 1

+ 0
- 6
Marlin/pins_MEGATRONICS_3.h View File

95
 #define BLEN_A 0
95
 #define BLEN_A 0
96
 
96
 
97
 #define SDCARDDETECT -1	// Megatronics does not use this port
97
 #define SDCARDDETECT -1	// Megatronics does not use this port
98
-
99
-// Encoder rotation values
100
-#define encrot0 0
101
-#define encrot1 2
102
-#define encrot2 3
103
-#define encrot3 1

+ 0
- 11
Marlin/pins_RAMBO.h View File

116
 
116
 
117
     #define SDCARDDETECT 81    // Ramps does not use this port
117
     #define SDCARDDETECT 81    // Ramps does not use this port
118
 
118
 
119
-    //encoder rotation values
120
-    #define encrot0 0
121
-    #define encrot1 2
122
-    #define encrot2 3
123
-    #define encrot3 1
124
   #else //!NEWPANEL - old style panel with shift register
119
   #else //!NEWPANEL - old style panel with shift register
125
     //arduino pin witch triggers an piezzo beeper
120
     //arduino pin witch triggers an piezzo beeper
126
     #define BEEPER 33    No Beeper added
121
     #define BEEPER 33    No Beeper added
138
     #define LCD_PINS_D6 27
133
     #define LCD_PINS_D6 27
139
     #define LCD_PINS_D7 29
134
     #define LCD_PINS_D7 29
140
 
135
 
141
-    //encoder rotation values
142
-    #define encrot0 0
143
-    #define encrot1 2
144
-    #define encrot2 3
145
-    #define encrot3 1
146
-
147
     //bits in the shift register that carry the buttons for:
136
     //bits in the shift register that carry the buttons for:
148
     // left up center down right red
137
     // left up center down right red
149
     #define BL_LE 7
138
     #define BL_LE 7

+ 82
- 97
Marlin/stepper.cpp View File

187
      SERIAL_ECHOPAIR(" Z:",(float)endstops_trigsteps[Z_AXIS]/axis_steps_per_unit[Z_AXIS]);
187
      SERIAL_ECHOPAIR(" Z:",(float)endstops_trigsteps[Z_AXIS]/axis_steps_per_unit[Z_AXIS]);
188
      LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "Z");
188
      LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "Z");
189
    }
189
    }
190
-   SERIAL_ECHOLN("");
190
+   SERIAL_EOL;
191
    endstop_x_hit=false;
191
    endstop_x_hit=false;
192
    endstop_y_hit=false;
192
    endstop_y_hit=false;
193
    endstop_z_hit=false;
193
    endstop_z_hit=false;
399
       count_direction[Y_AXIS]=1;
399
       count_direction[Y_AXIS]=1;
400
     }
400
     }
401
 
401
 
402
-    // Set direction en check limit switches
403
-    #ifndef COREXY
404
-    if ((out_bits & (1<<X_AXIS)) != 0)   // stepping along -X axis
405
-    #else
406
-    if ((out_bits & (1<<X_HEAD)) != 0)   //AlexBorro: Head direction in -X axis for CoreXY bots.
407
-    #endif
402
+    if(check_endstops) // check X and Y Endstops
408
     {
403
     {
409
-      CHECK_ENDSTOPS
410
-      {
411
-        #ifdef DUAL_X_CARRIAGE
412
-        // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
413
-        if ((current_block->active_extruder == 0 && X_HOME_DIR == -1) 
414
-            || (current_block->active_extruder != 0 && X2_HOME_DIR == -1))
415
-        #endif          
416
-        {
417
-          #if defined(X_MIN_PIN) && X_MIN_PIN > -1
418
-            bool x_min_endstop=(READ(X_MIN_PIN) != X_MIN_ENDSTOP_INVERTING);
419
-            if(x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0)) {
420
-              endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
421
-              endstop_x_hit=true;
422
-              step_events_completed = current_block->step_event_count;
404
+        #ifndef COREXY
405
+        if ((out_bits & (1<<X_AXIS)) != 0)   // stepping along -X axis (regular cartesians bot)
406
+        #else
407
+        if (!((current_block->steps_x == current_block->steps_y) && ((out_bits & (1<<X_AXIS))>>X_AXIS != (out_bits & (1<<Y_AXIS))>>Y_AXIS))) // AlexBorro: If DeltaX == -DeltaY, the movement is only in Y axis
408
+        if ((out_bits & (1<<X_HEAD)) != 0) //AlexBorro: Head direction in -X axis for CoreXY bots.
409
+        #endif
410
+        { // -direction
411
+            #ifdef DUAL_X_CARRIAGE
412
+            // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
413
+            if ((current_block->active_extruder == 0 && X_HOME_DIR == -1) || (current_block->active_extruder != 0 && X2_HOME_DIR == -1))
414
+            #endif          
415
+            {
416
+                #if defined(X_MIN_PIN) && X_MIN_PIN > -1
417
+                bool x_min_endstop=(READ(X_MIN_PIN) != X_MIN_ENDSTOP_INVERTING);
418
+                if(x_min_endstop && old_x_min_endstop && (current_block->steps_x > 0))
419
+                {
420
+                    endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
421
+                    endstop_x_hit=true;
422
+                    step_events_completed = current_block->step_event_count;
423
+                }
424
+                old_x_min_endstop = x_min_endstop;
425
+                #endif
423
             }
426
             }
424
-            old_x_min_endstop = x_min_endstop;
425
-          #endif
426
         }
427
         }
427
-      }
428
-    }
429
-    else 
430
-    { // +direction
431
-      CHECK_ENDSTOPS
432
-      {
433
-        #ifdef DUAL_X_CARRIAGE
434
-        // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
435
-        if ((current_block->active_extruder == 0 && X_HOME_DIR == 1) 
436
-            || (current_block->active_extruder != 0 && X2_HOME_DIR == 1))
437
-        #endif          
438
-        {
439
-          #if defined(X_MAX_PIN) && X_MAX_PIN > -1
440
-            bool x_max_endstop=(READ(X_MAX_PIN) != X_MAX_ENDSTOP_INVERTING);
441
-            if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0)){
442
-              endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
443
-              endstop_x_hit=true;
444
-              step_events_completed = current_block->step_event_count;
428
+        else 
429
+        { // +direction
430
+            #ifdef DUAL_X_CARRIAGE
431
+            // with 2 x-carriages, endstops are only checked in the homing direction for the active extruder
432
+            if ((current_block->active_extruder == 0 && X_HOME_DIR == 1) || (current_block->active_extruder != 0 && X2_HOME_DIR == 1))
433
+            #endif          
434
+            {
435
+                #if defined(X_MAX_PIN) && X_MAX_PIN > -1
436
+                bool x_max_endstop=(READ(X_MAX_PIN) != X_MAX_ENDSTOP_INVERTING);
437
+                if(x_max_endstop && old_x_max_endstop && (current_block->steps_x > 0))
438
+                {
439
+                    endstops_trigsteps[X_AXIS] = count_position[X_AXIS];
440
+                    endstop_x_hit=true;
441
+                    step_events_completed = current_block->step_event_count;
442
+                }
443
+                old_x_max_endstop = x_max_endstop;
444
+                #endif
445
             }
445
             }
446
-            old_x_max_endstop = x_max_endstop;
447
-          #endif
448
         }
446
         }
449
-      }
450
-    }
451
 
447
 
452
-    #ifndef COREXY
453
-    if ((out_bits & (1<<Y_AXIS)) != 0)   // -direction
454
-    #else
455
-    if ((out_bits & (1<<Y_HEAD)) != 0)  //AlexBorro: Head direction in -Y axis for CoreXY bots.
456
-    #endif
457
-    {
458
-      CHECK_ENDSTOPS
459
-      {
460
-        #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
461
-          bool y_min_endstop=(READ(Y_MIN_PIN) != Y_MIN_ENDSTOP_INVERTING);
462
-          if(y_min_endstop && old_y_min_endstop && (current_block->steps_y > 0)) {
463
-            endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
464
-            endstop_y_hit=true;
465
-            step_events_completed = current_block->step_event_count;
466
-          }
467
-          old_y_min_endstop = y_min_endstop;
468
-        #endif
469
-      }
470
-    }
471
-    else 
472
-    { // +direction
473
-      CHECK_ENDSTOPS
474
-      {
475
-        #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
476
-          bool y_max_endstop=(READ(Y_MAX_PIN) != Y_MAX_ENDSTOP_INVERTING);
477
-          if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0)){
478
-            endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
479
-            endstop_y_hit=true;
480
-            step_events_completed = current_block->step_event_count;
481
-          }
482
-          old_y_max_endstop = y_max_endstop;
448
+        #ifndef COREXY
449
+        if ((out_bits & (1<<Y_AXIS)) != 0)   // -direction
450
+        #else
451
+        if (!((current_block->steps_x == current_block->steps_y) && ((out_bits & (1<<X_AXIS))>>X_AXIS == (out_bits & (1<<Y_AXIS))>>Y_AXIS))) // AlexBorro: If DeltaX == DeltaY, the movement is only in X axis
452
+        if ((out_bits & (1<<Y_HEAD)) != 0)  //AlexBorro: Head direction in -Y axis for CoreXY bots.
483
         #endif
453
         #endif
484
-      }
454
+        { // -direction
455
+            #if defined(Y_MIN_PIN) && Y_MIN_PIN > -1
456
+            bool y_min_endstop=(READ(Y_MIN_PIN) != Y_MIN_ENDSTOP_INVERTING);
457
+            if(y_min_endstop && old_y_min_endstop && (current_block->steps_y > 0))
458
+            {
459
+                endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
460
+                endstop_y_hit=true;
461
+                step_events_completed = current_block->step_event_count;
462
+            }
463
+            old_y_min_endstop = y_min_endstop;
464
+            #endif
465
+        }
466
+        else 
467
+        { // +direction
468
+            #if defined(Y_MAX_PIN) && Y_MAX_PIN > -1
469
+            bool y_max_endstop=(READ(Y_MAX_PIN) != Y_MAX_ENDSTOP_INVERTING);
470
+            if(y_max_endstop && old_y_max_endstop && (current_block->steps_y > 0))
471
+            {
472
+                endstops_trigsteps[Y_AXIS] = count_position[Y_AXIS];
473
+                endstop_y_hit=true;
474
+                step_events_completed = current_block->step_event_count;
475
+            }
476
+            old_y_max_endstop = y_max_endstop;
477
+            #endif
478
+
479
+        }
485
     }
480
     }
486
 
481
 
487
     if ((out_bits & (1<<Z_AXIS)) != 0) {   // -direction
482
     if ((out_bits & (1<<Z_AXIS)) != 0) {   // -direction
964
 
959
 
965
   //Initialize Step Pins
960
   //Initialize Step Pins
966
   #if defined(X_STEP_PIN) && (X_STEP_PIN > -1)
961
   #if defined(X_STEP_PIN) && (X_STEP_PIN > -1)
967
-    SET_OUTPUT(X_STEP_PIN);
968
-    WRITE(X_STEP_PIN,INVERT_X_STEP_PIN);
962
+    OUT_WRITE(X_STEP_PIN,INVERT_X_STEP_PIN);
969
     disable_x();
963
     disable_x();
970
   #endif
964
   #endif
971
   #if defined(X2_STEP_PIN) && (X2_STEP_PIN > -1)
965
   #if defined(X2_STEP_PIN) && (X2_STEP_PIN > -1)
972
-    SET_OUTPUT(X2_STEP_PIN);
973
-    WRITE(X2_STEP_PIN,INVERT_X_STEP_PIN);
966
+    OUT_WRITE(X2_STEP_PIN,INVERT_X_STEP_PIN);
974
     disable_x();
967
     disable_x();
975
   #endif
968
   #endif
976
   #if defined(Y_STEP_PIN) && (Y_STEP_PIN > -1)
969
   #if defined(Y_STEP_PIN) && (Y_STEP_PIN > -1)
977
-    SET_OUTPUT(Y_STEP_PIN);
978
-    WRITE(Y_STEP_PIN,INVERT_Y_STEP_PIN);
970
+    OUT_WRITE(Y_STEP_PIN,INVERT_Y_STEP_PIN);
979
     #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_STEP_PIN) && (Y2_STEP_PIN > -1)
971
     #if defined(Y_DUAL_STEPPER_DRIVERS) && defined(Y2_STEP_PIN) && (Y2_STEP_PIN > -1)
980
-      SET_OUTPUT(Y2_STEP_PIN);
981
-      WRITE(Y2_STEP_PIN,INVERT_Y_STEP_PIN);
972
+      OUT_WRITE(Y2_STEP_PIN,INVERT_Y_STEP_PIN);
982
     #endif
973
     #endif
983
     disable_y();
974
     disable_y();
984
   #endif
975
   #endif
985
   #if defined(Z_STEP_PIN) && (Z_STEP_PIN > -1)
976
   #if defined(Z_STEP_PIN) && (Z_STEP_PIN > -1)
986
-    SET_OUTPUT(Z_STEP_PIN);
987
-    WRITE(Z_STEP_PIN,INVERT_Z_STEP_PIN);
977
+    OUT_WRITE(Z_STEP_PIN,INVERT_Z_STEP_PIN);
988
     #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_STEP_PIN) && (Z2_STEP_PIN > -1)
978
     #if defined(Z_DUAL_STEPPER_DRIVERS) && defined(Z2_STEP_PIN) && (Z2_STEP_PIN > -1)
989
-      SET_OUTPUT(Z2_STEP_PIN);
990
-      WRITE(Z2_STEP_PIN,INVERT_Z_STEP_PIN);
979
+      OUT_WRITE(Z2_STEP_PIN,INVERT_Z_STEP_PIN);
991
     #endif
980
     #endif
992
     disable_z();
981
     disable_z();
993
   #endif
982
   #endif
994
   #if defined(E0_STEP_PIN) && (E0_STEP_PIN > -1)
983
   #if defined(E0_STEP_PIN) && (E0_STEP_PIN > -1)
995
-    SET_OUTPUT(E0_STEP_PIN);
996
-    WRITE(E0_STEP_PIN,INVERT_E_STEP_PIN);
984
+    OUT_WRITE(E0_STEP_PIN,INVERT_E_STEP_PIN);
997
     disable_e0();
985
     disable_e0();
998
   #endif
986
   #endif
999
   #if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
987
   #if defined(E1_STEP_PIN) && (E1_STEP_PIN > -1)
1000
-    SET_OUTPUT(E1_STEP_PIN);
1001
-    WRITE(E1_STEP_PIN,INVERT_E_STEP_PIN);
988
+    OUT_WRITE(E1_STEP_PIN,INVERT_E_STEP_PIN);
1002
     disable_e1();
989
     disable_e1();
1003
   #endif
990
   #endif
1004
   #if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
991
   #if defined(E2_STEP_PIN) && (E2_STEP_PIN > -1)
1005
-    SET_OUTPUT(E2_STEP_PIN);
1006
-    WRITE(E2_STEP_PIN,INVERT_E_STEP_PIN);
992
+    OUT_WRITE(E2_STEP_PIN,INVERT_E_STEP_PIN);
1007
     disable_e2();
993
     disable_e2();
1008
   #endif
994
   #endif
1009
   #if defined(E3_STEP_PIN) && (E3_STEP_PIN > -1)
995
   #if defined(E3_STEP_PIN) && (E3_STEP_PIN > -1)
1010
-    SET_OUTPUT(E3_STEP_PIN);
1011
-    WRITE(E3_STEP_PIN,INVERT_E_STEP_PIN);
996
+    OUT_WRITE(E3_STEP_PIN,INVERT_E_STEP_PIN);
1012
     disable_e3();
997
     disable_e3();
1013
   #endif
998
   #endif
1014
 
999
 

+ 4
- 10
Marlin/temperature.cpp View File

901
   #ifdef HEATER_0_USES_MAX6675
901
   #ifdef HEATER_0_USES_MAX6675
902
 
902
 
903
     #ifndef SDSUPPORT
903
     #ifndef SDSUPPORT
904
-      SET_OUTPUT(SCK_PIN);
905
-      WRITE(SCK_PIN,0);
906
-    
907
-      SET_OUTPUT(MOSI_PIN);
908
-      WRITE(MOSI_PIN,1);
909
-    
910
-      SET_INPUT(MISO_PIN);
911
-      WRITE(MISO_PIN,1);
904
+      OUT_WRITE(SCK_PIN, LOW);
905
+      OUT_WRITE(MOSI_PIN, HIGH);
906
+      OUT_WRITE(MISO_PIN, HIGH);
912
     #else
907
     #else
913
       pinMode(SS_PIN, OUTPUT);
908
       pinMode(SS_PIN, OUTPUT);
914
       digitalWrite(SS_PIN, HIGH);
909
       digitalWrite(SS_PIN, HIGH);
915
     #endif
910
     #endif
916
     
911
     
917
-    SET_OUTPUT(MAX6675_SS);
918
-    WRITE(MAX6675_SS,1);
912
+    OUT_WRITE(MAX6675_SS,HIGH);
919
 
913
 
920
   #endif //HEATER_0_USES_MAX6675
914
   #endif //HEATER_0_USES_MAX6675
921
 
915
 

+ 11
- 0
Marlin/ultralcd.cpp View File

1394
 
1394
 
1395
 #ifdef ULTIPANEL
1395
 #ifdef ULTIPANEL
1396
 
1396
 
1397
+////////////////////////
1398
+// Setup Rotary Encoder Bit Values (for two pin encoders to indicate movement)
1399
+// These values are independent of which pins are used for EN_A and EN_B indications
1400
+// The rotary encoder part is also independent to the chipset used for the LCD
1401
+#if defined(EN_A) && defined(EN_B)
1402
+  #define encrot0 0
1403
+  #define encrot1 2
1404
+  #define encrot2 3
1405
+  #define encrot3 1
1406
+#endif 
1407
+
1397
 /* Warning: This function is called from interrupt context */
1408
 /* Warning: This function is called from interrupt context */
1398
 void lcd_buttons_update() {
1409
 void lcd_buttons_update() {
1399
   #ifdef NEWPANEL
1410
   #ifdef NEWPANEL

+ 17
- 32
Marlin/ultralcd_implementation_hitachi_HD44780.h View File

123
   #define LCD_CLICKED (buttons&(B_MI|B_ST))
123
   #define LCD_CLICKED (buttons&(B_MI|B_ST))
124
 #endif
124
 #endif
125
 
125
 
126
-////////////////////////
127
-// Setup Rotary Encoder Bit Values (for two pin encoders to indicate movement)
128
-// These values are independent of which pins are used for EN_A and EN_B indications
129
-// The rotary encoder part is also independent to the chipset used for the LCD
130
-#if defined(EN_A) && defined(EN_B)
131
-    #define encrot0 0
132
-    #define encrot1 2
133
-    #define encrot2 3
134
-    #define encrot3 1
135
-#endif 
136
-
137
 #endif //ULTIPANEL
126
 #endif //ULTIPANEL
138
 
127
 
139
 ////////////////////////////////////
128
 ////////////////////////////////////
832
 
821
 
833
 static void lcd_implementation_quick_feedback()
822
 static void lcd_implementation_quick_feedback()
834
 {
823
 {
835
-#ifdef LCD_USE_I2C_BUZZER
836
-	#if !defined(LCD_FEEDBACK_FREQUENCY_HZ) || !defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS)
837
-	  lcd_buzz(1000/6,100);
838
-	#else
839
-	  lcd_buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS,LCD_FEEDBACK_FREQUENCY_HZ);
840
-	#endif
841
-#elif defined(BEEPER) && BEEPER > -1
824
+  #ifdef LCD_USE_I2C_BUZZER
825
+    #if defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS) && defined(LCD_FEEDBACK_FREQUENCY_HZ)
826
+      lcd_buzz(LCD_FEEDBACK_FREQUENCY_DURATION_MS, LCD_FEEDBACK_FREQUENCY_HZ);
827
+    #else
828
+      lcd_buzz(1000/6, 100);
829
+    #endif
830
+  #elif defined(BEEPER) && BEEPER > -1
842
     SET_OUTPUT(BEEPER);
831
     SET_OUTPUT(BEEPER);
843
-	#if !defined(LCD_FEEDBACK_FREQUENCY_HZ) || !defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS)
844
-    for(int8_t i=0;i<10;i++)
845
-    {
846
-      WRITE(BEEPER,HIGH);
847
-      delayMicroseconds(100);
848
-      WRITE(BEEPER,LOW);
849
-      delayMicroseconds(100);
850
-    }
832
+    #if !defined(LCD_FEEDBACK_FREQUENCY_HZ) || !defined(LCD_FEEDBACK_FREQUENCY_DURATION_MS)
833
+      const unsigned int delay = 100;
834
+      uint8_t i = 10;
851
     #else
835
     #else
852
-    for(int8_t i=0;i<(LCD_FEEDBACK_FREQUENCY_DURATION_MS / (1000 / LCD_FEEDBACK_FREQUENCY_HZ));i++)
853
-    {
836
+      const unsigned int delay = 1000000 / LCD_FEEDBACK_FREQUENCY_HZ / 2;
837
+      int8_t i = LCD_FEEDBACK_FREQUENCY_DURATION_MS * LCD_FEEDBACK_FREQUENCY_HZ / 1000;
838
+    #endif
839
+    while (i--) {
854
       WRITE(BEEPER,HIGH);
840
       WRITE(BEEPER,HIGH);
855
-      delayMicroseconds(1000000 / LCD_FEEDBACK_FREQUENCY_HZ / 2);
841
+      delayMicroseconds(delay);
856
       WRITE(BEEPER,LOW);
842
       WRITE(BEEPER,LOW);
857
-      delayMicroseconds(1000000 / LCD_FEEDBACK_FREQUENCY_HZ / 2);
843
+      delayMicroseconds(delay);
858
     }
844
     }
859
-    #endif
860
-#endif
845
+  #endif
861
 }
846
 }
862
 
847
 
863
 #ifdef LCD_HAS_STATUS_INDICATORS
848
 #ifdef LCD_HAS_STATUS_INDICATORS

+ 3
- 6
Marlin/ultralcd_st7920_u8glib_rrd.h View File

47
   {
47
   {
48
     case U8G_DEV_MSG_INIT:
48
     case U8G_DEV_MSG_INIT:
49
       {
49
       {
50
-        SET_OUTPUT(ST7920_CS_PIN);
51
-        WRITE(ST7920_CS_PIN,0);
52
-        SET_OUTPUT(ST7920_DAT_PIN);
53
-        WRITE(ST7920_DAT_PIN,0);
54
-        SET_OUTPUT(ST7920_CLK_PIN);
55
-        WRITE(ST7920_CLK_PIN,1);
50
+        OUT_WRITE(ST7920_CS_PIN,LOW);
51
+        OUT_WRITE(ST7920_DAT_PIN,LOW);
52
+        OUT_WRITE(ST7920_CLK_PIN,HIGH);
56
 
53
 
57
         ST7920_CS();
54
         ST7920_CS();
58
         u8g_Delay(120);                 //initial delay for boot up
55
         u8g_Delay(120);                 //initial delay for boot up

Loading…
Cancel
Save