chiark / gitweb /
reformating and some minor bugs/things found on the way.
authorBernhard Kubicek <kubicek@gmx.at>
Sun, 6 Nov 2011 18:23:08 +0000 (19:23 +0100)
committerBernhard Kubicek <kubicek@gmx.at>
Sun, 6 Nov 2011 18:23:08 +0000 (19:23 +0100)
17 files changed:
Marlin/Configuration.h
Marlin/EEPROMwrite.h
Marlin/Marlin.h
Marlin/Marlin.pde
Marlin/motion_control.cpp
Marlin/pins.h
Marlin/planner.cpp
Marlin/planner.h
Marlin/speed_lookuptable.h
Marlin/stepper.cpp
Marlin/stepper.h
Marlin/temperature.cpp
Marlin/temperature.h
Marlin/ultralcd.h
Marlin/ultralcd.pde
Marlin/watchdog.h
Marlin/watchdog.pde

index 8ad0e2282920e2b1657d3d3a7c05ca5e4392411b..b437bfb01f62733646196b07fab40c86d39da3ad 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef CONFIGURATION_H
-#define CONFIGURATION_H
+#ifndef __CONFIGURATION_H
+#define __CONFIGURATION_H
 
 //#define DEBUG_STEPS
 
@@ -118,10 +118,7 @@ const int dropsegments=5; //everything with this number of steps  will be ignore
 #define NUM_AXIS 4 // The axis order in all axis related arrays is X, Y, Z, E
 //note: on bernhards ultimaker 200 200 12 are working well.
 #define HOMING_FEEDRATE {50*60, 50*60, 12*60, 0}  // set the homing speeds
-//the followint checks if an extrusion is existent in the move. if _not_, the speed of the move is set to the maximum speed. 
-//!!!!!!Use only if you know that your printer works at the maximum declared speeds.
-// works around the skeinforge cool-bug. There all moves are slowed to have a minimum layer time. However slow travel moves= ooze
-#define TRAVELING_AT_MAXSPEED  
+
 #define AXIS_RELATIVE_MODES {false, false, false, false}
 
 #define MAX_STEP_FREQUENCY 40000 // Max step frequency for Ultimaker (5000 pps / half step)
@@ -177,41 +174,50 @@ const int dropsegments=5; //everything with this number of steps  will be ignore
 //#define_HEATER_1_MAXTEMP 275
 //#define BED_MAXTEMP 150
 
-
-
-
-
-
-
+/// PID settings:
+// Uncomment the following line to enable PID support.
+  
 #define PIDTEMP
 #ifdef PIDTEMP
-  /// PID settings:
-  // Uncomment the following line to enable PID support.
-  //#define SMOOTHING
-  //#define SMOOTHFACTOR 5.0
-  //float current_raw_average=0;
-    #define K1 0.95 //smoothing of the PID
   //#define PID_DEBUG // Sends debug data to the serial port. 
   //#define PID_OPENLOOP 1 // Puts PID in open loop. M104 sets the output power in %
-  #define PID_MAX 255 // limits current to nozzle
-  #define PID_INTEGRAL_DRIVE_MAX 255
-  #define PID_dT 0.1
- //machine with red silicon: 1950:45 second ; with fan fully blowin 3000:47
+  
+  #define PID_MAX 255 // limits current to nozzle; 255=full current
+  #define PID_INTEGRAL_DRIVE_MAX 255  //limit for the integral term
+  #define K1 0.95 //smoothing factor withing the PID
+  #define PID_dT 0.1 //sampling period of the PID
+
+  //To develop some PID settings for your machine, you can initiall follow 
+  // the Ziegler-Nichols method.
+  // set Ki and Kd to zero. 
+  // heat with a defined Kp and see if the temperature stabilizes
+  // ideally you do this graphically with repg.
+  // the PID_CRITIAL_GAIN should be the Kp at which temperature oscillatins are not dampned out/decreas in amplitutde
+  // PID_SWING_AT_CRITIAL is the time for a full period of the oscillations at the critical Gain
+  // usually further manual tunine is necessary.
 
   #define PID_CRITIAL_GAIN 3000
   #define PID_SWING_AT_CRITIAL 45 //seconds
-  #define PIDIADD 5
-  /*
-  //PID according to Ziegler-Nichols method
-  float Kp = 0.6*PID_CRITIAL_GAIN; 
-  float Ki =PIDIADD+2*Kp/PID_SWING_AT_CRITIAL*PID_dT;  
-  float Kd = Kp*PID_SWING_AT_CRITIAL/8./PID_dT;  
-  */
-  //PI according to Ziegler-Nichols method
-  #define  DEFAULT_Kp (PID_CRITIAL_GAIN/2.2) 
-  #define  DEFAULT_Ki (1.2*Kp/PID_SWING_AT_CRITIAL*PID_dT)
-  #define  DEFAULT_Kd (0)
   
+  #define PID_PI    //no differentail term
+  //#define PID_PID //normal PID
+
+  #ifdef PID_PID
+    //PID according to Ziegler-Nichols method
+    #define  DEFAULT_Kp  (0.6*PID_CRITIAL_GAIN)
+    #define  DEFAULT_Ki (2*Kp/PID_SWING_AT_CRITIAL*PID_dT)  
+    #define  DEFAULT_Kd (PID_SWING_AT_CRITIAL/8./PID_dT)  
+  #endif
+  #ifdef PID_PI
+    //PI according to Ziegler-Nichols method
+    #define  DEFAULT_Kp (PID_CRITIAL_GAIN/2.2) 
+    #define  DEFAULT_Ki (1.2*Kp/PID_SWING_AT_CRITIAL*PID_dT)
+    #define  DEFAULT_Kd (0)
+  #endif
+  
+  // this adds an experimental additional term to the heatingpower, proportional to the extrusion speed.
+  // if Kc is choosen well, the additional required power due to increased melting should be compensated.
   #define PID_ADD_EXTRUSION_RATE  
   #ifdef PID_ADD_EXTRUSION_RATE
     #define  DEFAULT_Kc (5) //heatingpower=Kc*(e_speed)
@@ -228,22 +234,21 @@ const int dropsegments=5; //everything with this number of steps  will be ignore
 //#define ADVANCE
 
 #ifdef ADVANCE
-#define EXTRUDER_ADVANCE_K .3
+  #define EXTRUDER_ADVANCE_K .3
 
-#define D_FILAMENT 1.7
-#define STEPS_MM_E 65
-#define EXTRUTION_AREA (0.25 * D_FILAMENT * D_FILAMENT * 3.14159)
-#define STEPS_PER_CUBIC_MM_E (axis_steps_per_unit[E_AXIS]/ EXTRUTION_AREA)
+  #define D_FILAMENT 1.7
+  #define STEPS_MM_E 65
+  #define EXTRUTION_AREA (0.25 * D_FILAMENT * D_FILAMENT * 3.14159)
+  #define STEPS_PER_CUBIC_MM_E (axis_steps_per_unit[E_AXIS]/ EXTRUTION_AREA)
 
 #endif // ADVANCE
 
-// THE BLOCK_BUFFER_SIZE NEEDS TO BE A POWER OF 2, e.g. 8,16,32 
-#if defined SDSUPPORT
 // The number of linear motions that can be in the plan at any give time.  
+// THE BLOCK_BUFFER_SIZE NEEDS TO BE A POWER OF 2, i.g. 8,16,32 because shifts and ors are used to do the ringbuffering.
+#if defined SDSUPPORT
   #define BLOCK_BUFFER_SIZE 16   // SD,LCD,Buttons take more memory, block buffer needs to be smaller
 #else
   #define BLOCK_BUFFER_SIZE 16 // maximize block buffer
 #endif
 
-
-#endif
+#endif //__CONFIGURATION_H
index dba2d443e5e259bc6ffba58defea6f57ce4c8222..db9f2fde17fd16d522b1f4617e5b0eebcc910cba 100644 (file)
@@ -1,39 +1,42 @@
 #ifndef __EEPROMH
 #define __EEPROMH
+
+#include "Marlin.h"
 #include "planner.h"
 #include "temperature.h"
 #include <EEPROM.h>
-#include "Marlin.h"
-#include "streaming.h"
 
-//======================================================================================
 template <class T> int EEPROM_writeAnything(int &ee, const T& value)
 {
-    const byte* p = (const byte*)(const void*)&value;
-    int i;
-    for (i = 0; i < (int)sizeof(value); i++)
-         EEPROM.write(ee++, *p++);
-    return i;
+  const byte* p = (const byte*)(const void*)&value;
+  int i;
+  for (i = 0; i < (int)sizeof(value); i++)
+    EEPROM.write(ee++, *p++);
+  return i;
 }
-//======================================================================================
+
 template <class T> int EEPROM_readAnything(int &ee, T& value)
 {
-    byte* p = (byte*)(void*)&value;
-    int i;
-    for (i = 0; i < (int)sizeof(value); i++)
-         *p++ = EEPROM.read(ee++);
-    return i;
+  byte* p = (byte*)(void*)&value;
+  int i;
+  for (i = 0; i < (int)sizeof(value); i++)
+    *p++ = EEPROM.read(ee++);
+  return i;
 }
 //======================================================================================
 
 #define EEPROM_OFFSET 100
 
-#define EEPROM_VERSION "V04"  // IMPORTANT:  Whenever there are changes made to the variables stored in EEPROM
-                              // in the functions below, also increment the version number. This makes sure that
-                              // the default values are used whenever there is a change to the data, to prevent
-                              // wrong data being written to the variables.
-                              // ALSO:  always make sure the variables in the Store and retrieve sections are in the same order.
-void StoreSettings() {
+
+// IMPORTANT:  Whenever there are changes made to the variables stored in EEPROM
+// in the functions below, also increment the version number. This makes sure that
+// the default values are used whenever there is a change to the data, to prevent
+// wrong data being written to the variables.
+// ALSO:  always make sure the variables in the Store and retrieve sections are in the same order.
+#define EEPROM_VERSION "V04"  
+
+void StoreSettings() 
+{
   char ver[4]= "000";
   int i=EEPROM_OFFSET;
   EEPROM_writeAnything(i,ver); // invalidate data first 
@@ -48,52 +51,55 @@ void StoreSettings() {
   EEPROM_writeAnything(i,max_xy_jerk);
   EEPROM_writeAnything(i,max_z_jerk);
   #ifdef PIDTEMP
-  EEPROM_writeAnything(i,Kp);
-  EEPROM_writeAnything(i,Ki);
-  EEPROM_writeAnything(i,Kd);
-#else
-  EEPROM_writeAnything(i,3000);
-  EEPROM_writeAnything(i,0);
-  EEPROM_writeAnything(i,0);
-#endif
+    EEPROM_writeAnything(i,Kp);
+    EEPROM_writeAnything(i,Ki);
+    EEPROM_writeAnything(i,Kd);
+  #else
+    EEPROM_writeAnything(i,3000);
+    EEPROM_writeAnything(i,0);
+    EEPROM_writeAnything(i,0);
+  #endif
   char ver2[4]=EEPROM_VERSION;
   i=EEPROM_OFFSET;
   EEPROM_writeAnything(i,ver2); // validate data
-   SERIAL_ECHOLN("Settings Stored");
-
+  SERIAL_ECHOLN("Settings Stored");
 }
 
-void RetrieveSettings(bool def=false){  // if def=true, the default values will be used
+void RetrieveSettings(bool def=false)
+{  // if def=true, the default values will be used
   int i=EEPROM_OFFSET;
   char stored_ver[4];
   char ver[4]=EEPROM_VERSION;
   EEPROM_readAnything(i,stored_ver); //read stored version
-//  SERIAL_ECHOLN("Version: [" << ver << "] Stored version: [" << stored_ver << "]");
-  if ((!def)&&(strncmp(ver,stored_ver,3)==0)) {   // version number match
-      EEPROM_readAnything(i,axis_steps_per_unit);  
-      EEPROM_readAnything(i,max_feedrate);  
-      EEPROM_readAnything(i,max_acceleration_units_per_sq_second);
-      EEPROM_readAnything(i,acceleration);
-      EEPROM_readAnything(i,retract_acceleration);
-      EEPROM_readAnything(i,minimumfeedrate);
-      EEPROM_readAnything(i,mintravelfeedrate);
-      EEPROM_readAnything(i,minsegmenttime);
-      EEPROM_readAnything(i,max_xy_jerk);
-      EEPROM_readAnything(i,max_z_jerk);
-#ifndef PIDTEMP
+  //  SERIAL_ECHOLN("Version: [" << ver << "] Stored version: [" << stored_ver << "]");
+  if ((!def)&&(strncmp(ver,stored_ver,3)==0)) 
+  {   // version number match
+    EEPROM_readAnything(i,axis_steps_per_unit);  
+    EEPROM_readAnything(i,max_feedrate);  
+    EEPROM_readAnything(i,max_acceleration_units_per_sq_second);
+    EEPROM_readAnything(i,acceleration);
+    EEPROM_readAnything(i,retract_acceleration);
+    EEPROM_readAnything(i,minimumfeedrate);
+    EEPROM_readAnything(i,mintravelfeedrate);
+    EEPROM_readAnything(i,minsegmenttime);
+    EEPROM_readAnything(i,max_xy_jerk);
+    EEPROM_readAnything(i,max_z_jerk);
+    #ifndef PIDTEMP
       float Kp,Ki,Kd;
-#endif
-      EEPROM_readAnything(i,Kp);
-      EEPROM_readAnything(i,Ki);
-      EEPROM_readAnything(i,Kd);
+    #endif
+    EEPROM_readAnything(i,Kp);
+    EEPROM_readAnything(i,Ki);
+    EEPROM_readAnything(i,Kd);
 
-      SERIAL_ECHOLN("Stored settings retreived:");
+    SERIAL_ECHOLN("Stored settings retreived:");
   }
-  else {
+  else 
+  {
     float tmp1[]=DEFAULT_AXIS_STEPS_PER_UNIT;
     float tmp2[]=DEFAULT_MAX_FEEDRATE;
     long tmp3[]=DEFAULT_MAX_ACCELERATION;
-    for (int i=0;i<4;i++) {
+    for (short i=0;i<4;i++) 
+    {
       axis_steps_per_unit[i]=tmp1[i];  
       max_feedrate[i]=tmp2[i];  
       max_acceleration_units_per_sq_second[i]=tmp3[i];
@@ -117,11 +123,10 @@ void RetrieveSettings(bool def=false){  // if def=true, the default values will
   SERIAL_ECHOLN("   M204 S"  <<_FLOAT(acceleration,2) << " T" << _FLOAT(retract_acceleration,2));
   SERIAL_ECHOLN("Advanced variables: S=Min feedrate (mm/s), T=Min travel feedrate (mm/s), B=minimum segment time (ms), X=maximum xY jerk (mm/s),  Z=maximum Z jerk (mm/s)");
   SERIAL_ECHOLN("   M205 S"  <<_FLOAT(minimumfeedrate/60,2) << " T" << _FLOAT(mintravelfeedrate/60,2) << " B" << _FLOAT(minsegmenttime,2) << " X" << _FLOAT(max_xy_jerk/60,2) << " Z" << _FLOAT(max_z_jerk/60,2));
-#ifdef PIDTEMP
-  SERIAL_ECHOLN("PID settings:");
-  SERIAL_ECHOLN("   M301 P"  << _FLOAT(Kp,3) << " I" << _FLOAT(Ki,3) << " D" << _FLOAT(Kd,3));  
-#endif
-  
+  #ifdef PIDTEMP
+    SERIAL_ECHOLN("PID settings:");
+    SERIAL_ECHOLN("   M301 P"  << _FLOAT(Kp,3) << " I" << _FLOAT(Ki,3) << " D" << _FLOAT(Kd,3));  
+  #endif
 }  
 
 #endif
index 06f701d034c8e50196ff8febf291c82048d87652..21eadeeb5ecdc91b459f1cb447abe9833bdd1d59 100644 (file)
@@ -18,41 +18,39 @@ void process_commands();
 void manage_inactivity(byte debug);
 
 #if X_ENABLE_PIN > -1
-#define  enable_x() WRITE(X_ENABLE_PIN, X_ENABLE_ON)
-#define disable_x() WRITE(X_ENABLE_PIN,!X_ENABLE_ON)
+  #define  enable_x() WRITE(X_ENABLE_PIN, X_ENABLE_ON)
+  #define disable_x() WRITE(X_ENABLE_PIN,!X_ENABLE_ON)
 #else
-#define enable_x() ;
-#define disable_x() ;
+  #define enable_x() ;
+  #define disable_x() ;
 #endif
+
 #if Y_ENABLE_PIN > -1
-#define  enable_y() WRITE(Y_ENABLE_PIN, Y_ENABLE_ON)
-#define disable_y() WRITE(Y_ENABLE_PIN,!Y_ENABLE_ON)
+  #define  enable_y() WRITE(Y_ENABLE_PIN, Y_ENABLE_ON)
+  #define disable_y() WRITE(Y_ENABLE_PIN,!Y_ENABLE_ON)
 #else
-#define enable_y() ;
-#define disable_y() ;
+  #define enable_y() ;
+  #define disable_y() ;
 #endif
+
 #if Z_ENABLE_PIN > -1
-#define  enable_z() WRITE(Z_ENABLE_PIN, Z_ENABLE_ON)
-#define disable_z() WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON)
+  #define  enable_z() WRITE(Z_ENABLE_PIN, Z_ENABLE_ON)
+  #define disable_z() WRITE(Z_ENABLE_PIN,!Z_ENABLE_ON)
 #else
-#define enable_z() ;
-#define disable_z() ;
+  #define enable_z() ;
+  #define disable_z() ;
 #endif
 
 #if E_ENABLE_PIN > -1
-
-       #define  enable_e() WRITE(E_ENABLE_PIN, E_ENABLE_ON)
-       #define disable_e() WRITE(E_ENABLE_PIN,!E_ENABLE_ON)
-
+  #define  enable_e() WRITE(E_ENABLE_PIN, E_ENABLE_ON)
+  #define disable_e() WRITE(E_ENABLE_PIN,!E_ENABLE_ON)
 #else
-#define enable_e() ;
-#define disable_e() ;
+  #define enable_e() ;
+  #define disable_e() ;
 #endif
 
-#define X_AXIS 0
-#define Y_AXIS 1
-#define Z_AXIS 2
-#define E_AXIS 3
+enum AxisEnum {X_AXIS=0, Y_AXIS=1, Z_AXIS=2, E_AXIS=3};
+
 
 void FlushSerialRequestResend();
 void ClearToSend();
@@ -61,26 +59,15 @@ void get_coordinates();
 void prepare_move();
 void kill();
 
-//void check_axes_activity();
-//void plan_init();
-//void st_init();
-//void tp_init();
-//void plan_buffer_line(float x, float y, float z, float e, float feed_rate);
-//void plan_set_position(float x, float y, float z, float e);
-//void st_wake_up();
-//void st_synchronize();
 void enquecommand(const char *cmd); //put an ascii command at the end of the current buffer.
 
 
 #ifndef CRITICAL_SECTION_START
-#define CRITICAL_SECTION_START  unsigned char _sreg = SREG; cli();
-#define CRITICAL_SECTION_END    SREG = _sreg;
+  #define CRITICAL_SECTION_START  unsigned char _sreg = SREG; cli();
+  #define CRITICAL_SECTION_END    SREG = _sreg;
 #endif //CRITICAL_SECTION_START
 
 extern float homing_feedrate[];
 extern bool axis_relative_modes[];
 
-
-void kill();
-
 #endif
index b449bb069236c9c0b4b2d5aab506cdee049f5383..6c2c6c87a4b95353de0d441b5f7b09d257f7cbbf 100644 (file)
@@ -25,6 +25,7 @@
     http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  */
 
+#include <EEPROM.h>
 #include "EEPROMwrite.h"
 #include "fastio.h"
 #include "Configuration.h"
 #include "temperature.h"
 #include "motion_control.h"
 
-#ifdef SIMPLE_LCD
-  #include "Simplelcd.h"
-#endif
 
 char version_string[] = "1.0.0 Alpha 1";
 
 #ifdef SDSUPPORT
-#include "SdFat.h"
+  #include "SdFat.h"
 #endif //SDSUPPORT
 
 
@@ -109,12 +107,9 @@ char version_string[] = "1.0.0 Alpha 1";
 
 //Stepper Movement Variables
 
-char axis_codes[NUM_AXIS] = {
-  'X', 'Y', 'Z', 'E'};
-float destination[NUM_AXIS] = {
-  0.0, 0.0, 0.0, 0.0};
-float current_position[NUM_AXIS] = {
-  0.0, 0.0, 0.0, 0.0};
+const char axis_codes[NUM_AXIS] = {'X', 'Y', 'Z', 'E'};
+float destination[NUM_AXIS] = {  0.0, 0.0, 0.0, 0.0};
+float current_position[NUM_AXIS] = {  0.0, 0.0, 0.0, 0.0};
 float offset[3] = {0.0, 0.0, 0.0};
 bool home_all_axis = true;
 float feedrate = 1500.0, next_feedrate, saved_feedrate;
@@ -131,6 +126,7 @@ uint8_t fanpwm=0;
 volatile int feedmultiply=100; //100->1 200->2
 int saved_feedmultiply;
 volatile bool feedmultiplychanged=false;
+
 // comm variables
 #define MAX_CMD_SIZE 96
 #define BUFSIZE 4
@@ -146,13 +142,10 @@ boolean comment_mode = false;
 char *strchr_pointer; // just a pointer to find chars in the cmd string like X, Y, Z, E, etc
 extern float HeaterPower;
 
-#include "EEPROM.h"
-
 const int sensitive_pins[] = SENSITIVE_PINS; // Sensitive pin list for M42
 
 float tt = 0, bt = 0;
 
-
 //Inactivity shutdown variables
 unsigned long previous_millis_cmd = 0;
 unsigned long max_inactive_time = 0;
@@ -161,73 +154,81 @@ unsigned long stepper_inactive_time = 0;
 unsigned long starttime=0;
 unsigned long stoptime=0;
 #ifdef SDSUPPORT
-Sd2Card card;
-SdVolume volume;
-SdFile root;
-SdFile file;
-uint32_t filesize = 0;
-uint32_t sdpos = 0;
-bool sdmode = false;
-bool sdactive = false;
-bool savetosd = false;
-int16_t n;
-unsigned long autostart_atmillis=0;
-
-void initsd()
-{
-  sdactive = false;
-#if SDSS >- 1
-  if(root.isOpen())
-    root.close();
-  if (!card.init(SPI_FULL_SPEED,SDSS))
-  {
-    //if (!card.init(SPI_HALF_SPEED,SDSS))
-    SERIAL_ECHOLN("SD init fail");
-  }
-  else if (!volume.init(&card))
-  {
-    SERIAL_ERRORLN("volume.init failed");
-  }
-  else if (!root.openRoot(&volume)) 
+  Sd2Card card;
+  SdVolume volume;
+  SdFile root;
+  SdFile file;
+  uint32_t filesize = 0;
+  uint32_t sdpos = 0;
+  bool sdmode = false;
+  bool sdactive = false;
+  bool savetosd = false;
+  int16_t n;
+  unsigned long autostart_atmillis=0;
+  
+  bool autostart_stilltocheck=true; //the sd start is delayed, because otherwise the serial cannot answer fast enought to make contact with the hostsoftware.
+
+
+  void initsd()
   {
-    SERIAL_ERRORLN("openRoot failed");
+    sdactive = false;
+    #if SDSS >- 1
+      if(root.isOpen())
+       root.close();
+      if (!card.init(SPI_FULL_SPEED,SDSS))
+      {
+       //if (!card.init(SPI_HALF_SPEED,SDSS))
+       SERIAL_ECHOLN("SD init fail");
+      }
+      else if (!volume.init(&card))
+      {
+       SERIAL_ERRORLN("volume.init failed");
+      }
+      else if (!root.openRoot(&volume)) 
+      {
+       SERIAL_ERRORLN("openRoot failed");
+      }
+      else 
+      {
+       sdactive = true;
+       SERIAL_ECHOLN("SD card ok");
+      }
+    #endif //SDSS
   }
-  else 
+
+  void quickinitsd()
   {
-    sdactive = true;
-    SERIAL_ECHOLN("SD card ok");
+    sdactive=false;
+    autostart_atmillis=millis()+5000;
   }
-#endif //SDSS
-}
-
-void quickinitsd(){
-       sdactive=false;
-       autostart_atmillis=millis()+5000;
-}
 
-inline void write_command(char *buf){
-  char* begin = buf;
-  char* npos = 0;
-  char* end = buf + strlen(buf) - 1;
+  inline void write_command(char *buf)
+  {
+    char* begin = buf;
+    char* npos = 0;
+    char* end = buf + strlen(buf) - 1;
 
-  file.writeError = false;
-  if((npos = strchr(buf, 'N')) != NULL){
-    begin = strchr(npos, ' ') + 1;
-    end = strchr(npos, '*') - 1;
-  }
-  end[1] = '\r';
-  end[2] = '\n';
-  end[3] = '\0';
-  //Serial.println(begin);
-  file.write(begin);
-  if (file.writeError){
-    SERIAL_ERRORLN("error writing to file");
+    file.writeError = false;
+    if((npos = strchr(buf, 'N')) != NULL)
+    {
+      begin = strchr(npos, ' ') + 1;
+      end = strchr(npos, '*') - 1;
+    }
+    end[1] = '\r';
+    end[2] = '\n';
+    end[3] = '\0';
+    file.write(begin);
+    if (file.writeError)
+    {
+      SERIAL_ERRORLN("error writing to file");
+    }
   }
-}
 #endif //SDSUPPORT
 
 
-///adds an command to the main command buffer
+//adds an command to the main command buffer
+//thats really done in a non-safe way.
+//needs overworking someday
 void enquecommand(const char *cmd)
 {
   if(buflen < BUFSIZE)
@@ -242,106 +243,93 @@ void enquecommand(const char *cmd)
 
 void setup()
 { 
-       
   Serial.begin(BAUDRATE);
   SERIAL_ECHOLN("Marlin "<<version_string);
   Serial.println("start");
-#if defined FANCY_LCD || defined SIMPLE_LCD
-  lcd_init();
-#endif
-  for(int i = 0; i < BUFSIZE; i++){
+  for(int i = 0; i < BUFSIZE; i++)
+  {
     fromsd[i] = false;
   }
   
   RetrieveSettings(); // loads data from EEPROM if available
 
-
-  for(int i=0; i < NUM_AXIS; i++){
+  for(int i=0; i < NUM_AXIS; i++)
+  {
     axis_steps_per_sqr_second[i] = max_acceleration_units_per_sq_second[i] * axis_steps_per_unit[i];
   }
 
-#ifdef SDSUPPORT
-  //power to SD reader
-#if SDPOWER > -1
-  SET_OUTPUT(SDPOWER); 
-  WRITE(SDPOWER,HIGH);
-#endif //SDPOWER
-  quickinitsd();
-
-#endif //SDSUPPORT
+  #ifdef SDSUPPORT
+    //power to SD reader
+    #if SDPOWER > -1
+      SET_OUTPUT(SDPOWER); 
+      WRITE(SDPOWER,HIGH);
+    #endif //SDPOWER
+    quickinitsd();
+  #endif //SDSUPPORT
+  
   plan_init();  // Initialize planner;
   st_init();    // Initialize stepper;
   tp_init();    // Initialize temperature loop
-       //checkautostart();
 }
 
 #ifdef SDSUPPORT
-bool autostart_stilltocheck=true;
-
 
 void checkautostart(bool force)
 {
-       //this is to delay autostart and hence the initialisaiton of the sd card to some seconds after the normal init, so the device is available quick after a reset
-       if(!force)
-       {
-               if(!autostart_stilltocheck)
-                       return;
-               if(autostart_atmillis<millis())
-                       return;
-       }
-       autostart_stilltocheck=false;
-       if(!sdactive)
-       {
-               initsd();
-               if(!sdactive) //fail
-               return;
-       }
-        static int lastnr=0;
-        char autoname[30];
-        sprintf(autoname,"auto%i.g",lastnr);
-        for(int i=0;i<(int)strlen(autoname);i++)
-                autoname[i]=tolower(autoname[i]);
-        dir_t p;
-
-        root.rewind();
-        //char filename[11];
-        //int cnt=0;
-
-        bool found=false;
-        while (root.readDir(p) > 0) 
-        {
-                for(int i=0;i<(int)strlen((char*)p.name);i++)
-                        p.name[i]=tolower(p.name[i]);
-                //Serial.print((char*)p.name);
-                //Serial.print(" ");
-               //Serial.println(autoname);
-               if(p.name[9]!='~') //skip safety copies
-               if(strncmp((char*)p.name,autoname,5)==0)
-               {
-                       char cmd[30];
-                       
-                       sprintf(cmd,"M23 %s",autoname);
-                       //sprintf(cmd,"M115");
-                       //enquecommand("G92 Z0");
-                       //enquecommand("G1 Z10 F2000");
-                       //enquecommand("G28 X-105 Y-105");
-                       enquecommand(cmd);
-                       enquecommand("M24");
-                       found=true;
-                       
-               }
-       }
-       if(!found)
-               lastnr=-1;
-       else
-               lastnr++;
-       
-}
-#else
-
-inline void checkautostart(bool x)
-{
+//this is to delay autostart and hence the initialisaiton of the sd card to some seconds after the normal init, so the device is available quick after a reset
+  if(!force)
+  {
+    if(!autostart_stilltocheck)
+      return;
+    if(autostart_atmillis<millis())
+      return;
+  }
+  autostart_stilltocheck=false;
+  if(!sdactive)
+  {
+    initsd();
+    if(!sdactive) //fail
+      return;
+  }
+  static int lastnr=0;
+  char autoname[30];
+  sprintf(autoname,"auto%i.g",lastnr);
+  for(int i=0;i<(int)strlen(autoname);i++)
+    autoname[i]=tolower(autoname[i]);
+  dir_t p;
+
+  root.rewind();
+  
+  bool found=false;
+  while (root.readDir(p) > 0) 
+  {
+    for(int i=0;i<(int)strlen((char*)p.name);i++)
+    p.name[i]=tolower(p.name[i]);
+    //Serial.print((char*)p.name);
+    //Serial.print(" ");
+    //Serial.println(autoname);
+    if(p.name[9]!='~') //skip safety copies
+    if(strncmp((char*)p.name,autoname,5)==0)
+    {
+      char cmd[30];
+
+      sprintf(cmd,"M23 %s",autoname);
+      //sprintf(cmd,"M115");
+      //enquecommand("G92 Z0");
+      //enquecommand("G1 Z10 F2000");
+      //enquecommand("G28 X-105 Y-105");
+      enquecommand(cmd);
+      enquecommand("M24");
+      found=true;
+    }
+  }
+  if(!found)
+    lastnr=-1;
+  else
+    lastnr++;
 }
+#else  //NO SD SUPORT
+  inline void checkautostart(bool x){}
 #endif
 
 
@@ -349,28 +337,32 @@ void loop()
 {
   if(buflen<3)
     get_command();
-       checkautostart(false);
+  checkautostart(false);
   if(buflen)
   {
-#ifdef SDSUPPORT
-    if(savetosd){
-      if(strstr(cmdbuffer[bufindr],"M29") == NULL){
-        write_command(cmdbuffer[bufindr]);
-        Serial.println("ok");
+    #ifdef SDSUPPORT
+      if(savetosd)
+      {
+       if(strstr(cmdbuffer[bufindr],"M29") == NULL)
+       {
+         write_command(cmdbuffer[bufindr]);
+         Serial.println("ok");
+       }
+       else
+       {
+         file.sync();
+         file.close();
+         savetosd = false;
+         Serial.println("Done saving file.");
+       }
       }
-      else{
-        file.sync();
-        file.close();
-        savetosd = false;
-        Serial.println("Done saving file.");
+      else
+      {
+       process_commands();
       }
-    }
-    else{
+    #else
       process_commands();
-    }
-#else
-    process_commands();
-#endif //SDSUPPORT
+    #endif //SDSUPPORT
     buflen = (buflen-1);
     bufindr = (bufindr + 1)%BUFSIZE;
   }
@@ -449,10 +441,10 @@ inline void get_command()
           case 1:
           case 2:
           case 3:
-#ifdef SDSUPPORT
+           #ifdef SDSUPPORT
             if(savetosd)
               break;
-#endif //SDSUPPORT
+           #endif //SDSUPPORT
             Serial.println("ok"); 
             break;
           default:
@@ -473,7 +465,7 @@ inline void get_command()
       if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
     }
   }
-#ifdef SDSUPPORT
+  #ifdef SDSUPPORT
   if(!sdmode || serial_count!=0){
     return;
   }
@@ -486,18 +478,19 @@ inline void get_command()
       if(sdpos >= filesize){
         sdmode = false;
         Serial.println("echo: Done printing file");
-       stoptime=millis();
-       char time[30];
-       unsigned long t=(stoptime-starttime)/1000;
-       int sec,min;
-       min=t/60;
-       sec=t%60;
-       sprintf(time,"echo: %i min, %i sec",min,sec);
-       Serial.println(time);
-       LCD_MESSAGE(time);
-       checkautostart(true);
+        stoptime=millis();
+        char time[30];
+        unsigned long t=(stoptime-starttime)/1000;
+        int sec,min;
+        min=t/60;
+        sec=t%60;
+        sprintf(time,"echo: %i min, %i sec",min,sec);
+        Serial.println(time);
+        LCD_MESSAGE(time);
+        checkautostart(true);
       }
-      if(!serial_count) return; //if empty line
+      if(!serial_count) 
+       return; //if empty line
       cmdbuffer[bufindw][serial_count] = 0; //terminate string
       if(!comment_mode){
         fromsd[bufindw] = true;
@@ -513,20 +506,23 @@ inline void get_command()
       if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
     }
   }
-#endif //SDSUPPORT
+  #endif //SDSUPPORT
 
 }
 
 
-inline float code_value() { 
+inline float code_value() 
+{ 
   return (strtod(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL)); 
 }
-inline long code_value_long() { 
+inline long code_value_long() 
+{ 
   return (strtol(&cmdbuffer[bufindr][strchr_pointer - cmdbuffer[bufindr] + 1], NULL, 10)); 
 }
-inline bool code_seen(char code_string[]) { 
+inline bool code_seen(char code_string[]) //Return True if the string was found
+{ 
   return (strstr(cmdbuffer[bufindr], code_string) != NULL); 
-}  //Return True if the string was found
+}  
 
 inline bool code_seen(char code)
 {
@@ -579,10 +575,10 @@ inline void process_commands()
         destination[i] = current_position[i];
       }
       feedrate = 0.0;
-
       home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2])));
 
-      if((home_all_axis) || (code_seen(axis_codes[X_AXIS]))) {
+      if((home_all_axis) || (code_seen(axis_codes[X_AXIS]))) 
+      {
         if ((X_MIN_PIN > -1 && X_HOME_DIR==-1) || (X_MAX_PIN > -1 && X_HOME_DIR==1)){
 //          st_synchronize();
           current_position[X_AXIS] = 0;
@@ -689,7 +685,7 @@ inline void process_commands()
 
     switch( (int)code_value() ) 
     {
-#ifdef SDSUPPORT
+    #ifdef SDSUPPORT
 
     case 20: // M20 - list SD card
       Serial.println("Begin file list");
@@ -781,6 +777,8 @@ inline void process_commands()
       //processed in write to file routine above
       //savetosd = false;
       break;
+    #endif //SDSUPPORT
+
     case 30: //M30 take time since the start of the SD print or an M109 command
     {
       stoptime=millis();
@@ -794,133 +792,134 @@ inline void process_commands()
       LCD_MESSAGE(time);
     }
     break;
-#endif //SDSUPPORT
-      case 42: //M42 -Change pin status via gcode
-        if (code_seen('S'))
+    case 42: //M42 -Change pin status via gcode
+      if (code_seen('S'))
+      {
+        int pin_status = code_value();
+        if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
         {
-          int pin_status = code_value();
-          if (code_seen('P') && pin_status >= 0 && pin_status <= 255)
+          int pin_number = code_value();
+          for(int i = 0; i < (int)sizeof(sensitive_pins); i++)
           {
-            int pin_number = code_value();
-            for(int i = 0; i < (int)sizeof(sensitive_pins); i++)
+            if (sensitive_pins[i] == pin_number)
             {
-              if (sensitive_pins[i] == pin_number)
-              {
-                pin_number = -1;
-                break;
-              }
-            }
-            
-            if (pin_number > -1)
-            {              
-              pinMode(pin_number, OUTPUT);
-              digitalWrite(pin_number, pin_status);
-              analogWrite(pin_number, pin_status);
+              pin_number = -1;
+              break;
             }
           }
+          
+          if (pin_number > -1)
+          {              
+            pinMode(pin_number, OUTPUT);
+            digitalWrite(pin_number, pin_status);
+            analogWrite(pin_number, pin_status);
+          }
         }
-        break;
-      case 104: // M104
-       if (code_seen('S')) setTargetHotend0(code_value());
-        setWatch();
-        break;
-      case 140: // M140 set bed temp
-       if (code_seen('S')) setTargetBed(code_value());
-        break;
-      case 105: // M105
-        #if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595)
-                tt = degHotend0();
-        #endif
-        #if TEMP_1_PIN > -1
-                bt = degBed();
-        #endif
-        #if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595)
-            Serial.print("ok T:");
-            Serial.print(tt); 
-//            Serial.print(", raw:");
-//            Serial.print(current_raw);       
-          #if TEMP_1_PIN > -1 
-#ifdef PIDTEMP
+      }
+     break;
+    case 104: // M104
+      if (code_seen('S')) setTargetHotend0(code_value());
+      setWatch();
+      break;
+    case 140: // M140 set bed temp
+      if (code_seen('S')) setTargetBed(code_value());
+      break;
+    case 105: // M105
+      #if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595)
+        tt = degHotend0();
+      #endif
+      #if TEMP_1_PIN > -1
+          bt = degBed();
+      #endif
+      #if (TEMP_0_PIN > -1) || defined (HEATER_USES_AD595)
+        Serial.print("ok T:");
+        Serial.print(tt); 
+        #if TEMP_1_PIN > -1 
+          #ifdef PIDTEMP
             Serial.print(" B:");
             #if TEMP_1_PIN > -1
-            Serial.println(bt); 
+              Serial.println(bt); 
             #else
-            Serial.println(HeaterPower); 
+              Serial.println(HeaterPower); 
             #endif
-#else
+          #else //not PIDTEMP
             Serial.println();
-#endif
-          #else
+           #endif //PIDTEMP
+         #else
             Serial.println();
-          #endif
+          #endif //TEMP_1_PIN
         #else
           Serial.println("echo: No thermistors - no temp");
-        #endif
-        return;
-        //break;
-      case 109: {// M109 - Wait for extruder heater to reach target.
-            LCD_MESSAGE("Heating...");
-               if (code_seen('S')) setTargetHotend0(code_value());
-            
-            setWatch();
-            codenum = millis(); 
-     
-               /* See if we are heating up or cooling down */
-              bool target_direction = isHeatingHotend0(); // true if heating, false if cooling
-
-            #ifdef TEMP_RESIDENCY_TIME
-            long residencyStart;
-            residencyStart = -1;
-            /* continue to loop until we have reached the target temp   
-              _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
-            while((target_direction ? (isHeatingHotend0()) : (isCoolingHotend0()) ||
-                    (residencyStart > -1 && (millis() - residencyStart) < TEMP_RESIDENCY_TIME*1000) ) {
-            #else
-            while ( target_direction ? (isHeatingHotend0()) : (isCoolingHotend0()) ) {
-            #endif //TEMP_RESIDENCY_TIME
-              if( (millis() - codenum) > 1000 ) { //Print Temp Reading every 1 second while heating up/cooling down
-                Serial.print("T:");
-              Serial.println( degHotend0() ); 
-                codenum = millis();
-              }
-              manage_heater();
-              LCD_STATUS;
-              #ifdef TEMP_RESIDENCY_TIME
-               /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
-                  or when current temp falls outside the hysteresis after target temp was reached */
-              if ((residencyStart == -1 &&  target_direction && !isHeatingHotend0()) ||
-                  (residencyStart == -1 && !target_direction && !isCoolingHotend0()) ||
-                  (residencyStart > -1 && labs(degHotend0() - degTargetHotend0()) > TEMP_HYSTERESIS) ) {
-                residencyStart = millis();
-              }
-              #endif //TEMP_RESIDENCY_TIME
-            }
-            LCD_MESSAGE("Heating done.");
-           starttime=millis();
-          }
-          break;
-      case 190: // M190 - Wait bed for heater to reach target.
-      #if TEMP_1_PIN > -1
-          if (code_seen('S')) setTargetBed(code_value());
-         codenum = millis(); 
-          while(isHeatingBed()) 
-          {
-           if( (millis()-codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
-           {
-             float tt=degHotend0();
-             Serial.print("T:");
-             Serial.println( tt );
-             Serial.print("ok T:");
-             Serial.print( tt ); 
-             Serial.print(" B:");
-             Serial.println( degBed() ); 
-             codenum = millis(); 
-           }
-            manage_heater();
-         }
       #endif
+      return;
       break;
-#if FAN_PIN > -1
+    case 109: 
+    {// M109 - Wait for extruder heater to reach target.
+        LCD_MESSAGE("Heating...");
+        if (code_seen('S')) setTargetHotend0(code_value());
+        
+        setWatch();
+        codenum = millis(); 
+
+        /* See if we are heating up or cooling down */
+        bool target_direction = isHeatingHotend0(); // true if heating, false if cooling
+
+        #ifdef TEMP_RESIDENCY_TIME
+          long residencyStart;
+          residencyStart = -1;
+          /* continue to loop until we have reached the target temp   
+            _and_ until TEMP_RESIDENCY_TIME hasn't passed since we reached it */
+          while((target_direction ? (isHeatingHotend0()) : (isCoolingHotend0()) ||
+                  (residencyStart > -1 && (millis() - residencyStart) < TEMP_RESIDENCY_TIME*1000) ) {
+        #else
+          while ( target_direction ? (isHeatingHotend0()) : (isCoolingHotend0()) ) {
+        #endif //TEMP_RESIDENCY_TIME
+        if( (millis() - codenum) > 1000 ) 
+        { //Print Temp Reading every 1 second while heating up/cooling down
+          Serial.print("T:");
+        Serial.println( degHotend0() ); 
+          codenum = millis();
+        }
+        manage_heater();
+        LCD_STATUS;
+        #ifdef TEMP_RESIDENCY_TIME
+            /* start/restart the TEMP_RESIDENCY_TIME timer whenever we reach target temp for the first time
+              or when current temp falls outside the hysteresis after target temp was reached */
+          if ((residencyStart == -1 &&  target_direction && !isHeatingHotend0()) ||
+              (residencyStart == -1 && !target_direction && !isCoolingHotend0()) ||
+              (residencyStart > -1 && labs(degHotend0() - degTargetHotend0()) > TEMP_HYSTERESIS) ) 
+          {
+            residencyStart = millis();
+          }
+        #endif //TEMP_RESIDENCY_TIME
+        }
+        LCD_MESSAGE("Heating done.");
+        starttime=millis();
+      }
+      break;
+    case 190: // M190 - Wait bed for heater to reach target.
+    #if TEMP_1_PIN > -1
+        if (code_seen('S')) setTargetBed(code_value());
+        codenum = millis(); 
+        while(isHeatingBed()) 
+        {
+          if( (millis()-codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
+          {
+            float tt=degHotend0();
+            Serial.print("T:");
+            Serial.println( tt );
+            Serial.print("ok T:");
+            Serial.print( tt ); 
+            Serial.print(" B:");
+            Serial.println( degBed() ); 
+            codenum = millis(); 
+          }
+          manage_heater();
+        }
+    #endif
+    break;
+
+    #if FAN_PIN > -1
       case 106: //M106 Fan On
         if (code_seen('S')){
             WRITE(FAN_PIN,HIGH);
@@ -937,27 +936,29 @@ inline void process_commands()
         WRITE(FAN_PIN,LOW);
         analogWrite(FAN_PIN, 0);
         break;
-#endif
-#if (PS_ON_PIN > -1)
+    #endif //FAN_PIN
+
+    #if (PS_ON_PIN > -1)
       case 80: // M80 - ATX Power On
         SET_OUTPUT(PS_ON_PIN); //GND
         break;
       case 81: // M81 - ATX Power Off
         SET_INPUT(PS_ON_PIN); //Floating
         break;
-#endif
+    #endif
     case 82:
       axis_relative_modes[3] = false;
       break;
     case 83:
       axis_relative_modes[3] = true;
       break;
-               case 18:
+    case 18: //compatibility
     case 84:
       if(code_seen('S')){ 
         stepper_inactive_time = code_value() * 1000; 
       }
-      else{ 
+      else
+      { 
         st_synchronize(); 
         disable_x(); 
         disable_y(); 
@@ -970,13 +971,14 @@ inline void process_commands()
       max_inactive_time = code_value() * 1000; 
       break;
     case 92: // M92
-      for(int i=0; i < NUM_AXIS; i++) {
-        if(code_seen(axis_codes[i])) axis_steps_per_unit[i] = code_value();
+      for(int i=0; i < NUM_AXIS; i++) 
+      {
+        if(code_seen(axis_codes[i])) 
+          axis_steps_per_unit[i] = code_value();
       }
-
       break;
     case 115: // M115
-      Serial.println("FIRMWARE_NAME:Sprinter/grbl mashup for gen6 FIRMWARE_URL:http://www.mendel-parts.com PROTOCOL_VERSION:1.0 MACHINE_TYPE:Mendel EXTRUDER_COUNT:1");
+      Serial.println("FIRMWARE_NAME:Marlin; Sprinter/grbl mashup for gen6 FIRMWARE_URL:http://www.mendel-parts.com PROTOCOL_VERSION:1.0 MACHINE_TYPE:Mendel EXTRUDER_COUNT:1");
       break;
     case 114: // M114
       Serial.print("X:");
@@ -998,45 +1000,46 @@ inline void process_commands()
       Serial.println("");
       break;
     case 119: // M119
-#if (X_MIN_PIN > -1)
-      Serial.print("x_min:");
-      Serial.print((READ(X_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
-#endif
-#if (X_MAX_PIN > -1)
-      Serial.print("x_max:");
-      Serial.print((READ(X_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
-#endif
-#if (Y_MIN_PIN > -1)
-      Serial.print("y_min:");
-      Serial.print((READ(Y_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
-#endif
-#if (Y_MAX_PIN > -1)
-      Serial.print("y_max:");
-      Serial.print((READ(Y_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
-#endif
-#if (Z_MIN_PIN > -1)
-      Serial.print("z_min:");
-      Serial.print((READ(Z_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
-#endif
-#if (Z_MAX_PIN > -1)
-      Serial.print("z_max:");
-      Serial.print((READ(Z_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
-#endif
+      #if (X_MIN_PIN > -1)
+        Serial.print("x_min:");
+        Serial.print((READ(X_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
+      #endif
+      #if (X_MAX_PIN > -1)
+        Serial.print("x_max:");
+        Serial.print((READ(X_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
+      #endif
+      #if (Y_MIN_PIN > -1)
+        Serial.print("y_min:");
+        Serial.print((READ(Y_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
+      #endif
+      #if (Y_MAX_PIN > -1)
+        Serial.print("y_max:");
+        Serial.print((READ(Y_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
+      #endif
+      #if (Z_MIN_PIN > -1)
+        Serial.print("z_min:");
+        Serial.print((READ(Z_MIN_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
+      #endif
+      #if (Z_MAX_PIN > -1)
+        Serial.print("z_max:");
+        Serial.print((READ(Z_MAX_PIN)^ENDSTOPS_INVERTING)?"H ":"L ");
+      #endif
       Serial.println("");
       break;
       //TODO: update for all axis, use for loop
     case 201: // M201
-      for(int i=0; i < NUM_AXIS; i++) {
+      for(int i=0; i < NUM_AXIS; i++) 
+      {
         if(code_seen(axis_codes[i])) axis_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
       }
       break;
-#if 0 // Not used for Sprinter/grbl gen6
+    #if 0 // Not used for Sprinter/grbl gen6
     case 202: // M202
       for(int i=0; i < NUM_AXIS; i++) {
         if(code_seen(axis_codes[i])) axis_travel_steps_per_sqr_second[i] = code_value() * axis_steps_per_unit[i];
       }
       break;
-#endif
+    #endif
     case 203: // M203 max feedrate mm/sec
       for(int i=0; i < NUM_AXIS; i++) {
         if(code_seen(axis_codes[i])) max_feedrate[i] = code_value()*60 ;
@@ -1048,59 +1051,52 @@ inline void process_commands()
         if(code_seen('T')) retract_acceleration = code_value() ;
       }
       break;
-      case 205: //M205 advanced settings:  minimum travel speed S=while printing T=travel only,  B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
-      {
-        if(code_seen('S')) minimumfeedrate = code_value()*60 ;
-        if(code_seen('T')) mintravelfeedrate = code_value()*60 ;
-        if(code_seen('B')) minsegmenttime = code_value() ;
-        if(code_seen('X')) max_xy_jerk = code_value()*60 ;
-        if(code_seen('Z')) max_z_jerk = code_value()*60 ;
-      }
-      break;
-      case 220: // M220 S<factor in percent>- set speed factor override percentage
+    case 205: //M205 advanced settings:  minimum travel speed S=while printing T=travel only,  B=minimum segment time X= maximum xy jerk, Z=maximum Z jerk
+    {
+      if(code_seen('S')) minimumfeedrate = code_value()*60 ;
+      if(code_seen('T')) mintravelfeedrate = code_value()*60 ;
+      if(code_seen('B')) minsegmenttime = code_value() ;
+      if(code_seen('X')) max_xy_jerk = code_value()*60 ;
+      if(code_seen('Z')) max_z_jerk = code_value()*60 ;
+    }
+    break;
+    case 220: // M220 S<factor in percent>- set speed factor override percentage
+    {
+      if(code_seen('S')) 
       {
-        if(code_seen('S')) 
-        {
-          feedmultiply = code_value() ;
-          feedmultiplychanged=true;
-        }
+        feedmultiply = code_value() ;
+        feedmultiplychanged=true;
       }
-      break;
-#ifdef PIDTEMP
+    }
+    break;
+
+    #ifdef PIDTEMP
     case 301: // M301
       if(code_seen('P')) Kp = code_value();
       if(code_seen('I')) Ki = code_value()*PID_dT;
       if(code_seen('D')) Kd = code_value()/PID_dT;
-//      SERIAL_ECHOLN("Kp "<<_FLOAT(Kp,2));
-//      SERIAL_ECHOLN("Ki "<<_FLOAT(Ki/PID_dT,2));
-//      SERIAL_ECHOLN("Kd "<<_FLOAT(Kd*PID_dT,2));
-
-//      temp_iState_min = 0.0;
-//      if (Ki!=0) {
-//      temp_iState_max = PID_INTEGRAL_DRIVE_MAX / (Ki/100.0);
-//      }
-//      else       temp_iState_max = 1.0e10;
-      break;
-#endif //PIDTEMP
-      case 500: // Store settings in EEPROM
-      {
-          StoreSettings();
-      }
-      break;
-      case 501: // Read settings from EEPROM
-      {
-        RetrieveSettings();
-      }
-      break;
-      case 502: // Revert to default settings
-      {
-        RetrieveSettings(true);
-      }
       break;
+    #endif //PIDTEMP
+    case 500: // Store settings in EEPROM
+    {
+        StoreSettings();
+    }
+    break;
+    case 501: // Read settings from EEPROM
+    {
+      RetrieveSettings();
+    }
+    break;
+    case 502: // Revert to default settings
+    {
+      RetrieveSettings(true);
+    }
+    break;
 
     }
   }
-  else{
+  else
+  {
     Serial.print("echo: Unknown command:\"");
     Serial.print(cmdbuffer[bufindr]);
     Serial.println("\"");
@@ -1121,10 +1117,10 @@ void FlushSerialRequestResend()
 void ClearToSend()
 {
   previous_millis_cmd = millis();
-#ifdef SDSUPPORT
+  #ifdef SDSUPPORT
   if(fromsd[bufindr])
     return;
-#endif //SDSUPPORT
+  #endif //SDSUPPORT
   Serial.println("ok"); 
 }
 
@@ -1132,7 +1128,7 @@ inline void get_coordinates()
 {
   for(int 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?
+    else destination[i] = current_position[i]; //Are these else lines really needed?
   }
   if(code_seen('F')) {
     next_feedrate = code_value();
@@ -1276,14 +1272,19 @@ void prepare_arc_move(char isclockwise) {
 
 
 
-void manage_inactivity(byte debug) { 
-  if( (millis()-previous_millis_cmd) >  max_inactive_time ) if(max_inactive_time) kill(); 
-  if( (millis()-previous_millis_cmd) >  stepper_inactive_time ) if(stepper_inactive_time) { 
-    disable_x(); 
-    disable_y(); 
-    disable_z(); 
-    disable_e(); 
-  }
+void manage_inactivity(byte debug) 
+{ 
+  if( (millis()-previous_millis_cmd) >  max_inactive_time ) 
+    if(max_inactive_time) 
+      kill(); 
+  if( (millis()-previous_millis_cmd) >  stepper_inactive_time ) 
+    if(stepper_inactive_time) 
+    { 
+      disable_x(); 
+      disable_y(); 
+      disable_z(); 
+      disable_e(); 
+    }
   check_axes_activity();
 }
 
index 0b734de4ab1bc6872582366f750d625f966d3914..75396143aaa8806a41d66406af79462ab860bd06 100644 (file)
@@ -33,8 +33,8 @@
 void mc_arc(float *position, float *target, float *offset, uint8_t axis_0, uint8_t axis_1, 
   uint8_t axis_linear, float feed_rate, float radius, uint8_t isclockwise)
 {      
-//   int acceleration_manager_was_enabled = plan_is_acceleration_manager_enabled();
-//   plan_set_acceleration_manager_enabled(false); // disable acceleration management for the duration of the arc
+  //   int acceleration_manager_was_enabled = plan_is_acceleration_manager_enabled();
+  //   plan_set_acceleration_manager_enabled(false); // disable acceleration management for the duration of the arc
   SERIAL_ECHOLN("mc_arc.");
   float center_axis0 = position[axis_0] + offset[axis_0];
   float center_axis1 = position[axis_1] + offset[axis_1];
@@ -52,12 +52,12 @@ void mc_arc(float *position, float *target, float *offset, uint8_t axis_0, uint8
   float millimeters_of_travel = hypot(angular_travel*radius, fabs(linear_travel));
   if (millimeters_of_travel == 0.0) { return; }
   uint16_t segments = floor(millimeters_of_travel/MM_PER_ARC_SEGMENT);
-/*  
-  // Multiply inverse feed_rate to compensate for the fact that this movement is approximated
-  // by a number of discrete segments. The inverse feed_rate should be correct for the sum of 
-  // all segments.
-  if (invert_feed_rate) { feed_rate *= segments; }
-*/
+  /*  
+    // Multiply inverse feed_rate to compensate for the fact that this movement is approximated
+    // by a number of discrete segments. The inverse feed_rate should be correct for the sum of 
+    // all segments.
+    if (invert_feed_rate) { feed_rate *= segments; }
+  */
   float theta_per_segment = angular_travel/segments;
   float linear_per_segment = linear_travel/segments;
   
@@ -128,6 +128,6 @@ void mc_arc(float *position, float *target, float *offset, uint8_t axis_0, uint8
   // Ensure last segment arrives at target location.
   plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], feed_rate);
 
-//   plan_set_acceleration_manager_enabled(acceleration_manager_was_enabled);
+  //   plan_set_acceleration_manager_enabled(acceleration_manager_was_enabled);
 }
 
index e9ebd4412ce74655977bb8abced2b5bced7418fb..4df1bd0e3b96e67060464d7ae3fd5469f6f86b8f 100644 (file)
 #define FAN_PIN            7\r
 #define PS_ON_PIN          12\r
 #define KILL_PIN           -1\r
+\r
+#ifdef ULTRA_LCD\r
+\r
+  #ifdef NEWPANEL\r
+  //arduino pin witch triggers an piezzo beeper\r
+    #define BEEPER 18\r
+\r
+    #define LCD_PINS_RS 20 \r
+    #define LCD_PINS_ENABLE 17\r
+    #define LCD_PINS_D4 16\r
+    #define LCD_PINS_D5 21 \r
+    #define LCD_PINS_D6 5\r
+    #define LCD_PINS_D7 6\r
+    \r
+    //buttons are directly attached\r
+    #define BTN_EN1 40\r
+    #define BTN_EN2 42\r
+    #define BTN_ENC 19  //the click\r
+    \r
+    #define BLEN_C 2\r
+    #define BLEN_B 1\r
+    #define BLEN_A 0\r
+    \r
+    #define SDCARDDETECT 38\r
+    \r
+      //encoder rotation values\r
+    #define encrot0 0\r
+    #define encrot1 2\r
+    #define encrot2 3\r
+    #define encrot3 1\r
+  #else //old style panel with shift register\r
+    //arduino pin witch triggers an piezzo beeper\r
+    #define BEEPER 18\r
+\r
+    //buttons are attached to a shift register\r
+    #define SHIFT_CLK 38\r
+    #define SHIFT_LD 42\r
+    #define SHIFT_OUT 40\r
+    #define SHIFT_EN 17\r
+    \r
+    #define LCD_PINS_RS 16 \r
+    #define LCD_PINS_ENABLE 5\r
+    #define LCD_PINS_D4 6\r
+    #define LCD_PINS_D5 21 \r
+    #define LCD_PINS_D6 20\r
+    #define LCD_PINS_D7 19\r
+    \r
+    //encoder rotation values\r
+    #define encrot0 0\r
+    #define encrot1 2\r
+    #define encrot2 3\r
+    #define encrot3 1\r
+\r
+    \r
+    //bits in the shift register that carry the buttons for:\r
+    // left up center down right red\r
+    #define BL_LE 7\r
+    #define BL_UP 6\r
+    #define BL_MI 5\r
+    #define BL_DW 4\r
+    #define BL_RI 3\r
+    #define BL_ST 2\r
+\r
+    #define BLEN_B 1\r
+    #define BLEN_A 0\r
+  #endif \r
+#endif //ULTRA_LCD\r
+\r
 #endif\r
 \r
 \r
index 14f0bb1043fc533fa6d72c8e1c47571364c41f32..b4271bab35e36e03f36a3a3e50542b2ce6369bd5 100644 (file)
@@ -83,7 +83,7 @@ static volatile unsigned char block_buffer_head;           // Index of the next
 static volatile unsigned char block_buffer_tail;           // Index of the block to process now\r
 \r
 // The current position of the tool in absolute steps\r
- long position[4];   \r
+long position[4];   \r
 \r
 #define ONE_MINUTE_OF_MICROSECONDS 60000000.0\r
 \r
@@ -123,10 +123,10 @@ void calculate_trapezoid_for_block(block_t *block, float entry_speed, float exit
   long initial_rate = ceil(block->nominal_rate*entry_factor);\r
   long final_rate = ceil(block->nominal_rate*exit_factor);\r
   \r
-#ifdef ADVANCE\r
-  long initial_advance = block->advance*entry_factor*entry_factor;\r
-  long final_advance = block->advance*exit_factor*exit_factor;\r
-#endif // ADVANCE\r
+  #ifdef ADVANCE\r
+    long initial_advance = block->advance*entry_factor*entry_factor;\r
+    long final_advance = block->advance*exit_factor*exit_factor;\r
+  #endif // ADVANCE\r
 \r
   // Limit minimal step rate (Otherwise the timer will overflow.)\r
   if(initial_rate <120) initial_rate=120;\r
@@ -155,10 +155,10 @@ void calculate_trapezoid_for_block(block_t *block, float entry_speed, float exit
     block->decelerate_after = decelerate_after;\r
     block->initial_rate = initial_rate;\r
     block->final_rate = final_rate;\r
-#ifdef ADVANCE\r
-    block->initial_advance = initial_advance;\r
-    block->final_advance = final_advance;\r
-#endif //ADVANCE\r
+  #ifdef ADVANCE\r
+      block->initial_advance = initial_advance;\r
+      block->final_advance = final_advance;\r
+  #endif //ADVANCE\r
   }\r
   CRITICAL_SECTION_END;\r
 }                    \r
@@ -166,18 +166,15 @@ void calculate_trapezoid_for_block(block_t *block, float entry_speed, float exit
 // Calculates the maximum allowable speed at this point when you must be able to reach target_velocity using the \r
 // acceleration within the allotted distance.\r
 inline float max_allowable_speed(float acceleration, float target_velocity, float distance) {\r
-  return(\r
-  sqrt(target_velocity*target_velocity-2*acceleration*60*60*distance)\r
-    );\r
+  return  sqrt(target_velocity*target_velocity-2*acceleration*60*60*distance);\r
 }\r
 \r
 // "Junction jerk" in this context is the immediate change in speed at the junction of two blocks.\r
 // This method will calculate the junction jerk as the euclidean distance between the nominal \r
 // velocities of the respective blocks.\r
 inline float junction_jerk(block_t *before, block_t *after) {\r
-  return(sqrt(\r
-    pow((before->speed_x-after->speed_x), 2)+\r
-    pow((before->speed_y-after->speed_y), 2)));\r
+  return sqrt(\r
+    pow((before->speed_x-after->speed_x), 2)+pow((before->speed_y-after->speed_y), 2));\r
 }\r
 \r
 // Return the safe speed which is max_jerk/2, e.g. the \r
@@ -185,8 +182,10 @@ inline float junction_jerk(block_t *before, block_t *after) {
 float safe_speed(block_t *block) {\r
   float safe_speed;\r
   safe_speed = max_xy_jerk/2;  \r
-  if(abs(block->speed_z) > max_z_jerk/2) safe_speed = max_z_jerk/2;\r
-  if (safe_speed > block->nominal_speed) safe_speed = block->nominal_speed;\r
+  if(abs(block->speed_z) > max_z_jerk/2) \r
+    safe_speed = max_z_jerk/2;\r
+  if (safe_speed > block->nominal_speed) \r
+    safe_speed = block->nominal_speed;\r
   return safe_speed;  \r
 }\r
 \r
@@ -379,9 +378,8 @@ void check_axes_activity() {
 // Add a new linear movement to the buffer. steps_x, _y and _z is the absolute position in \r
 // mm. Microseconds specify how many microseconds the move should take to perform. To aid acceleration\r
 // calculation the caller must also provide the physical length of the line in millimeters.\r
-void plan_buffer_line(float x, float y, float z, float e, float feed_rate) {\r
-\r
-\r
+void plan_buffer_line(const float &x, const float &y, const float &z, const float &e,  float feed_rate)\r
+{\r
   // Calculate the buffer head after we push this byte\r
   int next_buffer_head = (block_buffer_head + 1) & (BLOCK_BUFFER_SIZE - 1);\r
 \r
@@ -469,11 +467,8 @@ void plan_buffer_line(float x, float y, float z, float e, float feed_rate) {
   // Limit speed per axis\r
   float speed_factor = 1; //factor <=1 do decrease speed\r
   if(abs(block->speed_x) > max_feedrate[X_AXIS]) {\r
-    //// [ErikDeBruijn] IS THIS THE BUG WE'RE LOOING FOR????\r
-    //// [bernhard] No its not, according to Zalm.\r
-               //// the if would always be true, since tmp_speedfactor <=0 due the inial if, so its safe to set. the next lines actually compare.\r
     speed_factor = max_feedrate[X_AXIS] / abs(block->speed_x);\r
-    //if(speed_factor > tmp_speed_factor) speed_factor = tmp_speed_factor;\r
+    //if(speed_factor > tmp_speed_factor) speed_factor = tmp_speed_factor; /is not need here because auf the init above\r
   }\r
   if(abs(block->speed_y) > max_feedrate[Y_AXIS]){\r
     float tmp_speed_factor = max_feedrate[Y_AXIS] / abs(block->speed_y);\r
@@ -495,7 +490,8 @@ void plan_buffer_line(float x, float y, float z, float e, float feed_rate) {
   block->nominal_speed = block->millimeters * multiplier;\r
   block->nominal_rate = ceil(block->step_event_count * multiplier / 60);  \r
 \r
-  if(block->nominal_rate < 120) block->nominal_rate = 120;\r
+  if(block->nominal_rate < 120) \r
+    block->nominal_rate = 120;\r
   block->entry_speed = safe_speed(block);\r
 \r
   // Compute the acceleration rate for the trapezoid generator. \r
@@ -527,25 +523,25 @@ void plan_buffer_line(float x, float y, float z, float e, float feed_rate) {
   block->acceleration = block->acceleration_st * travel_per_step;\r
   block->acceleration_rate = (long)((float)block->acceleration_st * 8.388608);\r
 \r
-#ifdef ADVANCE\r
-  // Calculate advance rate\r
-  if((block->steps_e == 0) || (block->steps_x == 0 && block->steps_y == 0 && block->steps_z == 0)) {\r
-    block->advance_rate = 0;\r
-    block->advance = 0;\r
-  }\r
-  else {\r
-    long acc_dist = estimate_acceleration_distance(0, block->nominal_rate, block->acceleration_st);\r
-    float advance = (STEPS_PER_CUBIC_MM_E * EXTRUDER_ADVANCE_K) * \r
-      (block->speed_e * block->speed_e * EXTRUTION_AREA * EXTRUTION_AREA / 3600.0)*65536;\r
-    block->advance = advance;\r
-    if(acc_dist == 0) {\r
+  #ifdef ADVANCE\r
+    // Calculate advance rate\r
+    if((block->steps_e == 0) || (block->steps_x == 0 && block->steps_y == 0 && block->steps_z == 0)) {\r
       block->advance_rate = 0;\r
-    } \r
+      block->advance = 0;\r
+    }\r
     else {\r
-      block->advance_rate = advance / (float)acc_dist;\r
+      long acc_dist = estimate_acceleration_distance(0, block->nominal_rate, block->acceleration_st);\r
+      float advance = (STEPS_PER_CUBIC_MM_E * EXTRUDER_ADVANCE_K) * \r
+        (block->speed_e * block->speed_e * EXTRUTION_AREA * EXTRUTION_AREA / 3600.0)*65536;\r
+      block->advance = advance;\r
+      if(acc_dist == 0) {\r
+        block->advance_rate = 0;\r
+      } \r
+      else {\r
+        block->advance_rate = advance / (float)acc_dist;\r
+      }\r
     }\r
-  }\r
-#endif // ADVANCE\r
+  #endif // ADVANCE\r
 \r
   // compute a preliminary conservative acceleration trapezoid\r
   float safespeed = safe_speed(block);\r
@@ -576,7 +572,7 @@ void plan_buffer_line(float x, float y, float z, float e, float feed_rate) {
   st_wake_up();\r
 }\r
 \r
-void plan_set_position(float x, float y, float z, float e)\r
+void plan_set_position(const float &x, const float &y, const float &z, const float &e)\r
 {\r
   position[X_AXIS] = lround(x*axis_steps_per_unit[X_AXIS]);\r
   position[Y_AXIS] = lround(y*axis_steps_per_unit[Y_AXIS]);\r
index 18ea1e2b1ceb3263518936ad8d9e55670e226911..79f54d879f8d2b874dfa315be5ffbd77c52cda9d 100644 (file)
@@ -32,16 +32,16 @@ typedef struct {
   // Fields used by the bresenham algorithm for tracing the line\r
   long steps_x, steps_y, steps_z, steps_e;  // Step count along each axis\r
   long step_event_count;                    // The number of step events required to complete this block\r
-  volatile long accelerate_until;                    // The index of the step event on which to stop acceleration\r
-  volatile long decelerate_after;                    // The index of the step event on which to start decelerating\r
-  volatile long acceleration_rate;                   // The acceleration rate used for acceleration calculation\r
+  volatile long accelerate_until;           // The index of the step event on which to stop acceleration\r
+  volatile long decelerate_after;           // The index of the step event on which to start decelerating\r
+  volatile long acceleration_rate;          // The acceleration rate used for acceleration calculation\r
   unsigned char direction_bits;             // The direction bit set for this block (refers to *_DIRECTION_BIT in config.h)\r
-#ifdef ADVANCE\r
-  long advance_rate;\r
-  volatile long initial_advance;\r
-  volatile long final_advance;\r
-  float advance;\r
-#endif\r
+  #ifdef ADVANCE\r
+    long advance_rate;\r
+    volatile long initial_advance;\r
+    volatile long final_advance;\r
+    float advance;\r
+  #endif\r
 \r
   // Fields used by the motion planner to manage acceleration\r
   float speed_x, speed_y, speed_z, speed_e;          // Nominal mm/minute for each axis\r
@@ -57,16 +57,17 @@ typedef struct {
   long acceleration_st;                              // acceleration steps/sec^2\r
   volatile char busy;\r
 } block_t;\r
-      \r
+\r
 // Initialize the motion plan subsystem      \r
 void plan_init();\r
 \r
 // Add a new linear movement to the buffer. x, y and z is the signed, absolute target position in \r
 // millimaters. Feed rate specifies the speed of the motion.\r
-void plan_buffer_line(float x, float y, float z, float e, float feed_rate);\r
+void plan_buffer_line(const float &x, const float &y, const float &z, const float &e, float feed_rate);\r
 \r
 // Set position. Used for G92 instructions.\r
-void plan_set_position(float x, float y, float z, float e);\r
+void plan_set_position(const float &x, const float &y, const float &z, const float &e);\r
+\r
 \r
 // Called when the current block is no longer needed. Discards the block and makes the memory\r
 // availible for new blocks.\r
index 05dae47dfb65e5a8d3722b69f89527533386bee2..002f852f6241a457e5a82907e6c1b2fe5bf8517e 100644 (file)
@@ -37,6 +37,7 @@ uint16_t speed_lookuptable_fast[256][2] PROGMEM = {\
 { 32, 0}, { 32, 0}, { 32, 0}, { 32, 0}, { 32, 1}, { 31, 0}, { 31, 0}, { 31, 0}, \r
 { 31, 0}, { 31, 0}, { 31, 0}, { 31, 1}, { 30, 0}, { 30, 0}, { 30, 0}, { 30, 0}\r
 };\r
+\r
 uint16_t speed_lookuptable_slow[256][2] PROGMEM = {\\r
 { 62500, 12500}, { 50000, 8334}, { 41666, 5952}, { 35714, 4464}, { 31250, 3473}, { 27777, 2777}, { 25000, 2273}, { 22727, 1894}, \r
 { 20833, 1603}, { 19230, 1373}, { 17857, 1191}, { 16666, 1041}, { 15625, 920}, { 14705, 817}, { 13888, 731}, { 13157, 657}, \r
index 9e5db44a829ef83206e65d0a01fc0b1445a98b2a..924521119ebb90502329fe6dfaf65b9eca5d99f9 100644 (file)
@@ -35,8 +35,8 @@
 // if DEBUG_STEPS is enabled, M114 can be used to compare two methods of determining the X,Y,Z position of the printer.\r
 // for debugging purposes only, should be disabled by default\r
 #ifdef DEBUG_STEPS\r
-volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};\r
-volatile int count_direction[NUM_AXIS] = { 1, 1, 1, 1};\r
+  volatile long count_position[NUM_AXIS] = { 0, 0, 0, 0};\r
+  volatile int count_direction[NUM_AXIS] = { 1, 1, 1, 1};\r
 #endif\r
 \r
 \r
@@ -117,6 +117,8 @@ asm volatile ( \
 \r
 block_t *current_block;  // A pointer to the block currently being traced\r
 \r
+//static makes it inpossible to be called from outside of this file by extern.!\r
+\r
 // Variables used by The Stepper Driver Interrupt\r
 static unsigned char out_bits;        // The next stepping-bits to be output\r
 static long counter_x,       // Counter variables for the bresenham line tracer\r
@@ -125,9 +127,9 @@ static long counter_x,       // Counter variables for the bresenham line tracer
             counter_e;\r
 static unsigned long step_events_completed; // The number of step events executed in the current block\r
 #ifdef ADVANCE\r
-static long advance_rate, advance, final_advance = 0;\r
-static short old_advance = 0;\r
-static short e_steps;\r
+  static long advance_rate, advance, final_advance = 0;\r
+  static short old_advance = 0;\r
+  static short e_steps;\r
 #endif\r
 static unsigned char busy = false; // TRUE when SIG_OUTPUT_COMPARE1A is being serviced. Used to avoid retriggering that handler.\r
 static long acceleration_time, deceleration_time;\r
@@ -195,10 +197,10 @@ inline unsigned short calc_timer(unsigned short step_rate) {
 // Initializes the trapezoid generator from the current block. Called whenever a new \r
 // block begins.\r
 inline void trapezoid_generator_reset() {\r
-#ifdef ADVANCE\r
-  advance = current_block->initial_advance;\r
-  final_advance = current_block->final_advance;\r
-#endif\r
+  #ifdef ADVANCE\r
+    advance = current_block->initial_advance;\r
+    final_advance = current_block->final_advance;\r
+  #endif\r
   deceleration_time = 0;\r
   // advance_rate = current_block->advance_rate;\r
   // step_rate to timer interval\r
@@ -211,7 +213,8 @@ inline void trapezoid_generator_reset() {
 // It pops blocks from the block_buffer and executes them by pulsing the stepper pins appropriately. \r
 ISR(TIMER1_COMPA_vect)\r
 {        \r
-  if(busy){ SERIAL_ERRORLN(*(unsigned short *)OCR1A<< " ISR overtaking itself.");\r
+  if(busy){ \r
+    SERIAL_ERRORLN(*(unsigned short *)OCR1A<< " ISR overtaking itself.");\r
     return; \r
   } // The busy-flag is used to avoid reentering this interrupt\r
 \r
@@ -242,74 +245,74 @@ ISR(TIMER1_COMPA_vect)
     // Set directions TO DO This should be done once during init of trapezoid. Endstops -> interrupt\r
     out_bits = current_block->direction_bits;\r
 \r
-#ifdef ADVANCE\r
-    // Calculate E early.\r
-    counter_e += current_block->steps_e;\r
-    if (counter_e > 0) {\r
-      counter_e -= current_block->step_event_count;\r
-      if ((out_bits & (1<<E_AXIS)) != 0) { // - direction\r
-        CRITICAL_SECTION_START;\r
-        e_steps--;\r
-        CRITICAL_SECTION_END;\r
-      }\r
-      else {\r
+    #ifdef ADVANCE\r
+        // Calculate E early.\r
+        counter_e += current_block->steps_e;\r
+        if (counter_e > 0) {\r
+          counter_e -= current_block->step_event_count;\r
+          if ((out_bits & (1<<E_AXIS)) != 0) { // - direction\r
+            CRITICAL_SECTION_START;\r
+            e_steps--;\r
+            CRITICAL_SECTION_END;\r
+          }\r
+          else {\r
+            CRITICAL_SECTION_START;\r
+            e_steps++;\r
+            CRITICAL_SECTION_END;\r
+          }\r
+        }    \r
+        // Do E steps + advance steps\r
         CRITICAL_SECTION_START;\r
-        e_steps++;\r
+        e_steps += ((advance >> 16) - old_advance);\r
         CRITICAL_SECTION_END;\r
-      }\r
-    }    \r
-    // Do E steps + advance steps\r
-    CRITICAL_SECTION_START;\r
-    e_steps += ((advance >> 16) - old_advance);\r
-    CRITICAL_SECTION_END;\r
-    old_advance = advance >> 16;  \r
-#endif //ADVANCE\r
+        old_advance = advance >> 16;  \r
+    #endif //ADVANCE\r
 \r
     // Set direction en check limit switches\r
-if ((out_bits & (1<<X_AXIS)) != 0) {   // -direction\r
+    if ((out_bits & (1<<X_AXIS)) != 0) {   // -direction\r
       WRITE(X_DIR_PIN, INVERT_X_DIR);\r
       #ifdef DEBUG_STEPS\r
-      count_direction[X_AXIS]=-1;\r
+        count_direction[X_AXIS]=-1;\r
+      #endif\r
+      #if X_MIN_PIN > -1\r
+            if(READ(X_MIN_PIN) != ENDSTOPS_INVERTING) {\r
+              step_events_completed = current_block->step_event_count;\r
+            }\r
       #endif\r
-#if X_MIN_PIN > -1\r
-      if(READ(X_MIN_PIN) != ENDSTOPS_INVERTING) {\r
-        step_events_completed = current_block->step_event_count;\r
-      }\r
-#endif\r
     }\r
     else { // +direction \r
-        WRITE(X_DIR_PIN,!INVERT_X_DIR);\r
-        #ifdef DEBUG_STEPS\r
+      WRITE(X_DIR_PIN,!INVERT_X_DIR);\r
+      #ifdef DEBUG_STEPS\r
         count_direction[X_AXIS]=1;\r
-        #endif\r
-#if X_MAX_PIN > -1\r
+      #endif\r
+      #if X_MAX_PIN > -1\r
         if((READ(X_MAX_PIN) != ENDSTOPS_INVERTING)  && (current_block->steps_x >0)){\r
           step_events_completed = current_block->step_event_count;\r
         }\r
-#endif\r
+        #endif\r
     }\r
 \r
     if ((out_bits & (1<<Y_AXIS)) != 0) {   // -direction\r
       WRITE(Y_DIR_PIN,INVERT_Y_DIR);\r
       #ifdef DEBUG_STEPS\r
-      count_direction[Y_AXIS]=-1;\r
+        count_direction[Y_AXIS]=-1;\r
+      #endif\r
+      #if Y_MIN_PIN > -1\r
+        if(READ(Y_MIN_PIN) != ENDSTOPS_INVERTING) {\r
+          step_events_completed = current_block->step_event_count;\r
+        }\r
       #endif\r
-#if Y_MIN_PIN > -1\r
-      if(READ(Y_MIN_PIN) != ENDSTOPS_INVERTING) {\r
-        step_events_completed = current_block->step_event_count;\r
-      }\r
-#endif\r
     }\r
     else { // +direction\r
     WRITE(Y_DIR_PIN,!INVERT_Y_DIR);\r
       #ifdef DEBUG_STEPS\r
-      count_direction[Y_AXIS]=1;\r
+        count_direction[Y_AXIS]=1;\r
+      #endif\r
+      #if Y_MAX_PIN > -1\r
+      if((READ(Y_MAX_PIN) != ENDSTOPS_INVERTING) && (current_block->steps_y >0)){\r
+          step_events_completed = current_block->step_event_count;\r
+        }\r
       #endif\r
-#if Y_MAX_PIN > -1\r
-    if((READ(Y_MAX_PIN) != ENDSTOPS_INVERTING) && (current_block->steps_y >0)){\r
-        step_events_completed = current_block->step_event_count;\r
-      }\r
-#endif\r
     }\r
 \r
     if ((out_bits & (1<<Z_AXIS)) != 0) {   // -direction\r
@@ -317,30 +320,30 @@ if ((out_bits & (1<<X_AXIS)) != 0) {   // -direction
       #ifdef DEBUG_STEPS\r
       count_direction[Z_AXIS]=-1;\r
       #endif\r
-#if Z_MIN_PIN > -1\r
-      if(READ(Z_MIN_PIN) != ENDSTOPS_INVERTING) {\r
-        step_events_completed = current_block->step_event_count;\r
-      }\r
-#endif\r
+      #if Z_MIN_PIN > -1\r
+        if(READ(Z_MIN_PIN) != ENDSTOPS_INVERTING) {\r
+          step_events_completed = current_block->step_event_count;\r
+        }\r
+      #endif\r
     }\r
     else { // +direction\r
-        WRITE(Z_DIR_PIN,!INVERT_Z_DIR);\r
-        #ifdef DEBUG_STEPS\r
+      WRITE(Z_DIR_PIN,!INVERT_Z_DIR);\r
+      #ifdef DEBUG_STEPS\r
         count_direction[Z_AXIS]=1;\r
-        #endif\r
-#if Z_MAX_PIN > -1\r
+      #endif\r
+      #if Z_MAX_PIN > -1\r
         if((READ(Z_MAX_PIN) != ENDSTOPS_INVERTING)  && (current_block->steps_z >0)){\r
           step_events_completed = current_block->step_event_count;\r
         }\r
-#endif\r
+      #endif\r
     }\r
 \r
-#ifndef ADVANCE\r
-    if ((out_bits & (1<<E_AXIS)) != 0)   // -direction\r
-      WRITE(E_DIR_PIN,INVERT_E_DIR);\r
-    else // +direction\r
-      WRITE(E_DIR_PIN,!INVERT_E_DIR);\r
-#endif //!ADVANCE\r
+    #ifndef ADVANCE\r
+      if ((out_bits & (1<<E_AXIS)) != 0)   // -direction\r
+        WRITE(E_DIR_PIN,INVERT_E_DIR);\r
+      else // +direction\r
+        WRITE(E_DIR_PIN,!INVERT_E_DIR);\r
+    #endif //!ADVANCE\r
 \r
     for(char i=0; i < step_loops; i++) { // Take multiple steps per interrupt (For high speed moves) \r
       counter_x += current_block->steps_x;\r
@@ -349,7 +352,7 @@ if ((out_bits & (1<<X_AXIS)) != 0) {   // -direction
         counter_x -= current_block->step_event_count;\r
         WRITE(X_STEP_PIN, LOW);\r
         #ifdef DEBUG_STEPS\r
-        count_position[X_AXIS]+=count_direction[X_AXIS];   \r
+          count_position[X_AXIS]+=count_direction[X_AXIS];   \r
         #endif\r
       }\r
 \r
@@ -359,7 +362,7 @@ if ((out_bits & (1<<X_AXIS)) != 0) {   // -direction
         counter_y -= current_block->step_event_count;\r
         WRITE(Y_STEP_PIN, LOW);\r
         #ifdef DEBUG_STEPS\r
-        count_position[Y_AXIS]+=count_direction[Y_AXIS];\r
+          count_position[Y_AXIS]+=count_direction[Y_AXIS];\r
         #endif\r
       }\r
 \r
@@ -369,18 +372,18 @@ if ((out_bits & (1<<X_AXIS)) != 0) {   // -direction
         counter_z -= current_block->step_event_count;\r
         WRITE(Z_STEP_PIN, LOW);\r
         #ifdef DEBUG_STEPS\r
-        count_position[Z_AXIS]+=count_direction[Z_AXIS];\r
+          count_position[Z_AXIS]+=count_direction[Z_AXIS];\r
         #endif\r
       }\r
 \r
-#ifndef ADVANCE\r
-      counter_e += current_block->steps_e;\r
-      if (counter_e > 0) {\r
-        WRITE(E_STEP_PIN, HIGH);\r
-        counter_e -= current_block->step_event_count;\r
-        WRITE(E_STEP_PIN, LOW);\r
-      }\r
-#endif //!ADVANCE\r
+      #ifndef ADVANCE\r
+        counter_e += current_block->steps_e;\r
+        if (counter_e > 0) {\r
+          WRITE(E_STEP_PIN, HIGH);\r
+          counter_e -= current_block->step_event_count;\r
+          WRITE(E_STEP_PIN, LOW);\r
+        }\r
+      #endif //!ADVANCE\r
       step_events_completed += 1;  \r
       if(step_events_completed >= current_block->step_event_count) break;\r
     }\r
@@ -397,9 +400,9 @@ if ((out_bits & (1<<X_AXIS)) != 0) {   // -direction
 \r
       // step_rate to timer interval\r
       timer = calc_timer(acc_step_rate);\r
-#ifdef ADVANCE\r
-      advance += advance_rate;\r
-#endif\r
+      #ifdef ADVANCE\r
+        advance += advance_rate;\r
+      #endif\r
       acceleration_time += timer;\r
       OCR1A = timer;\r
     } \r
@@ -419,11 +422,11 @@ if ((out_bits & (1<<X_AXIS)) != 0) {   // -direction
 \r
       // step_rate to timer interval\r
       timer = calc_timer(step_rate);\r
-#ifdef ADVANCE\r
-      advance -= advance_rate;\r
-      if(advance < final_advance)\r
-        advance = final_advance;\r
-#endif //ADVANCE\r
+      #ifdef ADVANCE\r
+        advance -= advance_rate;\r
+        if(advance < final_advance)\r
+          advance = final_advance;\r
+      #endif //ADVANCE\r
       deceleration_time += timer;\r
       OCR1A = timer;\r
     }       \r
@@ -438,127 +441,126 @@ if ((out_bits & (1<<X_AXIS)) != 0) {   // -direction
 }\r
 \r
 #ifdef ADVANCE\r
-\r
-unsigned char old_OCR0A;\r
-// Timer interrupt for E. e_steps is set in the main routine;\r
-// Timer 0 is shared with millies\r
-ISR(TIMER0_COMPA_vect)\r
-{\r
-  // Critical section needed because Timer 1 interrupt has higher priority. \r
-  // The pin set functions are placed on trategic position to comply with the stepper driver timing.\r
-  WRITE(E_STEP_PIN, LOW);\r
-  // Set E direction (Depends on E direction + advance)\r
-  if (e_steps < 0) {\r
-    WRITE(E_DIR_PIN,INVERT_E_DIR);    \r
-    e_steps++;\r
-    WRITE(E_STEP_PIN, HIGH);\r
-  } \r
-  if (e_steps > 0) {\r
-    WRITE(E_DIR_PIN,!INVERT_E_DIR);\r
-    e_steps--;\r
-    WRITE(E_STEP_PIN, HIGH);\r
+  unsigned char old_OCR0A;\r
+  // Timer interrupt for E. e_steps is set in the main routine;\r
+  // Timer 0 is shared with millies\r
+  ISR(TIMER0_COMPA_vect)\r
+  {\r
+    // Critical section needed because Timer 1 interrupt has higher priority. \r
+    // The pin set functions are placed on trategic position to comply with the stepper driver timing.\r
+    WRITE(E_STEP_PIN, LOW);\r
+    // Set E direction (Depends on E direction + advance)\r
+    if (e_steps < 0) {\r
+      WRITE(E_DIR_PIN,INVERT_E_DIR);    \r
+      e_steps++;\r
+      WRITE(E_STEP_PIN, HIGH);\r
+    } \r
+    if (e_steps > 0) {\r
+      WRITE(E_DIR_PIN,!INVERT_E_DIR);\r
+      e_steps--;\r
+      WRITE(E_STEP_PIN, HIGH);\r
+    }\r
+    old_OCR0A += 25; // 10kHz interrupt\r
+    OCR0A = old_OCR0A;\r
   }\r
-  old_OCR0A += 25; // 10kHz interrupt\r
-  OCR0A = old_OCR0A;\r
-}\r
 #endif // ADVANCE\r
 \r
 void st_init()\r
 {\r
     //Initialize Dir Pins\r
-#if X_DIR_PIN > -1\r
-  SET_OUTPUT(X_DIR_PIN);\r
-#endif\r
-#if Y_DIR_PIN > -1 \r
-  SET_OUTPUT(Y_DIR_PIN);\r
-#endif\r
-#if Z_DIR_PIN > -1 \r
-  SET_OUTPUT(Z_DIR_PIN);\r
-#endif\r
-#if E_DIR_PIN > -1 \r
-  SET_OUTPUT(E_DIR_PIN);\r
-#endif\r
+  #if X_DIR_PIN > -1\r
+    SET_OUTPUT(X_DIR_PIN);\r
+  #endif\r
+  #if Y_DIR_PIN > -1 \r
+    SET_OUTPUT(Y_DIR_PIN);\r
+  #endif\r
+  #if Z_DIR_PIN > -1 \r
+    SET_OUTPUT(Z_DIR_PIN);\r
+  #endif\r
+  #if E_DIR_PIN > -1 \r
+    SET_OUTPUT(E_DIR_PIN);\r
+  #endif\r
 \r
   //Initialize Enable Pins - steppers default to disabled.\r
 \r
-#if (X_ENABLE_PIN > -1)\r
-  SET_OUTPUT(X_ENABLE_PIN);\r
-  if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);\r
-#endif\r
-#if (Y_ENABLE_PIN > -1)\r
-  SET_OUTPUT(Y_ENABLE_PIN);\r
-  if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);\r
-#endif\r
-#if (Z_ENABLE_PIN > -1)\r
-  SET_OUTPUT(Z_ENABLE_PIN);\r
-  if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);\r
-#endif\r
-#if (E_ENABLE_PIN > -1)\r
-  SET_OUTPUT(E_ENABLE_PIN);\r
-  if(!E_ENABLE_ON) WRITE(E_ENABLE_PIN,HIGH);\r
-#endif\r
+  #if (X_ENABLE_PIN > -1)\r
+    SET_OUTPUT(X_ENABLE_PIN);\r
+    if(!X_ENABLE_ON) WRITE(X_ENABLE_PIN,HIGH);\r
+  #endif\r
+  #if (Y_ENABLE_PIN > -1)\r
+    SET_OUTPUT(Y_ENABLE_PIN);\r
+    if(!Y_ENABLE_ON) WRITE(Y_ENABLE_PIN,HIGH);\r
+  #endif\r
+  #if (Z_ENABLE_PIN > -1)\r
+    SET_OUTPUT(Z_ENABLE_PIN);\r
+    if(!Z_ENABLE_ON) WRITE(Z_ENABLE_PIN,HIGH);\r
+  #endif\r
+  #if (E_ENABLE_PIN > -1)\r
+    SET_OUTPUT(E_ENABLE_PIN);\r
+    if(!E_ENABLE_ON) WRITE(E_ENABLE_PIN,HIGH);\r
+  #endif\r
 \r
   //endstops and pullups\r
-#ifdef ENDSTOPPULLUPS\r
-#if X_MIN_PIN > -1\r
-  SET_INPUT(X_MIN_PIN); \r
-  WRITE(X_MIN_PIN,HIGH);\r
-#endif\r
-#if X_MAX_PIN > -1\r
-  SET_INPUT(X_MAX_PIN); \r
-  WRITE(X_MAX_PIN,HIGH);\r
-#endif\r
-#if Y_MIN_PIN > -1\r
-  SET_INPUT(Y_MIN_PIN); \r
-  WRITE(Y_MIN_PIN,HIGH);\r
-#endif\r
-#if Y_MAX_PIN > -1\r
-  SET_INPUT(Y_MAX_PIN); \r
-  WRITE(Y_MAX_PIN,HIGH);\r
-#endif\r
-#if Z_MIN_PIN > -1\r
-  SET_INPUT(Z_MIN_PIN); \r
-  WRITE(Z_MIN_PIN,HIGH);\r
-#endif\r
-#if Z_MAX_PIN > -1\r
-  SET_INPUT(Z_MAX_PIN); \r
-  WRITE(Z_MAX_PIN,HIGH);\r
-#endif\r
-#else //ENDSTOPPULLUPS\r
-#if X_MIN_PIN > -1\r
-  SET_INPUT(X_MIN_PIN); \r
-#endif\r
-#if X_MAX_PIN > -1\r
-  SET_INPUT(X_MAX_PIN); \r
-#endif\r
-#if Y_MIN_PIN > -1\r
-  SET_INPUT(Y_MIN_PIN); \r
-#endif\r
-#if Y_MAX_PIN > -1\r
-  SET_INPUT(Y_MAX_PIN); \r
-#endif\r
-#if Z_MIN_PIN > -1\r
-  SET_INPUT(Z_MIN_PIN); \r
-#endif\r
-#if Z_MAX_PIN > -1\r
-  SET_INPUT(Z_MAX_PIN); \r
-#endif\r
-#endif //ENDSTOPPULLUPS\r
+  #ifdef ENDSTOPPULLUPS\r
+    #if X_MIN_PIN > -1\r
+      SET_INPUT(X_MIN_PIN); \r
+      WRITE(X_MIN_PIN,HIGH);\r
+    #endif\r
+    #if X_MAX_PIN > -1\r
+      SET_INPUT(X_MAX_PIN); \r
+      WRITE(X_MAX_PIN,HIGH);\r
+    #endif\r
+    #if Y_MIN_PIN > -1\r
+      SET_INPUT(Y_MIN_PIN); \r
+      WRITE(Y_MIN_PIN,HIGH);\r
+    #endif\r
+    #if Y_MAX_PIN > -1\r
+      SET_INPUT(Y_MAX_PIN); \r
+      WRITE(Y_MAX_PIN,HIGH);\r
+    #endif\r
+    #if Z_MIN_PIN > -1\r
+      SET_INPUT(Z_MIN_PIN); \r
+      WRITE(Z_MIN_PIN,HIGH);\r
+    #endif\r
+    #if Z_MAX_PIN > -1\r
+      SET_INPUT(Z_MAX_PIN); \r
+      WRITE(Z_MAX_PIN,HIGH);\r
+    #endif\r
+  #else //ENDSTOPPULLUPS\r
+    #if X_MIN_PIN > -1\r
+      SET_INPUT(X_MIN_PIN); \r
+    #endif\r
+    #if X_MAX_PIN > -1\r
+      SET_INPUT(X_MAX_PIN); \r
+    #endif\r
+    #if Y_MIN_PIN > -1\r
+      SET_INPUT(Y_MIN_PIN); \r
+    #endif\r
+    #if Y_MAX_PIN > -1\r
+      SET_INPUT(Y_MAX_PIN); \r
+    #endif\r
+    #if Z_MIN_PIN > -1\r
+      SET_INPUT(Z_MIN_PIN); \r
+    #endif\r
+    #if Z_MAX_PIN > -1\r
+      SET_INPUT(Z_MAX_PIN); \r
+    #endif\r
+  #endif //ENDSTOPPULLUPS\r
  \r
 \r
   //Initialize Step Pins\r
-#if (X_STEP_PIN > -1) \r
-  SET_OUTPUT(X_STEP_PIN);\r
-#endif  \r
-#if (Y_STEP_PIN > -1) \r
-  SET_OUTPUT(Y_STEP_PIN);\r
-#endif  \r
-#if (Z_STEP_PIN > -1) \r
-  SET_OUTPUT(Z_STEP_PIN);\r
-#endif  \r
-#if (E_STEP_PIN > -1) \r
-  SET_OUTPUT(E_STEP_PIN);\r
-#endif  \r
+  #if (X_STEP_PIN > -1) \r
+    SET_OUTPUT(X_STEP_PIN);\r
+  #endif  \r
+  #if (Y_STEP_PIN > -1) \r
+    SET_OUTPUT(Y_STEP_PIN);\r
+  #endif  \r
+  #if (Z_STEP_PIN > -1) \r
+    SET_OUTPUT(Z_STEP_PIN);\r
+  #endif  \r
+  #if (E_STEP_PIN > -1) \r
+    SET_OUTPUT(E_STEP_PIN);\r
+  #endif  \r
 \r
   // waveform generation = 0100 = CTC\r
   TCCR1B &= ~(1<<WGM13);\r
@@ -574,10 +576,10 @@ void st_init()
   OCR1A = 0x4000;\r
   DISABLE_STEPPER_DRIVER_INTERRUPT();  \r
 \r
-#ifdef ADVANCE\r
-  e_steps = 0;\r
-  TIMSK0 |= (1<<OCIE0A);\r
-#endif //ADVANCE\r
+  #ifdef ADVANCE\r
+    e_steps = 0;\r
+    TIMSK0 |= (1<<OCIE0A);\r
+  #endif //ADVANCE\r
   sei();\r
 }\r
 \r
index dfcc5469560970c35bc30a5965b4c9b7eb9e2667..fb649692d334ab8d84b5ce4cba3038f2b0748812 100644 (file)
@@ -36,8 +36,8 @@ void st_wake_up();
 // if DEBUG_STEPS is enabled, M114 can be used to compare two methods of determining the X,Y,Z position of the printer.\r
 // for debugging purposes only, should be disabled by default\r
 #ifdef DEBUG_STEPS\r
-extern volatile long count_position[NUM_AXIS];\r
-extern volatile int count_direction[NUM_AXIS];\r
+  extern volatile long count_position[NUM_AXIS];\r
+  extern volatile int count_direction[NUM_AXIS];\r
 #endif\r
 \r
 extern block_t *current_block;  // A pointer to the block currently being traced\r
index 6971739242cc3ae0da6973d5b11bf916c46baafe..c25b9c31204825dca05eddfb8edc22c235b80690 100644 (file)
@@ -74,24 +74,24 @@ unsigned long previous_millis_heater, previous_millis_bed_heater;
 #endif //WATCHPERIOD\r
 \r
 #ifdef HEATER_0_MINTEMP\r
-int minttemp_0 = temp2analog(HEATER_0_MINTEMP);\r
+  int minttemp_0 = temp2analog(HEATER_0_MINTEMP);\r
 #endif //MINTEMP\r
 #ifdef HEATER_0_MAXTEMP\r
-int maxttemp_0 = temp2analog(HEATER_0_MAXTEMP);\r
+  int maxttemp_0 = temp2analog(HEATER_0_MAXTEMP);\r
 #endif //MAXTEMP\r
 \r
 #ifdef HEATER_1_MINTEMP\r
-int minttemp_1 = temp2analog(HEATER_1_MINTEMP);\r
+  int minttemp_1 = temp2analog(HEATER_1_MINTEMP);\r
 #endif //MINTEMP\r
 #ifdef HEATER_1_MAXTEMP\r
-int maxttemp_1 = temp2analog(HEATER_1_MAXTEMP);\r
+  int maxttemp_1 = temp2analog(HEATER_1_MAXTEMP);\r
 #endif //MAXTEMP\r
 \r
 #ifdef BED_MINTEMP\r
-int bed_minttemp = temp2analog(BED_MINTEMP);\r
+  int bed_minttemp = temp2analog(BED_MINTEMP);\r
 #endif //BED_MINTEMP\r
 #ifdef BED_MAXTEMP\r
-int bed_maxttemp = temp2analog(BED_MAXTEMP);\r
+  int bed_maxttemp = temp2analog(BED_MAXTEMP);\r
 #endif //BED_MAXTEMP\r
 \r
 void manage_heater()\r
@@ -105,50 +105,49 @@ void manage_heater()
   if(temp_meas_ready != true)   //better readability\r
     return; \r
 \r
-CRITICAL_SECTION_START;\r
+  CRITICAL_SECTION_START;\r
     temp_meas_ready = false;\r
-CRITICAL_SECTION_END;\r
+  CRITICAL_SECTION_END;\r
 \r
-#ifdef PIDTEMP\r
+  #ifdef PIDTEMP\r
     pid_input = analog2temp(current_raw[TEMPSENSOR_HOTEND_0]);\r
 \r
-#ifndef PID_OPENLOOP\r
-    pid_error = pid_setpoint - pid_input;\r
-    if(pid_error > 10){\r
-      pid_output = PID_MAX;\r
-      pid_reset = true;\r
-    }\r
-    else if(pid_error < -10) {\r
-      pid_output = 0;\r
-      pid_reset = true;\r
-    }\r
-    else {\r
-      if(pid_reset == true) {\r
-        temp_iState = 0.0;\r
-        pid_reset = false;\r
-      }\r
-      pTerm = Kp * pid_error;\r
-      temp_iState += pid_error;\r
-      temp_iState = constrain(temp_iState, temp_iState_min, temp_iState_max);\r
-      iTerm = Ki * temp_iState;\r
-      //K1 defined in Configuration.h in the PID settings\r
-      #define K2 (1.0-K1)\r
-      dTerm = (Kd * (pid_input - temp_dState))*K2 + (K1 * dTerm);\r
-      temp_dState = pid_input;\r
-      #ifdef PID_ADD_EXTRUSION_RATE\r
-        pTerm+=Kc*current_block->speed_e; //additional heating if extrusion speed is high\r
-      #endif\r
-      pid_output = constrain(pTerm + iTerm - dTerm, 0, PID_MAX);\r
-    }\r
-#endif //PID_OPENLOOP\r
-#ifdef PID_DEBUG\r
-     SERIAL_ECHOLN(" PIDDEBUG Input "<<pid_input<<" Output "<<pid_output" pTerm "<<pTerm<<" iTerm "<<iTerm<<" dTerm "<<dTerm); \r
-     \r
-#endif //PID_DEBUG\r
+    #ifndef PID_OPENLOOP\r
+        pid_error = pid_setpoint - pid_input;\r
+        if(pid_error > 10){\r
+          pid_output = PID_MAX;\r
+          pid_reset = true;\r
+        }\r
+        else if(pid_error < -10) {\r
+          pid_output = 0;\r
+          pid_reset = true;\r
+        }\r
+        else {\r
+          if(pid_reset == true) {\r
+            temp_iState = 0.0;\r
+            pid_reset = false;\r
+          }\r
+          pTerm = Kp * pid_error;\r
+          temp_iState += pid_error;\r
+          temp_iState = constrain(temp_iState, temp_iState_min, temp_iState_max);\r
+          iTerm = Ki * temp_iState;\r
+          //K1 defined in Configuration.h in the PID settings\r
+          #define K2 (1.0-K1)\r
+          dTerm = (Kd * (pid_input - temp_dState))*K2 + (K1 * dTerm);\r
+          temp_dState = pid_input;\r
+          #ifdef PID_ADD_EXTRUSION_RATE\r
+            pTerm+=Kc*current_block->speed_e; //additional heating if extrusion speed is high\r
+          #endif\r
+          pid_output = constrain(pTerm + iTerm - dTerm, 0, PID_MAX);\r
+        }\r
+    #endif //PID_OPENLOOP\r
+    #ifdef PID_DEBUG\r
+     SERIAL_ECHOLN(" PIDDEBUG Input "<<pid_input<<" Output "<<pid_output" pTerm "<<pTerm<<" iTerm "<<iTerm<<" dTerm "<<dTerm);  \r
+    #endif //PID_DEBUG\r
     analogWrite(HEATER_0_PIN, pid_output);\r
-#endif //PIDTEMP\r
+  #endif //PIDTEMP\r
 \r
-#ifndef PIDTEMP\r
+  #ifndef PIDTEMP\r
     if(current_raw[0] >= target_raw[0])\r
     {\r
       WRITE(HEATER_0_PIN,LOW);\r
@@ -157,7 +156,7 @@ CRITICAL_SECTION_END;
     {\r
       WRITE(HEATER_0_PIN,HIGH);\r
     }\r
-#endif\r
+  #endif\r
     \r
   if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)\r
     return;\r
@@ -173,7 +172,7 @@ CRITICAL_SECTION_END;
       WRITE(HEATER_1_PIN,HIGH);\r
     }\r
   #endif\r
-  }\r
+}\r
 \r
 // Takes hot end temperature value as input and returns corresponding raw value. \r
 // For a thermistor, it uses the RepRap thermistor temp table.\r
@@ -300,26 +299,26 @@ float analog2tempBed(int raw) {
 \r
 void tp_init()\r
 {\r
-#if (HEATER_0_PIN > -1) \r
-  SET_OUTPUT(HEATER_0_PIN);\r
-#endif  \r
-#if (HEATER_1_PIN > -1) \r
-  SET_OUTPUT(HEATER_1_PIN);\r
-#endif  \r
-#if (HEATER_2_PIN > -1) \r
-  SET_OUTPUT(HEATER_2_PIN);\r
-#endif  \r
-\r
-#ifdef PIDTEMP\r
-  temp_iState_min = 0.0;\r
-  temp_iState_max = PID_INTEGRAL_DRIVE_MAX / Ki;\r
-#endif //PIDTEMP\r
-\r
-// Set analog inputs\r
+  #if (HEATER_0_PIN > -1) \r
+    SET_OUTPUT(HEATER_0_PIN);\r
+  #endif  \r
+  #if (HEATER_1_PIN > -1) \r
+    SET_OUTPUT(HEATER_1_PIN);\r
+  #endif  \r
+  #if (HEATER_2_PIN > -1) \r
+    SET_OUTPUT(HEATER_2_PIN);\r
+  #endif  \r
+\r
+  #ifdef PIDTEMP\r
+    temp_iState_min = 0.0;\r
+    temp_iState_max = PID_INTEGRAL_DRIVE_MAX / Ki;\r
+  #endif //PIDTEMP\r
+\r
+  // Set analog inputs\r
   ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;\r
   \r
-// Use timer0 for temperature measurement\r
-// Interleave temperature interrupt with millies interrupt\r
+  // Use timer0 for temperature measurement\r
+  // Interleave temperature interrupt with millies interrupt\r
   OCR0B = 128;\r
   TIMSK0 |= (1<<OCIE0B);  \r
 }\r
@@ -344,23 +343,25 @@ void setWatch()
 \r
 void disable_heater()\r
 {\r
-   #if TEMP_0_PIN > -1\r
+  #if TEMP_0_PIN > -1\r
   target_raw[0]=0;\r
    #if HEATER_0_PIN > -1  \r
      WRITE(HEATER_0_PIN,LOW);\r
    #endif\r
   #endif\r
+     \r
   #if TEMP_1_PIN > -1\r
-  target_raw[1]=0;\r
-  #if HEATER_1_PIN > -1 \r
-    WRITE(HEATER_1_PIN,LOW);\r
-  #endif\r
+    target_raw[1]=0;\r
+    #if HEATER_1_PIN > -1 \r
+      WRITE(HEATER_1_PIN,LOW);\r
+    #endif\r
   #endif\r
+      \r
   #if TEMP_2_PIN > -1\r
-  target_raw[2]=0;\r
-  #if HEATER_2_PIN > -1  \r
-    WRITE(HEATER_2_PIN,LOW);\r
-  #endif\r
+    target_raw[2]=0;\r
+    #if HEATER_2_PIN > -1  \r
+      WRITE(HEATER_2_PIN,LOW);\r
+    #endif\r
   #endif \r
 }\r
 \r
@@ -376,75 +377,75 @@ ISR(TIMER0_COMPB_vect)
   \r
   switch(temp_state) {\r
     case 0: // Prepare TEMP_0\r
-            #if (TEMP_0_PIN > -1)\r
-              #if TEMP_0_PIN < 8\r
-                DIDR0 = 1 << TEMP_0_PIN; \r
-              #else\r
-                DIDR2 = 1<<(TEMP_0_PIN - 8); \r
-                ADCSRB = 1<<MUX5;\r
-              #endif\r
-              ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));\r
-              ADCSRA |= 1<<ADSC; // Start conversion\r
-            #endif\r
-            #ifdef ULTIPANEL\r
-              buttons_check();\r
-            #endif\r
-            temp_state = 1;\r
-            break;\r
+      #if (TEMP_0_PIN > -1)\r
+        #if TEMP_0_PIN < 8\r
+          DIDR0 = 1 << TEMP_0_PIN; \r
+        #else\r
+          DIDR2 = 1<<(TEMP_0_PIN - 8); \r
+          ADCSRB = 1<<MUX5;\r
+        #endif\r
+        ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));\r
+        ADCSRA |= 1<<ADSC; // Start conversion\r
+      #endif\r
+      #ifdef ULTIPANEL\r
+        buttons_check();\r
+      #endif\r
+      temp_state = 1;\r
+      break;\r
     case 1: // Measure TEMP_0\r
-            #if (TEMP_0_PIN > -1)\r
-              raw_temp_0_value += ADC;\r
-            #endif\r
-            temp_state = 2;\r
-            break;\r
+      #if (TEMP_0_PIN > -1)\r
+        raw_temp_0_value += ADC;\r
+      #endif\r
+      temp_state = 2;\r
+      break;\r
     case 2: // Prepare TEMP_1\r
-            #if (TEMP_1_PIN > -1)\r
-              #if TEMP_1_PIN < 7\r
-                DIDR0 = 1<<TEMP_1_PIN; \r
-              #else\r
-                DIDR2 = 1<<(TEMP_1_PIN - 8); \r
-                ADCSRB = 1<<MUX5;\r
-              #endif\r
-              ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));\r
-              ADCSRA |= 1<<ADSC; // Start conversion\r
-            #endif\r
-            #ifdef ULTIPANEL\r
-              buttons_check();\r
-            #endif\r
-            temp_state = 3;\r
-            break;\r
+      #if (TEMP_1_PIN > -1)\r
+        #if TEMP_1_PIN < 7\r
+          DIDR0 = 1<<TEMP_1_PIN; \r
+        #else\r
+          DIDR2 = 1<<(TEMP_1_PIN - 8); \r
+          ADCSRB = 1<<MUX5;\r
+        #endif\r
+        ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));\r
+        ADCSRA |= 1<<ADSC; // Start conversion\r
+      #endif\r
+      #ifdef ULTIPANEL\r
+        buttons_check();\r
+      #endif\r
+      temp_state = 3;\r
+      break;\r
     case 3: // Measure TEMP_1\r
-            #if (TEMP_1_PIN > -1)\r
-              raw_temp_1_value += ADC;\r
-            #endif\r
-            temp_state = 4;\r
-            break;\r
+      #if (TEMP_1_PIN > -1)\r
+        raw_temp_1_value += ADC;\r
+      #endif\r
+      temp_state = 4;\r
+      break;\r
     case 4: // Prepare TEMP_2\r
-            #if (TEMP_2_PIN > -1)\r
-              #if TEMP_2_PIN < 7\r
-                DIDR0 = 1 << TEMP_2_PIN; \r
-              #else\r
-                DIDR2 = 1<<(TEMP_2_PIN - 8); \r
-                ADCSRB = 1<<MUX5;\r
-              #endif\r
-              ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));\r
-              ADCSRA |= 1<<ADSC; // Start conversion\r
-            #endif\r
-            #ifdef ULTIPANEL\r
-              buttons_check();\r
-            #endif\r
-            temp_state = 5;\r
-            break;\r
+      #if (TEMP_2_PIN > -1)\r
+        #if TEMP_2_PIN < 7\r
+          DIDR0 = 1 << TEMP_2_PIN; \r
+        #else\r
+          DIDR2 = 1<<(TEMP_2_PIN - 8); \r
+          ADCSRB = 1<<MUX5;\r
+        #endif\r
+        ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));\r
+        ADCSRA |= 1<<ADSC; // Start conversion\r
+      #endif\r
+      #ifdef ULTIPANEL\r
+        buttons_check();\r
+      #endif\r
+      temp_state = 5;\r
+      break;\r
     case 5: // Measure TEMP_2\r
-            #if (TEMP_2_PIN > -1)\r
-              raw_temp_2_value += ADC;\r
-            #endif\r
-            temp_state = 0;\r
-            temp_count++;\r
-            break;\r
+      #if (TEMP_2_PIN > -1)\r
+        raw_temp_2_value += ADC;\r
+      #endif\r
+      temp_state = 0;\r
+      temp_count++;\r
+      break;\r
     default:\r
-            SERIAL_ERRORLN("Temp measurement error!");\r
-            break;\r
+      SERIAL_ERRORLN("Temp measurement error!");\r
+      break;\r
   }\r
     \r
   if(temp_count >= 16) // 6 ms * 16 = 96ms.\r
@@ -472,67 +473,71 @@ ISR(TIMER0_COMPB_vect)
     raw_temp_0_value = 0;\r
     raw_temp_1_value = 0;\r
     raw_temp_2_value = 0;\r
-#ifdef HEATER_0_MAXTEMP\r
-  #if (HEATER_0_PIN > -1)\r
-    if(current_raw[TEMPSENSOR_HOTEND_0] >= maxttemp_0) {\r
-      target_raw[TEMPSENSOR_HOTEND_0] = 0;\r
-      analogWrite(HEATER_0_PIN, 0);\r
-      SERIAL_ERRORLN("Temperature extruder 0 switched off. MAXTEMP triggered !!");\r
-      kill();\r
-    }\r
-  #endif\r
-#endif\r
-#ifdef HEATER_1_MAXTEMP\r
-  #if (HEATER_1_PIN > -1)\r
-    if(current_raw[TEMPSENSOR_HOTEND_1] >= maxttemp_1) {\r
-      target_raw[TEMPSENSOR_HOTEND_1] = 0;\r
-    if(current_raw[2] >= maxttemp_1) {\r
-      analogWrite(HEATER_2_PIN, 0);\r
-      SERIAL_ERRORLN("Temperature extruder 1 switched off. MAXTEMP triggered !!");\r
-      kill()\r
-    }\r
-  #endif\r
-#endif //MAXTEMP\r
-#ifdef HEATER_0_MINTEMP\r
-  #if (HEATER_0_PIN > -1)\r
-    if(current_raw[TEMPSENSOR_HOTEND_0] <= minttemp_0) {\r
-      target_raw[TEMPSENSOR_HOTEND_0] = 0;\r
-      analogWrite(HEATER_0_PIN, 0);\r
-      SERIAL_ERRORLN("Temperature extruder 0 switched off. MINTEMP triggered !!");\r
-      kill();\r
-    }\r
-  #endif\r
-#endif\r
-#ifdef HEATER_1_MINTEMP\r
-  #if (HEATER_2_PIN > -1)\r
-    if(current_raw[TEMPSENSOR_HOTEND_1] <= minttemp_1) {\r
-      target_raw[TEMPSENSOR_HOTEND_1] = 0;\r
-      analogWrite(HEATER_2_PIN, 0);\r
-      SERIAL_ERRORLN("Temperature extruder 1 switched off. MINTEMP triggered !!");\r
-      kill();\r
-    }\r
+    #ifdef HEATER_0_MAXTEMP\r
+      #if (HEATER_0_PIN > -1)\r
+        if(current_raw[TEMPSENSOR_HOTEND_0] >= maxttemp_0) {\r
+          target_raw[TEMPSENSOR_HOTEND_0] = 0;\r
+          analogWrite(HEATER_0_PIN, 0);\r
+          SERIAL_ERRORLN("Temperature extruder 0 switched off. MAXTEMP triggered !!");\r
+          kill();\r
+        }\r
+      #endif\r
+    #endif\r
+  #ifdef HEATER_1_MAXTEMP\r
+    #if (HEATER_1_PIN > -1)\r
+      if(current_raw[TEMPSENSOR_HOTEND_1] >= maxttemp_1) {\r
+        target_raw[TEMPSENSOR_HOTEND_1] = 0;\r
+      if(current_raw[2] >= maxttemp_1) {\r
+        analogWrite(HEATER_2_PIN, 0);\r
+        SERIAL_ERRORLN("Temperature extruder 1 switched off. MAXTEMP triggered !!");\r
+        kill()\r
+      }\r
+    #endif\r
+  #endif //MAXTEMP\r
+  \r
+  #ifdef HEATER_0_MINTEMP\r
+    #if (HEATER_0_PIN > -1)\r
+      if(current_raw[TEMPSENSOR_HOTEND_0] <= minttemp_0) {\r
+        target_raw[TEMPSENSOR_HOTEND_0] = 0;\r
+        analogWrite(HEATER_0_PIN, 0);\r
+        SERIAL_ERRORLN("Temperature extruder 0 switched off. MINTEMP triggered !!");\r
+        kill();\r
+      }\r
+    #endif\r
   #endif\r
-#endif //MAXTEMP\r
-#ifdef BED_MINTEMP\r
-  #if (HEATER_1_PIN > -1)\r
-    if(current_raw[1] <= bed_minttemp) {\r
-      target_raw[1] = 0;\r
-      WRITE(HEATER_1_PIN, 0);\r
-      SERIAL_ERRORLN("Temperatur heated bed switched off. MINTEMP triggered !!");\r
-      kill();\r
-    }\r
+  \r
+  #ifdef HEATER_1_MINTEMP\r
+    #if (HEATER_2_PIN > -1)\r
+      if(current_raw[TEMPSENSOR_HOTEND_1] <= minttemp_1) {\r
+        target_raw[TEMPSENSOR_HOTEND_1] = 0;\r
+        analogWrite(HEATER_2_PIN, 0);\r
+        SERIAL_ERRORLN("Temperature extruder 1 switched off. MINTEMP triggered !!");\r
+        kill();\r
+      }\r
+    #endif\r
+  #endif //MAXTEMP\r
+  \r
+  #ifdef BED_MINTEMP\r
+    #if (HEATER_1_PIN > -1)\r
+      if(current_raw[1] <= bed_minttemp) {\r
+        target_raw[1] = 0;\r
+        WRITE(HEATER_1_PIN, 0);\r
+        SERIAL_ERRORLN("Temperatur heated bed switched off. MINTEMP triggered !!");\r
+        kill();\r
+      }\r
+    #endif\r
   #endif\r
-#endif\r
-#ifdef BED_MAXTEMP\r
-  #if (HEATER_1_PIN > -1)\r
-    if(current_raw[1] >= bed_maxttemp) {\r
-      target_raw[1] = 0;\r
-      WRITE(HEATER_1_PIN, 0);\r
-      SERIAL_ERRORLN("Temperature heated bed switched off. MAXTEMP triggered !!");\r
-      kill();\r
-    }\r
+  \r
+  #ifdef BED_MAXTEMP\r
+    #if (HEATER_1_PIN > -1)\r
+      if(current_raw[1] >= bed_maxttemp) {\r
+        target_raw[1] = 0;\r
+        WRITE(HEATER_1_PIN, 0);\r
+        SERIAL_ERRORLN("Temperature heated bed switched off. MAXTEMP triggered !!");\r
+        kill();\r
+      }\r
+    #endif\r
   #endif\r
-#endif\r
   }\r
 }\r
 \r
index 44d54db141e5bcda9a3116a54e9cfab3d052343d..9c4b59a241c077e171d492eb44c7488509102adc 100644 (file)
   #include "stepper.h"\r
 #endif\r
 \r
+// public functions\r
 void tp_init();  //initialise the heating\r
 void manage_heater(); //it is critical that this is called periodically.\r
 \r
+\r
 enum TempSensor {TEMPSENSOR_HOTEND_0=0,TEMPSENSOR_BED=1, TEMPSENSOR_HOTEND_1=2};\r
 \r
 //low leven conversion routines\r
@@ -41,9 +43,11 @@ float analog2tempBed(int raw);
 extern int target_raw[3];  \r
 extern int current_raw[3];\r
 extern float Kp,Ki,Kd,Kc;\r
+\r
 #ifdef PIDTEMP\r
   extern float pid_setpoint ;\r
 #endif\r
+  \r
 #ifdef WATCHPERIOD\r
   extern int watch_raw[3] ;\r
   extern unsigned long watchmillis;\r
@@ -63,15 +67,15 @@ inline float degTargetHotend0() {  return analog2temp(target_raw[TEMPSENSOR_HOTE
 inline float degTargetHotend1() {  return analog2temp(target_raw[TEMPSENSOR_HOTEND_1]);};\r
 inline float degTargetBed() {   return analog2tempBed(target_raw[TEMPSENSOR_BED]);};\r
 \r
-inline void setTargetHotend0(float celsius) \r
+inline void setTargetHotend0(const float &celsius) \r
 {  \r
   target_raw[TEMPSENSOR_HOTEND_0]=temp2analog(celsius);\r
   #ifdef PIDTEMP\r
     pid_setpoint = celsius;\r
   #endif //PIDTEMP\r
 };\r
-inline void setTargetHotend1(float celsius) {  target_raw[TEMPSENSOR_HOTEND_1]=temp2analog(celsius);};\r
-inline void setTargetBed(float celsius)     {  target_raw[TEMPSENSOR_BED     ]=temp2analogBed(celsius);};\r
+inline void setTargetHotend1(const float &celsius) {  target_raw[TEMPSENSOR_HOTEND_1]=temp2analog(celsius);};\r
+inline void setTargetBed(const float &celsius)     {  target_raw[TEMPSENSOR_BED     ]=temp2analogBed(celsius);};\r
 \r
 inline bool isHeatingHotend0() {return target_raw[TEMPSENSOR_HOTEND_0] > current_raw[TEMPSENSOR_HOTEND_0];};\r
 inline bool isHeatingHotend1() {return target_raw[TEMPSENSOR_HOTEND_1] > current_raw[TEMPSENSOR_HOTEND_1];};\r
@@ -84,16 +88,5 @@ inline bool isCoolingBed() {return target_raw[TEMPSENSOR_BED] < current_raw[TEMP
 void disable_heater();\r
 void setWatch();\r
 \r
-#ifdef HEATER_0_USES_THERMISTOR\r
-    #define HEATERSOURCE 1\r
-#endif\r
-#ifdef BED_USES_THERMISTOR\r
-    #define BEDSOURCE 1\r
-#endif\r
-\r
-\r
-\r
-\r
-\r
 #endif\r
 \r
index b7092c9cb1d00ef81aa32f7ed93cec4c19581562..18d1b7f24943e8698803ef14a71f222a2daa621c 100644 (file)
   void lcd_status(const char* message);\r
   void beep();\r
   void buttons_check();\r
-  #define LCDSTATUSRIGHT\r
+\r
 \r
   #define LCD_UPDATE_INTERVAL 100\r
   #define STATUSTIMEOUT 15000\r
 \r
-  #include "Configuration.h"\r
 \r
   #include <LiquidCrystal.h>\r
   extern LiquidCrystal lcd;\r
 \r
-  //lcd display size\r
-\r
-#ifdef NEWPANEL\r
- //arduino pin witch triggers an piezzo beeper\r
-  #define BEEPER 18\r
 \r
-  #define LCD_PINS_RS 20 \r
-  #define LCD_PINS_ENABLE 17\r
-  #define LCD_PINS_D4 16\r
-  #define LCD_PINS_D5 21 \r
-  #define LCD_PINS_D6 5\r
-  #define LCD_PINS_D7 6\r
-  \r
-  //buttons are directly attached\r
-  #define BTN_EN1 40\r
-  #define BTN_EN2 42\r
-  #define BTN_ENC 19  //the click\r
-  \r
-  #define BLEN_C 2\r
-  #define BLEN_B 1\r
-  #define BLEN_A 0\r
-  \r
-  #define SDCARDDETECT 38\r
-  \r
-  #define EN_C (1<<BLEN_C)\r
-  #define EN_B (1<<BLEN_B)\r
-  #define EN_A (1<<BLEN_A)\r
-  \r
-   //encoder rotation values\r
-  #define encrot0 0\r
-  #define encrot1 2\r
-  #define encrot2 3\r
-  #define encrot3 1\r
+  #ifdef NEWPANEL\r
 \r
-  \r
-  #define CLICKED (buttons&EN_C)\r
-  #define BLOCK {blocking=millis()+blocktime;}\r
-  #define CARDINSERTED (READ(SDCARDDETECT)==0)\r
-  \r
-#else\r
-  //arduino pin witch triggers an piezzo beeper\r
-  #define BEEPER 18\r
-\r
-  //buttons are attached to a shift register\r
-  #define SHIFT_CLK 38\r
-  #define SHIFT_LD 42\r
-  #define SHIFT_OUT 40\r
-  #define SHIFT_EN 17\r
-  \r
-  #define LCD_PINS_RS 16 \r
-  #define LCD_PINS_ENABLE 5\r
-  #define LCD_PINS_D4 6\r
-  #define LCD_PINS_D5 21 \r
-  #define LCD_PINS_D6 20\r
-  #define LCD_PINS_D7 19\r
-  \r
-   //bits in the shift register that carry the buttons for:\r
-  // left up center down right red\r
-  #define BL_LE 7\r
-  #define BL_UP 6\r
-  #define BL_MI 5\r
-  #define BL_DW 4\r
-  #define BL_RI 3\r
-  #define BL_ST 2\r
-\r
-  #define BLEN_B 1\r
-  #define BLEN_A 0\r
-\r
-  //encoder rotation values\r
-  #define encrot0 0\r
-  #define encrot1 2\r
-  #define encrot2 3\r
-  #define encrot3 1\r
-\r
-  //atomatic, do not change\r
-  #define B_LE (1<<BL_LE)\r
-  #define B_UP (1<<BL_UP)\r
-  #define B_MI (1<<BL_MI)\r
-  #define B_DW (1<<BL_DW)\r
-  #define B_RI (1<<BL_RI)\r
-  #define B_ST (1<<BL_ST)\r
-  #define EN_B (1<<BLEN_B)\r
-  #define EN_A (1<<BLEN_A)\r
-  \r
-  #define CLICKED ((buttons&B_MI)||(buttons&B_ST))\r
-  #define BLOCK {blocking[BL_MI]=millis()+blocktime;blocking[BL_ST]=millis()+blocktime;}\r
-  \r
-#endif\r
+    \r
+    #define EN_C (1<<BLEN_C)\r
+    #define EN_B (1<<BLEN_B)\r
+    #define EN_A (1<<BLEN_A)\r
+    \r
+    #define CLICKED (buttons&EN_C)\r
+    #define BLOCK {blocking=millis()+blocktime;}\r
+    #define CARDINSERTED (READ(SDCARDDETECT)==0)\r
+    \r
+  #else\r
+\r
+    //atomatic, do not change\r
+    #define B_LE (1<<BL_LE)\r
+    #define B_UP (1<<BL_UP)\r
+    #define B_MI (1<<BL_MI)\r
+    #define B_DW (1<<BL_DW)\r
+    #define B_RI (1<<BL_RI)\r
+    #define B_ST (1<<BL_ST)\r
+    #define EN_B (1<<BLEN_B)\r
+    #define EN_A (1<<BLEN_A)\r
+    \r
+    #define CLICKED ((buttons&B_MI)||(buttons&B_ST))\r
+    #define BLOCK {blocking[BL_MI]=millis()+blocktime;blocking[BL_ST]=millis()+blocktime;}\r
+    \r
+  #endif\r
+    \r
   // blocking time for recognizing a new keypress of one key, ms\r
-#define blocktime 500\r
-#define lcdslow 5\r
+  #define blocktime 500\r
+  #define lcdslow 5\r
+    \r
   enum MainStatus{Main_Status, Main_Menu, Main_Prepare, Main_Control, Main_SD};\r
 \r
   class MainMenu{\r
     bool linechanging;\r
   };\r
 \r
+  //conversion routines, could need some overworking\r
   char *fillto(int8_t n,char *c);\r
   char *ftostr51(const float &x);\r
   char *ftostr31(const float &x);\r
 #else //no lcd\r
   #define LCD_STATUS\r
   #define LCD_MESSAGE(x)\r
+  inline void lcd_status() {};\r
 #endif\r
   \r
 #ifndef ULTIPANEL  \r
  #define CLICKED false\r
-#define BLOCK ;\r
+  #define BLOCK ;\r
 #endif \r
+  \r
+  \r
+  \r
 #endif //ULTRALCD\r
 \r
index bd256100ab4c9709214b6c4f4cd3cec260011e65..a45b3683e7cf91c688934e408a13212924d4c665 100644 (file)
@@ -1,7 +1,7 @@
 #include "ultralcd.h"\r
+#ifdef ULTRA_LCD\r
 \r
 \r
-#ifdef ULTRA_LCD\r
 extern volatile int feedmultiply;\r
 extern long position[4];   \r
 \r
@@ -122,58 +122,57 @@ void lcd_status()
   menu.update();\r
 }\r
 #ifdef ULTIPANEL  \r
+\r
+\r
 void buttons_init()\r
 {\r
-#ifdef NEWPANEL\r
-  pinMode(BTN_EN1,INPUT);\r
-  pinMode(BTN_EN2,INPUT); \r
-  pinMode(BTN_ENC,INPUT); \r
-  pinMode(SDCARDDETECT,INPUT);\r
-  WRITE(BTN_EN1,HIGH);\r
-  WRITE(BTN_EN2,HIGH);\r
-  WRITE(BTN_ENC,HIGH);\r
-  WRITE(SDCARDDETECT,HIGH);\r
-#else\r
-  pinMode(SHIFT_CLK,OUTPUT);\r
-  pinMode(SHIFT_LD,OUTPUT);\r
-  pinMode(SHIFT_EN,OUTPUT);\r
-  pinMode(SHIFT_OUT,INPUT);\r
-  WRITE(SHIFT_OUT,HIGH);\r
-  WRITE(SHIFT_LD,HIGH); \r
-  WRITE(SHIFT_EN,LOW); \r
-#endif\r
+  #ifdef NEWPANEL\r
+    pinMode(BTN_EN1,INPUT);\r
+    pinMode(BTN_EN2,INPUT); \r
+    pinMode(BTN_ENC,INPUT); \r
+    pinMode(SDCARDDETECT,INPUT);\r
+    WRITE(BTN_EN1,HIGH);\r
+    WRITE(BTN_EN2,HIGH);\r
+    WRITE(BTN_ENC,HIGH);\r
+    WRITE(SDCARDDETECT,HIGH);\r
+  #else\r
+    pinMode(SHIFT_CLK,OUTPUT);\r
+    pinMode(SHIFT_LD,OUTPUT);\r
+    pinMode(SHIFT_EN,OUTPUT);\r
+    pinMode(SHIFT_OUT,INPUT);\r
+    WRITE(SHIFT_OUT,HIGH);\r
+    WRITE(SHIFT_LD,HIGH); \r
+    WRITE(SHIFT_EN,LOW); \r
+  #endif\r
 }\r
 \r
 \r
 void buttons_check()\r
 {\r
-//  volatile static bool busy=false;\r
-//  if(busy) \r
-//    return;\r
-//  busy=true;\r
   \r
-#ifdef NEWPANEL\r
-  uint8_t newbutton=0;\r
-  if(READ(BTN_EN1)==0)  newbutton|=EN_A;\r
-  if(READ(BTN_EN2)==0)  newbutton|=EN_B;\r
-  if((blocking<millis()) &&(READ(BTN_ENC)==0))\r
-    newbutton|=EN_C;\r
-  buttons=newbutton;\r
-#else   //read it from the shift register\r
-  uint8_t newbutton=0;\r
-  WRITE(SHIFT_LD,LOW);\r
-  WRITE(SHIFT_LD,HIGH);\r
-  unsigned char tmp_buttons=0;\r
-  for(unsigned char i=0;i<8;i++)\r
-  { \r
-    newbutton = newbutton>>1;\r
-    if(READ(SHIFT_OUT))\r
-      newbutton|=(1<<7);\r
-    WRITE(SHIFT_CLK,HIGH);\r
-    WRITE(SHIFT_CLK,LOW);\r
-  }\r
-  buttons=~newbutton; //invert it, because a pressed switch produces a logical 0\r
-#endif\r
+  #ifdef NEWPANEL\r
+    uint8_t newbutton=0;\r
+    if(READ(BTN_EN1)==0)  newbutton|=EN_A;\r
+    if(READ(BTN_EN2)==0)  newbutton|=EN_B;\r
+    if((blocking<millis()) &&(READ(BTN_ENC)==0))\r
+      newbutton|=EN_C;\r
+    buttons=newbutton;\r
+  #else   //read it from the shift register\r
+    uint8_t newbutton=0;\r
+    WRITE(SHIFT_LD,LOW);\r
+    WRITE(SHIFT_LD,HIGH);\r
+    unsigned char tmp_buttons=0;\r
+    for(unsigned char i=0;i<8;i++)\r
+    { \r
+      newbutton = newbutton>>1;\r
+      if(READ(SHIFT_OUT))\r
+        newbutton|=(1<<7);\r
+      WRITE(SHIFT_CLK,HIGH);\r
+      WRITE(SHIFT_CLK,LOW);\r
+    }\r
+    buttons=~newbutton; //invert it, because a pressed switch produces a logical 0\r
+  #endif\r
+  \r
   char enc=0;\r
   if(buttons&EN_A)\r
     enc|=(1<<0);\r
@@ -212,7 +211,6 @@ void buttons_check()
     }\r
   }\r
   lastenc=enc;\r
-//  busy=false;\r
 }\r
 \r
 #endif\r
@@ -223,9 +221,9 @@ MainMenu::MainMenu()
   displayStartingRow=0;\r
   activeline=0;\r
   force_lcd_update=true;\r
-#ifdef ULTIPANEL\r
-  buttons_init();\r
-#endif\r
+  #ifdef ULTIPANEL\r
+    buttons_init();\r
+  #endif\r
   lcd_init();\r
   linechanging=false;\r
 }\r
@@ -1154,12 +1152,13 @@ uint8_t getnrfilenames()
     cnt++;\r
   }\r
   return cnt;\r
+#else\r
+  return 0;\r
 #endif\r
 }\r
 \r
 void MainMenu::showSD()\r
 {\r
-\r
 #ifdef SDSUPPORT\r
  uint8_t line=0;\r
 \r
@@ -1205,11 +1204,11 @@ void MainMenu::showSD()
         if(force_lcd_update)\r
         {\r
           lcd.setCursor(0,line);\r
-#ifdef CARDINSERTED\r
+           #ifdef CARDINSERTED\r
           if(CARDINSERTED)\r
-#else\r
+          #else\r
           if(true)\r
-#endif\r
+          #endif\r
           {\r
             lcd.print(" \004Refresh");\r
           }\r
@@ -1306,9 +1305,9 @@ void MainMenu::showMainMenu()
 {\r
    //if(int(encoderpos/lcdslow)!=int(lastencoderpos/lcdslow))\r
    //  force_lcd_update=true;\r
-#ifndef ULTIPANEL\r
-   force_lcd_update=false;\r
-#endif\r
+  #ifndef ULTIPANEL\r
+    force_lcd_update=false;\r
+  #endif\r
    //Serial.println((int)activeline);\r
    if(force_lcd_update)\r
      clear();\r
@@ -1347,17 +1346,17 @@ void MainMenu::showMainMenu()
           beepshort();\r
         }\r
       }break;\r
-#ifdef SDSUPPORT\r
+      #ifdef SDSUPPORT\r
       case ItemM_file:    \r
       {\r
         if(force_lcd_update) \r
         {\r
           lcd.setCursor(0,line);\r
-#ifdef CARDINSERTED\r
-          if(CARDINSERTED)\r
-#else\r
-          if(true)\r
-#endif\r
+          #ifdef CARDINSERTED\r
+            if(CARDINSERTED)\r
+          #else\r
+            if(true)\r
+          #endif\r
           {\r
             if(sdmode)\r
               lcd.print(" Stop Print   \x7E");\r
@@ -1370,7 +1369,7 @@ void MainMenu::showMainMenu()
           }\r
         }\r
         #ifdef CARDINSERTED\r
-        if(CARDINSERTED)\r
+          if(CARDINSERTED)\r
         #endif\r
         if((activeline==line)&&CLICKED)\r
         {\r
@@ -1380,28 +1379,30 @@ void MainMenu::showMainMenu()
           beepshort();\r
         }\r
       }break;\r
-#endif\r
+      #endif\r
       default: \r
         SERIAL_ERRORLN("Something is wrong in the MenuStructure.");\r
       break;\r
     }\r
   }\r
-  if(activeline<0) activeline=0;\r
-  if(activeline>=LCD_HEIGHT) activeline=LCD_HEIGHT-1;\r
+  if(activeline<0) \r
+    activeline=0;\r
+  if(activeline>=LCD_HEIGHT) \r
+    activeline=LCD_HEIGHT-1;\r
   if((encoderpos!=lastencoderpos)||force_lcd_update)\r
   {\r
     lcd.setCursor(0,activeline);lcd.print(activeline?' ':' ');\r
     if(encoderpos<0) encoderpos=0;\r
-    if(encoderpos>3*lcdslow) encoderpos=3*lcdslow;\r
+    if(encoderpos>3*lcdslow) \r
+      encoderpos=3*lcdslow;\r
     activeline=abs(encoderpos/lcdslow)%LCD_HEIGHT;\r
-     if(activeline<0) activeline=0;\r
-  if(activeline>=LCD_HEIGHT) activeline=LCD_HEIGHT-1;\r
+    if(activeline<0) \r
+      activeline=0;\r
+    if(activeline>=LCD_HEIGHT) \r
+      activeline=LCD_HEIGHT-1;\r
     lastencoderpos=encoderpos;\r
     lcd.setCursor(0,activeline);lcd.print(activeline?'>':'\003');\r
   }\r
-\r
-  \r
-  \r
 }\r
 \r
 void MainMenu::update()\r
@@ -1409,25 +1410,24 @@ void MainMenu::update()
   static MainStatus oldstatus=Main_Menu;  //init automatically causes foce_lcd_update=true\r
   static long timeoutToStatus=0;\r
   static bool oldcardstatus=false;\r
-#ifdef CARDINSERTED\r
-  if((CARDINSERTED != oldcardstatus))\r
-  {\r
-    force_lcd_update=true;\r
-    oldcardstatus=CARDINSERTED;\r
-    //Serial.println("echo: SD CHANGE");\r
-    if(CARDINSERTED)\r
-    {\r
-      initsd();\r
-      lcd_status("Card inserted");\r
-    }\r
-    else\r
+  #ifdef CARDINSERTED\r
+    if((CARDINSERTED != oldcardstatus))\r
     {\r
-      sdactive=false;\r
-      lcd_status("Card removed");\r
-      \r
+      force_lcd_update=true;\r
+      oldcardstatus=CARDINSERTED;\r
+      //Serial.println("echo: SD CHANGE");\r
+      if(CARDINSERTED)\r
+      {\r
+        initsd();\r
+        lcd_status("Card inserted");\r
+      }\r
+      else\r
+      {\r
+        sdactive=false;\r
+        lcd_status("Card removed");\r
+      }\r
     }\r
-  }\r
-#endif\r
+  #endif\r
  \r
   if(status!=oldstatus)\r
   {\r
@@ -1484,9 +1484,9 @@ void MainMenu::update()
 \r
 \r
 //return for string conversion routines\r
-char conv[8];\r
+static char conv[8];\r
 \r
-///  convert float to string with +123.4 format\r
+//  convert float to string with +123.4 format\r
 char *ftostr3(const float &x)\r
 {\r
   //sprintf(conv,"%5.1f",x);\r
@@ -1497,6 +1497,7 @@ char *ftostr3(const float &x)
   conv[3]=0;\r
   return conv;\r
 }\r
+\r
 char *itostr2(const uint8_t &x)\r
 {\r
   //sprintf(conv,"%5.1f",x);\r
@@ -1506,10 +1507,10 @@ char *itostr2(const uint8_t &x)
   conv[2]=0;\r
   return conv;\r
 }\r
-///  convert float to string with +123.4 format\r
+\r
+//  convert float to string with +123.4 format\r
 char *ftostr31(const float &x)\r
 {\r
-  //sprintf(conv,"%5.1f",x);\r
   int xx=x*10;\r
   conv[0]=(xx>=0)?'+':'-';\r
   xx=abs(xx);\r
@@ -1524,7 +1525,6 @@ char *ftostr31(const float &x)
 \r
 char *itostr31(const int &xx)\r
 {\r
-  //sprintf(conv,"%5.1f",x);\r
   conv[0]=(xx>=0)?'+':'-';\r
   conv[1]=(xx/1000)%10+'0';\r
   conv[2]=(xx/100)%10+'0';\r
@@ -1534,6 +1534,7 @@ char *itostr31(const int &xx)
   conv[6]=0;\r
   return conv;\r
 }\r
+\r
 char *itostr3(const int &xx)\r
 {\r
   conv[0]=(xx/100)%10+'0';\r
@@ -1553,7 +1554,7 @@ char *itostr4(const int &xx)
   return conv;\r
 }\r
 \r
-///  convert float to string with +1234.5 format\r
+//  convert float to string with +1234.5 format\r
 char *ftostr51(const float &x)\r
 {\r
   int xx=x*10;\r
@@ -1587,11 +1588,9 @@ char *fillto(int8_t n,char *c)
   }\r
   ret[n]=0;\r
   return ret;\r
-  \r
 }\r
 \r
-#else\r
-inline void lcd_status() {};\r
-#endif\r
+\r
+#endif //ULTRA_LCD\r
 \r
 \r
index 7dd31f9f7e4117d5dd570b83d7595e50cd7168cb..44ee9de35d927d22edbb92a0ed0447177f7e9589 100644 (file)
@@ -1,13 +1,16 @@
 #ifndef __WATCHDOGH
 #define __WATCHDOGH
 #include "Configuration.h"
-//#ifdef USE_WATCHDOG
+#ifdef USE_WATCHDOG
 
-/// intialise watch dog with a 1 sec interrupt time
-void wd_init();
-/// pad the dog/reset watchdog. MUST be called at least every second after the first wd_init or avr will go into emergency procedures..
-void wd_reset();
+  // intialise watch dog with a 1 sec interrupt time
+  void wd_init();
+  // pad the dog/reset watchdog. MUST be called at least every second after the first wd_init or avr will go into emergency procedures..
+  void wd_reset();
 
-//#endif
+#else
+  inline void wd_init() {};
+  inline void wd_reset() {};
+#endif
 
 #endif
index 09d881d1d0ecd34fdac356bb447cfe04e7e88c2e..71868cec5ba50f0d4e01c267449b729a8072c56c 100644 (file)
@@ -3,7 +3,7 @@
 #include  <avr/wdt.h>
 #include  <avr/interrupt.h>
 
-volatile uint8_t timeout_seconds=0;
+static volatile uint8_t timeout_seconds=0;
 
 void(* ctrlaltdelete) (void) = 0; //does not work on my atmega2560