From 7919a40d066116203dd2e703df9af7f3fe4d1bd4 Mon Sep 17 00:00:00 2001
From: Bernhard Kubicek <kubicek@gmx.at>
Date: Tue, 15 Nov 2011 20:54:40 +0100
Subject: [PATCH] watchdog,percent done,

---
 Marlin/Configuration.h | 11 ++++----
 Marlin/Marlin.h        |  2 +-
 Marlin/Marlin.pde      | 60 ++++++++++++++++++++++++------------------
 Marlin/cardreader.h    |  1 +
 Marlin/ultralcd.h      |  2 +-
 Marlin/ultralcd.pde    | 10 +++++++
 Marlin/watchdog.pde    |  2 ++
 7 files changed, 55 insertions(+), 33 deletions(-)

diff --git a/Marlin/Configuration.h b/Marlin/Configuration.h
index 5b15dd0a47..f0dbe6a6e7 100644
--- a/Marlin/Configuration.h
+++ b/Marlin/Configuration.h
@@ -220,11 +220,12 @@ const bool ENDSTOPS_INVERTING = true; // set to true to invert the logic of the
 
 // The watchdog waits for the watchperiod in milliseconds whenever an M104 or M109 increases the target temperature
 // this enables the watchdog interrupt.
-#define USE_WATCHDOG
-// you cannot reboot on a mega2560 due to a bug in he bootloader. Hence, you have to reset manually, and this is done hereby:
-#define RESET_MANUAL
-#define WATCHDOG_TIMEOUT 4  //seconds
-
+//#define USE_WATCHDOG
+#ifdef USE_WATCHDOG
+  // you cannot reboot on a mega2560 due to a bug in he bootloader. Hence, you have to reset manually, and this is done hereby:
+  #define RESET_MANUAL
+  #define WATCHDOG_TIMEOUT 4  //seconds
+#endif
 
 
 
diff --git a/Marlin/Marlin.h b/Marlin/Marlin.h
index 440a44a579..acfc3c2fa6 100644
--- a/Marlin/Marlin.h
+++ b/Marlin/Marlin.h
@@ -100,7 +100,7 @@ void prepare_move();
 void kill();
 
 void enquecommand(const char *cmd); //put an ascii command at the end of the current buffer.
-
+void prepare_arc_move(char isclockwise);
 
 #ifndef CRITICAL_SECTION_START
   #define CRITICAL_SECTION_START  unsigned char _sreg = SREG; cli();
diff --git a/Marlin/Marlin.pde b/Marlin/Marlin.pde
index 84f07b3e68..cacd728742 100644
--- a/Marlin/Marlin.pde
+++ b/Marlin/Marlin.pde
@@ -37,6 +37,7 @@
 #include "temperature.h"
 #include "motion_control.h"
 #include "cardreader.h"
+#include "watchdog.h"
 
 
 #define VERSION_STRING  "1.0.0 Alpha 1"
@@ -191,6 +192,36 @@ extern "C"{
 }
 
 
+
+inline void get_coordinates()
+{
+  for(int8_t i=0; i < NUM_AXIS; i++) {
+    if(code_seen(axis_codes[i])) destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
+    else destination[i] = current_position[i]; //Are these else lines really needed?
+  }
+  if(code_seen('F')) {
+    next_feedrate = code_value();
+    if(next_feedrate > 0.0) feedrate = next_feedrate;
+  }
+}
+
+inline void get_arc_coordinates()
+{
+   get_coordinates();
+   if(code_seen('I')) offset[0] = code_value();
+   if(code_seen('J')) offset[1] = code_value();
+}
+
+void prepare_move()
+{
+  plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60.0/100.0);
+  for(int8_t i=0; i < NUM_AXIS; i++) {
+    current_position[i] = destination[i];
+  }
+}
+
+
+
 //adds an command to the main command buffer
 //thats really done in a non-safe way.
 //needs overworking someday
@@ -234,6 +265,7 @@ void setup()
   plan_init();  // Initialize planner;
   st_init();    // Initialize stepper;
   tp_init();    // Initialize temperature loop
+  wd_init();
 }
 
 
@@ -656,7 +688,8 @@ inline void process_commands()
       break;
     case 105: // M105
       //SERIAL_ECHOLN(freeMemory());
-          
+       //test watchdog:
+       //delay(20000);
       #if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595)
         SERIAL_PROTOCOLPGM("ok T:");
         SERIAL_PROTOCOL( degHotend0()); 
@@ -975,32 +1008,7 @@ void ClearToSend()
   SERIAL_PROTOCOLLNPGM("ok"); 
 }
 
-inline void get_coordinates()
-{
-  for(int8_t i=0; i < NUM_AXIS; i++) {
-    if(code_seen(axis_codes[i])) destination[i] = (float)code_value() + (axis_relative_modes[i] || relative_mode)*current_position[i];
-    else destination[i] = current_position[i]; //Are these else lines really needed?
-  }
-  if(code_seen('F')) {
-    next_feedrate = code_value();
-    if(next_feedrate > 0.0) feedrate = next_feedrate;
-  }
-}
 
-inline void get_arc_coordinates()
-{
-   get_coordinates();
-   if(code_seen('I')) offset[0] = code_value();
-   if(code_seen('J')) offset[1] = code_value();
-}
-
-void prepare_move()
-{
-  plan_buffer_line(destination[X_AXIS], destination[Y_AXIS], destination[Z_AXIS], destination[E_AXIS], feedrate*feedmultiply/60.0/100.0);
-  for(int8_t i=0; i < NUM_AXIS; i++) {
-    current_position[i] = destination[i];
-  }
-}
 
 void prepare_arc_move(char isclockwise) {
   float r = hypot(offset[X_AXIS], offset[Y_AXIS]); // Compute arc radius for mc_arc
diff --git a/Marlin/cardreader.h b/Marlin/cardreader.h
index b3f514f61f..d75f93a523 100644
--- a/Marlin/cardreader.h
+++ b/Marlin/cardreader.h
@@ -33,6 +33,7 @@ public:
   inline bool eof() { return sdpos>=filesize ;};
   inline int16_t get() {  sdpos = file.curPosition();return (int16_t)file.read();};
   inline void setIndex(long index) {sdpos = index;file.seekSet(index);};
+  inline uint8_t percentDone(){if(!sdprinting) return 0; if(filesize) return sdpos*100/filesize; else return 0;};
 
 public:
   bool saving;
diff --git a/Marlin/ultralcd.h b/Marlin/ultralcd.h
index 4c725329df..c836757e1c 100644
--- a/Marlin/ultralcd.h
+++ b/Marlin/ultralcd.h
@@ -96,7 +96,7 @@
   #define BLOCK ;
 #endif 
   
-  
+void lcd_statuspgm(const char* message);
   
 #endif //ULTRALCD
 
diff --git a/Marlin/ultralcd.pde b/Marlin/ultralcd.pde
index d95167166d..1bf225641a 100644
--- a/Marlin/ultralcd.pde
+++ b/Marlin/ultralcd.pde
@@ -374,6 +374,16 @@ void MainMenu::showStatus()
     lcd.print(fillto(LCD_WIDTH,messagetext));
     messagetext[0]='\0';
   }
+  
+  static uint8_t oldpercent=101;
+  uint8_t percent=card.percentDone();
+  if(oldpercent!=percent)
+  {
+     lcd.setCursor(6,3);
+    lcd.print(oldpercent);
+    lcdprintPGM("done");
+  }
+  
 #else //smaller LCDS----------------------------------
   static int olddegHotEnd0=-1;
   static int oldtargetHotEnd0=-1;
diff --git a/Marlin/watchdog.pde b/Marlin/watchdog.pde
index 9cf710a0c7..6c883c9d40 100644
--- a/Marlin/watchdog.pde
+++ b/Marlin/watchdog.pde
@@ -42,10 +42,12 @@ ISR(WDT_vect)
  
     #ifdef RESET_MANUAL
       LCD_MESSAGEPGM("Please Reset!");
+      LCD_STATUS;
       SERIAL_ERROR_START;
       SERIAL_ERRORLNPGM("Something is wrong, please turn off the printer.");
     #else
       LCD_MESSAGEPGM("Timeout, resetting!");
+      LCD_STATUS;
     #endif 
     //disable watchdog, it will survife reboot.
     WDTCSR |= (1<<WDCE) | (1<<WDE);