From 651cde8b98b9499796e96cad9e267dcd6bbb86d4 Mon Sep 17 00:00:00 2001
From: Bernhard <bkubicek@x201.(none)>
Date: Thu, 22 Dec 2011 09:20:34 +0100
Subject: [PATCH] untestest commit, so I don't loose code.. features runout
 prevention addons

---
 Marlin/Configuration.h |  2 +-
 Marlin/Marlin.pde      | 86 +++++++++++++++++++++++++++++++-----------
 Marlin/stepper.cpp     | 12 ++++--
 Marlin/temperature.cpp |  5 +++
 4 files changed, 80 insertions(+), 25 deletions(-)

diff --git a/Marlin/Configuration.h b/Marlin/Configuration.h
index dc8c799f5bd..16672d01bed 100644
--- a/Marlin/Configuration.h
+++ b/Marlin/Configuration.h
@@ -248,7 +248,7 @@ const bool Z_ENDSTOPS_INVERTING = true; // set to true to invert the logic of th
 #define SLOWDOWN
 
 //default stepper release if idle
-#define DEFAULT_STEPPER_DEACTIVE_TIME 60
+#define DEFAULT_STEPPER_DEACTIVE_TIME 1200
 #define DEFAULT_STEPPER_DEACTIVE_COMMAND "M84 X Y E"  //z stays  powered
 
 
diff --git a/Marlin/Marlin.pde b/Marlin/Marlin.pde
index 4d031ea7883..0e62eea254c 100644
--- a/Marlin/Marlin.pde
+++ b/Marlin/Marlin.pde
@@ -176,8 +176,10 @@ const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
 
 //Inactivity shutdown variables
 static unsigned long previous_millis_cmd = 0;
-static unsigned long max_inactive_time = 0;
-static unsigned long stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000;
+static unsigned long previous_millis_runoutprevent = 0;
+//static unsigned long previous_millis_beep = 0;
+static unsigned long max_inactive_time=0;
+static unsigned long stepper_inactive_time = 0;
 static unsigned long last_stepperdisabled_time=30*1000; //first release check after 30 seconds
 
 static unsigned long starttime=0;
@@ -243,6 +245,10 @@ void setup()
     fromsd[i] = false;
   }
   
+  max_inactive_time = DEFAULT_MAX_INACTIVE_TIME*1000;
+  stepper_inactive_time = DEFAULT_STEPPER_DEACTIVE_TIME*1000;
+
+  
   EEPROM_RetrieveSettings(); // loads data from EEPROM if available
 
   for(int8_t i=0; i < NUM_AXIS; i++)
@@ -415,6 +421,7 @@ FORCE_INLINE void get_command()
         SERIAL_ECHO_START;
         SERIAL_ECHOLN(time);
         LCD_MESSAGE(time);
+        st_synchronize();
         card.printingHasFinished();
         card.checkautostart(true);
         
@@ -1200,11 +1207,34 @@ void prepare_arc_move(char isclockwise) {
 
 void manage_inactivity(byte debug) 
 { 
-  if( (millis()-previous_millis_cmd) >  max_inactive_time ) 
+  unsigned long curtime=millis();
+  SERIAL_ECHO_START;
+  SERIAL_ECHO("MS");
+  SERIAL_ECHO(millis());
+  SERIAL_ECHO(" PREV_CMD");
+  SERIAL_ECHO(previous_millis_cmd);
+  SERIAL_ECHO(" maxinactive");
+  SERIAL_ECHOLN(max_inactive_time );
+  
+  if( (curtime-previous_millis_cmd) >  max_inactive_time ) 
     if(max_inactive_time) 
+    {
+      LCD_MESSAGEPGM("Bored Shutdown.");
+      SERIAL_ERROR_START;
+      SERIAL_ERRORLNPGM("Bored Shutdown.");
       kill(); 
+    }
+    
+  if( (curtime-previous_millis_cmd) >  DEFAULT_MAX_HOT_TIME*1000 ) 
+    if(DEFAULT_MAX_HOT_TIME && (degHotend0()>HOTTEMP) ) 
+    {
+      LCD_MESSAGEPGM("Hot too long.");
+      SERIAL_ECHO_START;
+      SERIAL_ECHOLNPGM("Hot too long");
+      disable_heater();
+    }
   if(stepper_inactive_time)  
-  if( (millis()-last_stepperdisabled_time) >  stepper_inactive_time ) 
+  if( (curtime-last_stepperdisabled_time) >  stepper_inactive_time ) 
   {
     if(previous_millis_cmd>last_stepperdisabled_time)
       last_stepperdisabled_time=previous_millis_cmd;
@@ -1216,23 +1246,37 @@ void manage_inactivity(byte debug)
     }
   }
   #ifdef EXTRUDER_RUNOUT_PREVENT
-    if( (millis()-previous_millis_cmd) >  EXTRUDER_RUNOUT_SECONDS*1000 ) 
-    if(degHotend(active_extruder)>EXTRUDER_RUNOUT_MINTEMP)
+    if(!blocks_queued())
+    if( (curtime-previous_millis_cmd) >  EXTRUDER_RUNOUT_SECONDS*1000 ) 
     {
-     bool oldstatus=READ(E_ENABLE_PIN);
-     enable_e();
-     float oldepos=current_position[E_AXIS];
-     float oldedes=destination[E_AXIS];
-     plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], 
-                      current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], 
-                      EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
-     current_position[E_AXIS]=oldepos;
-     destination[E_AXIS]=oldedes;
-     plan_set_e_position(oldepos);
-     previous_millis_cmd=millis();
-     //enquecommand(DEFAULT_STEPPER_DEACTIVE_COMMAND);
-     st_synchronize();
-     WRITE(E_ENABLE_PIN,oldstatus);
+      if(previous_millis_cmd>previous_millis_runoutprevent)
+      {
+       previous_millis_runoutprevent=previous_millis_cmd; 
+      }
+      
+      if(degHotend0()>EXTRUDER_RUNOUT_MINTEMP)
+      if((curtime-previous_millis_runoutprevent) >  EXTRUDER_RUNOUT_SECONDS*1000)
+      {
+        bool oldstatus=READ(E_ENABLE_PIN);
+        enable_e();
+        float oldepos=current_position[E_AXIS];
+        float oldedes=destination[E_AXIS];
+        plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], 
+                          current_position[E_AXIS]+EXTRUDER_RUNOUT_EXTRUDE*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], 
+                          EXTRUDER_RUNOUT_SPEED/60.*EXTRUDER_RUNOUT_ESTEPS/axis_steps_per_unit[E_AXIS], active_extruder);
+        current_position[E_AXIS]=oldepos;
+        destination[E_AXIS]=oldedes;
+        plan_set_e_position(oldepos);
+        
+        while(current_block!=NULL || blocks_queued()) 
+        {
+          manage_heater();
+          LCD_STATUS;
+        }
+        
+        previous_millis_runoutprevent=millis();
+        WRITE(E_ENABLE_PIN,oldstatus);
+      }
     }
   #endif
   check_axes_activity();
@@ -1247,7 +1291,7 @@ void kill()
   disable_z();
   disable_e();
   
-  if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);
+  if(PS_ON_PIN > -1) pinMode(PS_ON_PIN,INPUT);  //Turn powersupply off
   SERIAL_ERROR_START;
   SERIAL_ERRORLNPGM("Printer halted. kill() called !!");
   LCD_MESSAGEPGM("KILLED. ");
diff --git a/Marlin/stepper.cpp b/Marlin/stepper.cpp
index 51a9fe0719b..e9c505d3f85 100644
--- a/Marlin/stepper.cpp
+++ b/Marlin/stepper.cpp
@@ -706,15 +706,21 @@ void st_init()
   sei();
 }
 
-
+#include "util/delay.h"
 // Block until all buffered steps are executed
 void st_synchronize()
 {
-    while( blocks_queued()) {
+    while(current_block!=NULL || blocks_queued()) {
     manage_heater();
     manage_inactivity(1);
-    LCD_STATUS;
+     LCD_STATUS;
+    //_delay_ms(100);
+    ;
   }
+//   _delay_ms(250);_delay_ms(250);_delay_ms(250);_delay_ms(250);
+//   _delay_ms(250);_delay_ms(250);_delay_ms(250);_delay_ms(250);
+//   _delay_ms(250);_delay_ms(250);_delay_ms(250);_delay_ms(250);
+//   _delay_ms(250);_delay_ms(250);_delay_ms(250);_delay_ms(250);
 }
 
 void st_set_position(const long &x, const long &y, const long &z, const long &e)
diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp
index 785da6c9264..4290313c5bb 100644
--- a/Marlin/temperature.cpp
+++ b/Marlin/temperature.cpp
@@ -451,6 +451,11 @@ void setWatch()
 
 void disable_heater()
 {
+  for(int i=0;i<EXTRUDERS;i++)
+   setTargetHotend(0,i);
+  setTargetBed(0);
+  
+
   #if TEMP_0_PIN > -1
   target_raw[0]=0;
    #if HEATER_0_PIN > -1