From 852a8d6685ec1137eb65e78fa748cae41fbd36b6 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <github@thinkyhead.com>
Date: Tue, 5 May 2020 03:31:03 -0500
Subject: [PATCH] Configurable kill pin state

Co-Authored-By: AbdullahGheith <abdullahgheith@users.noreply.github.com>
---
 Marlin/src/MarlinCore.cpp                   | 12 ++++++++----
 Marlin/src/MarlinCore.h                     |  7 +++++++
 Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp |  3 ++-
 3 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/Marlin/src/MarlinCore.cpp b/Marlin/src/MarlinCore.cpp
index 26a7fb0684..994d0f7ec8 100644
--- a/Marlin/src/MarlinCore.cpp
+++ b/Marlin/src/MarlinCore.cpp
@@ -245,7 +245,11 @@ millis_t max_inactive_time, // = 0
 
 void setup_killpin() {
   #if HAS_KILL
-    SET_INPUT_PULLUP(KILL_PIN);
+    #if KILL_PIN_STATE
+      SET_INPUT_PULLDOWN(KILL_PIN);
+    #else
+      SET_INPUT_PULLUP(KILL_PIN);
+    #endif
   #endif
 }
 
@@ -496,7 +500,7 @@ inline void manage_inactivity(const bool ignore_stepper_queue=false) {
     // -------------------------------------------------------------------------------
     static int killCount = 0;   // make the inactivity button a bit less responsive
     const int KILL_DELAY = 750;
-    if (!READ(KILL_PIN))
+    if (kill_state())
       killCount++;
     else if (killCount > 0)
       killCount--;
@@ -770,10 +774,10 @@ void minkill(const bool steppers_off/*=false*/) {
   #if HAS_KILL
 
     // Wait for kill to be released
-    while (!READ(KILL_PIN)) watchdog_refresh();
+    while (kill_state()) watchdog_refresh();
 
     // Wait for kill to be pressed
-    while (READ(KILL_PIN)) watchdog_refresh();
+    while (!kill_state()) watchdog_refresh();
 
     void (*resetFunc)() = 0;      // Declare resetFunc() at address 0
     resetFunc();                  // Jump to address 0
diff --git a/Marlin/src/MarlinCore.h b/Marlin/src/MarlinCore.h
index 7f6d4432db..1ed8974835 100644
--- a/Marlin/src/MarlinCore.h
+++ b/Marlin/src/MarlinCore.h
@@ -110,6 +110,13 @@ void protected_pin_err();
   inline void suicide() { OUT_WRITE(SUICIDE_PIN, SUICIDE_PIN_INVERTING); }
 #endif
 
+#if HAS_KILL
+  #ifndef KILL_PIN_STATE
+    #define KILL_PIN_STATE LOW
+  #endif
+  inline bool kill_state() { return READ(KILL_PIN) == KILL_PIN_STATE; }
+#endif
+
 #if ENABLED(G29_RETRY_AND_RECOVER)
   void event_probe_recover();
   void event_probe_failure();
diff --git a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp
index 7d991fcccd..8385c4bdf9 100644
--- a/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp
+++ b/Marlin/src/feature/bedlevel/ubl/ubl_G29.cpp
@@ -1683,8 +1683,9 @@
       SERIAL_EOL();
 
       #if HAS_KILL
-        SERIAL_ECHOLNPAIR("Kill pin on :", int(KILL_PIN), "  state:", READ(KILL_PIN));
+        SERIAL_ECHOLNPAIR("Kill pin on :", int(KILL_PIN), "  state:", int(kill_state()));
       #endif
+
       SERIAL_EOL();
       serial_delay(50);
 
-- 
GitLab