浏览代码

syncing with upstream

PavelSindler 7 年之前
父节点
当前提交
c76a9dda64

+ 10 - 3
Firmware/Configuration.h

@@ -5,7 +5,7 @@
 #include "Configuration_prusa.h"
 
 // Firmware version
-#define FW_version "3.0.12-3"
+#define FW_version "3.0.12-7"
 
 #define FW_PRUSA3D_MAGIC "PRUSA3DFW"
 #define FW_PRUSA3D_MAGIC_LEN 10
@@ -49,6 +49,7 @@
 #define EEPROM_CALIBRATION_STATUS_PINDA (EEPROM_BOWDEN_LENGTH - 1) //0 - not calibrated; 1 - calibrated
 #define EEPROM_SD_SORT (EEPROM_CALIBRATION_STATUS_PINDA - 1) //0 -time, 1-alpha, 2-none
 #define EEPROM_XYZ_CAL_SKEW (EEPROM_SD_SORT - 4)
+#define EEPROM_WIZARD_ACTIVE (EEPROM_XYZ_CAL_SKEW - 1)
 
 // Currently running firmware, each digit stored as uint16_t.
 // The flavor differentiates a dev, alpha, beta, release candidate or a release version.
@@ -463,7 +464,13 @@ const bool Z_MAX_ENDSTOP_INVERTING = true; // set to true to invert the logic of
 // please keep turned on if you can.
 //#define EEPROM_CHITCHAT
 
-
+// Host Keepalive
+//
+// When enabled Marlin will send a busy status message to the host
+// every couple of seconds when it can't accept commands.
+//
+#define HOST_KEEPALIVE_FEATURE    // Disable this if your host doesn't like keepalive messages
+#define HOST_KEEPALIVE_INTERVAL 2 // Number of seconds between "busy" messages. Set with M113.
 
 //LCD and SD support
 #define ULTRA_LCD  //general LCD support, also 16x2
@@ -716,7 +723,7 @@ enum CalibrationStatus
 	CALIBRATION_STATUS_ASSEMBLED = 255,
 
 	// For the wizard: self test has been performed, now the XYZ calibration is needed.
-	// CALIBRATION_STATUS_XYZ_CALIBRATION = 250,
+	CALIBRATION_STATUS_XYZ_CALIBRATION = 250,
 
 	// For the wizard: factory assembled, needs to run Z calibration.
 	CALIBRATION_STATUS_Z_CALIBRATION = 240,

+ 2 - 2
Firmware/Configuration_adv.h

@@ -261,7 +261,7 @@
   #define SD_SORT_ALPHA 1
   #define SD_SORT_NONE 2
 
-  #define SDSORT_LIMIT       40    // Maximum number of sorted items (10-256).
+  #define SDSORT_LIMIT       20    // Maximum number of sorted items (10-256).
   #define FOLDER_SORTING     -1     // -1=above  0=none  1=below
   #define SDSORT_GCODE       false  // Allow turning sorting on/off with LCD and M34 g-code.
   #define SDSORT_USES_RAM    true  // Pre-allocate a static array for faster pre-sorting.
@@ -323,7 +323,7 @@
  * K=0 means advance disabled.
  * See Marlin documentation for calibration instructions.
  */
-//#define LIN_ADVANCE
+#define LIN_ADVANCE
 
 #ifdef LIN_ADVANCE
   #define LIN_ADVANCE_K 0 //Try around 45 for PLA, around 25 for ABS.

+ 34 - 2
Firmware/Marlin.h

@@ -109,6 +109,8 @@ FORCE_INLINE void serialprintPGM(const char *str)
   }
 }
 
+#define NOMORE(v,n) do{ if (v > n) v = n; }while(0)
+
 bool is_buffer_empty();
 void get_command();
 void process_commands();
@@ -220,6 +222,7 @@ void enquecommand(const char *cmd, bool from_progmem = false);
 //put an ASCII command at the end of the current buffer, read from flash
 #define enquecommand_P(cmd) enquecommand(cmd, true)
 void enquecommand_front(const char *cmd, bool from_progmem = false);
+bool cmd_buffer_empty();
 //put an ASCII command at the end of the current buffer, read from flash
 #define enquecommand_P(cmd) enquecommand(cmd, true)
 #define enquecommand_front_P(cmd) enquecommand_front(cmd, true)
@@ -281,6 +284,10 @@ extern float retract_length, retract_length_swap, retract_feedrate, retract_zlif
 extern float retract_recover_length, retract_recover_length_swap, retract_recover_feedrate;
 #endif
 
+#ifdef HOST_KEEPALIVE_FEATURE
+extern uint8_t host_keepalive_interval;
+#endif
+
 extern unsigned long starttime;
 extern unsigned long stoptime;
 extern int bowden_length[4];
@@ -316,8 +323,6 @@ extern void digipot_i2c_set_current( int channel, float current );
 extern void digipot_i2c_init();
 #endif
 
-#endif
-
 //Long pause
 extern int saved_feedmultiply;
 extern float HotendTempBckp;
@@ -350,9 +355,36 @@ float d_ReadData();
 void bed_analysis(float x_dimension, float y_dimension, int x_points_num, int y_points_num, float shift_x, float shift_y);
 
 #endif
+
 float temp_comp_interpolation(float temperature);
 void temp_compensation_apply();
 void temp_compensation_start();
 void wait_for_heater(long codenum);
 void serialecho_temperatures();
 void proc_commands();
+bool check_commands();
+
+#ifdef HOST_KEEPALIVE_FEATURE
+
+// States for managing Marlin and host communication
+// Marlin sends messages if blocked or busy
+enum MarlinBusyState {
+	NOT_BUSY,           // Not in a handler
+	IN_HANDLER,         // Processing a GCode
+	IN_PROCESS,         // Known to be blocking command input (as in G29)
+	PAUSED_FOR_USER,    // Blocking pending any input
+	PAUSED_FOR_INPUT    // Blocking pending text input (concept)
+};
+
+#define KEEPALIVE_STATE(n) do { busy_state = n;} while (0)
+extern void host_keepalive();
+extern MarlinBusyState busy_state;
+
+#endif //HOST_KEEPALIVE_FEATURE
+
+bool gcode_M45(bool onlyZ);
+void gcode_M701();
+
+
+#endif //ifndef marlin.h
+

+ 8 - 4
Firmware/MarlinSerial.cpp

@@ -64,7 +64,7 @@ FORCE_INLINE void store_char(unsigned char c)
           store_char(c);
       }
   }
-  
+#ifndef SNMM
   SIGNAL(USART2_RX_vect)
   {
       if (selectedSerialPort == 1) {
@@ -82,6 +82,7 @@ FORCE_INLINE void store_char(unsigned char c)
       }
   }
 #endif
+#endif
 
 // Constructors ////////////////////////////////////////////////////////////////
 
@@ -122,7 +123,7 @@ void MarlinSerial::begin(long baud)
   sbi(M_UCSRxB, M_TXENx);
   sbi(M_UCSRxB, M_RXCIEx);
   
-  
+#ifndef SNMM
 // set up the second serial port
   if (useU2X) {
         UCSR2A = 1 << U2X2;
@@ -139,6 +140,7 @@ void MarlinSerial::begin(long baud)
     sbi(UCSR2B, RXEN2);
     sbi(UCSR2B, TXEN2);
     sbi(UCSR2B, RXCIE2);
+#endif
 }
 
 void MarlinSerial::end()
@@ -146,10 +148,12 @@ void MarlinSerial::end()
   cbi(M_UCSRxB, M_RXENx);
   cbi(M_UCSRxB, M_TXENx);
   cbi(M_UCSRxB, M_RXCIEx);
-  
+
+#ifndef SNMM
   cbi(UCSR2B, RXEN2);
   cbi(UCSR2B, TXEN2);
-  cbi(UCSR2B, RXCIE2); 
+  cbi(UCSR2B, RXCIE2);
+#endif
 }
 
 

+ 46 - 7
Firmware/MarlinSerial.h

@@ -101,18 +101,56 @@ class MarlinSerial //: public Stream
     {
       return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE;
     }
-    
-    FORCE_INLINE void write(uint8_t c)
-    {
-      while (!((M_UCSRxA) & (1 << M_UDREx)))
-        ;
 
-      M_UDRx = c;
-    }
+	void write(uint8_t c)
+	{
+#ifdef SNMM // don't do the second serial port when multimaterialing
+        while (!((M_UCSRxA) & (1 << M_UDREx)))
+				;
+
+        M_UDRx = c;
+#else
+		if (selectedSerialPort == 0) {
+			while (!((M_UCSRxA) & (1 << M_UDREx)))
+				;
+
+			M_UDRx = c;
+		}
+		else if (selectedSerialPort == 1) {
+			while (!((UCSR2A) & (1 << UDRE2)))
+				;
+
+			UDR2 = c;
+		}
+#endif
+	}
     
     
     void checkRx(void)
     {
+        
+#ifdef SNMM
+        if((M_UCSRxA & (1<<M_RXCx)) != 0) {
+                // Test for a framing error.
+                if (M_UCSRxA & (1<<M_FEx)) {
+                    // Characters received with the framing errors will be ignored.
+                    // The temporary variable "c" was made volatile, so the compiler does not optimize this out.
+                    volatile unsigned char c = M_UDRx;
+                } else {
+                    unsigned char c  =  M_UDRx;
+                    int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
+                    // if we should be storing the received character into the location
+                    // just before the tail (meaning that the head would advance to the
+                    // current location of the tail), we're about to overflow the buffer
+                    // and so we don't write the character or advance the head.
+                    if (i != rx_buffer.tail) {
+                        rx_buffer.buffer[rx_buffer.head] = c;
+                        rx_buffer.head = i;
+                    }
+                    selectedSerialPort = 0;
+                }
+            }
+#else
         if (selectedSerialPort == 0) {
             if((M_UCSRxA & (1<<M_RXCx)) != 0) {
                 // Test for a framing error.
@@ -156,6 +194,7 @@ class MarlinSerial //: public Stream
                 }
             }
         }
+#endif
     }
     
     

+ 343 - 191
Firmware/Marlin_main.cpp

@@ -150,6 +150,7 @@
 //        Rxxx Wait for extruder current temp to reach target temp. Waits when heating and cooling
 //        IF AUTOTEMP is enabled, S<mintemp> B<maxtemp> F<factor>. Exit autotemp by any M109 without F
 // M112 - Emergency stop
+// M113 - Get or set the timeout interval for Host Keepalive "busy" messages
 // M114 - Output current position to serial port
 // M115 - Capabilities string
 // M117 - display message
@@ -372,6 +373,16 @@ int fanSpeed=0;
 
 bool cancel_heatup = false ;
 
+#ifdef HOST_KEEPALIVE_FEATURE
+  
+  MarlinBusyState busy_state = NOT_BUSY;
+  static long prev_busy_signal_ms = -1;
+  uint8_t host_keepalive_interval = HOST_KEEPALIVE_INTERVAL;
+#else
+  #define host_keepalive();
+  #define KEEPALIVE_STATE(n);
+#endif
+
 #ifdef FILAMENT_SENSOR
   //Variables for Filament Sensor input 
   float filament_width_nominal=DEFAULT_NOMINAL_FILAMENT_DIA;  //Set nominal filament width, can be changed with M404 
@@ -761,10 +772,12 @@ void enquecommand(const char *cmd, bool from_progmem)
             strcpy_P(cmdbuffer + bufindw + 1, cmd);
         else
             strcpy(cmdbuffer + bufindw + 1, cmd);
-        SERIAL_ECHO_START;
-        SERIAL_ECHORPGM(MSG_Enqueing);
-        SERIAL_ECHO(cmdbuffer + bufindw + 1);
-        SERIAL_ECHOLNPGM("\"");
+		if (!farm_mode) {
+			SERIAL_ECHO_START;
+			SERIAL_ECHORPGM(MSG_Enqueing);
+			SERIAL_ECHO(cmdbuffer + bufindw + 1);
+			SERIAL_ECHOLNPGM("\"");
+		}
         bufindw += len + 2;
         if (bufindw == sizeof(cmdbuffer))
             bufindw = 0;
@@ -786,6 +799,11 @@ void enquecommand(const char *cmd, bool from_progmem)
     }
 }
 
+bool cmd_buffer_empty()
+{
+	return (buflen == 0);
+}
+
 void enquecommand_front(const char *cmd, bool from_progmem)
 {
     int len = from_progmem ? strlen_P(cmd) : strlen(cmd);
@@ -797,10 +815,12 @@ void enquecommand_front(const char *cmd, bool from_progmem)
         else
             strcpy(cmdbuffer + bufindr + 1, cmd);
         ++ buflen;
-        SERIAL_ECHO_START;
-        SERIAL_ECHOPGM("Enqueing to the front: \"");
-        SERIAL_ECHO(cmdbuffer + bufindr + 1);
-        SERIAL_ECHOLNPGM("\"");
+		if (!farm_mode) {
+			SERIAL_ECHO_START;
+			SERIAL_ECHOPGM("Enqueing to the front: \"");
+			SERIAL_ECHO(cmdbuffer + bufindr + 1);
+			SERIAL_ECHOLNPGM("\"");
+		}
 #ifdef CMDBUFFER_DEBUG
         cmdqueue_dump_to_serial();
 #endif /* CMDBUFFER_DEBUG */
@@ -1026,6 +1046,7 @@ void setup()
 	SERIAL_PROTOCOLLNPGM("start");
 	SERIAL_ECHO_START;
 
+
 #if 0
 	SERIAL_ECHOLN("Reading eeprom from 0 to 100: start");
 	for (int i = 0; i < 4096; ++i) {
@@ -1086,6 +1107,7 @@ void setup()
 	world2machine_reset();
 
 	lcd_init();
+	KEEPALIVE_STATE(PAUSED_FOR_USER);
 	if (!READ(BTN_ENC))
 	{
 		_delay_ms(1000);
@@ -1107,7 +1129,7 @@ void setup()
 
 
 			_delay_ms(2000);
-
+			
 			char level = reset_menu();
 			factory_reset(level, false);
 
@@ -1168,7 +1190,7 @@ void setup()
 #if defined(Z_AXIS_ALWAYS_ON)
 	enable_z();
 #endif
-
+	
 	// Enable Toshiba FlashAir SD card / WiFi enahanced card.
 	card.ToshibaFlashAir_enable(eeprom_read_byte((unsigned char*)EEPROM_TOSHIBA_FLASH_AIR_COMPATIBLITY) == 1);
 	// Force SD card update. Otherwise the SD card update is done from loop() on card.checkautostart(false), 
@@ -1184,6 +1206,8 @@ void setup()
 		// EEPROM_LANG to number lower than 0x0ff.
 		// 1) Set a high power mode.
 		eeprom_write_byte((uint8_t*)EEPROM_SILENT, 0);
+		eeprom_write_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 1); //run wizard
+
 	}
 #ifdef SNMM
 	if (eeprom_read_dword((uint32_t*)EEPROM_BOWDEN_LENGTH) == 0x0ffffffff) { //bowden length used for SNMM
@@ -1214,30 +1238,39 @@ void setup()
 
 #ifndef DEBUG_DISABLE_STARTMSGS
 	check_babystep(); //checking if Z babystep is in allowed range
-	
-  if (calibration_status() == CALIBRATION_STATUS_ASSEMBLED ||
-      calibration_status() == CALIBRATION_STATUS_UNKNOWN) {
-      // Reset the babystepping values, so the printer will not move the Z axis up when the babystepping is enabled.
-      eeprom_update_word((uint16_t*)EEPROM_BABYSTEP_Z, 0);
-      // Show the message.
-      lcd_show_fullscreen_message_and_wait_P(MSG_FOLLOW_CALIBRATION_FLOW);
-  } else if (calibration_status() == CALIBRATION_STATUS_LIVE_ADJUST) {
-      // Show the message.
-      lcd_show_fullscreen_message_and_wait_P(MSG_BABYSTEP_Z_NOT_SET);
-      lcd_update_enable(true);
-  } else if (calibration_status() == CALIBRATION_STATUS_CALIBRATED && temp_cal_active == true && calibration_status_pinda() == false) {
-	  lcd_show_fullscreen_message_and_wait_P(MSG_PINDA_NOT_CALIBRATED);
-	  lcd_update_enable(true);
-  } else if (calibration_status() == CALIBRATION_STATUS_Z_CALIBRATION) {
-      // Show the message.
-      lcd_show_fullscreen_message_and_wait_P(MSG_FOLLOW_CALIBRATION_FLOW);
+
+  if (eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) == 1) {
+	  lcd_wizard(0);
   }
-  for (int i = 0; i<4; i++) EEPROM_read_B(EEPROM_BOWDEN_LENGTH + i * 2, &bowden_length[i]);
-  
-  //If eeprom version for storing parameters to eeprom using M500 changed, default settings are used. Inform user in this case
-  if (!previous_settings_retrieved) {
-	  lcd_show_fullscreen_message_and_wait_P(MSG_DEFAULT_SETTINGS_LOADED);
+  else if (eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) == 0) { //dont show calibration status messages if wizard is currently active
+
+	  if (calibration_status() == CALIBRATION_STATUS_ASSEMBLED ||
+		  calibration_status() == CALIBRATION_STATUS_UNKNOWN ||
+	      calibration_status() == CALIBRATION_STATUS_XYZ_CALIBRATION){
+		  // Reset the babystepping values, so the printer will not move the Z axis up when the babystepping is enabled.
+		  eeprom_update_word((uint16_t*)EEPROM_BABYSTEP_Z, 0);
+		  // Show the message.
+		  lcd_show_fullscreen_message_and_wait_P(MSG_FOLLOW_CALIBRATION_FLOW);
+	  }
+	  else if (calibration_status() == CALIBRATION_STATUS_LIVE_ADJUST) {
+		  // Show the message.
+		  lcd_show_fullscreen_message_and_wait_P(MSG_BABYSTEP_Z_NOT_SET);
+		  lcd_update_enable(true);
+	  }
+	  else if (calibration_status() == CALIBRATION_STATUS_CALIBRATED && temp_cal_active == true && calibration_status_pinda() == false) {
+		  lcd_show_fullscreen_message_and_wait_P(MSG_PINDA_NOT_CALIBRATED);
+		  lcd_update_enable(true);
+	  }
+	  else if (calibration_status() == CALIBRATION_STATUS_Z_CALIBRATION) {
+		  // Show the message.
+		  lcd_show_fullscreen_message_and_wait_P(MSG_FOLLOW_CALIBRATION_FLOW);
+	  }
+	  //If eeprom version for storing parameters to eeprom using M500 changed, default settings are used. Inform user in this case
+	  if (!previous_settings_retrieved) {
+		  lcd_show_fullscreen_message_and_wait_P(MSG_DEFAULT_SETTINGS_LOADED);
+	  }
   }
+  for (int i = 0; i < 4; i++) EEPROM_read_B(EEPROM_BOWDEN_LENGTH + i * 2, &bowden_length[i]);
   
 #endif //DEBUG_DISABLE_STARTMSGS
   lcd_update_enable(true);
@@ -1245,6 +1278,7 @@ void setup()
   // Store the currently running firmware into an eeprom,
   // so the next time the firmware gets updated, it will know from which version it has been updated.
   update_current_firmware_version_to_eeprom();
+  KEEPALIVE_STATE(NOT_BUSY);
 }
 
 void trace();
@@ -1321,6 +1355,36 @@ int serial_read_stream() {
     }
 }
 
+#ifdef HOST_KEEPALIVE_FEATURE
+/**
+* Output a "busy" message at regular intervals
+* while the machine is not accepting commands.
+*/
+void host_keepalive() {
+  if (farm_mode) return;
+  long ms = millis();
+  if (host_keepalive_interval && busy_state != NOT_BUSY) {
+    if (ms - prev_busy_signal_ms < 1000UL * host_keepalive_interval) return;
+	switch (busy_state) {
+      case IN_HANDLER:
+      case IN_PROCESS:
+        SERIAL_ECHO_START;
+        SERIAL_ECHOLNPGM("busy: processing");
+        break;
+      case PAUSED_FOR_USER:
+        SERIAL_ECHO_START;
+        SERIAL_ECHOLNPGM("busy: paused for user");
+        break;
+      case PAUSED_FOR_INPUT:
+        SERIAL_ECHO_START;
+        SERIAL_ECHOLNPGM("busy: paused for input");
+        break;
+    }
+  }
+  prev_busy_signal_ms = ms;
+}
+#endif
+
 // The loop() function is called in an endless loop by the Arduino framework from the default main() routine.
 // Before loop(), the setup() function is called by the main() routine.
 void loop()
@@ -1376,6 +1440,7 @@ void loop()
       if (! cmdbuffer_front_already_processed)
           cmdqueue_pop_front();
       cmdbuffer_front_already_processed = false;
+	  host_keepalive();
   }
 }
   //check heater every n milliseconds
@@ -1882,6 +1947,19 @@ static float probe_pt(float x, float y, float z_before) {
   }
 #endif // LIN_ADVANCE
 
+bool check_commands() {
+	  bool end_command_found = false;
+
+	  if (buflen)
+	  {
+		  if ((code_seen("M84")) || (code_seen("M 84"))) end_command_found = true;
+		  if (!cmdbuffer_front_already_processed)
+			  cmdqueue_pop_front();
+		  cmdbuffer_front_already_processed = false;
+	  }
+	  return end_command_found;
+}
+
 void homeaxis(int axis) {
 #define HOMEAXIS_DO(LETTER) \
   ((LETTER##_MIN_PIN > -1 && LETTER##_HOME_DIR==-1) || (LETTER##_MAX_PIN > -1 && LETTER##_HOME_DIR==1))
@@ -2066,6 +2144,165 @@ void ramming() {
 	}
   }
 */
+
+
+void gcode_M701() {
+#ifdef SNMM
+	extr_adj(snmm_extruder);//loads current extruder
+#else
+	enable_z();
+	custom_message = true;
+	custom_message_type = 2;
+
+	lcd_setstatuspgm(MSG_LOADING_FILAMENT);
+	current_position[E_AXIS] += 70;
+	plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 400 / 60, active_extruder); //fast sequence
+
+	current_position[E_AXIS] += 25;
+	plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 100 / 60, active_extruder); //slow sequence
+	st_synchronize();
+
+	if (!farm_mode && loading_flag) {
+		bool clean = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FILAMENT_CLEAN, false, true);
+
+		while (!clean) {
+			lcd_update_enable(true);
+			lcd_update(2);
+			current_position[E_AXIS] += 25;
+			plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 100 / 60, active_extruder); //slow sequence
+			st_synchronize();
+			clean = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FILAMENT_CLEAN, false, true);
+		}
+	}
+	lcd_update_enable(true);
+	lcd_update(2);
+	lcd_setstatuspgm(WELCOME_MSG);
+	disable_z();
+	loading_flag = false;
+	custom_message = false;
+	custom_message_type = 0;
+#endif
+}
+
+bool gcode_M45(bool onlyZ) {
+	bool final_result = false;
+	if (!onlyZ) {
+		setTargetBed(0);
+		setTargetHotend(0, 0);
+		setTargetHotend(0, 1);
+		setTargetHotend(0, 2);
+		adjust_bed_reset(); //reset bed level correction
+	}
+
+	// Disable the default update procedure of the display. We will do a modal dialog.
+	lcd_update_enable(false);
+	// Let the planner use the uncorrected coordinates.
+	mbl.reset();
+	// Reset world2machine_rotation_and_skew and world2machine_shift, therefore
+	// the planner will not perform any adjustments in the XY plane. 
+	// Wait for the motors to stop and update the current position with the absolute values.
+	world2machine_revert_to_uncorrected();
+	// Reset the baby step value applied without moving the axes.
+	babystep_reset();
+	// Mark all axes as in a need for homing.
+	memset(axis_known_position, 0, sizeof(axis_known_position));
+
+	// Let the user move the Z axes up to the end stoppers.
+	KEEPALIVE_STATE(PAUSED_FOR_USER);
+	if (lcd_calibrate_z_end_stop_manual(onlyZ)) {
+		KEEPALIVE_STATE(IN_HANDLER);
+		refresh_cmd_timeout();
+		if (((degHotend(0) > MAX_HOTEND_TEMP_CALIBRATION) || (degBed() > MAX_BED_TEMP_CALIBRATION)) && (!onlyZ)) {
+			lcd_wait_for_cool_down();
+			lcd_show_fullscreen_message_and_wait_P(MSG_PAPER);
+			lcd_display_message_fullscreen_P(MSG_FIND_BED_OFFSET_AND_SKEW_LINE1);
+			lcd_implementation_print_at(0, 2, 1);
+			lcd_printPGM(MSG_FIND_BED_OFFSET_AND_SKEW_LINE2);
+		}
+
+		// Move the print head close to the bed.
+		current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
+		plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], homing_feedrate[Z_AXIS] / 40, active_extruder);
+		st_synchronize();
+
+		// Home in the XY plane.
+		set_destination_to_current();
+		setup_for_endstop_move();
+		home_xy();
+
+		int8_t verbosity_level = 0;
+		if (code_seen('V')) {
+			// Just 'V' without a number counts as V1.
+			char c = strchr_pointer[1];
+			verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short();
+		}
+
+		if (onlyZ) {
+			clean_up_after_endstop_move();
+			// Z only calibration.
+			// Load the machine correction matrix
+			world2machine_initialize();
+			// and correct the current_position to match the transformed coordinate system.
+			world2machine_update_current();
+			//FIXME
+			bool result = sample_mesh_and_store_reference();
+			if (result) {
+				if (calibration_status() == CALIBRATION_STATUS_Z_CALIBRATION)
+					// Shipped, the nozzle height has been set already. The user can start printing now.
+					calibration_status_store(CALIBRATION_STATUS_CALIBRATED);
+				// babystep_apply();
+				final_result = true;
+			}
+		}
+		else {
+			//if wizard is active and selftest was succefully completed, we dont want to loose information about it
+			if (calibration_status() != 250 || eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) == 0) {				
+				calibration_status_store(CALIBRATION_STATUS_ASSEMBLED);
+			}
+			// Reset the baby step value and the baby step applied flag.
+			eeprom_update_word((uint16_t*)EEPROM_BABYSTEP_Z, 0);
+			// Complete XYZ calibration.
+			uint8_t point_too_far_mask = 0;
+			BedSkewOffsetDetectionResultType result = find_bed_offset_and_skew(verbosity_level, point_too_far_mask);
+			clean_up_after_endstop_move();
+			// Print head up.
+			current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
+			plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], homing_feedrate[Z_AXIS] / 40, active_extruder);
+			st_synchronize();
+			if (result >= 0) {
+				point_too_far_mask = 0;
+				// Second half: The fine adjustment.
+				// Let the planner use the uncorrected coordinates.
+				mbl.reset();
+				world2machine_reset();
+				// Home in the XY plane.
+				setup_for_endstop_move();
+				home_xy();
+				result = improve_bed_offset_and_skew(1, verbosity_level, point_too_far_mask);
+				clean_up_after_endstop_move();
+				// Print head up.
+				current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
+				plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], homing_feedrate[Z_AXIS] / 40, active_extruder);
+				st_synchronize();
+				// if (result >= 0) babystep_apply();
+			}
+			lcd_bed_calibration_show_result(result, point_too_far_mask);
+			if (result >= 0) {
+				// Calibration valid, the machine should be able to print. Advise the user to run the V2Calibration.gcode.
+				calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST);
+				if(eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) != 1) lcd_show_fullscreen_message_and_wait_P(MSG_BABYSTEP_Z_NOT_SET);
+				final_result = true;
+			}
+		}
+	}
+	else {
+		// Timeouted.
+		KEEPALIVE_STATE(IN_HANDLER);
+	}
+	lcd_update_enable(true);
+	return final_result;
+}
+
 void process_commands()
 {
   #ifdef FILAMENT_RUNOUT_SUPPORT
@@ -2094,11 +2331,17 @@ void process_commands()
   float tmp_motor_loud[3] = DEFAULT_PWM_MOTOR_CURRENT_LOUD;
   int8_t SilentMode;
 #endif
+  KEEPALIVE_STATE(IN_HANDLER);
+
   if (code_seen("M117")) { //moved to highest priority place to be able to to print strings which includes "G", "PRUSA" and "^"
+	  custom_message = true; //fixes using M117 during SD print, but needs to be be updated in future
+	  custom_message_type = 2; //fixes using M117 during SD print, but needs to be be updated in future
 	  starpos = (strchr(strchr_pointer + 5, '*'));
 	  if (starpos != NULL)
 		  *(starpos) = '\0';
 	  lcd_setstatus(strchr_pointer + 5);
+	  custom_message = false;
+	  custom_message_type = 0;
   }
   else if(code_seen("PRUSA")){
 		if (code_seen("Ping")) {  //PRUSA Ping
@@ -2427,7 +2670,7 @@ void process_commands()
         prepare_arc_move(false);
       }
       break;
-    case 4: // G4 dwell      
+    case 4: // G4 dwell   
       codenum = 0;
       if(code_seen('P')) codenum = code_value(); // milliseconds to wait
       if(code_seen('S')) codenum = code_value() * 1000; // seconds to wait
@@ -2460,8 +2703,7 @@ void process_commands()
       #endif //FWRETRACT
     case 28: //G28 Home all Axis one at a time
 		homing_flag = true;
-
-#ifdef ENABLE_AUTO_BED_LEVELING
+		#ifdef ENABLE_AUTO_BED_LEVELING
       plan_bed_level_matrix.set_to_identity();  //Reset the plane ("erase" all leveling data)
 #endif //ENABLE_AUTO_BED_LEVELING
             
@@ -2908,6 +3150,7 @@ void process_commands()
 			enquecommand_front_P((PSTR("G28 W0")));
 			break;
 		}
+		KEEPALIVE_STATE(NOT_BUSY); //no need to print busy messages as we print current temperatures periodicaly
 		SERIAL_ECHOLNPGM("PINDA probe calibration start");
 		custom_message = true;
 		custom_message_type = 4;
@@ -3308,6 +3551,7 @@ void process_commands()
 			current_position[E_AXIS] += DEFAULT_RETRACTION;
 			plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 400, active_extruder);
 		}
+		KEEPALIVE_STATE(NOT_BUSY);
 		// Restore custom message state
 		custom_message = custom_message_old;
 		custom_message_type = custom_message_type_old;
@@ -3416,13 +3660,16 @@ void process_commands()
 			calibration_status_store(CALIBRATION_STATUS_CALIBRATED);
             break;
 
-            /**
-             * G88: Prusa3D specific: Don't know what it is for, it is in V2Calibration.gcode
-             */
-		    case 88:
-			      break;
-
-
+		/*case 88: //just for test
+			SERIAL_ECHOPGM("Calibration status:");
+			MYSERIAL.println(int(calibration_status()));
+			if (code_seen('S')) codenum = code_value();
+			calibration_status_store(codenum);
+			SERIAL_ECHOPGM("Calibration status:");
+			MYSERIAL.println(int(calibration_status()));
+			eeprom_write_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 1); //run wizard
+			break;
+		*/
 #endif  // ENABLE_MESH_BED_LEVELING
             
             
@@ -3452,7 +3699,9 @@ void process_commands()
 	case 98: //activate farm mode
 		farm_mode = 1;
 		PingTime = millis();
+		EEPROM_save_B(EEPROM_FARM_NUMBER, &farm_no);
 		eeprom_update_byte((unsigned char *)EEPROM_FARM_MODE, farm_mode);
+		
 		break;
 
 	case 99: //deactivate farm mode
@@ -3487,9 +3736,11 @@ void process_commands()
     case 0: // M0 - Unconditional stop - Wait for user button press on LCD
     case 1: // M1 - Conditional stop - Wait for user button press on LCD
     {
-      char *src = strchr_pointer + 2;
+	  custom_message = true;  //fixes using M1 during SD print, but needs to be be updated in future
+	  custom_message_type = 2; //fixes using M1 during SD print, but needs to be be updated in future
 
-      codenum = 0;
+      char *src = strchr_pointer + 2;
+	  codenum = 0;
 
       bool hasP = false, hasS = false;
       if (code_seen('P')) {
@@ -3505,35 +3756,41 @@ void process_commands()
       while (*src == ' ') ++src;
       if (!hasP && !hasS && *src != '\0') {
         lcd_setstatus(src);
-      } else {
-        LCD_MESSAGERPGM(MSG_USERWAIT);
-      }
+	  } else {
+		  LCD_MESSAGERPGM(MSG_USERWAIT);
+	  }
 
       lcd_ignore_click();				//call lcd_ignore_click aslo for else ???
       st_synchronize();
       previous_millis_cmd = millis();
       if (codenum > 0){
         codenum += millis();  // keep track of when we started waiting
-        while(millis() < codenum && !lcd_clicked()){
+		KEEPALIVE_STATE(PAUSED_FOR_USER);
+		while(millis() < codenum && !lcd_clicked()){
           manage_heater();
           manage_inactivity(true);
           lcd_update();
         }
+		KEEPALIVE_STATE(IN_HANDLER);
         lcd_ignore_click(false);
       }else{
           if (!lcd_detected())
             break;
+		  KEEPALIVE_STATE(PAUSED_FOR_USER);
         while(!lcd_clicked()){
           manage_heater();
           manage_inactivity(true);
           lcd_update();
         }
+		KEEPALIVE_STATE(IN_HANDLER);
       }
       if (IS_SD_PRINTING)
         LCD_MESSAGERPGM(MSG_RESUMING);
       else
         LCD_MESSAGERPGM(WELCOME_MSG);
-    }
+	  custom_message = false; 
+	  custom_message_type = 0;
+	}
     break;
 #endif
     case 17:
@@ -3714,112 +3971,8 @@ void process_commands()
     case 45: // M45: Prusa3D: bed skew and offset with manual Z up
     {
 		// Only Z calibration?
-		bool onlyZ = code_seen('Z');
-
-		if (!onlyZ) {
-			setTargetBed(0);
-			setTargetHotend(0, 0);
-			setTargetHotend(0, 1);
-			setTargetHotend(0, 2);
-			adjust_bed_reset(); //reset bed level correction
-		}
-		
-        // Disable the default update procedure of the display. We will do a modal dialog.
-        lcd_update_enable(false);
-        // Let the planner use the uncorrected coordinates.
-        mbl.reset();
-        // Reset world2machine_rotation_and_skew and world2machine_shift, therefore
-        // the planner will not perform any adjustments in the XY plane. 
-        // Wait for the motors to stop and update the current position with the absolute values.
-        world2machine_revert_to_uncorrected();
-        // Reset the baby step value applied without moving the axes.
-        babystep_reset();
-        // Mark all axes as in a need for homing.
-        memset(axis_known_position, 0, sizeof(axis_known_position));
-                
-        // Let the user move the Z axes up to the end stoppers.
-        if (lcd_calibrate_z_end_stop_manual( onlyZ )) {
-            refresh_cmd_timeout();
-			if (((degHotend(0) > MAX_HOTEND_TEMP_CALIBRATION) || (degBed() > MAX_BED_TEMP_CALIBRATION)) && (!onlyZ)) {
-				lcd_wait_for_cool_down();
-				lcd_show_fullscreen_message_and_wait_P(MSG_PAPER);
-				lcd_display_message_fullscreen_P(MSG_FIND_BED_OFFSET_AND_SKEW_LINE1);
-				lcd_implementation_print_at(0, 2, 1);
-				lcd_printPGM(MSG_FIND_BED_OFFSET_AND_SKEW_LINE2);
-			}
-
-            // Move the print head close to the bed.
-            current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
-            plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
-            st_synchronize();
-
-            // Home in the XY plane.
-            set_destination_to_current();
-            setup_for_endstop_move();
-            home_xy();
-
-            int8_t verbosity_level = 0;
-            if (code_seen('V')) {
-                // Just 'V' without a number counts as V1.
-                char c = strchr_pointer[1];
-                verbosity_level = (c == ' ' || c == '\t' || c == 0) ? 1 : code_value_short();
-            }
-            
-            if (onlyZ) {
-                clean_up_after_endstop_move();
-                // Z only calibration.
-                // Load the machine correction matrix
-                world2machine_initialize();
-                // and correct the current_position to match the transformed coordinate system.
-                world2machine_update_current();
-                //FIXME
-                bool result = sample_mesh_and_store_reference();
-                if (result) {
-                    if (calibration_status() == CALIBRATION_STATUS_Z_CALIBRATION)
-                        // Shipped, the nozzle height has been set already. The user can start printing now.
-                        calibration_status_store(CALIBRATION_STATUS_CALIBRATED);
-                    // babystep_apply();
-                }
-            } else {
-                // Reset the baby step value and the baby step applied flag.
-                calibration_status_store(CALIBRATION_STATUS_ASSEMBLED);
-                eeprom_update_word((uint16_t*)EEPROM_BABYSTEP_Z, 0);
-                // Complete XYZ calibration.
-				uint8_t point_too_far_mask = 0;
-                BedSkewOffsetDetectionResultType result = find_bed_offset_and_skew(verbosity_level, point_too_far_mask);
-				clean_up_after_endstop_move();
-                // Print head up.
-                current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
-                plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
-                st_synchronize();
-                if (result >= 0) {
-					point_too_far_mask = 0;
-                    // Second half: The fine adjustment.
-                    // Let the planner use the uncorrected coordinates.
-                    mbl.reset();
-                    world2machine_reset();
-                    // Home in the XY plane.
-                    setup_for_endstop_move();
-                    home_xy();
-                    result = improve_bed_offset_and_skew(1, verbosity_level, point_too_far_mask);
-                    clean_up_after_endstop_move();
-                    // Print head up.
-                    current_position[Z_AXIS] = MESH_HOME_Z_SEARCH;
-                    plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS],current_position[Z_AXIS] , current_position[E_AXIS], homing_feedrate[Z_AXIS]/40, active_extruder);
-                    st_synchronize();
-                    // if (result >= 0) babystep_apply();
-                }
-                lcd_bed_calibration_show_result(result, point_too_far_mask);
-                if (result >= 0) {
-                    // Calibration valid, the machine should be able to print. Advise the user to run the V2Calibration.gcode.
-                    calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST);
-                    lcd_show_fullscreen_message_and_wait_P(MSG_BABYSTEP_Z_NOT_SET);
-                }
-            }
-        } else {
-            // Timeouted.
-        }
-        lcd_update_enable(true);
+		bool only_Z = code_seen('Z');
+		gcode_M45(only_Z);		
         break;
     }
 
@@ -3848,8 +4001,10 @@ void process_commands()
 
     case 47:
         // M47: Prusa3D: Show end stops dialog on the display.
+		KEEPALIVE_STATE(PAUSED_FOR_USER);
         lcd_diag_show_end_stops();
-        break;
+		KEEPALIVE_STATE(IN_HANDLER);
+		break;
 
 #if 0
     case 48: // M48: scan the bed induction sensor points, print the sensor trigger coordinates to the serial line for visualization on the PC.
@@ -4232,6 +4387,7 @@ Sigma_Exit:
           }}
         #endif
 		SERIAL_PROTOCOLLN("");
+		KEEPALIVE_STATE(NOT_BUSY);
       return;
       break;
     case 109:
@@ -4269,11 +4425,14 @@ Sigma_Exit:
       /* See if we are heating up or cooling down */
       target_direction = isHeatingHotend(tmp_extruder); // true if heating, false if cooling
 
+	  KEEPALIVE_STATE(NOT_BUSY);
+
       cancel_heatup = false;
 
 	  wait_for_heater(codenum); //loops until target temperature is reached
 
         LCD_MESSAGERPGM(MSG_HEATING_COMPLETE);
+		KEEPALIVE_STATE(IN_HANDLER);
 		heating_status = 2;
 		if (farm_mode) { prusa_statistics(2); };
         
@@ -4301,6 +4460,7 @@ Sigma_Exit:
         cancel_heatup = false;
         target_direction = isHeatingBed(); // true if heating, false if cooling
 
+		KEEPALIVE_STATE(NOT_BUSY);
         while ( (target_direction)&&(!cancel_heatup) ? (isHeatingBed()) : (isCoolingBed()&&(CooldownNoWait==false)) )
         {
           if(( millis() - codenum) > 1000 ) //Print Temp Reading every 1 second while heating up.
@@ -4323,6 +4483,7 @@ Sigma_Exit:
           lcd_update();
         }
         LCD_MESSAGERPGM(MSG_BED_DONE);
+		KEEPALIVE_STATE(IN_HANDLER);
 		heating_status = 4;
 
         previous_millis_cmd = millis();
@@ -4466,6 +4627,18 @@ Sigma_Exit:
 		else
 			gcode_LastN = 0;
 		break;
+#ifdef HOST_KEEPALIVE_FEATURE
+	case 113: // M113 - Get or set Host Keepalive interval
+      if (code_seen('S')) {
+		host_keepalive_interval = (uint8_t)code_value_short();
+		NOMORE(host_keepalive_interval, 60);
+      } else {
+        SERIAL_ECHO_START;
+        SERIAL_ECHOPAIR("M113 S", (unsigned long)host_keepalive_interval);
+		SERIAL_PROTOCOLLN("");
+      }
+	break;
+#endif
     case 115: // M115
       if (code_seen('V')) {
           // Report the Prusa version number.
@@ -5017,7 +5190,8 @@ Sigma_Exit:
           temp=70;
       if (code_seen('S')) temp=code_value();
       if (code_seen('C')) c=code_value();
-      PID_autotune(temp, e, c);
+	  
+	  PID_autotune(temp, e, c);
     }
     break;
     case 400: // M400 finish all moves
@@ -5294,6 +5468,7 @@ case 404:  //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or disp
         int counterBeep = 0;
         lcd_wait_interact();
 		load_filament_time = millis();
+		KEEPALIVE_STATE(PAUSED_FOR_USER);
         while(!lcd_clicked()){
 
 		  cnt++;
@@ -5330,14 +5505,17 @@ case 404:  //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or disp
           }
 
         }
+		KEEPALIVE_STATE(IN_HANDLER);
 		WRITE(BEEPER, LOW);
 #ifdef SNMM
 		display_loading();
+		KEEPALIVE_STATE(PAUSED_FOR_USER);
 		do {
 			target[E_AXIS] += 0.002;
 			plan_buffer_line(target[X_AXIS], target[Y_AXIS], target[Z_AXIS], target[E_AXIS], 500, active_extruder);
 			delay_keep_alive(2);
-		} while (!lcd_clicked());		
+		} while (!lcd_clicked());
+		KEEPALIVE_STATE(IN_HANDLER);
 		/*if (millis() - load_filament_time > 2) {
 			load_filament_time = millis();
 			target[E_AXIS] += 0.001;
@@ -5377,7 +5555,9 @@ case 404:  //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or disp
         lcd_loading_filament();
         while ((lcd_change_fil_state == 0)||(lcd_change_fil_state != 1)){
           lcd_change_fil_state = 0;
+		  KEEPALIVE_STATE(PAUSED_FOR_USER);
           lcd_alright();
+		  KEEPALIVE_STATE(IN_HANDLER);
           switch(lcd_change_fil_state){
             
              // Filament failed to load so load it again
@@ -5550,37 +5730,7 @@ case 404:  //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or disp
     break;
 	case 701: //M701: load filament
 	{
-		enable_z();
-		custom_message = true;
-		custom_message_type = 2;
-		
-		lcd_setstatuspgm(MSG_LOADING_FILAMENT);
-		current_position[E_AXIS] += 70;
-		plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 400 / 60, active_extruder); //fast sequence
-
-		current_position[E_AXIS] += 25;
-		plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 100 / 60, active_extruder); //slow sequence
-		st_synchronize();
-
-		if (!farm_mode && loading_flag) {
-			bool clean = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FILAMENT_CLEAN, false, true);
-
-			while (!clean) {
-				lcd_update_enable(true);
-				lcd_update(2);
-				current_position[E_AXIS] += 25;
-				plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 100 / 60, active_extruder); //slow sequence
-				st_synchronize();
-				clean = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FILAMENT_CLEAN, false, true);
-			}
-		}
-		lcd_update_enable(true);
-		lcd_update(2);
-		lcd_setstatuspgm(WELCOME_MSG);
-		disable_z();
-		loading_flag = false;
-		custom_message = false;
-		custom_message_type = 0;
+		gcode_M701();
 	}
 	break;
 	case 702:
@@ -5790,6 +5940,8 @@ case 404:  //M404 Enter the nominal filament width (3mm, 1.75mm ) N<3.0> or disp
     SERIAL_ECHOLNPGM("\"");
   }
 
+  KEEPALIVE_STATE(NOT_BUSY);
+
   ClearToSend();
 }
 
@@ -6033,7 +6185,7 @@ void manage_inactivity(bool ignore_stepper_queue/*=false*/) //default argument s
 	static int killCount = 0;   // make the inactivity button a bit less responsive
    const int KILL_DELAY = 10000;
 #endif
-	
+
     if(buflen < (BUFSIZE-1)){
         get_command();
     }
@@ -6309,7 +6461,7 @@ void calculate_volumetric_multipliers() {
 
 void delay_keep_alive(unsigned int ms)
 {
-    for (;;) {
+	for (;;) {
         manage_heater();
         // Manage inactivity, but don't disable steppers on timeout.
         manage_inactivity(true);

+ 2 - 0
Firmware/SdBaseFile.cpp

@@ -277,6 +277,8 @@ int16_t SdBaseFile::fgets(char* str, int16_t num, char* delim) {
  * \return The value one, true, is returned for success and
  * the value zero, false, is returned for failure.
  */
+
+
 bool SdBaseFile::getFilename(char* name) {
   if (!isOpen()) return false;
 

+ 8 - 2
Firmware/cardreader.cpp

@@ -162,6 +162,7 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
 	} // while readDir
 }
 
+
 void CardReader::ls() 
 {
   lsAction=LS_SerialPrint;
@@ -500,6 +501,11 @@ void CardReader::removeFile(char* name)
   
 }
 
+uint32_t CardReader::getFileSize()
+{
+	return filesize;
+}
+
 void CardReader::getStatus()
 {
   if(sdprinting){
@@ -963,8 +969,8 @@ void CardReader::printingHasFinished()
       sdprinting = false;
       if(SD_FINISHED_STEPPERRELEASE)
       {
-          //finishAndDisableSteppers();
-          enquecommand_P(PSTR(SD_FINISHED_RELEASECOMMAND));
+          finishAndDisableSteppers();
+          //enquecommand_P(PSTR(SD_FINISHED_RELEASECOMMAND));
       }
       autotempShutdown();
 	  #ifdef SDCARD_SORT_ALPHA

+ 1 - 0
Firmware/cardreader.h

@@ -27,6 +27,7 @@ public:
   void release();
   void startFileprint();
   void pauseSDPrint();
+  uint32_t getFileSize();
   void getStatus();
   void printingHasFinished();
 

+ 120 - 0
Firmware/language_all.cpp

@@ -1092,6 +1092,11 @@ const char * const MSG_FILAMENT_LOADING_T3_LANG_TABLE[LANG_NUM] PROGMEM = {
 	MSG_FILAMENT_LOADING_T3_DE
 };
 
+const char MSG_FILE_INCOMPLETE_EN[] PROGMEM = "File incomplete. Continue anyway?";
+const char * const MSG_FILE_INCOMPLETE_LANG_TABLE[1] PROGMEM = {
+	MSG_FILE_INCOMPLETE_EN
+};
+
 const char MSG_FILE_PRINTED_EN[] PROGMEM = "Done printing file";
 const char * const MSG_FILE_PRINTED_LANG_TABLE[1] PROGMEM = {
 	MSG_FILE_PRINTED_EN
@@ -2097,6 +2102,16 @@ const char * const MSG_PLANNER_BUFFER_BYTES_LANG_TABLE[1] PROGMEM = {
 	MSG_PLANNER_BUFFER_BYTES_EN
 };
 
+const char MSG_PLA_FILAMENT_LOADED_EN[] PROGMEM = "Is PLA filament loaded?";
+const char * const MSG_PLA_FILAMENT_LOADED_LANG_TABLE[1] PROGMEM = {
+	MSG_PLA_FILAMENT_LOADED_EN
+};
+
+const char MSG_PLEASE_LOAD_PLA_EN[] PROGMEM = "Please load PLA filament first.";
+const char * const MSG_PLEASE_LOAD_PLA_LANG_TABLE[1] PROGMEM = {
+	MSG_PLEASE_LOAD_PLA_EN
+};
+
 const char MSG_PLEASE_WAIT_EN[] PROGMEM = "Please wait";
 const char MSG_PLEASE_WAIT_CZ[] PROGMEM = "Prosim cekejte";
 const char MSG_PLEASE_WAIT_IT[] PROGMEM = "Aspetta";
@@ -3422,6 +3437,11 @@ const char * const MSG_USERWAIT_LANG_TABLE[LANG_NUM] PROGMEM = {
 	MSG_USERWAIT_DE
 };
 
+const char MSG_V2_CALIBRATION_EN[] PROGMEM = "First layer cal.";
+const char * const MSG_V2_CALIBRATION_LANG_TABLE[1] PROGMEM = {
+	MSG_V2_CALIBRATION_EN
+};
+
 const char MSG_VMIN_EN[] PROGMEM = "Vmin";
 const char * const MSG_VMIN_LANG_TABLE[1] PROGMEM = {
 	MSG_VMIN_EN
@@ -3477,6 +3497,106 @@ const char * const MSG_WATCHDOG_RESET_LANG_TABLE[1] PROGMEM = {
 	MSG_WATCHDOG_RESET_EN
 };
 
+const char MSG_WIZARD_EN[] PROGMEM = "Wizard";
+const char * const MSG_WIZARD_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_EN
+};
+
+const char MSG_WIZARD_CALIBRATION_FAILED_EN[] PROGMEM = "Please check our handbook and fix the problem. Then resume the Wizard by rebooting the printer.";
+const char * const MSG_WIZARD_CALIBRATION_FAILED_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_CALIBRATION_FAILED_EN
+};
+
+const char MSG_WIZARD_CLEAN_HEATBED_EN[] PROGMEM = "Please clean heatbed and then press the knob.";
+const char * const MSG_WIZARD_CLEAN_HEATBED_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_CLEAN_HEATBED_EN
+};
+
+const char MSG_WIZARD_DONE_EN[] PROGMEM = "All is done. Happy printing!";
+const char * const MSG_WIZARD_DONE_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_DONE_EN
+};
+
+const char MSG_WIZARD_FILAMENT_LOADED_EN[] PROGMEM = "Is filament loaded?";
+const char * const MSG_WIZARD_FILAMENT_LOADED_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_FILAMENT_LOADED_EN
+};
+
+const char MSG_WIZARD_HEATING_EN[] PROGMEM = "Preheating nozzle. Please wait.";
+const char * const MSG_WIZARD_HEATING_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_HEATING_EN
+};
+
+const char MSG_WIZARD_INSERT_CORRECT_FILAMENT_EN[] PROGMEM = "Please load PLA filament and then resume Wizard by rebooting the printer.";
+const char * const MSG_WIZARD_INSERT_CORRECT_FILAMENT_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_INSERT_CORRECT_FILAMENT_EN
+};
+
+const char MSG_WIZARD_LANGUAGE_EN[] PROGMEM = "Please choose your language";
+const char * const MSG_WIZARD_LANGUAGE_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_LANGUAGE_EN
+};
+
+const char MSG_WIZARD_LOAD_FILAMENT_EN[] PROGMEM = "Please insert PLA filament to the extruder, then press knob to load it.";
+const char * const MSG_WIZARD_LOAD_FILAMENT_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_LOAD_FILAMENT_EN
+};
+
+const char MSG_WIZARD_PLA_FILAMENT_EN[] PROGMEM = "Is it PLA filament?";
+const char * const MSG_WIZARD_PLA_FILAMENT_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_PLA_FILAMENT_EN
+};
+
+const char MSG_WIZARD_QUIT_EN[] PROGMEM = "You can always resume the Wizard from Calibration -> Wizard.";
+const char * const MSG_WIZARD_QUIT_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_QUIT_EN
+};
+
+const char MSG_WIZARD_REPEAT_V2_CAL_EN[] PROGMEM = "Do you want to repeat last step to readjust distance between nozzle and heatbed?";
+const char * const MSG_WIZARD_REPEAT_V2_CAL_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_REPEAT_V2_CAL_EN
+};
+
+const char MSG_WIZARD_RERUN_EN[] PROGMEM = "Running Wizard will delete current calibration results and start from the beginning. Continue?";
+const char * const MSG_WIZARD_RERUN_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_RERUN_EN
+};
+
+const char MSG_WIZARD_SELFTEST_EN[] PROGMEM = "First, I will run the selftest to check most common assembly problems.";
+const char * const MSG_WIZARD_SELFTEST_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_SELFTEST_EN
+};
+
+const char MSG_WIZARD_V2_CAL_EN[] PROGMEM = "Now I will calibrate distance between tip of the nozzle and heatbed surface.";
+const char * const MSG_WIZARD_V2_CAL_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_V2_CAL_EN
+};
+
+const char MSG_WIZARD_V2_CAL_2_EN[] PROGMEM = "I will start to print line and you will gradually lower the nozzle by rotating the knob, until you reach optimal height. Check the pictures in our handbook in chapter Calibration.";
+const char * const MSG_WIZARD_V2_CAL_2_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_V2_CAL_2_EN
+};
+
+const char MSG_WIZARD_WELCOME_EN[] PROGMEM = "Hi, I am your Original Prusa i3 printer. Would you like me to guide you through the setup process?";
+const char * const MSG_WIZARD_WELCOME_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_WELCOME_EN
+};
+
+const char MSG_WIZARD_WILL_PREHEAT_EN[] PROGMEM = "Now I will preheat nozzle for PLA.";
+const char * const MSG_WIZARD_WILL_PREHEAT_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_WILL_PREHEAT_EN
+};
+
+const char MSG_WIZARD_XYZ_CAL_EN[] PROGMEM = "I will run xyz calibration now. It will take approx. 12 mins.";
+const char * const MSG_WIZARD_XYZ_CAL_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_XYZ_CAL_EN
+};
+
+const char MSG_WIZARD_Z_CAL_EN[] PROGMEM = "I will run z calibration now.";
+const char * const MSG_WIZARD_Z_CAL_LANG_TABLE[1] PROGMEM = {
+	MSG_WIZARD_Z_CAL_EN
+};
+
 const char MSG_XYZ_DETAILS_EN[] PROGMEM = "XYZ cal. details";
 const char MSG_XYZ_DETAILS_CZ[] PROGMEM = "Detaily XYZ kal.";
 const char MSG_XYZ_DETAILS_IT[] PROGMEM = "XYZ Cal. dettagli";

+ 48 - 0
Firmware/language_all.h

@@ -218,6 +218,8 @@ extern const char* const MSG_FILAMENT_LOADING_T2_LANG_TABLE[LANG_NUM];
 #define MSG_FILAMENT_LOADING_T2 LANG_TABLE_SELECT(MSG_FILAMENT_LOADING_T2_LANG_TABLE)
 extern const char* const MSG_FILAMENT_LOADING_T3_LANG_TABLE[LANG_NUM];
 #define MSG_FILAMENT_LOADING_T3 LANG_TABLE_SELECT(MSG_FILAMENT_LOADING_T3_LANG_TABLE)
+extern const char* const MSG_FILE_INCOMPLETE_LANG_TABLE[1];
+#define MSG_FILE_INCOMPLETE LANG_TABLE_SELECT_EXPLICIT(MSG_FILE_INCOMPLETE_LANG_TABLE, 0)
 extern const char* const MSG_FILE_PRINTED_LANG_TABLE[1];
 #define MSG_FILE_PRINTED LANG_TABLE_SELECT_EXPLICIT(MSG_FILE_PRINTED_LANG_TABLE, 0)
 extern const char* const MSG_FILE_SAVED_LANG_TABLE[1];
@@ -396,6 +398,10 @@ extern const char* const MSG_PINDA_PREHEAT_LANG_TABLE[LANG_NUM];
 #define MSG_PINDA_PREHEAT LANG_TABLE_SELECT(MSG_PINDA_PREHEAT_LANG_TABLE)
 extern const char* const MSG_PLANNER_BUFFER_BYTES_LANG_TABLE[1];
 #define MSG_PLANNER_BUFFER_BYTES LANG_TABLE_SELECT_EXPLICIT(MSG_PLANNER_BUFFER_BYTES_LANG_TABLE, 0)
+extern const char* const MSG_PLA_FILAMENT_LOADED_LANG_TABLE[1];
+#define MSG_PLA_FILAMENT_LOADED LANG_TABLE_SELECT_EXPLICIT(MSG_PLA_FILAMENT_LOADED_LANG_TABLE, 0)
+extern const char* const MSG_PLEASE_LOAD_PLA_LANG_TABLE[1];
+#define MSG_PLEASE_LOAD_PLA LANG_TABLE_SELECT_EXPLICIT(MSG_PLEASE_LOAD_PLA_LANG_TABLE, 0)
 extern const char* const MSG_PLEASE_WAIT_LANG_TABLE[LANG_NUM];
 #define MSG_PLEASE_WAIT LANG_TABLE_SELECT(MSG_PLEASE_WAIT_LANG_TABLE)
 extern const char* const MSG_POSITION_UNKNOWN_LANG_TABLE[1];
@@ -626,6 +632,8 @@ extern const char* const MSG_USED_LANG_TABLE[LANG_NUM];
 #define MSG_USED LANG_TABLE_SELECT(MSG_USED_LANG_TABLE)
 extern const char* const MSG_USERWAIT_LANG_TABLE[LANG_NUM];
 #define MSG_USERWAIT LANG_TABLE_SELECT(MSG_USERWAIT_LANG_TABLE)
+extern const char* const MSG_V2_CALIBRATION_LANG_TABLE[1];
+#define MSG_V2_CALIBRATION LANG_TABLE_SELECT_EXPLICIT(MSG_V2_CALIBRATION_LANG_TABLE, 0)
 extern const char* const MSG_VMIN_LANG_TABLE[1];
 #define MSG_VMIN LANG_TABLE_SELECT_EXPLICIT(MSG_VMIN_LANG_TABLE, 0)
 extern const char* const MSG_VOLUMETRIC_LANG_TABLE[1];
@@ -640,6 +648,46 @@ extern const char* const MSG_WATCH_LANG_TABLE[LANG_NUM];
 #define MSG_WATCH LANG_TABLE_SELECT(MSG_WATCH_LANG_TABLE)
 extern const char* const MSG_WATCHDOG_RESET_LANG_TABLE[1];
 #define MSG_WATCHDOG_RESET LANG_TABLE_SELECT_EXPLICIT(MSG_WATCHDOG_RESET_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_LANG_TABLE[1];
+#define MSG_WIZARD LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_CALIBRATION_FAILED_LANG_TABLE[1];
+#define MSG_WIZARD_CALIBRATION_FAILED LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_CALIBRATION_FAILED_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_CLEAN_HEATBED_LANG_TABLE[1];
+#define MSG_WIZARD_CLEAN_HEATBED LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_CLEAN_HEATBED_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_DONE_LANG_TABLE[1];
+#define MSG_WIZARD_DONE LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_DONE_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_FILAMENT_LOADED_LANG_TABLE[1];
+#define MSG_WIZARD_FILAMENT_LOADED LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_FILAMENT_LOADED_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_HEATING_LANG_TABLE[1];
+#define MSG_WIZARD_HEATING LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_HEATING_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_INSERT_CORRECT_FILAMENT_LANG_TABLE[1];
+#define MSG_WIZARD_INSERT_CORRECT_FILAMENT LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_INSERT_CORRECT_FILAMENT_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_LANGUAGE_LANG_TABLE[1];
+#define MSG_WIZARD_LANGUAGE LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_LANGUAGE_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_LOAD_FILAMENT_LANG_TABLE[1];
+#define MSG_WIZARD_LOAD_FILAMENT LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_LOAD_FILAMENT_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_PLA_FILAMENT_LANG_TABLE[1];
+#define MSG_WIZARD_PLA_FILAMENT LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_PLA_FILAMENT_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_QUIT_LANG_TABLE[1];
+#define MSG_WIZARD_QUIT LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_QUIT_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_REPEAT_V2_CAL_LANG_TABLE[1];
+#define MSG_WIZARD_REPEAT_V2_CAL LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_REPEAT_V2_CAL_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_RERUN_LANG_TABLE[1];
+#define MSG_WIZARD_RERUN LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_RERUN_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_SELFTEST_LANG_TABLE[1];
+#define MSG_WIZARD_SELFTEST LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_SELFTEST_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_V2_CAL_LANG_TABLE[1];
+#define MSG_WIZARD_V2_CAL LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_V2_CAL_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_V2_CAL_2_LANG_TABLE[1];
+#define MSG_WIZARD_V2_CAL_2 LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_V2_CAL_2_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_WELCOME_LANG_TABLE[1];
+#define MSG_WIZARD_WELCOME LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_WELCOME_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_WILL_PREHEAT_LANG_TABLE[1];
+#define MSG_WIZARD_WILL_PREHEAT LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_WILL_PREHEAT_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_XYZ_CAL_LANG_TABLE[1];
+#define MSG_WIZARD_XYZ_CAL LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_XYZ_CAL_LANG_TABLE, 0)
+extern const char* const MSG_WIZARD_Z_CAL_LANG_TABLE[1];
+#define MSG_WIZARD_Z_CAL LANG_TABLE_SELECT_EXPLICIT(MSG_WIZARD_Z_CAL_LANG_TABLE, 0)
 extern const char* const MSG_XYZ_DETAILS_LANG_TABLE[LANG_NUM];
 #define MSG_XYZ_DETAILS LANG_TABLE_SELECT(MSG_XYZ_DETAILS_LANG_TABLE)
 extern const char* const MSG_X_MAX_LANG_TABLE[1];

+ 25 - 1
Firmware/language_en.h

@@ -315,4 +315,28 @@
 #define(length=17, lines=1) MSG_SORT_TIME						"Sort: [Time]"
 #define(length=17, lines=1) MSG_SORT_ALPHA						"Sort: [Alphabet]"
 #define(length=17, lines=1) MSG_SORT_NONE						"Sort: [None]"
-#define(length=20, lines=1) MSG_SORTING							"Sorting files"
+#define(length=20, lines=1) MSG_SORTING							"Sorting files"
+#define MSG_FILE_INCOMPLETE										"File incomplete. Continue anyway?"
+#define(length=17, lines=1) MSG_WIZARD							"Wizard"
+#define	MSG_WIZARD_LANGUAGE					"Please choose your language"
+#define	MSG_WIZARD_WELCOME					"Hi, I am your Original Prusa i3 printer. Would you like me to guide you through the setup process?"
+#define	MSG_WIZARD_QUIT						"You can always resume the Wizard from Calibration -> Wizard."
+#define MSG_WIZARD_SELFTEST					"First, I will run the selftest to check most common assembly problems."
+#define MSG_WIZARD_CALIBRATION_FAILED		"Please check our handbook and fix the problem. Then resume the Wizard by rebooting the printer."
+#define MSG_WIZARD_XYZ_CAL					"I will run xyz calibration now. It will take approx. 12 mins."
+#define MSG_WIZARD_FILAMENT_LOADED		"Is filament loaded?"
+#define MSG_WIZARD_Z_CAL					"I will run z calibration now."
+#define MSG_WIZARD_WILL_PREHEAT				"Now I will preheat nozzle for PLA."
+#define MSG_WIZARD_HEATING					"Preheating nozzle. Please wait."
+#define MSG_WIZARD_V2_CAL					"Now I will calibrate distance between tip of the nozzle and heatbed surface."
+#define MSG_WIZARD_V2_CAL_2					"I will start to print line and you will gradually lower the nozzle by rotating the knob, until you reach optimal height. Check the pictures in our handbook in chapter Calibration."
+#define MSG_V2_CALIBRATION					"First layer cal."
+#define MSG_WIZARD_DONE						"All is done. Happy printing!"
+#define MSG_WIZARD_LOAD_FILAMENT			"Please insert PLA filament to the extruder, then press knob to load it."
+#define MSG_WIZARD_RERUN					"Running Wizard will delete current calibration results and start from the beginning. Continue?"
+#define MSG_WIZARD_REPEAT_V2_CAL					"Do you want to repeat last step to readjust distance between nozzle and heatbed?"
+#define MSG_WIZARD_CLEAN_HEATBED					"Please clean heatbed and then press the knob."
+#define MSG_WIZARD_PLA_FILAMENT					"Is it PLA filament?"
+#define MSG_WIZARD_INSERT_CORRECT_FILAMENT			"Please load PLA filament and then resume Wizard by rebooting the printer."
+#define MSG_PLA_FILAMENT_LOADED				"Is PLA filament loaded?"
+#define MSG_PLEASE_LOAD_PLA					"Please load PLA filament first."

+ 8 - 7
Firmware/temperature.cpp

@@ -220,7 +220,7 @@ unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
 		  pid_cycle = 0;
           return;
         }
-	
+
   SERIAL_ECHOLN("PID Autotune start");
   
   disable_heater(); // switch off all heaters.
@@ -336,13 +336,13 @@ unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
         p=soft_pwm_bed;       
         SERIAL_PROTOCOLPGM("ok B:");
       }else{
-        p=soft_pwm[extruder];       
-        SERIAL_PROTOCOLPGM("ok T:");
+        p=soft_pwm[extruder]; 		
+		SERIAL_PROTOCOLPGM("ok T:");
       }
-			
-      SERIAL_PROTOCOL(input);   
-      SERIAL_PROTOCOLPGM(" @:");
-      SERIAL_PROTOCOLLN(p);       
+	  
+		SERIAL_PROTOCOL(input);   
+		SERIAL_PROTOCOLPGM(" @:");
+		SERIAL_PROTOCOLLN(p);       
 
       temp_millis = millis();
     }
@@ -686,6 +686,7 @@ void manage_heater()
 		    	 volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]=0.01;
 	}
 #endif
+  host_keepalive();
 }
 
 #define PGM_RD_W(x)   (short)pgm_read_word(&x)

+ 617 - 47
Firmware/ultralcd.cpp

@@ -105,7 +105,7 @@ int lcd_commands_step=0;
 bool isPrintPaused = false;
 uint8_t farm_mode = 0;
 int farm_no = 0;
-int farm_timer = 30;
+int farm_timer = 8;
 int farm_status = 0;
 unsigned long allert_timer = millis();
 bool printer_connected = true;
@@ -408,15 +408,15 @@ static void lcd_status_screen()
 		farm_timer--;
 		if (farm_timer < 1)
 		{
-			farm_timer = 180;
+			farm_timer = 10;
 			prusa_statistics(0);
 		}
 		switch (farm_timer)
 		{
-		case 45:
+		case 8:
 			prusa_statistics(21);
 			break;
-		case 10:
+		case 5:
 			if (IS_SD_PRINTING)
 			{
 				prusa_statistics(20);
@@ -589,6 +589,251 @@ void lcd_commands()
 			lcd_commands_step = 3;
 		}
 	}
+#ifdef SNMM
+	if (lcd_commands_type == LCD_COMMAND_V2_CAL)
+	{
+		lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+		if (lcd_commands_step == 0)
+		{
+			lcd_commands_step = 6;
+		}
+		if (lcd_commands_step == 6 && !blocks_queued() && cmd_buffer_empty())
+		{
+			enquecommand_P(PSTR("M107"));
+			enquecommand_P(PSTR("M104 S210"));
+			enquecommand_P(PSTR("M140 S55"));
+			enquecommand_P(PSTR("M190 S55"));
+			enquecommand_P(PSTR("M109 S210"));
+			enquecommand_P(PSTR("T0"));
+			enquecommand_P(PSTR("M117 First layer cal."));
+			enquecommand_P(PSTR("G87")); //sets calibration status
+			enquecommand_P(PSTR("G28"));
+			enquecommand_P(PSTR("G21")); //set units to millimeters
+			enquecommand_P(PSTR("G90")); //use absolute coordinates
+			enquecommand_P(PSTR("M83")); //use relative distances for extrusion
+			enquecommand_P(PSTR("G92 E0"));
+			enquecommand_P(PSTR("M203 E100"));
+			enquecommand_P(PSTR("M92 E140"));
+			lcd_commands_step = 5;
+		}
+		if (lcd_commands_step == 5 && !blocks_queued() && cmd_buffer_empty())
+		{
+			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			enquecommand_P(PSTR("G1 Z0.250 F7200.000"));
+			enquecommand_P(PSTR("G1 X50.0 E80.0  F1000.0));
+			nquecommand_P(PSTR("G1 X160.0 E20.0  F1000.0));
+			enquecommand_P(PSTR("G1 Z0.200 F7200.000));
+			enquecommand_P(PSTR("G1 X220.0 E13 F1000.0"));
+			enquecommand_P(PSTR("G1 X240.0 E0 F1000.0"));
+			enquecommand_P(PSTR("G92 E0.0"));
+			enquecommand_P(PSTR("G21"));
+			enquecommand_P(PSTR("G90"));
+			enquecommand_P(PSTR("M83"));
+			enquecommand_P(PSTR("G1 E-4 F2100.00000"));
+			enquecommand_P(PSTR("G1 Z0.150 F7200.000"));
+			enquecommand_P(PSTR("M204 S1000"));
+			enquecommand_P(PSTR("G1 F4000"));
+
+			lcd_implementation_clear();
+			lcd_goto_menu(lcd_babystep_z, 0, false);
+
+
+			lcd_commands_step = 4;
+		}
+		if (lcd_commands_step == 4 && !blocks_queued() && cmd_buffer_empty()) //draw meander
+		{
+			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+
+			
+			enquecommand_P(PSTR("G1 X50 Y155"));
+			enquecommand_P(PSTR("G1 X60 Y155 E4"));
+			enquecommand_P(PSTR("G1 F1080"));
+			enquecommand_P(PSTR("G1 X75 Y155 E2.5"));
+			enquecommand_P(PSTR("G1 X100 Y155 E2"));
+			enquecommand_P(PSTR("G1 X200 Y155 E2.62773"));
+			enquecommand_P(PSTR("G1 X200 Y135 E0.66174"));
+			enquecommand_P(PSTR("G1 X50 Y135 E3.62773"));
+			enquecommand_P(PSTR("G1 X50 Y115 E0.49386"));
+			enquecommand_P(PSTR("G1 X200 Y115 E3.62773"));
+			enquecommand_P(PSTR("G1 X200 Y95 E0.49386"));
+			enquecommand_P(PSTR("G1 X50 Y95 E3.62773"));
+			enquecommand_P(PSTR("G1 X50 Y75 E0.49386"));
+			enquecommand_P(PSTR("G1 X200 Y75 E3.62773"));
+			enquecommand_P(PSTR("G1 X200 Y55 E0.49386"));
+			enquecommand_P(PSTR("G1 X50 Y55 E3.62773"));
+			enquecommand_P(PSTR("G1 E - 0.07500 F2100.00000"));
+			lcd_commands_step = 3;
+		}
+
+		if (lcd_commands_step == 3 && !blocks_queued() && cmd_buffer_empty())
+		{
+			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+
+			enquecommand_P(PSTR("G4 S0"));
+			enquecommand_P(PSTR("G1 E-4 F2100.00000"));
+			enquecommand_P(PSTR("G1 Z0.5 F7200.000"));
+			enquecommand_P(PSTR("G1 X245 Y1"));
+			enquecommand_P(PSTR("G1 X240 E4"));
+			enquecommand_P(PSTR("G1 F4000"));
+			enquecommand_P(PSTR("G1 X190 E2.7"));
+			enquecommand_P(PSTR("G1 F4600"));
+			enquecommand_P(PSTR("G1 X110 E2.8"));
+			enquecommand_P(PSTR("G1 F5200"));
+			enquecommand_P(PSTR("G1 X40 E3"));
+			enquecommand_P(PSTR("G1 E-15.0000 F5000"));
+			enquecommand_P(PSTR("G1 E-50.0000 F5400"));
+			enquecommand_P(PSTR("G1 E-15.0000 F3000"));
+			enquecommand_P(PSTR("G1 E-12.0000 F2000"));
+			enquecommand_P(PSTR("G1 F1600"));
+
+			lcd_commands_step = 2;
+		}
+		if (lcd_commands_step == 2 && !blocks_queued() && cmd_buffer_empty())
+		{
+			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			
+			enquecommand_P(PSTR("G1 X0 Y1 E3.0000"));
+			enquecommand_P(PSTR("G1 X50 Y1 E-5.0000"));
+			enquecommand_P(PSTR("G1 F2000"));
+			enquecommand_P(PSTR("G1 X0 Y1 E5.0000"));
+			enquecommand_P(PSTR("G1 X50 Y1 E-5.0000"));
+			enquecommand_P(PSTR("G1 F2400"));
+			enquecommand_P(PSTR("G1 X0 Y1 E5.0000"));
+			enquecommand_P(PSTR("G1 X50 Y1 E - 5.0000"));
+			enquecommand_P(PSTR("G1 F2400"));
+			enquecommand_P(PSTR("G1 X0 Y1 E5.0000"));
+			enquecommand_P(PSTR("G1 X50 Y1 E-3.0000"));
+			enquecommand_P(PSTR("G4 S0"));
+			enquecommand_P(PSTR("M107"));
+			enquecommand_P(PSTR("M104 S0"));
+			enquecommand_P(PSTR("M140 S0"));
+			enquecommand_P(PSTR("G1 X10 Y180 F4000"));
+			enquecommand_P(PSTR("G1 Z10 F1300.000"));
+			enquecommand_P(PSTR("M84"));
+
+			lcd_commands_step = 1;
+
+		}
+
+		if (lcd_commands_step == 1 && !blocks_queued() && cmd_buffer_empty())
+		{
+			lcd_setstatuspgm(WELCOME_MSG);
+			lcd_commands_step = 0;
+			lcd_commands_type = 0;
+			if (eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) == 1) {
+				lcd_wizard(10);
+			}
+		}
+
+	}
+
+#else //if not SNMM
+
+
+	if (lcd_commands_type == LCD_COMMAND_V2_CAL) 
+	{
+		lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+		if (lcd_commands_step == 0)
+		{
+			lcd_commands_step = 5;
+		}
+		if (lcd_commands_step == 5 && !blocks_queued() && cmd_buffer_empty())
+		{
+			enquecommand_P(PSTR("M107"));
+			enquecommand_P(PSTR("M104 S210"));
+			enquecommand_P(PSTR("M140 S55"));
+			enquecommand_P(PSTR("M190 S55"));
+			enquecommand_P(PSTR("M109 S210"));
+			enquecommand_P(PSTR("M117 First layer cal."));
+			enquecommand_P(PSTR("G87")); //sets calibration status
+			enquecommand_P(PSTR("G28"));
+			enquecommand_P(PSTR("G92 E0.0"));
+			lcd_commands_step = 4;
+		}
+		if (lcd_commands_step == 4 && !blocks_queued() && cmd_buffer_empty())
+		{
+			
+			lcd_implementation_clear();
+			lcd_goto_menu(lcd_babystep_z, 0, false);			
+			enquecommand_P(PSTR("G1 X60.0 E9.0  F1000.0")); //intro line
+			enquecommand_P(PSTR("G1 X100.0 E12.5  F1000.0")); //intro line			
+			enquecommand_P(PSTR("G92 E0.0"));
+			enquecommand_P(PSTR("G21")); //set units to millimeters
+			enquecommand_P(PSTR("G90")); //use absolute coordinates
+			enquecommand_P(PSTR("M83")); //use relative distances for extrusion
+			enquecommand_P(PSTR("G1 E - 1.50000 F2100.00000"));
+			enquecommand_P(PSTR("G1 Z0.150 F7200.000"));
+			enquecommand_P(PSTR("M204 S1000")); //set acceleration
+			enquecommand_P(PSTR("G1 F4000"));
+			lcd_commands_step = 3;
+		}
+		if (lcd_commands_step == 3 && !blocks_queued() && cmd_buffer_empty()) //draw meander
+		{
+			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+		
+
+			//just opposite direction
+			/*enquecommand_P(PSTR("G1 X50 Y55"));
+			enquecommand_P(PSTR("G1 F1080"));
+			enquecommand_P(PSTR("G1 X200 Y55 E3.62773"));
+			enquecommand_P(PSTR("G1 X200 Y75 E0.49386"));
+			enquecommand_P(PSTR("G1 X50 Y75 E3.62773"));
+			enquecommand_P(PSTR("G1 X50 Y95 E0.49386"));
+			enquecommand_P(PSTR("G1 X200 Y95 E3.62773"));
+			enquecommand_P(PSTR("G1 X200 Y115 E0.49386"));
+			enquecommand_P(PSTR("G1 X50 Y115 E3.62773"));
+			enquecommand_P(PSTR("G1 X50 Y135 E0.49386"));
+			enquecommand_P(PSTR("G1 X200 Y135 E3.62773"));
+			enquecommand_P(PSTR("G1 X200 Y155 E0.66174"));
+			enquecommand_P(PSTR("G1 X100 Y155 E2.62773"));
+			enquecommand_P(PSTR("G1 X75 Y155 E2"));
+			enquecommand_P(PSTR("G1 X50 Y155 E2.5"));
+			enquecommand_P(PSTR("G1 E - 0.07500 F2100.00000"));*/
+
+
+			enquecommand_P(PSTR("G1 X50 Y155"));
+			enquecommand_P(PSTR("G1 F1080"));
+			enquecommand_P(PSTR("G1 X75 Y155 E2.5"));
+			enquecommand_P(PSTR("G1 X100 Y155 E2"));
+			enquecommand_P(PSTR("G1 X200 Y155 E2.62773"));
+			enquecommand_P(PSTR("G1 X200 Y135 E0.66174"));
+			enquecommand_P(PSTR("G1 X50 Y135 E3.62773"));
+			enquecommand_P(PSTR("G1 X50 Y115 E0.49386"));
+			enquecommand_P(PSTR("G1 X200 Y115 E3.62773"));
+			enquecommand_P(PSTR("G1 X200 Y95 E0.49386"));
+			enquecommand_P(PSTR("G1 X50 Y95 E3.62773"));
+			enquecommand_P(PSTR("G1 X50 Y75 E0.49386"));
+			enquecommand_P(PSTR("G1 X200 Y75 E3.62773"));
+			enquecommand_P(PSTR("G1 X200 Y55 E0.49386"));
+			enquecommand_P(PSTR("G1 X50 Y55 E3.62773"));
+			enquecommand_P(PSTR("G1 E - 0.07500 F2100.00000"));
+			lcd_commands_step = 2;
+		}
+
+		if (lcd_commands_step == 2 && !blocks_queued() && cmd_buffer_empty())
+		{
+			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			enquecommand_P(PSTR("M107")); //turn off printer fan
+			enquecommand_P(PSTR("M104 S0")); // turn off temperature
+			enquecommand_P(PSTR("M140 S0")); // turn off heatbed
+			enquecommand_P(PSTR("G1 Z10 F1300.000"));
+			enquecommand_P(PSTR("G1 X10 Y180 F4000")); //home X axis
+			enquecommand_P(PSTR("M84"));// disable motors
+			lcd_commands_step = 1;
+		}
+		if (lcd_commands_step == 1 && !blocks_queued() && cmd_buffer_empty())
+		{		
+			lcd_setstatuspgm(WELCOME_MSG);
+			lcd_commands_step = 0;
+			lcd_commands_type = 0;
+			if (eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) == 1) {
+				lcd_wizard(10);
+			}
+		}
+
+	}
+
+#endif // not SNMM
 
 	if (lcd_commands_type == LCD_COMMAND_STOP_PRINT)   /// stop print
 	{
@@ -1283,13 +1528,14 @@ void lcd_menu_statistics()
 		lcd.print(itostr3(_days));
 
 
-
+		KEEPALIVE_STATE(PAUSED_FOR_USER);
 		while (!lcd_clicked())
 		{
 			manage_heater();
 			manage_inactivity(true);
 			delay(100);
 		}
+		KEEPALIVE_STATE(NOT_BUSY);
 
 		lcd_quick_feedback();
 		lcd_return_to_status();
@@ -1367,6 +1613,7 @@ void lcd_service_mode_show_result() {
 		} else lcd_print_at_PGM(11, i + 1, PSTR("N/A"));
 	}
 	delay_keep_alive(500);
+	KEEPALIVE_STATE(PAUSED_FOR_USER);
 	while (!lcd_clicked()) {
 		delay_keep_alive(100);
 	}
@@ -1393,6 +1640,7 @@ void lcd_service_mode_show_result() {
 	while (!lcd_clicked()) {
 		delay_keep_alive(100);
 	}
+	KEEPALIVE_STATE(NOT_BUSY);
 	delay_keep_alive(500);
 	lcd_set_custom_characters_arrows();
 	lcd_return_to_status();
@@ -1848,13 +2096,13 @@ const char* lcd_display_message_fullscreen_P(const char *msg, uint8_t &nlines)
     }
 
     if (multi_screen) {
-        // Display the "next screen" indicator character.
-        // lcd_set_custom_characters_arrows();
-        lcd_set_custom_characters_nextpage();
-        lcd.setCursor(19, 3);
+		// Display the "next screen" indicator character.
+		// lcd_set_custom_characters_arrows();
+		lcd_set_custom_characters_nextpage();
+		lcd.setCursor(19, 3);
         // Display the down arrow.
         lcd.print(char(1));
-    }
+	}
 
     nlines = row;
     return multi_screen ? msgend : NULL;
@@ -1865,8 +2113,15 @@ void lcd_show_fullscreen_message_and_wait_P(const char *msg)
     const char *msg_next = lcd_display_message_fullscreen_P(msg);
     bool multi_screen = msg_next != NULL;
 
+	lcd_set_custom_characters_nextpage();
+	KEEPALIVE_STATE(PAUSED_FOR_USER);
     // Until confirmed by a button click.
     for (;;) {
+		if (!multi_screen) {
+			lcd.setCursor(19, 3);
+			// Display the confirm char.
+			lcd.print(char(2));
+		}
         // Wait for 5 seconds before displaying the next text.
         for (uint8_t i = 0; i < 100; ++ i) {
             delay_keep_alive(50);
@@ -1874,19 +2129,30 @@ void lcd_show_fullscreen_message_and_wait_P(const char *msg)
                 while (lcd_clicked()) ;
                 delay(10);
                 while (lcd_clicked()) ;
+				lcd_set_custom_characters();
+				lcd_update_enable(true);
+				lcd_update(2);
+				KEEPALIVE_STATE(IN_HANDLER);
                 return;
             }
         }
         if (multi_screen) {
-            if (msg_next == NULL)
-                msg_next = msg;
+			if (msg_next == NULL) 
+				msg_next = msg;
             msg_next = lcd_display_message_fullscreen_P(msg_next);
-        }
+			if (msg_next == NULL) {
+				
+				lcd.setCursor(19, 3);
+				// Display the confirm char.
+				lcd.print(char(2));
+			}
+		}
     }
 }
 
 void lcd_wait_for_click()
 {
+	KEEPALIVE_STATE(PAUSED_FOR_USER);
     for (;;) {
         manage_heater();
         manage_inactivity(true);
@@ -1894,14 +2160,81 @@ void lcd_wait_for_click()
             while (lcd_clicked()) ;
             delay(10);
             while (lcd_clicked()) ;
+			KEEPALIVE_STATE(IN_HANDLER);
             return;
         }
     }
 }
 
-int8_t lcd_show_fullscreen_message_yes_no_and_wait_P(const char *msg, bool allow_timeouting, bool default_yes)
+int8_t lcd_show_multiscreen_message_yes_no_and_wait_P(const char *msg, bool allow_timeouting, bool default_yes) //currently just max. n*4 + 3 lines supported (set in language header files)
 {
+	const char *msg_next = lcd_display_message_fullscreen_P(msg);
+	bool multi_screen = msg_next != NULL;
+	bool yes = default_yes ? true : false;
+
+	// Wait for user confirmation or a timeout.
+	unsigned long previous_millis_cmd = millis();
+	int8_t        enc_dif = encoderDiff;
+	KEEPALIVE_STATE(PAUSED_FOR_USER);
+	for (;;) {
+		for (uint8_t i = 0; i < 100; ++i) {
+			delay_keep_alive(50);
+			if (allow_timeouting && millis() - previous_millis_cmd > LCD_TIMEOUT_TO_STATUS)
+				return -1;
+			manage_heater();
+			manage_inactivity(true);
+			
+			if (abs(enc_dif - encoderDiff) > 4) {
+				if (msg_next == NULL) {
+					lcd.setCursor(0, 3);
+					if (enc_dif < encoderDiff && yes) {
+						lcd_printPGM((PSTR(" ")));
+						lcd.setCursor(7, 3);
+						lcd_printPGM((PSTR(">")));
+						yes = false;
+					}
+					else if (enc_dif > encoderDiff && !yes) {
+						lcd_printPGM((PSTR(">")));
+						lcd.setCursor(7, 3);
+						lcd_printPGM((PSTR(" ")));
+						yes = true;
+					}
+					enc_dif = encoderDiff;
+				}
+				else {
+					break; //turning knob skips waiting loop
+				}
+			}
+			if (lcd_clicked()) {
+				while (lcd_clicked());
+				delay(10);
+				while (lcd_clicked());
+				KEEPALIVE_STATE(IN_HANDLER);
+				if(msg_next == NULL) return yes;
+				else break;
+			}
+		}
+		if (multi_screen) {
+			if (msg_next == NULL) {
+				msg_next = msg;
+			}
+			msg_next = lcd_display_message_fullscreen_P(msg_next);
+		}
+		if (msg_next == NULL){
+			lcd.setCursor(0, 3);
+			if (yes) lcd_printPGM(PSTR(">"));
+			lcd.setCursor(1, 3);
+			lcd_printPGM(MSG_YES);
+			lcd.setCursor(7, 3);
+			if (!yes) lcd_printPGM(PSTR(">"));
+			lcd.setCursor(8, 3);
+			lcd_printPGM(MSG_NO);
+		}
+	}
+}
 
+int8_t lcd_show_fullscreen_message_yes_no_and_wait_P(const char *msg, bool allow_timeouting, bool default_yes)
+{
 	lcd_display_message_fullscreen_P(msg);
 	
 	if (default_yes) {
@@ -1923,6 +2256,7 @@ int8_t lcd_show_fullscreen_message_yes_no_and_wait_P(const char *msg, bool allow
 	// Wait for user confirmation or a timeout.
 	unsigned long previous_millis_cmd = millis();
 	int8_t        enc_dif = encoderDiff;
+	KEEPALIVE_STATE(PAUSED_FOR_USER);
 	for (;;) {
 		if (allow_timeouting && millis() - previous_millis_cmd > LCD_TIMEOUT_TO_STATUS)
 			return -1;
@@ -1948,6 +2282,7 @@ int8_t lcd_show_fullscreen_message_yes_no_and_wait_P(const char *msg, bool allow
 			while (lcd_clicked());
 			delay(10);
 			while (lcd_clicked());
+			KEEPALIVE_STATE(IN_HANDLER);
 			return yes;
 		}
 	}
@@ -2158,7 +2493,7 @@ void prusa_statistics(int _message, uint8_t _fil_nr) {
 		prusa_stat_printerstatus(status_number);
 		prusa_stat_farm_number();
 		SERIAL_ECHOLN("}");
-		farm_timer = 5;
+		farm_timer = 4;
 		break;
 	case 21: // temperatures
 		SERIAL_ECHO("{");
@@ -2185,7 +2520,7 @@ void prusa_statistics(int _message, uint8_t _fil_nr) {
 		SERIAL_ECHOLN("}");
 		break;
 	case 92: // Error - Min temp
-		SERIAL_ECHOLN("{[ERR:3]");
+		SERIAL_ECHO("{[ERR:3]");
 		prusa_stat_farm_number();
 		SERIAL_ECHOLN("}");
 		break;
@@ -2610,6 +2945,202 @@ void lcd_toshiba_flash_air_compatibility_toggle()
    eeprom_update_byte((uint8_t*)EEPROM_TOSHIBA_FLASH_AIR_COMPATIBLITY, card.ToshibaFlashAir_isEnabled());
 }
 
+void lcd_v2_calibration() {
+		bool loaded = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_PLA_FILAMENT_LOADED, false, true);
+		if (loaded) {
+			lcd_commands_type = LCD_COMMAND_V2_CAL;
+		}
+		else {
+			lcd_display_message_fullscreen_P(MSG_PLEASE_LOAD_PLA);
+			for (int i = 0; i < 20; i++) { //wait max. 2s
+				delay_keep_alive(100);
+				if (lcd_clicked()) {
+					while (lcd_clicked());
+					delay(10);
+					while (lcd_clicked());
+					break;
+				}
+			}
+		}
+		lcd_return_to_status();
+		lcd_update_enable(true);		
+}
+
+void lcd_wizard() {
+	bool result = true;
+	if(calibration_status() != CALIBRATION_STATUS_ASSEMBLED){
+		result = lcd_show_multiscreen_message_yes_no_and_wait_P(MSG_WIZARD_RERUN, true ,false);
+	}
+	if (result) {
+		calibration_status_store(CALIBRATION_STATUS_ASSEMBLED);
+		lcd_wizard(0);
+	}
+	else {
+		lcd_update_enable(true);
+		lcd_update(2);
+	}
+}
+
+void lcd_wizard(int state) {
+
+	bool end = false;
+	int wizard_event;
+	const char *msg = NULL;
+	while (!end) {
+		switch (state) { 
+		case 0: // run wizard?
+			wizard_event = lcd_show_multiscreen_message_yes_no_and_wait_P(MSG_WIZARD_WELCOME, false, true);
+			if (wizard_event) {
+				state = 1;
+				eeprom_write_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 1);
+			}
+			else {
+				eeprom_write_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 0);
+				end = true;
+			}
+			break;
+		case 1: // restore calibration status
+			switch (calibration_status()) {
+			case CALIBRATION_STATUS_ASSEMBLED: state = 2; break; //run selftest
+			case CALIBRATION_STATUS_XYZ_CALIBRATION: state = 3; break; //run xyz cal.
+			case CALIBRATION_STATUS_Z_CALIBRATION: state = 4; break; //run z cal.
+			case CALIBRATION_STATUS_LIVE_ADJUST: state = 5; break; //run live adjust
+			case CALIBRATION_STATUS_CALIBRATED: end = true; eeprom_write_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 0); break;
+			default: state = 2; break; //if calibration status is unknown, run wizard from the beginning
+			}
+			break;
+		case 2: //selftest
+			lcd_show_fullscreen_message_and_wait_P(MSG_WIZARD_SELFTEST);
+			wizard_event = lcd_selftest();
+			if (wizard_event) {
+				calibration_status_store(CALIBRATION_STATUS_XYZ_CALIBRATION);
+				state = 3;
+			}
+			else end = true;
+			break;
+		case 3: //xyz cal.
+			lcd_show_fullscreen_message_and_wait_P(MSG_WIZARD_XYZ_CAL);
+			wizard_event = gcode_M45(false);
+			if (wizard_event) state = 5;
+			else end = true;
+			break;
+		case 4: //z cal.
+			lcd_show_fullscreen_message_and_wait_P(MSG_WIZARD_Z_CAL);
+			wizard_event = gcode_M45(true);
+			if (wizard_event) state = 11; //shipped, no need to set first layer, go to final message directly
+			else end = true;
+			break;
+		case 5: //is filament loaded?
+			//start to preheat nozzle and bed to save some time later
+			setTargetHotend(PLA_PREHEAT_HOTEND_TEMP, 0);
+			setTargetBed(PLA_PREHEAT_HPB_TEMP); 
+			wizard_event = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_WIZARD_FILAMENT_LOADED, false);
+			if (wizard_event) state = 8;
+			else state = 6;
+			break;
+		case 6: //waiting for preheat nozzle for PLA;
+			lcd_display_message_fullscreen_P(MSG_WIZARD_WILL_PREHEAT);
+			current_position[Z_AXIS] = 100; //move in z axis to make space for loading filament
+			plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], homing_feedrate[Z_AXIS] / 60, active_extruder);
+			delay_keep_alive(2000);
+			lcd_display_message_fullscreen_P(MSG_WIZARD_HEATING);
+			while (abs(degHotend(0) - PLA_PREHEAT_HOTEND_TEMP) > 3) {
+				lcd_display_message_fullscreen_P(MSG_WIZARD_HEATING);
+
+				lcd.setCursor(0, 4);
+				lcd.print(LCD_STR_THERMOMETER[0]);
+				lcd.print(ftostr3(degHotend(0)));
+				lcd.print("/");
+				lcd.print(PLA_PREHEAT_HOTEND_TEMP);
+				lcd.print(LCD_STR_DEGREE);
+				lcd_set_custom_characters();
+				delay_keep_alive(1000);
+			}
+			state = 7;
+			break;
+		case 7: //load filament 
+			lcd_show_fullscreen_message_and_wait_P(MSG_WIZARD_LOAD_FILAMENT);
+			lcd_implementation_clear();
+			lcd_print_at_PGM(0,2,MSG_LOADING_FILAMENT);
+			loading_flag = true;
+#ifdef SNMM
+			change_extr(0);
+#endif
+			gcode_M701();
+			state = 9;
+			break;
+		case 8:
+			wizard_event = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_WIZARD_PLA_FILAMENT, false, true);
+			if (wizard_event) state = 9;
+			else end = true;
+			break;
+		case 9:
+			lcd_show_fullscreen_message_and_wait_P(MSG_WIZARD_V2_CAL);
+			lcd_show_fullscreen_message_and_wait_P(MSG_WIZARD_V2_CAL_2);
+			lcd_commands_type = LCD_COMMAND_V2_CAL;
+			end = true;
+			break;
+		case 10: //repeat first layer cal.?
+			wizard_event = lcd_show_multiscreen_message_yes_no_and_wait_P(MSG_WIZARD_REPEAT_V2_CAL, false);
+			if (wizard_event) {
+				calibration_status_store(CALIBRATION_STATUS_LIVE_ADJUST);
+				lcd_show_fullscreen_message_and_wait_P(MSG_WIZARD_CLEAN_HEATBED);
+				state = 9;
+			}
+			else {
+				state = 11;
+			}
+			break;
+		case 11: //we are finished
+			eeprom_write_byte((uint8_t*)EEPROM_WIZARD_ACTIVE, 0);
+			end = true;
+			break;
+
+		default: break;
+		}
+	}
+
+	SERIAL_ECHOPGM("State: ");
+	MYSERIAL.println(state);
+	switch (state) { //final message
+	case 0: //user dont want to use wizard
+		msg = MSG_WIZARD_QUIT; 
+		break;
+
+	case 1: //printer was already calibrated
+		msg = MSG_WIZARD_DONE;
+		break; 
+	case 2: //selftest
+		msg = MSG_WIZARD_CALIBRATION_FAILED;
+		break;
+	case 3: //xyz cal.
+		msg = MSG_WIZARD_CALIBRATION_FAILED;
+		break;
+	case 4: //z cal.
+		msg = MSG_WIZARD_CALIBRATION_FAILED;
+		break;
+	case 8: 
+		msg = MSG_WIZARD_INSERT_CORRECT_FILAMENT;
+		break;
+	case 9: break; //exit wizard for v2 calibration, which is implemted in lcd_commands (we need lcd_update running)
+	case 11: //we are finished
+
+		msg = MSG_WIZARD_DONE;
+		lcd_reset_alert_level();
+		lcd_setstatuspgm(WELCOME_MSG);
+		break;
+
+	default: 
+		msg = MSG_WIZARD_QUIT;
+		break;
+	
+	}
+	if(state != 9) lcd_show_fullscreen_message_and_wait_P(msg);
+	lcd_update_enable(true);
+	lcd_return_to_status();
+	lcd_update(2);
+}
+
 static void lcd_settings_menu()
 {
   EEPROM_read(EEPROM_SILENT, (uint8_t*)&SilentModeMenu, sizeof(SilentModeMenu));
@@ -2660,6 +3191,7 @@ static void lcd_calibration_menu()
   MENU_ITEM(back, MSG_MAIN, lcd_main_menu);
   if (!isPrintPaused)
   {
+	MENU_ITEM(gcode, MSG_AUTO_HOME, PSTR("G28 W"));
     MENU_ITEM(function, MSG_SELFTEST, lcd_selftest);
 #ifdef MK1BP
     // MK1
@@ -2667,17 +3199,19 @@ static void lcd_calibration_menu()
     MENU_ITEM(gcode, MSG_HOMEYZ, PSTR("G28 Z"));
 #else //MK1BP
     // MK2
-MENU_ITEM(function, MSG_CALIBRATE_BED, lcd_mesh_calibration);
+	MENU_ITEM(function, MSG_CALIBRATE_BED, lcd_mesh_calibration);
     // "Calibrate Z" with storing the reference values to EEPROM.
     MENU_ITEM(submenu, MSG_HOMEYZ, lcd_mesh_calibration_z);
+	MENU_ITEM(submenu, MSG_V2_CALIBRATION, lcd_v2_calibration);
 	
 #ifndef SNMM
 	//MENU_ITEM(function, MSG_CALIBRATE_E, lcd_calibrate_extruder);
 #endif
     // "Mesh Bed Leveling"
     MENU_ITEM(submenu, MSG_MESH_BED_LEVELING, lcd_mesh_bedleveling);
+	MENU_ITEM(function, MSG_WIZARD, lcd_wizard);
 #endif //MK1BP
-    MENU_ITEM(gcode, MSG_AUTO_HOME, PSTR("G28 W"));
+    
     MENU_ITEM(submenu, MSG_BED_CORRECTION_MENU, lcd_adjust_bed);
 #ifndef MK1BP
 	MENU_ITEM(submenu, MSG_CALIBRATION_PINDA_MENU, lcd_pinda_calibration_menu);
@@ -3001,7 +3535,7 @@ static char snmm_stop_print_menu() { //menu for choosing which filaments will be
 	lcd_print_at_PGM(1,3,MSG_CURRENT);
 	char cursor_pos = 1;
 	int enc_dif = 0;
-
+	KEEPALIVE_STATE(PAUSED_FOR_USER);
 	while (1) {
 		manage_heater();
 		manage_inactivity(true);
@@ -3029,10 +3563,10 @@ static char snmm_stop_print_menu() { //menu for choosing which filaments will be
 			while (lcd_clicked());
 			delay(10);
 			while (lcd_clicked());
+			KEEPALIVE_STATE(IN_HANDLER);
 			return(cursor_pos - 1);
 		}
-	}
-	
+	}	
 }
 
 char choose_extruder_menu() {
@@ -3051,7 +3585,7 @@ char choose_extruder_menu() {
 	for (int i = 0; i < 3; i++) {
 		lcd_print_at_PGM(1, i + 1, MSG_EXTRUDER);
 	}
-
+	KEEPALIVE_STATE(PAUSED_FOR_USER);
 	while (1) {
 
 		for (int i = 0; i < 3; i++) {
@@ -3115,6 +3649,7 @@ char choose_extruder_menu() {
 			while (lcd_clicked());
 			delay(10);
 			while (lcd_clicked());
+			KEEPALIVE_STATE(IN_HANDLER);
 			return(cursor_pos + first - 1);
 			
 		}
@@ -3320,7 +3855,7 @@ void display_loading() {
 	}
 }
 
-static void extr_adj(int extruder) //loading filament for SNMM
+void extr_adj(int extruder) //loading filament for SNMM
 {
 	bool correct;
 	max_feedrate[E_AXIS] =80;
@@ -3334,13 +3869,13 @@ static void extr_adj(int extruder) //loading filament for SNMM
 	case 3: lcd_display_message_fullscreen_P(MSG_FILAMENT_LOADING_T3); break;
 	default: lcd_display_message_fullscreen_P(MSG_FILAMENT_LOADING_T0); break;   
 	}
-			
+	KEEPALIVE_STATE(PAUSED_FOR_USER);
 	do{
 		extr_mov(0.001,1000);
 		delay_keep_alive(2);
 	} while (!lcd_clicked());
 	//delay_keep_alive(500);
-
+	KEEPALIVE_STATE(IN_HANDLER);
 	st_synchronize();
 	//correct = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FIL_LOADED_CHECK, false);
 	//if (!correct) goto	START;
@@ -3656,8 +4191,8 @@ unsigned char lcd_choose_color() {
 	//-----------------------------------------------------
 	unsigned char items_no = 2;
 	const char *item[items_no];
-	item[0] = "Black";
-	item[1] = "Orange";
+	item[0] = "Orange";
+	item[1] = "Black";
 	//-----------------------------------------------------
 	unsigned char active_rows;
 	static int first = 0;
@@ -3679,10 +4214,9 @@ unsigned char lcd_choose_color() {
 
 		manage_heater();
 		manage_inactivity(true);
-
-		if (abs((enc_dif - encoderDiff)) > 4) {
-
-			if ((abs(enc_dif - encoderDiff)) > 1) {
+		proc_commands();
+		if (abs((enc_dif - encoderDiff)) > 12) {
+					
 				if (enc_dif > encoderDiff) {
 					cursor_pos--;
 				}
@@ -3690,7 +4224,7 @@ unsigned char lcd_choose_color() {
 				if (enc_dif < encoderDiff) {
 					cursor_pos++;
 				}
-
+				
 				if (cursor_pos > active_rows) {
 					cursor_pos = active_rows;
 					if (first < items_no - active_rows) {
@@ -3716,7 +4250,6 @@ unsigned char lcd_choose_color() {
 				lcd.print(">");
 				enc_dif = encoderDiff;
 				delay(100);
-			}
 
 		}
 
@@ -3724,7 +4257,11 @@ unsigned char lcd_choose_color() {
 			while (lcd_clicked());
 			delay(10);
 			while (lcd_clicked());
-			return(cursor_pos + first - 1);
+			switch(cursor_pos + first - 1) {
+			case 0: return 1; break;
+			case 1: return 0; break;
+			default: return 99; break;
+			}
 		}
 
 	}
@@ -3739,7 +4276,7 @@ void lcd_confirm_print()
 	int _ret = 0;
 	int _t = 0;
 
-
+	enc_dif = encoderDiff;
 	lcd_implementation_clear();
 
 	lcd.setCursor(0, 0);
@@ -3747,8 +4284,7 @@ void lcd_confirm_print()
 
 	do
 	{
-
-		if (abs((enc_dif - encoderDiff)) > 2) {
+		if (abs(enc_dif - encoderDiff) > 12) {
 			if (enc_dif > encoderDiff) {
 				cursor_pos--;
 			}
@@ -3756,6 +4292,7 @@ void lcd_confirm_print()
 			if (enc_dif < encoderDiff) {
 				cursor_pos++;
 			}
+			enc_dif = encoderDiff;
 		}
 
 		if (cursor_pos > 2) { cursor_pos = 2; }
@@ -3800,9 +4337,10 @@ void lcd_confirm_print()
 				NcTime = millis();
 			}
 		}
-
+		
 		manage_heater();
 		manage_inactivity();
+		proc_commands();
 
 	} while (_ret == 0);
 
@@ -4351,7 +4889,7 @@ menu_edit_type(float, float51, ftostr51, 10)
 menu_edit_type(float, float52, ftostr52, 100)
 menu_edit_type(unsigned long, long5, ftostr5, 0.01)
 
-static void lcd_selftest()
+static bool lcd_selftest()
 {
 	int _progress = 0;
 	bool _result = false;
@@ -4414,8 +4952,10 @@ static void lcd_selftest()
 		current_position[Y_AXIS] = current_position[Y_AXIS] - 14;
 		_progress = lcd_selftest_screen(4, _progress, 3, true, 1500);
 		_result = lcd_selfcheck_axis(2, Z_MAX_POS);
-		enquecommand_P(PSTR("G28 W"));
-		enquecommand_P(PSTR("G1 Z15"));
+		if (eeprom_read_byte((uint8_t*)EEPROM_WIZARD_ACTIVE) != 1) {
+			enquecommand_P(PSTR("G28 W"));
+			enquecommand_P(PSTR("G1 Z15"));
+		}
 	}
 
 	if (_result)
@@ -4438,12 +4978,13 @@ static void lcd_selftest()
 
 	if (_result)
 	{
-		LCD_ALERTMESSAGERPGM(MSG_SELFTEST_OK);				
+		LCD_ALERTMESSAGERPGM(MSG_SELFTEST_OK);	
 	}
 	else
 	{
 		LCD_ALERTMESSAGERPGM(MSG_SELFTEST_FAILED);
 	}
+	return(_result);
 }
 
 static bool lcd_selfcheck_axis(int _axis, int _travel)
@@ -4822,6 +5363,7 @@ static bool lcd_selftest_fan_dialog(int _fan)
 	lcd.setCursor(1, 3); lcd_printPGM(MSG_SELFTEST_FAN_NO);
 
 	int8_t enc_dif = 0;
+	KEEPALIVE_STATE(PAUSED_FOR_USER);
 	do
 	{
 		switch (_fan)
@@ -4862,7 +5404,7 @@ static bool lcd_selftest_fan_dialog(int _fan)
 		delay(100);
 
 	} while (!lcd_clicked());
-
+	KEEPALIVE_STATE(IN_HANDLER);
 	SET_OUTPUT(EXTRUDER_0_AUTO_FAN_PIN);
 	WRITE(EXTRUDER_0_AUTO_FAN_PIN, 0);
 	SET_OUTPUT(FAN_PIN);
@@ -4981,16 +5523,43 @@ static void menu_action_setlang(unsigned char lang) {
 static void menu_action_function(menuFunc_t data) {
   (*data)();
 }
+
+static bool check_file(const char* filename) {
+	bool result = false;
+	uint32_t filesize;
+	card.openFile(filename, true);
+	filesize = card.getFileSize();
+	if (filesize > END_FILE_SECTION) {
+		card.setIndex(filesize - END_FILE_SECTION);
+	}
+
+	while (!card.eof() && !result) {
+		card.sdprinting = true;
+		get_command();
+		result = check_commands();
+	}
+	card.printingHasFinished();
+	strncpy_P(lcd_status_message, WELCOME_MSG, LCD_WIDTH);
+	return result;
+}
+
 static void menu_action_sdfile(const char* filename, char* longFilename)
-{
+{	
   loading_flag = false;
   char cmd[30];
   char* c;
+  bool result = true;
   sprintf_P(cmd, PSTR("M23 %s"), filename);
   for (c = &cmd[4]; *c; c++)
-    *c = tolower(*c);
-  enquecommand(cmd);
-  enquecommand_P(PSTR("M24"));
+	  *c = tolower(*c);
+  if (!check_file(filename)) {
+	  result = lcd_show_fullscreen_message_yes_no_and_wait_P(MSG_FILE_INCOMPLETE, false, false);
+	  lcd_update_enable(true);
+  }  
+  if (result) {	  
+	  enquecommand(cmd);
+	  enquecommand_P(PSTR("M24"));	  
+  }
   lcd_return_to_status();
 }
 static void menu_action_sddirectory(const char* filename, char* longFilename)
@@ -5225,6 +5794,7 @@ void lcd_update(uint8_t lcdDrawUpdateOverride)
 	if (!SdFatUtil::test_stack_integrity()) stack_error();
 	lcd_ping(); //check that we have received ping command if we are in farm mode
 	lcd_send_status();
+	if (lcd_commands_type == LCD_COMMAND_V2_CAL) lcd_commands();
 }
 
 void lcd_printer_connected() {

+ 9 - 3
Firmware/ultralcd.h

@@ -33,7 +33,7 @@ void lcd_mylang();
   bool lcd_detected(void);
 
   
-  static void lcd_selftest();
+  static bool lcd_selftest();
   static bool lcd_selfcheck_endstops();
   static bool lcd_selfcheck_axis(int _axis, int _travel);
   static bool lcd_selfcheck_check_heater(bool _isbed);
@@ -44,6 +44,7 @@ void lcd_mylang();
   void lcd_menu_statistics();
   static bool lcd_selfcheck_pulleys(int axis);
 
+  
   extern const char* lcd_display_message_fullscreen_P(const char *msg, uint8_t &nlines);
   inline const char* lcd_display_message_fullscreen_P(const char *msg) 
     { uint8_t nlines; return lcd_display_message_fullscreen_P(msg, nlines); }
@@ -52,7 +53,7 @@ void lcd_mylang();
   extern void lcd_show_fullscreen_message_and_wait_P(const char *msg);
   // 0: no, 1: yes, -1: timeouted
   extern int8_t lcd_show_fullscreen_message_yes_no_and_wait_P(const char *msg, bool allow_timeouting = true, bool default_yes = false);
-
+  extern int8_t lcd_show_multiscreen_message_yes_no_and_wait_P(const char *msg, bool allow_timeouting = true, bool default_yes = false);
   // Ask the user to move the Z axis up to the end stoppers and let
   // the user confirm that it has been done.
   extern bool lcd_calibrate_z_end_stop_manual(bool only_z);
@@ -95,6 +96,7 @@ void lcd_mylang();
   #define LCD_COMMAND_LONG_PAUSE 5
   #define LCD_COMMAND_LONG_PAUSE_RESUME 6
   #define LCD_COMMAND_PID_EXTRUDER 7 
+  #define LCD_COMMAND_V2_CAL 8
 
   extern unsigned long lcd_timeoutToStatus;
   extern int lcd_commands_type;
@@ -211,7 +213,7 @@ extern void lcd_implementation_print_at(uint8_t x, uint8_t y, const char *str);
 void change_extr(int extr);
 static void lcd_colorprint_change();
 static int get_ext_nr();
-static void extr_adj(int extruder);
+void extr_adj(int extruder);
 static void extr_adj_0();
 static void extr_adj_1();
 static void extr_adj_2();
@@ -227,6 +229,7 @@ void extr_unload_all();
 void extr_unload_used();
 void extr_unload();
 static char snmm_stop_print_menu();
+static void lcd_babystep_z();
 #ifdef SDCARD_SORT_ALPHA
 static void lcd_sort_type_set();
 #endif
@@ -269,4 +272,7 @@ void lcd_set_progress();
 static void lcd_send_status();
 static void lcd_connect_printer();
 
+void lcd_wizard();
+void lcd_wizard(int state);
+
 #endif //ULTRALCD_H

+ 10 - 0
Firmware/ultralcd_implementation_hitachi_HD44780.h

@@ -491,8 +491,18 @@ void lcd_set_custom_characters_nextpage()
     B01010,
     B00100
   }; 
+  byte confirm[8] = {
+	B00000,
+	B00001,
+	B00011,
+	B10110,
+	B11100,
+	B01000,
+	B00000
+  };
 
     lcd.createChar(1, arrdown);
+	lcd.createChar(2, confirm);
 }
 
 void lcd_set_custom_characters_degree()

+ 2 - 0
Firmware/variants/1_75mm_MK1-RAMBo10a-E3Dv6full.h

@@ -403,4 +403,6 @@ THERMISTORS SETTINGS
 
 #define DEFAULT_RETRACTION 1 //used for PINDA temp calibration
 
+#define END_FILE_SECTION 10000 //number of bytes from end of file used for checking if file is complete
+
 #endif //__CONFIGURATION_PRUSA_H

+ 2 - 0
Firmware/variants/1_75mm_MK1-RAMBo13a-E3Dv6full.h

@@ -403,4 +403,6 @@ THERMISTORS SETTINGS
 
 #define DEFAULT_RETRACTION 1 //used for PINDA temp calibration
 
+#define END_FILE_SECTION 10000 //number of bytes from end of file used for checking if file is complete
+
 #endif //__CONFIGURATION_PRUSA_H

+ 2 - 0
Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo10a-E3Dv6full.h

@@ -400,4 +400,6 @@ THERMISTORS SETTINGS
 #define DEFAULT_RETRACTION 1 //used for PINDA temp calibration and pause print
 #endif
 
+#define END_FILE_SECTION 10000 //number of bytes from end of file used for checking if file is complete
+
 #endif //__CONFIGURATION_PRUSA_H

+ 2 - 0
Firmware/variants/1_75mm_MK2-MultiMaterial-RAMBo13a-E3Dv6full.h

@@ -402,4 +402,6 @@ THERMISTORS SETTINGS
 #define DEFAULT_RETRACTION 1 //used for PINDA temp calibration and pause print
 #endif
 
+#define END_FILE_SECTION 10000 //number of bytes from end of file used for checking if file is complete
+
 #endif //__CONFIGURATION_PRUSA_H

+ 2 - 0
Firmware/variants/1_75mm_MK2-RAMBo10a-E3Dv6full.h

@@ -400,4 +400,6 @@ THERMISTORS SETTINGS
 #define DEFAULT_RETRACTION 1 //used for PINDA temp calibration and pause print
 #endif
 
+#define END_FILE_SECTION 10000 //number of bytes from end of file used for checking if file is complete
+
 #endif //__CONFIGURATION_PRUSA_H

+ 2 - 0
Firmware/variants/1_75mm_MK2-RAMBo13a-E3Dv6full.h

@@ -402,4 +402,6 @@ THERMISTORS SETTINGS
 #define DEFAULT_RETRACTION 1 //used for PINDA temp calibration and pause print
 #endif
 
+#define END_FILE_SECTION 10000 //number of bytes from end of file used for checking if file is complete
+
 #endif //__CONFIGURATION_PRUSA_H