From a508d835dbee8a180d506c85076c7671a66791fa Mon Sep 17 00:00:00 2001
From: Chris Roadfeldt <chris@roadfeldt.com>
Date: Wed, 1 Apr 2015 02:14:55 -0500
Subject: [PATCH] Changed Z_PROBE_AND_ENDSTOP to Z_PROBE_ENDSTOP. Updated
 documentation in Configuration.h. Cleaned up and commented some code relating
 to Z_PROBE_ENDSTOP. Separated Z_MIN_ENDSTOP and Z_PROBE_ENDSTOP completely.

---
 Marlin/Configuration.h | 22 ++++++++++++----------
 Marlin/Marlin_main.cpp | 18 +++++++++++-------
 Marlin/SanityCheck.h   | 31 ++++++++++++++++---------------
 Marlin/pins_RAMPS_13.h |  2 +-
 Marlin/stepper.cpp     | 16 +++++++++-------
 5 files changed, 49 insertions(+), 40 deletions(-)

diff --git a/Marlin/Configuration.h b/Marlin/Configuration.h
index 2ca9fa231c5..3d6bd50c26c 100644
--- a/Marlin/Configuration.h
+++ b/Marlin/Configuration.h
@@ -335,7 +335,7 @@ const bool Z_PROBE_ENDSTOP_INVERTING = false; // set to true to invert the logic
 //#define DISABLE_MIN_ENDSTOPS
 // If you want to enable the Z Probe pin, but disable its use, uncomment the line below.
 // This only affects a Z Probe Endstop if you have separate Z min endstop as well and have
-// activated Z_PROBE_AND_ENDSTOP below. If you are using the Z Min endstop on your Z Probe,
+// activated Z_PROBE_ENDSTOP below. If you are using the Z Min endstop on your Z Probe,
 // this has no effect.
 //#define DISABLE_Z_PROBE_ENDSTOP
 
@@ -500,17 +500,19 @@ const bool Z_PROBE_ENDSTOP_INVERTING = false; // set to true to invert the logic
 
   #endif
 
-// Support for concurrent and seperate Z Probe and Z min endstop use.
-// Added by Chris Roadfeldt 3-28-2015
-// If you would like to use both a Z Probe and a Z min endstop at the same time, uncomment #define Z_PROBE_AND_ENDSTOP below
-// You will want to disable Z_SAFE_HOMING above as you will still use the Z min endstop for homing.
-// In order to use this, you must have a Z_PROBE_PIN defined in the pins.h file for your control board.
-// RAMPS 1.3/1.4 boards may be able to use the 5V, Ground and the D32 pin in the Aux 4 section of the RAMPS board for the signal.
+// Support for a dedicated Z PROBE endstop separate from the Z MIN endstop.
+// If you would like to use both a Z PROBE and a Z MIN endstop together or just a Z PROBE with a custom pin, uncomment #define Z_PROBE_ENDSTOP and read the instructions below.
+// If you want to still use the Z min endstop for homing, disable Z_SAFE_HOMING above. Eg; to park the head outside the bed area when homing with G28.
+// WARNING: The Z MIN endstop will need to set properly as it would without a Z PROBE to prevent head crashes and premature stopping during a print.
+// To use a separte Z PROBE endstop, you must have a Z_PROBE_PIN defined in the pins.h file for your control board.
+// If you are using a servo based Z PROBE, you will need to enable NUM_SERVOS, SERVO_ENDSTOPS and SERVO_ENDSTOPS_ANGLES in the R/C Servo below.
+// RAMPS 1.3/1.4 boards may be able to use the 5V, Ground and the D32 pin in the Aux 4 section of the RAMPS board. Use 5V for powered sensors, otherwise connect to ground and D32
+// for normally closed configuration and 5V and D32 for normally open configurations. Normally closed configuration is advised and assumed.
 // The D32 pin in Aux 4 on RAMPS maps to the Arduino D32 pin. Z_PROBE_PIN is setting the pin to use on the Arduino. Since the D32 pin on the RAMPS maps to D32 on Arduino, this works.
-// D32 is currently selected in the RAMPS 1.3/1.4 pin file. Update the pins.h file for your control board to make use of this. Not doing so nullifies Z_PROBE_AND_ENDSTOP
-// WARNING: Setting the wrong pin may have unexpected and disastrous outcomes. Use with caution and do your homework.
+// D32 is currently selected in the RAMPS 1.3/1.4 pin file. All other boards will need changes to the respective pins_XXXXX.h file.
+// WARNING: Setting the wrong pin may have unexpected and potentially disastrous outcomes. Use with caution and do your homework.
 
-//  #define Z_PROBE_AND_ENDSTOP
+//  #define Z_PROBE_ENDSTOP
 
 #endif // ENABLE_AUTO_BED_LEVELING
 
diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp
index d95ad890942..98a4df1a3f0 100644
--- a/Marlin/Marlin_main.cpp
+++ b/Marlin/Marlin_main.cpp
@@ -1240,7 +1240,7 @@ inline void sync_plan_position() {
 
       st_synchronize();
 
-    #if defined(Z_PROBE_AND_ENDSTOP)
+    #if defined(Z_PROBE_ENDSTOP)
       bool z_probe_endstop = (READ(Z_PROBE_PIN) != Z_PROBE_ENDSTOP_INVERTING);
       if (z_probe_endstop) {
     #else
@@ -1314,7 +1314,7 @@ inline void sync_plan_position() {
 
       st_synchronize();
 
-    #if defined(Z_PROBE_AND_ENDSTOP)
+    #if defined(Z_PROBE_ENDSTOP)
       bool z_probe_endstop = (READ(Z_PROBE_PIN) != Z_PROBE_ENDSTOP_INVERTING);
       if (z_probe_endstop) {
     #else
@@ -2805,13 +2805,17 @@ inline void gcode_M42() {
   } // code_seen('S')
 }
 
-// If Z_PROBE_AND_ENDSTOP is changed to completely break it's bonds from Z_MIN_ENDSTOP and become
-// it's own unique entity, then the following logic will need to be modified
-// so it only uses the Z_PROBE
 #if defined(ENABLE_AUTO_BED_LEVELING) && defined(Z_PROBE_REPEATABILITY_TEST)
 
-  #if (Z_MIN_PIN == -1) && (! defined (Z_PROBE_PIN) || Z_PROBE_PIN == -1)
-    #error "You must have a Z_MIN or Z_PROBE endstop in order to enable calculation of Z-Probe repeatability."
+  // This is redudant since the SanityCheck.h already checks for a valid Z_PROBE_PIN, but here for clarity.
+  #if defined (Z_PROBE_ENDSTOP)
+    #if (! defined (Z_PROBE_PIN) || Z_PROBE_PIN == -1)
+      #error "You must have a Z_PROBE_PIN defined in order to enable calculation of Z-Probe repeatability."
+    #endif
+  #else
+    #if (Z_MIN_PIN == -1) &&
+      #error "You must have a Z_MIN_PIN defined in order to enable calculation of Z-Probe repeatability."
+    #endif
   #endif
 
   /**
diff --git a/Marlin/SanityCheck.h b/Marlin/SanityCheck.h
index 4d4153a97fa..cac7c55e0c9 100644
--- a/Marlin/SanityCheck.h
+++ b/Marlin/SanityCheck.h
@@ -103,26 +103,27 @@
     #endif
 
     /**
-     * Require a Z Probe Pin if Z_PROBE_AND_ENDSTOP is enabled.
+     * Require a Z Probe Pin if Z_PROBE_ENDSTOP is enabled.
      */
-    #if defined(Z_PROBE_AND_ENDSTOP)
+    #if defined(Z_PROBE_ENDSTOP)
       #ifndef Z_PROBE_PIN
-        #error You must have a Z_PROBE_PIN defined in your pins_XXXX.h file if you enable Z_PROBE_AND_ENDSTOP
+        #error You must have a Z_PROBE_PIN defined in your pins_XXXX.h file if you enable Z_PROBE_ENDSTOP
       #endif
       #if Z_PROBE_PIN == -1
-        #error You must set Z_PROBE_PIN to a valid pin if you enable Z_PROBE_AND_ENDSTOP
+        #error You must set Z_PROBE_PIN to a valid pin if you enable Z_PROBE_ENDSTOP
       #endif
-      #ifndef NUM_SERVOS
-        #error You must have NUM_SERVOS defined and there must be at least 1 configured to use Z_PROBE_AND_ENDSTOP
-      #endif
-      #if defined(NUM_SERVOS) && NUM_SERVOS < 1
-        #error You must have at least 1 servo defined for NUM_SERVOS to use Z_PROBE_AND_ENDSTOP
-      #endif
-      #ifndef SERVO_ENDSTOPS
-        #error You must have SERVO_ENDSTOPS defined and have the Z index set to at least 0 or above to use Z_PROBE_AND_ENDSTOP
-      #endif
-      #ifndef SERVO_ENDSTOP_ANGLES
-        #error You must have SERVO_ENDSTOP_ANGLES defined for Z Extend and Retract to use Z_PROBE_AND_ENSTOP
+// Forcing Servo definitions can break some hall effect sensor setups. Leaving these here for further comment.
+//      #ifndef NUM_SERVOS
+//        #error You must have NUM_SERVOS defined and there must be at least 1 configured to use Z_PROBE_ENDSTOP
+//      #endif
+//      #if defined(NUM_SERVOS) && NUM_SERVOS < 1
+//        #error You must have at least 1 servo defined for NUM_SERVOS to use Z_PROBE_ENDSTOP
+//      #endif
+//      #ifndef SERVO_ENDSTOPS
+//        #error You must have SERVO_ENDSTOPS defined and have the Z index set to at least 0 or above to use Z_PROBE_ENDSTOP
+//      #endif
+//      #ifndef SERVO_ENDSTOP_ANGLES
+//        #error You must have SERVO_ENDSTOP_ANGLES defined for Z Extend and Retract to use Z_PROBE_AND_ENSTOP
       #endif
     #endif
     /**
diff --git a/Marlin/pins_RAMPS_13.h b/Marlin/pins_RAMPS_13.h
index 3ca12dd1568..11ecddeda2a 100644
--- a/Marlin/pins_RAMPS_13.h
+++ b/Marlin/pins_RAMPS_13.h
@@ -62,7 +62,7 @@
   #define FILWIDTH_PIN        5
 #endif
 
-#if defined(Z_PROBE_AND_ENDSTOP)
+#if defined(Z_PROBE_ENDSTOP)
   // Define a pin to use as the signal pin on Arduino for the Z_PROBE endstop.
  #define Z_PROBE_PIN 32
 #endif
diff --git a/Marlin/stepper.cpp b/Marlin/stepper.cpp
index 58029c8ccd6..0fb4d8a6726 100644
--- a/Marlin/stepper.cpp
+++ b/Marlin/stepper.cpp
@@ -76,7 +76,7 @@ volatile long endstops_stepsTotal, endstops_stepsDone;
 static volatile bool endstop_x_hit = false;
 static volatile bool endstop_y_hit = false;
 static volatile bool endstop_z_hit = false;
-static volatile bool endstop_z_probe_hit = false;
+static volatile bool endstop_z_probe_hit = false; // Leaving this in even if Z_PROBE_ENDSTOP isn't defined, keeps code below cleaner. #ifdef it and usage below to save space.
 
 #ifdef ABORT_ON_ENDSTOP_HIT_FEATURE_ENABLED
   bool abort_on_endstop_hit = false;
@@ -113,7 +113,7 @@ static volatile bool endstop_z_probe_hit = false;
   #endif
 #endif
 
-#ifdef Z_PROBE_AND_ENDSTOP
+#ifdef Z_PROBE_ENDSTOP // No need to check for valid pin, SanityCheck.h already does this.
 static bool old_z_probe_endstop = false;
 #endif
 
@@ -259,11 +259,11 @@ volatile signed char count_direction[NUM_AXIS] = { 1, 1, 1, 1 };
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() TIMSK1 &= ~BIT(OCIE1A)
 
 void endstops_hit_on_purpose() {
-  endstop_x_hit = endstop_y_hit = endstop_z_hit = endstop_z_probe_hit = false;
+  endstop_x_hit = endstop_y_hit = endstop_z_hit = endstop_z_probe_hit = false; // #ifdef endstop_z_probe_hit = to save space if needed.
 }
 
 void checkHitEndstops() {
-  if (endstop_x_hit || endstop_y_hit || endstop_z_hit || endstop_z_probe_hit) {
+  if (endstop_x_hit || endstop_y_hit || endstop_z_hit || endstop_z_probe_hit) { // #ifdef || endstop_z_probe_hit to save space if needed.
     SERIAL_ECHO_START;
     SERIAL_ECHOPGM(MSG_ENDSTOPS_HIT);
     if (endstop_x_hit) {
@@ -278,10 +278,12 @@ void checkHitEndstops() {
       SERIAL_ECHOPAIR(" Z:", (float)endstops_trigsteps[Z_AXIS] / axis_steps_per_unit[Z_AXIS]);
       LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "Z");
     }
+    #ifdef Z_PROBE_ENDSTOP
     if (endstop_z_probe_hit) {
     	SERIAL_ECHOPAIR(" Z_PROBE:", (float)endstops_trigsteps[Z_AXIS] / axis_steps_per_unit[Z_AXIS]);
     	LCD_MESSAGEPGM(MSG_ENDSTOPS_HIT "ZP");
     }
+    #endif
     SERIAL_EOL;
 
     endstops_hit_on_purpose();
@@ -550,7 +552,7 @@ ISR(TIMER1_COMPA_vect) {
           #endif
         #endif
 
-        #if defined(Z_PROBE_PIN) && Z_PROBE_PIN > -1
+        #ifdef Z_PROBE_ENDSTOP
           UPDATE_ENDSTOP(z, Z, probe, PROBE);
           z_probe_endstop=(READ(Z_PROBE_PIN) != Z_PROBE_ENDSTOP_INVERTING);
           if(z_probe_endstop && old_z_probe_endstop)
@@ -597,7 +599,7 @@ ISR(TIMER1_COMPA_vect) {
           #endif
         #endif
 
-        #if defined(Z_PROBE_PIN) && Z_PROBE_PIN > -1
+        #ifdef Z_PROBE_ENDSTOP
           UPDATE_ENDSTOP(z, Z, probe, PROBE);
           z_probe_endstop=(READ(Z_PROBE_PIN) != Z_PROBE_ENDSTOP_INVERTING);
           if(z_probe_endstop && old_z_probe_endstop)
@@ -972,7 +974,7 @@ void st_init() {
     #endif
   #endif  
   
-#if defined(Z_PROBE_PIN) && Z_PROBE_PIN >= 0
+#if (defined(Z_PROBE_PIN) && Z_PROBE_PIN >= 0) && defined(Z_PROBE_ENDSTOP) // Check for Z_PROBE_ENDSTOP so we don't pull a pin high unless it's to be used.
   SET_INPUT(Z_PROBE_PIN);
   #ifdef ENDSTOPPULLUP_ZPROBE
     WRITE(Z_PROBE_PIN,HIGH);