From de0448343f198369f018ed370d357364b9bd81f6 Mon Sep 17 00:00:00 2001
From: stohn <stohn@stohn.de>
Date: Tue, 21 Aug 2012 14:48:29 +0200
Subject: [PATCH] added KILL_PIN support / cleaned up manage_inactivity
 function definition

---
 Marlin/Marlin.h    |  2 +-
 Marlin/Marlin.pde  | 46 +++++++++++++++++++++++++++++-----------------
 Marlin/planner.cpp |  2 +-
 Marlin/stepper.cpp |  2 +-
 4 files changed, 32 insertions(+), 20 deletions(-)

diff --git a/Marlin/Marlin.h b/Marlin/Marlin.h
index 937fabb36f3..5b59885cd39 100644
--- a/Marlin/Marlin.h
+++ b/Marlin/Marlin.h
@@ -107,7 +107,7 @@ FORCE_INLINE void serialprintPGM(const char *str)
 void get_command();
 void process_commands();
 
-void manage_inactivity(byte debug);
+void manage_inactivity();
 
 #if X_ENABLE_PIN > -1
   #define  enable_x() WRITE(X_ENABLE_PIN, X_ENABLE_ON)
diff --git a/Marlin/Marlin.pde b/Marlin/Marlin.pde
index f48a41921dc..d46a5a62f0c 100644
--- a/Marlin/Marlin.pde
+++ b/Marlin/Marlin.pde
@@ -245,6 +245,14 @@ void enquecommand(const char *cmd)
   }
 }
 
+void setup_killpin()
+{
+  #if( KILL_PIN>-1 )
+    pinMode(KILL_PIN,INPUT);
+    WRITE(KILL_PIN,HIGH);
+  #endif
+}
+    
 void setup_photpin()
 {
   #ifdef PHOTOGRAPH_PIN
@@ -276,7 +284,8 @@ void suicide()
 }
 
 void setup()
-{ 
+{
+  setup_killpin(); 
   setup_powerhold();
   MYSERIAL.begin(BAUDRATE);
   SERIAL_PROTOCOLLNPGM("start");
@@ -365,7 +374,7 @@ void loop()
   }
   //check heater every n milliseconds
   manage_heater();
-  manage_inactivity(1);
+  manage_inactivity();
   checkHitEndstops();
   LCD_STATUS;
 }
@@ -653,8 +662,8 @@ void process_commands()
       previous_millis_cmd = millis();
       while(millis()  < codenum ){
         manage_heater();
-        manage_inactivity(1);
-		LCD_STATUS;
+        manage_inactivity();
+        LCD_STATUS;
       }
       break;
       #ifdef FWRETRACT  
@@ -816,21 +825,20 @@ void process_commands()
       
       st_synchronize();
       previous_millis_cmd = millis();
-	  if (codenum > 0)
-	  {
+      if (codenum > 0){
         codenum += millis();  // keep track of when we started waiting
         while(millis()  < codenum && !CLICKED){
           manage_heater();
-          manage_inactivity(1);
-		  LCD_STATUS;
-		}
+          manage_inactivity();
+          LCD_STATUS;
+        }
       }else{
-        while(!CLICKED) {
+        while(!CLICKED){
           manage_heater();
-          manage_inactivity(1);
-		  LCD_STATUS;
-		}
-	  }
+          manage_inactivity();
+          LCD_STATUS;
+        }
+      }
     }
     break;
 #endif
@@ -1064,7 +1072,7 @@ void process_commands()
             codenum = millis();
           }
           manage_heater();
-          manage_inactivity(1);
+          manage_inactivity();
           LCD_STATUS;
         #ifdef TEMP_RESIDENCY_TIME
             /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
@@ -1102,7 +1110,7 @@ void process_commands()
             codenum = millis(); 
           }
           manage_heater();
-          manage_inactivity(1);
+          manage_inactivity();
           LCD_STATUS;
         }
         LCD_MESSAGEPGM(MSG_BED_DONE);
@@ -1664,7 +1672,7 @@ void controllerFan()
 }
 #endif
 
-void manage_inactivity(byte debug) 
+void manage_inactivity() 
 { 
   if( (millis() - previous_millis_cmd) >  max_inactive_time ) 
     if(max_inactive_time) 
@@ -1682,6 +1690,10 @@ void manage_inactivity(byte debug)
       }
     }
   }
+  #if( KILL_PIN>-1 )
+    if( 0 == READ(KILL_PIN) )
+      kill();
+  #endif
   #ifdef CONTROLLERFAN_PIN
     controllerFan(); //Check if fan should be turned on to cool stepper drivers down
   #endif
diff --git a/Marlin/planner.cpp b/Marlin/planner.cpp
index 93bd8225acd..6c47e298d50 100644
--- a/Marlin/planner.cpp
+++ b/Marlin/planner.cpp
@@ -501,7 +501,7 @@ void plan_buffer_line(const float &x, const float &y, const float &z, const floa
   // Rest here until there is room in the buffer.
   while(block_buffer_tail == next_buffer_head) { 
     manage_heater(); 
-    manage_inactivity(1); 
+    manage_inactivity(); 
     LCD_STATUS;
   }
 
diff --git a/Marlin/stepper.cpp b/Marlin/stepper.cpp
index a222f154a84..d5c8164b73e 100644
--- a/Marlin/stepper.cpp
+++ b/Marlin/stepper.cpp
@@ -899,7 +899,7 @@ void st_synchronize()
 {
     while( blocks_queued()) {
     manage_heater();
-    manage_inactivity(1);
+    manage_inactivity();
     LCD_STATUS;
   }
 }