Skip to content

Commit

Permalink
🔧 Refactor endstop state config (#25574)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead authored Mar 26, 2023
1 parent cb0a8f2 commit ea5d7e2
Show file tree
Hide file tree
Showing 15 changed files with 437 additions and 616 deletions.
43 changes: 23 additions & 20 deletions Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -1165,26 +1165,29 @@
//#define ENDSTOPPULLDOWN_ZMIN_PROBE
#endif

// Mechanical endstop with COM to ground and NC to Signal uses "false" here (most common setup).
#define X_MIN_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define Y_MIN_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define Z_MIN_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define I_MIN_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define J_MIN_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define K_MIN_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define U_MIN_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define V_MIN_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define W_MIN_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define X_MAX_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define Y_MAX_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define Z_MAX_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define I_MAX_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define J_MAX_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define K_MAX_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define U_MAX_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define V_MAX_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define W_MAX_ENDSTOP_INVERTING false // Set to true to invert the logic of the endstop.
#define Z_MIN_PROBE_ENDSTOP_INVERTING false // Set to true to invert the logic of the probe.
/**
* Endstop "Hit" State
* Set to the state (HIGH or LOW) that applies to each endstop.
*/
#define X_MIN_ENDSTOP_HIT_STATE HIGH
#define X_MAX_ENDSTOP_HIT_STATE HIGH
#define Y_MIN_ENDSTOP_HIT_STATE HIGH
#define Y_MAX_ENDSTOP_HIT_STATE HIGH
#define Z_MIN_ENDSTOP_HIT_STATE HIGH
#define Z_MAX_ENDSTOP_HIT_STATE HIGH
#define I_MIN_ENDSTOP_HIT_STATE HIGH
#define I_MAX_ENDSTOP_HIT_STATE HIGH
#define J_MIN_ENDSTOP_HIT_STATE HIGH
#define J_MAX_ENDSTOP_HIT_STATE HIGH
#define K_MIN_ENDSTOP_HIT_STATE HIGH
#define K_MAX_ENDSTOP_HIT_STATE HIGH
#define U_MIN_ENDSTOP_HIT_STATE HIGH
#define U_MAX_ENDSTOP_HIT_STATE HIGH
#define V_MIN_ENDSTOP_HIT_STATE HIGH
#define V_MAX_ENDSTOP_HIT_STATE HIGH
#define W_MIN_ENDSTOP_HIT_STATE HIGH
#define W_MAX_ENDSTOP_HIT_STATE HIGH
#define Z_MIN_PROBE_ENDSTOP_HIT_STATE HIGH

// Enable this feature if all enabled endstop pins are interrupt-capable.
// This will remove the need to poll the interrupt pins, saving many CPU cycles.
Expand Down
42 changes: 17 additions & 25 deletions Marlin/src/gcode/config/M43.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,17 @@ inline void servo_probe_test() {
bool deploy_state = false, stow_state;

#if ENABLED(Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN)

constexpr bool probe_hit_state = Z_MIN_ENDSTOP_HIT_STATE;
#define PROBE_TEST_PIN Z_MIN_PIN
constexpr bool probe_inverting = Z_MIN_ENDSTOP_INVERTING;

SERIAL_ECHOLNPGM(". Probe Z_MIN_PIN: ", PROBE_TEST_PIN);
SERIAL_ECHOPGM(". Z_MIN_ENDSTOP_INVERTING: ");

#define _PROBE_PREF "Z_MIN"
#else

constexpr bool probe_hit_state = Z_MIN_PROBE_ENDSTOP_HIT_STATE;
#define PROBE_TEST_PIN Z_MIN_PROBE_PIN
constexpr bool probe_inverting = Z_MIN_PROBE_ENDSTOP_INVERTING;

SERIAL_ECHOLNPGM(". Probe Z_MIN_PROBE_PIN: ", PROBE_TEST_PIN);
SERIAL_ECHOPGM( ". Z_MIN_PROBE_ENDSTOP_INVERTING: ");

#define _PROBE_PREF "Z_MIN_PROBE"
#endif

serialprint_truefalse(probe_inverting);
SERIAL_ECHOLNPGM(". Probe " _PROBE_PREF "_PIN: ", PROBE_TEST_PIN);
serial_ternary(probe_hit_state, F(". " _PROBE_PREF "_ENDSTOP_HIT_STATE: "), F("HIGH"), F("LOW"));
SERIAL_EOL();

SET_INPUT_PULLUP(PROBE_TEST_PIN);
Expand All @@ -173,11 +166,11 @@ inline void servo_probe_test() {
SERIAL_ECHOLNPGM(". Check for BLTOUCH");
bltouch._reset();
bltouch._stow();
if (probe_inverting == READ(PROBE_TEST_PIN)) {
if (READ(PROBE_TEST_PIN) != probe_hit_state) {
bltouch._set_SW_mode();
if (probe_inverting != READ(PROBE_TEST_PIN)) {
if (READ(PROBE_TEST_PIN) == probe_hit_state) {
bltouch._deploy();
if (probe_inverting == READ(PROBE_TEST_PIN)) {
if (READ(PROBE_TEST_PIN) != probe_hit_state) {
bltouch._stow();
SERIAL_ECHOLNPGM("= BLTouch Classic 1.2, 1.3, Smart 1.0, 2.0, 2.2, 3.0, 3.1 detected.");
// Check for a 3.1 by letting the user trigger it, later
Expand All @@ -195,31 +188,30 @@ inline void servo_probe_test() {
if (!blt) {
// DEPLOY and STOW 4 times and see if the signal follows
// Then it is a mechanical switch
uint8_t i = 0;
SERIAL_ECHOLNPGM(". Deploy & stow 4 times");
do {
LOOP_L_N(i, 4) {
servo[probe_index].move(servo_angles[Z_PROBE_SERVO_NR][0]); // Deploy
safe_delay(500);
deploy_state = READ(PROBE_TEST_PIN);
servo[probe_index].move(servo_angles[Z_PROBE_SERVO_NR][1]); // Stow
safe_delay(500);
stow_state = READ(PROBE_TEST_PIN);
} while (++i < 4);
}

if (probe_inverting != deploy_state) SERIAL_ECHOLNPGM("WARNING: INVERTING setting probably backwards.");
if (probe_hit_state == deploy_state) SERIAL_ECHOLNPGM("WARNING: " _PROBE_PREF "_ENDSTOP_HIT_STATE is probably wrong.");

if (deploy_state != stow_state) {
SERIAL_ECHOLNPGM("= Mechanical Switch detected");
if (deploy_state) {
SERIAL_ECHOLNPGM(" DEPLOYED state: HIGH (logic 1)",
" STOWED (triggered) state: LOW (logic 0)");
SERIAL_ECHOLNPGM(". DEPLOYED state: HIGH (logic 1)\n"
". STOWED (triggered) state: LOW (logic 0)");
}
else {
SERIAL_ECHOLNPGM(" DEPLOYED state: LOW (logic 0)",
" STOWED (triggered) state: HIGH (logic 1)");
SERIAL_ECHOLNPGM(". DEPLOYED state: LOW (logic 0)\n"
". STOWED (triggered) state: HIGH (logic 1)");
}
#if ENABLED(BLTOUCH)
SERIAL_ECHOLNPGM("FAIL: BLTOUCH enabled - Set up this device as a Servo Probe with INVERTING set to 'true'.");
SERIAL_ECHOLNPGM("FAIL: Can't enable BLTOUCH. Check your settings.");
#endif
return;
}
Expand Down
38 changes: 18 additions & 20 deletions Marlin/src/inc/Conditionals_LCD.h
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,8 @@
#undef AVOID_OBSTACLES
#undef ENDSTOPPULLUP_YMIN
#undef ENDSTOPPULLUP_YMAX
#undef Y_MIN_ENDSTOP_INVERTING
#undef Y_MAX_ENDSTOP_INVERTING
#undef Y_MIN_ENDSTOP_HIT_STATE
#undef Y_MAX_ENDSTOP_HIT_STATE
#undef Y2_DRIVER_TYPE
#undef Y_ENABLE_ON
#undef DISABLE_Y
Expand All @@ -821,8 +821,8 @@
#else
#undef ENDSTOPPULLUP_ZMIN
#undef ENDSTOPPULLUP_ZMAX
#undef Z_MIN_ENDSTOP_INVERTING
#undef Z_MAX_ENDSTOP_INVERTING
#undef Z_MIN_ENDSTOP_HIT_STATE
#undef Z_MAX_ENDSTOP_HIT_STATE
#undef Z2_DRIVER_TYPE
#undef Z3_DRIVER_TYPE
#undef Z4_DRIVER_TYPE
Expand All @@ -841,8 +841,8 @@
#if !HAS_I_AXIS
#undef ENDSTOPPULLUP_IMIN
#undef ENDSTOPPULLUP_IMAX
#undef I_MIN_ENDSTOP_INVERTING
#undef I_MAX_ENDSTOP_INVERTING
#undef I_MIN_ENDSTOP_HIT_STATE
#undef I_MAX_ENDSTOP_HIT_STATE
#undef I_ENABLE_ON
#undef DISABLE_I
#undef INVERT_I_DIR
Expand All @@ -857,8 +857,8 @@
#if !HAS_J_AXIS
#undef ENDSTOPPULLUP_JMIN
#undef ENDSTOPPULLUP_JMAX
#undef J_MIN_ENDSTOP_INVERTING
#undef J_MAX_ENDSTOP_INVERTING
#undef J_MIN_ENDSTOP_HIT_STATE
#undef J_MAX_ENDSTOP_HIT_STATE
#undef J_ENABLE_ON
#undef DISABLE_J
#undef INVERT_J_DIR
Expand All @@ -873,8 +873,8 @@
#if !HAS_K_AXIS
#undef ENDSTOPPULLUP_KMIN
#undef ENDSTOPPULLUP_KMAX
#undef K_MIN_ENDSTOP_INVERTING
#undef K_MAX_ENDSTOP_INVERTING
#undef K_MIN_ENDSTOP_HIT_STATE
#undef K_MAX_ENDSTOP_HIT_STATE
#undef K_ENABLE_ON
#undef DISABLE_K
#undef INVERT_K_DIR
Expand All @@ -889,8 +889,8 @@
#if !HAS_U_AXIS
#undef ENDSTOPPULLUP_UMIN
#undef ENDSTOPPULLUP_UMAX
#undef U_MIN_ENDSTOP_INVERTING
#undef U_MAX_ENDSTOP_INVERTING
#undef U_MIN_ENDSTOP_HIT_STATE
#undef U_MAX_ENDSTOP_HIT_STATE
#undef U_ENABLE_ON
#undef DISABLE_U
#undef INVERT_U_DIR
Expand All @@ -905,8 +905,8 @@
#if !HAS_V_AXIS
#undef ENDSTOPPULLUP_VMIN
#undef ENDSTOPPULLUP_VMAX
#undef V_MIN_ENDSTOP_INVERTING
#undef V_MAX_ENDSTOP_INVERTING
#undef V_MIN_ENDSTOP_HIT_STATE
#undef V_MAX_ENDSTOP_HIT_STATE
#undef V_ENABLE_ON
#undef DISABLE_V
#undef INVERT_V_DIR
Expand All @@ -921,8 +921,8 @@
#if !HAS_W_AXIS
#undef ENDSTOPPULLUP_WMIN
#undef ENDSTOPPULLUP_WMAX
#undef W_MIN_ENDSTOP_INVERTING
#undef W_MAX_ENDSTOP_INVERTING
#undef W_MIN_ENDSTOP_HIT_STATE
#undef W_MAX_ENDSTOP_HIT_STATE
#undef W_ENABLE_ON
#undef DISABLE_W
#undef INVERT_W_DIR
Expand Down Expand Up @@ -1081,10 +1081,8 @@
* The BLTouch Probe emulates a servo probe
* and uses "special" angles for its state.
*/
#if ENABLED(BLTOUCH)
#ifndef Z_PROBE_SERVO_NR
#define Z_PROBE_SERVO_NR 0
#endif
#if ENABLED(BLTOUCH) && !defined(Z_PROBE_SERVO_NR)
#define Z_PROBE_SERVO_NR 0
#endif

/**
Expand Down
Loading

0 comments on commit ea5d7e2

Please sign in to comment.