From f9dbd73652b79913032f4cfb4748563bb1ad7242 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Sat, 4 Apr 2015 05:25:53 -0700
Subject: [PATCH 1/3] Fix tr_target_temperature type - float!

---
 Marlin/temperature.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp
index 1e3c2b6638..07af7ee752 100644
--- a/Marlin/temperature.cpp
+++ b/Marlin/temperature.cpp
@@ -1007,7 +1007,7 @@ void setWatch() {
 
   void thermal_runaway_protection(TRState *state, unsigned long *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc) {
 
-    static int tr_target_temperature[EXTRUDERS+1];
+    static float tr_target_temperature[EXTRUDERS+1];
 
     /*
         SERIAL_ECHO_START;
@@ -1053,18 +1053,18 @@ void setWatch() {
         }
 
         // If the temperature is over the target (-hysteresis) restart the timer
-        if (temperature >= tr_target_temperature[heater_index] - hysteresis_degc) *timer = millis();
-
-        // If the timer goes too long without a reset, trigger shutdown
+        if (temperature >= tr_target_temperature[heater_index] - hysteresis_degc) {
+          *timer = millis();
+        } // If the timer goes too long without a reset, trigger shutdown
         else if (millis() > *timer + period_seconds * 1000UL) {
           SERIAL_ERROR_START;
           SERIAL_ERRORLNPGM(MSG_THERMAL_RUNAWAY_STOP);
           if (heater_id < 0) SERIAL_ERRORLNPGM("bed"); else SERIAL_ERRORLN(heater_id);
           LCD_ALERTMESSAGEPGM(MSG_THERMAL_RUNAWAY);
           thermal_runaway = true;
+          disable_heater();
+          disable_all_steppers();
           for (;;) {
-            disable_heater();
-            disable_all_steppers();
             manage_heater();
             lcd_update();
           }

From 60f8e0386fef5358a51bb5dc1a9b1bad261b74cd Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Sat, 4 Apr 2015 05:45:36 -0700
Subject: [PATCH 2/3] More thermal runaway states

---
 Marlin/temperature.cpp | 61 +++++++++++++++++++-----------------------
 1 file changed, 28 insertions(+), 33 deletions(-)

diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp
index 07af7ee752..1bdb7fdbee 100644
--- a/Marlin/temperature.cpp
+++ b/Marlin/temperature.cpp
@@ -76,15 +76,14 @@ unsigned char soft_pwm_bed;
 #define HAS_HEATER_THERMAL_PROTECTION (defined(THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0)
 #define HAS_BED_THERMAL_PROTECTION (defined(THERMAL_RUNAWAY_PROTECTION_BED_PERIOD) && THERMAL_RUNAWAY_PROTECTION_BED_PERIOD > 0 && TEMP_SENSOR_BED != 0)
 #if HAS_HEATER_THERMAL_PROTECTION || HAS_BED_THERMAL_PROTECTION
-  enum TRState { TRInactive, TRFirstHeating, TRStable };
-  static bool thermal_runaway = false;
+  enum TRState { TRReset, TRInactive, TRFirstHeating, TRStable, TRRunaway };
   void thermal_runaway_protection(TRState *state, unsigned long *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc);
   #if HAS_HEATER_THERMAL_PROTECTION
-    static TRState thermal_runaway_state_machine[4] = { TRInactive, TRInactive, TRInactive, TRInactive };
+    static TRState thermal_runaway_state_machine[4] = { TRReset, TRReset, TRReset, TRReset };
     static unsigned long thermal_runaway_timer[4]; // = {0,0,0,0};
   #endif
   #if HAS_BED_THERMAL_PROTECTION
-    static TRState thermal_runaway_bed_state_machine = TRInactive;
+    static TRState thermal_runaway_bed_state_machine = TRReset;
     static unsigned long thermal_runaway_bed_timer;
   #endif
 #endif
@@ -1007,7 +1006,7 @@ void setWatch() {
 
   void thermal_runaway_protection(TRState *state, unsigned long *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc) {
 
-    static float tr_target_temperature[EXTRUDERS+1];
+    static float tr_target_temperature[EXTRUDERS+1] = { 0.0 };
 
     /*
         SERIAL_ECHO_START;
@@ -1023,20 +1022,23 @@ void setWatch() {
         SERIAL_ECHOPGM(target_temperature);
         SERIAL_EOL;
     */
-    if (target_temperature == 0 || thermal_runaway) {
-      *state = TRInactive;
-      *timer = 0;
-      return;
-    }
+
+    // If the target temperature changes, restart
+    if (tr_target_temperature[heater_index] != target_temperature)
+      *state = TRReset;
 
     int heater_index = heater_id >= 0 ? heater_id : EXTRUDERS;
 
     switch (*state) {
+      case TRReset:
+        *timer = 0;
+        *state = TRInactive;
+        break;
       // Inactive state waits for a target temperature to be set
       case TRInactive:
         if (target_temperature > 0) {
-          *state = TRFirstHeating;
           tr_target_temperature[heater_index] = target_temperature;
+          *state = TRFirstHeating;
         }
         break;
       // When first heating, wait for the temperature to be reached then go to Stable state
@@ -1045,31 +1047,24 @@ void setWatch() {
         break;
       // While the temperature is stable watch for a bad temperature
       case TRStable:
-      {
-        // If the target temperature changes, restart
-        if (tr_target_temperature[heater_index] != target_temperature) {
-          *state = TRInactive;
-          break;
-        }
-
         // If the temperature is over the target (-hysteresis) restart the timer
-        if (temperature >= tr_target_temperature[heater_index] - hysteresis_degc) {
+        if (temperature >= tr_target_temperature[heater_index] - hysteresis_degc)
           *timer = millis();
-        } // If the timer goes too long without a reset, trigger shutdown
-        else if (millis() > *timer + period_seconds * 1000UL) {
-          SERIAL_ERROR_START;
-          SERIAL_ERRORLNPGM(MSG_THERMAL_RUNAWAY_STOP);
-          if (heater_id < 0) SERIAL_ERRORLNPGM("bed"); else SERIAL_ERRORLN(heater_id);
-          LCD_ALERTMESSAGEPGM(MSG_THERMAL_RUNAWAY);
-          thermal_runaway = true;
-          disable_heater();
-          disable_all_steppers();
-          for (;;) {
-            manage_heater();
-            lcd_update();
-          }
+          // If the timer goes too long without a reset, trigger shutdown
+        else if (millis() > *timer + period_seconds * 1000UL)
+          *state = TRRunaway;
+        break;
+      case TRRunaway:
+        SERIAL_ERROR_START;
+        SERIAL_ERRORLNPGM(MSG_THERMAL_RUNAWAY_STOP);
+        if (heater_id < 0) SERIAL_ERRORLNPGM("bed"); else SERIAL_ERRORLN(heater_id);
+        LCD_ALERTMESSAGEPGM(MSG_THERMAL_RUNAWAY);
+        disable_heater();
+        disable_all_steppers();
+        for (;;) {
+          manage_heater();
+          lcd_update();
         }
-      } break;
     }
   }
 

From b0f198c153774aed21807e8464429b5231933249 Mon Sep 17 00:00:00 2001
From: Scott Lahteine <sourcetree@thinkyhead.com>
Date: Sat, 4 Apr 2015 06:18:08 -0700
Subject: [PATCH 3/3] heater_index above its use

---
 Marlin/temperature.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp
index 1bdb7fdbee..28e1afb7ce 100644
--- a/Marlin/temperature.cpp
+++ b/Marlin/temperature.cpp
@@ -1023,12 +1023,12 @@ void setWatch() {
         SERIAL_EOL;
     */
 
+    int heater_index = heater_id >= 0 ? heater_id : EXTRUDERS;
+
     // If the target temperature changes, restart
     if (tr_target_temperature[heater_index] != target_temperature)
       *state = TRReset;
 
-    int heater_index = heater_id >= 0 ? heater_id : EXTRUDERS;
-
     switch (*state) {
       case TRReset:
         *timer = 0;