Browse Source

Merge branch 'MK3' into fix_compiler_warnings

PavelSindler 5 years ago
parent
commit
85c37d1225

+ 2 - 2
Firmware/Configuration.h

@@ -7,8 +7,8 @@
 #define STR(x) STR_HELPER(x)
 
 // Firmware version
-#define FW_VERSION "3.3.0"
-#define FW_COMMIT_NR   830
+#define FW_VERSION "3.3.1"
+#define FW_COMMIT_NR   845
 // FW_VERSION_UNKNOWN means this is an unofficial build.
 // The firmware should only be checked into github with this symbol.
 #define FW_DEV_VERSION FW_VERSION_UNKNOWN

+ 3 - 1
Firmware/Marlin.h

@@ -455,7 +455,9 @@ void force_high_power_mode(bool start_high_power_section);
 #endif //TMC2130
 
 // G-codes
-void gcode_G28(bool home_x, bool home_y, bool home_z, bool calib);
+void gcode_G28(bool home_x_axis, long home_x_value, bool home_y_axis, long home_y_value, bool home_z_axis, long home_z_value, bool calib, bool without_mbl);
+void gcode_G28(bool home_x_axis, bool home_y_axis, bool home_z_axis);
+
 bool gcode_M45(bool onlyZ, int8_t verbosity_level);
 void gcode_M114();
 void gcode_M701();

+ 48 - 23
Firmware/Marlin_main.cpp

@@ -736,7 +736,7 @@ void crashdet_detected(uint8_t mask)
 	lcd_update_enable(true);
 	lcd_update(2);
 	lcd_setstatuspgm(_T(MSG_CRASH_DETECTED));
-	gcode_G28(true, true, false, false); //home X and Y
+	gcode_G28(true, true, false); //home X and Y
 	st_synchronize();
 
 	if (automatic_recovery_after_crash) {
@@ -1153,10 +1153,6 @@ void list_sec_lang_from_external_flash()
 // are initialized by the main() routine provided by the Arduino framework.
 void setup()
 {
-#ifdef W25X20CL
-  // Enter an STK500 compatible Optiboot boot loader waiting for flashing the languages to an external flash memory.
-  optiboot_w25x20cl_enter();
-#endif
     lcd_init();
 	fdev_setup_stream(lcdout, lcd_putchar, NULL, _FDEV_SETUP_WRITE); //setup lcdout stream
 
@@ -1164,6 +1160,11 @@ void setup()
 
 	lcd_splash();
 
+	#ifdef W25X20CL
+	// Enter an STK500 compatible Optiboot boot loader waiting for flashing the languages to an external flash memory.
+	optiboot_w25x20cl_enter();
+	#endif
+
 #if (LANG_MODE != 0) //secondary language support
 #ifdef W25X20CL
 	if (w25x20cl_init())
@@ -2615,7 +2616,11 @@ void force_high_power_mode(bool start_high_power_section) {
 }
 #endif //TMC2130
 
-void gcode_G28(bool home_x, bool home_y, bool home_z, bool calib) {
+void gcode_G28(bool home_x_axis, bool home_y_axis, bool home_z_axis) {
+	gcode_G28(home_x_axis, 0, home_y_axis, 0, home_z_axis, 0, false, true);
+}
+
+void gcode_G28(bool home_x_axis, long home_x_value, bool home_y_axis, long home_y_value, bool home_z_axis, long home_z_value, bool calib, bool without_mbl) {
 	st_synchronize();
 
 #if 0
@@ -2626,6 +2631,11 @@ void gcode_G28(bool home_x, bool home_y, bool home_z, bool calib) {
 	// Flag for the display update routine and to disable the print cancelation during homing.
 	homing_flag = true;
 
+	// Which axes should be homed?
+	bool home_x = home_x_axis;
+	bool home_y = home_y_axis;
+	bool home_z = home_z_axis;
+
 	// Either all X,Y,Z codes are present, or none of them.
 	bool home_all_axes = home_x == home_y && home_x == home_z;
 	if (home_all_axes)
@@ -2734,11 +2744,11 @@ void gcode_G28(bool home_x, bool home_y, bool home_z, bool calib) {
 #endif //TMC2130
 
 
-      if(code_seen(axis_codes[X_AXIS]) && code_value_long() != 0)
-        current_position[X_AXIS]=code_value()+add_homing[X_AXIS];
+      if(home_x_axis && home_x_value != 0)
+        current_position[X_AXIS]=home_x_value+add_homing[X_AXIS];
 
-      if(code_seen(axis_codes[Y_AXIS]) && code_value_long() != 0)
-		    current_position[Y_AXIS]=code_value()+add_homing[Y_AXIS];
+      if(home_y_axis && home_y_value != 0)
+        current_position[Y_AXIS]=home_y_value+add_homing[Y_AXIS];
 
       #if Z_HOME_DIR < 0                      // If homing towards BED do Z last
         #ifndef Z_SAFE_HOMING
@@ -2833,8 +2843,8 @@ void gcode_G28(bool home_x, bool home_y, bool home_z, bool calib) {
         #endif // Z_SAFE_HOMING
       #endif // Z_HOME_DIR < 0
 
-      if(code_seen(axis_codes[Z_AXIS]) && code_value_long() != 0)
-        current_position[Z_AXIS]=code_value()+add_homing[Z_AXIS];
+      if(home_z_axis && home_z_value != 0)
+        current_position[Z_AXIS]=home_z_value+add_homing[Z_AXIS];
       #ifdef ENABLE_AUTO_BED_LEVELING
         if(home_z)
           current_position[Z_AXIS] += zprobe_zoffset;  //Add Z_Probe offset (the distance is negative)
@@ -2866,7 +2876,7 @@ void gcode_G28(bool home_x, bool home_y, bool home_z, bool calib) {
     world2machine_update_current();
 
 #if (defined(MESH_BED_LEVELING) && !defined(MK1BP))
-	if (code_seen(axis_codes[X_AXIS]) || code_seen(axis_codes[Y_AXIS]) || code_seen('W') || code_seen(axis_codes[Z_AXIS]))
+	if (home_x_axis || home_y_axis || without_mbl || home_z_axis)
 		{
       if (! home_z && mbl_was_active) {
         // Re-enable the mesh bed leveling if only the X and Y axes were re-homed.
@@ -2879,10 +2889,6 @@ void gcode_G28(bool home_x, bool home_y, bool home_z, bool calib) {
 		{
 			st_synchronize();
 			homing_flag = false;
-			// Push the commands to the front of the message queue in the reverse order!
-			// There shall be always enough space reserved for these commands.
-			enquecommand_front_P((PSTR("G80")));
-			//goto case_G80;
 	  }
 #endif
 
@@ -3115,10 +3121,17 @@ void gcode_M701()
 	custom_message = true;
 	custom_message_type = 2;
 
+	
+
 	lcd_setstatuspgm(_T(MSG_LOADING_FILAMENT));
-	current_position[E_AXIS] += 70;
+	current_position[E_AXIS] += 40;
 	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
+	st_synchronize();
 
+	if (current_position[Z_AXIS] < 20) current_position[Z_AXIS] += 30;
+	current_position[E_AXIS] += 30;
+	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
+	st_synchronize();
 	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();
@@ -3338,6 +3351,8 @@ void process_commands()
             // careful!
             if (farm_mode) {
 #ifdef WATCHDOG
+                boot_app_magic = BOOT_APP_MAGIC;
+                boot_app_flags = BOOT_APP_FLG_RUN;
 				wdt_enable(WDTO_15MS);
 				cli();
 				while(1);
@@ -3650,16 +3665,26 @@ void process_commands()
       #endif //FWRETRACT
     case 28: //G28 Home all Axis one at a time
     {
+      long home_x_value = 0;
+      long home_y_value = 0;
+      long home_z_value = 0;
       // Which axes should be homed?
       bool home_x = code_seen(axis_codes[X_AXIS]);
+      home_x_value = code_value_long();
       bool home_y = code_seen(axis_codes[Y_AXIS]);
+      home_y_value = code_value_long();
       bool home_z = code_seen(axis_codes[Z_AXIS]);
+      home_z_value = code_value_long();
+      bool without_mbl = code_seen('W');
       // calibrate?
       bool calib = code_seen('C');
-
-	  gcode_G28(home_x, home_y, home_z, calib);
-	  
-	  break;
+      gcode_G28(home_x, home_x_value, home_y, home_y_value, home_z, home_z_value, calib, without_mbl);
+      if ((home_x || home_y || without_mbl || home_z) == false) {
+         // Push the commands to the front of the message queue in the reverse order!
+         // There shall be always enough space reserved for these commands.
+         goto case_G80;
+      }
+      break;
     }
 #ifdef ENABLE_AUTO_BED_LEVELING
     case 29: // G29 Detailed Z-Probe, probes the bed at 3 or more points.
@@ -3899,7 +3924,7 @@ void process_commands()
 				current_position[X_AXIS] = pgm_read_float(bed_ref_points_4);
 				plan_buffer_line(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS], 3000 / 60, active_extruder);
 				st_synchronize();
-				gcode_G28(false, false, true, false);
+				gcode_G28(false, false, true);
 
 			}
 			if ((current_temperature_pinda > 35) && (farm_mode == false)) {

+ 1 - 0
Firmware/bootapp.h

@@ -17,6 +17,7 @@
 #define BOOT_APP_FLG_ERASE 0x01
 #define BOOT_APP_FLG_COPY  0x02
 #define BOOT_APP_FLG_FLASH 0x04
+#define BOOT_APP_FLG_RUN 0x08
 
 #define BOOT_APP_FLG_USER0 0x80
 

+ 1 - 1
Firmware/cardreader.cpp

@@ -972,7 +972,7 @@ void CardReader::presort() {
 #endif
 	lcd_update(2);
 	KEEPALIVE_STATE(NOT_BUSY);
-	lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+	lcd_timeoutToStatus.start();
 }
 
 void CardReader::flush_presort() {

+ 1 - 0
Firmware/cmdqueue.cpp

@@ -476,6 +476,7 @@ void get_command()
             SERIAL_ERROR_START;
             SERIAL_ERRORRPGM(_n("No Line Number with checksum, Last Line: "));////MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM c=0 r=0
             SERIAL_ERRORLN(gcode_LastN);
+			FlushSerialRequestResend();
             serial_count = 0;
             return;
         }

+ 69 - 50
Firmware/ultralcd.cpp

@@ -10,7 +10,6 @@
 #include "stepper.h"
 #include "ConfigurationStore.h"
 #include <string.h>
-#include "Timer.h"
 
 #include "util.h"
 #include "mesh_bed_leveling.h"
@@ -180,9 +179,10 @@ float pid_temp = DEFAULT_PID_TEMP;
 
 bool long_press_active = false;
 static ShortTimer longPressTimer;
-unsigned long button_blanking_time = millis();
+static ShortTimer buttonBlanking;
 bool button_pressed = false;
 
+static bool forceMenuExpire = false;
 bool menuExiting = false;
 
 #ifdef FILAMENT_LCD_DISPLAY
@@ -990,7 +990,7 @@ void lcd_commands()
 		float extr = count_e(0.2, width, length);
 		float extr_short_segment = count_e(0.2, width, width);
 
-		if (lcd_commands_step>1) lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS; //if user dont confirm live adjust Z value by pressing the knob, we are saving last value by timeout to status screen
+		if (lcd_commands_step>1) lcd_timeoutToStatus.start(); //if user dont confirm live adjust Z value by pressing the knob, we are saving last value by timeout to status screen
 		if (lcd_commands_step == 0)
 		{
 			lcd_commands_step = 10;
@@ -1016,7 +1016,7 @@ void lcd_commands()
 		}
 		if (lcd_commands_step == 9 && !blocks_queued() && cmd_buffer_empty())
 		{
-			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			lcd_timeoutToStatus.start();
 			enquecommand_P(PSTR("G1 Z0.250 F7200.000"));
 			enquecommand_P(PSTR("G1 X50.0 E80.0 F1000.0"));
 			enquecommand_P(PSTR("G1 X160.0 E20.0 F1000.0"));
@@ -1040,7 +1040,7 @@ void lcd_commands()
 		}
 		if (lcd_commands_step == 8 && !blocks_queued() && cmd_buffer_empty()) //draw meander
 		{
-			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			lcd_timeoutToStatus.start();
 
 
 			enquecommand_P(PSTR("G1 X50 Y155"));
@@ -1065,7 +1065,7 @@ void lcd_commands()
 
 		if (lcd_commands_step == 7 && !blocks_queued() && cmd_buffer_empty())
 		{
-			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			lcd_timeoutToStatus.start();
 			strcpy(cmd1, "G1 X50 Y35 E");
 			strcat(cmd1, ftostr43(extr));
 			enquecommand(cmd1);
@@ -1098,7 +1098,7 @@ void lcd_commands()
 
 		if (lcd_commands_step == 6 && !blocks_queued() && cmd_buffer_empty())
 		{
-			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			lcd_timeoutToStatus.start();
 			for (int i = 4; i < 8; i++) {
 				strcpy(cmd1, "G1 X70 Y");
 				strcat(cmd1, ftostr32(35 - i*width * 2));
@@ -1127,7 +1127,7 @@ void lcd_commands()
 
 		if (lcd_commands_step == 5 && !blocks_queued() && cmd_buffer_empty())
 		{
-			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			lcd_timeoutToStatus.start();
 			for (int i = 8; i < 12; i++) {
 				strcpy(cmd1, "G1 X70 Y");
 				strcat(cmd1, ftostr32(35 - i*width * 2));
@@ -1156,7 +1156,7 @@ void lcd_commands()
 
 		if (lcd_commands_step == 4 && !blocks_queued() && cmd_buffer_empty())
 		{
-			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			lcd_timeoutToStatus.start();
 			for (int i = 12; i < 16; i++) {
 				strcpy(cmd1, "G1 X70 Y");
 				strcat(cmd1, ftostr32(35 - i*width * 2));
@@ -1185,7 +1185,7 @@ void lcd_commands()
 
 		if (lcd_commands_step == 3 && !blocks_queued() && cmd_buffer_empty())
 		{
-			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			lcd_timeoutToStatus.start();
 			enquecommand_P(PSTR("G1 E-0.07500 F2100.00000"));
 			enquecommand_P(PSTR("G4 S0"));
 			enquecommand_P(PSTR("G1 E-4 F2100.00000"));
@@ -1254,7 +1254,7 @@ void lcd_commands()
 		float length = 20 - width;
 		float extr = count_e(0.2, width, length);
 		float extr_short_segment = count_e(0.2, width, width);
-		if(lcd_commands_step>1) lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS; //if user dont confirm live adjust Z value by pressing the knob, we are saving last value by timeout to status screen
+		if(lcd_commands_step>1) lcd_timeoutToStatus.start(); //if user dont confirm live adjust Z value by pressing the knob, we are saving last value by timeout to status screen
 		if (lcd_commands_step == 0)
 		{
 			lcd_commands_step = 9;
@@ -1291,7 +1291,7 @@ void lcd_commands()
 		}
 		if (lcd_commands_step == 7 && !blocks_queued() && cmd_buffer_empty()) //draw meander
 		{
-			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			lcd_timeoutToStatus.start();
 
 
 			//just opposite direction
@@ -1339,7 +1339,7 @@ void lcd_commands()
 		if (lcd_commands_step == 6 && !blocks_queued() && cmd_buffer_empty())
 		{
 
-			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			lcd_timeoutToStatus.start();
 
 			for (int i = 0; i < 4; i++) {
 				strcpy(cmd1, "G1 X70 Y");
@@ -1369,7 +1369,7 @@ void lcd_commands()
 
 		if (lcd_commands_step == 5 && !blocks_queued() && cmd_buffer_empty())
 		{
-			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			lcd_timeoutToStatus.start();
 			for (int i = 4; i < 8; i++) {
 				strcpy(cmd1, "G1 X70 Y");
 				strcat(cmd1, ftostr32(35 - i*width * 2));
@@ -1398,7 +1398,7 @@ void lcd_commands()
 
 		if (lcd_commands_step == 4 && !blocks_queued() && cmd_buffer_empty())
 		{
-			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			lcd_timeoutToStatus.start();
 			for (int i = 8; i < 12; i++) {
 				strcpy(cmd1, "G1 X70 Y");
 				strcat(cmd1, ftostr32(35 - i*width * 2));
@@ -1427,7 +1427,7 @@ void lcd_commands()
 
 		if (lcd_commands_step == 3 && !blocks_queued() && cmd_buffer_empty())
 		{
-			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			lcd_timeoutToStatus.start();
 			for (int i = 12; i < 16; i++) {
 				strcpy(cmd1, "G1 X70 Y");
 				strcat(cmd1, ftostr32(35 - i*width * 2));
@@ -1456,7 +1456,7 @@ void lcd_commands()
 
 		if (lcd_commands_step == 2 && !blocks_queued() && cmd_buffer_empty())
 		{
-			lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+			lcd_timeoutToStatus.start();
 			enquecommand_P(PSTR("G1 E-0.07500 F2100.00000"));
 			enquecommand_P(PSTR("M107")); //turn off printer fan
 			enquecommand_P(PSTR("M104 S0")); // turn off temperature
@@ -1464,7 +1464,7 @@ void lcd_commands()
 			enquecommand_P(PSTR("G1 Z10 F1300.000"));
 			enquecommand_P(PSTR("G1 X10 Y180 F4000")); //home X axis
 			enquecommand_P(PSTR("M84"));// disable motors
-			lcd_timeoutToStatus = millis() - 1; //if user dont confirm live adjust Z value by pressing the knob, we are saving last value by timeout to status screen
+			forceMenuExpire = true; //if user dont confirm live adjust Z value by pressing the knob, we are saving last value by timeout to status screen
 			lcd_commands_step = 1;
 		}
 		if (lcd_commands_step == 1 && !blocks_queued() && cmd_buffer_empty())
@@ -2353,7 +2353,7 @@ void lcd_menu_statistics()
 {
 	if (IS_SD_PRINTING)
 	{
-		int _met = total_filament_used / 100000;
+		float _met = ((float)total_filament_used) / (100000.f);
 		int _cm = (total_filament_used - (_met * 100000)) / 10;
 		int _t = (millis() - starttime) / 1000;
 		int _h = _t / 3600;
@@ -2368,12 +2368,12 @@ void lcd_menu_statistics()
 		lcd_printf_P(_N(
 		  ESC_2J
 		  "%S:"
-		  ESC_H(6,1) "%8.2f m\n"
+		  ESC_H(6,1) "%8.2fm \n"
 		  "%S :"
 		  ESC_H(8,3) "%2dh %02dm %02d"
 		  ),
 		 _i("Filament used"),
-		 _met, _cm,
+		 _met,
 		 _i("Print time"),
 		 _h, _m, _s
 		);
@@ -2637,7 +2637,7 @@ static void _lcd_babystep(int axis, const char *msg)
 		//SERIAL_ECHO("Z baby step: ");
 		//SERIAL_ECHO(menuData.babyStep.babystepMem[2]);
         // Wait 90 seconds before closing the live adjust dialog.
-        lcd_timeoutToStatus = millis() + 90000;
+        lcd_timeoutToStatus.start();
     }
 
   if (encoderPosition != 0) 
@@ -3944,7 +3944,7 @@ void menu_setlang(unsigned char lang)
 		lcd_update_enable(true);
 		lcd_implementation_clear();
 		lcd_goto_menu(lcd_language_menu);
-		lcd_timeoutToStatus = -1; //infinite timeout
+		lcd_timeoutToStatus.stop(); //infinite timeout
 		lcdDrawUpdate = 2;
 	}
 }
@@ -4177,7 +4177,7 @@ void lcd_language()
 	lcd_update_enable(true);
 	lcd_implementation_clear();
 	lcd_goto_menu(lcd_language_menu);
-	lcd_timeoutToStatus = -1; //infinite timeout
+	lcd_timeoutToStatus.stop(); //infinite timeout
 	lcdDrawUpdate = 2;
 	while ((currentMenu != lcd_status_screen) && (!lang_is_selected()))
 	{
@@ -4573,7 +4573,7 @@ static void lcd_homing_accuracy_menu_advanced_back()
 
 static void lcd_homing_accuracy_menu_advanced()
 {
-	lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+	lcd_timeoutToStatus.start();
 	MENU_BEGIN();
 ///!	MENU_ITEM_BACK_P(PSTR("Homing accuracy"), lcd_homing_accuracy_menu_advanced_back);
 	MENU_ITEM_FUNCTION_P(PSTR("Reset def. steps"), lcd_homing_accuracy_menu_advanced_reset);
@@ -4654,7 +4654,7 @@ static void lcd_ustep_resolution_reset_def_xyze()
 
 static void lcd_ustep_resolution_menu()
 {
-	lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+	lcd_timeoutToStatus.start();
 	MENU_BEGIN();
 ///!	MENU_ITEM_BACK_P(PSTR("Experimental"), lcd_ustep_resolution_menu_back);
 	MENU_ITEM_FUNCTION_P(PSTR("Reset defaults"),  lcd_ustep_resolution_reset_def_xyze);
@@ -4702,7 +4702,7 @@ static void lcd_ustep_linearity_menu_reset()
 
 static void lcd_ustep_linearity_menu()
 {
-	lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+	lcd_timeoutToStatus.start();
 	MENU_BEGIN();
 ///!	MENU_ITEM_BACK_P(PSTR("Experimental"), lcd_ustep_linearity_menu_back);
 	MENU_ITEM_FUNCTION_P(PSTR("Reset correction"), lcd_ustep_linearity_menu_reset);
@@ -7512,7 +7512,7 @@ void lcd_init()
 //#include <avr/pgmspace.h>
 
 static volatile bool lcd_update_enabled = true;
-unsigned long lcd_timeoutToStatus = 0;
+LongTimer lcd_timeoutToStatus;
 
 void lcd_update_enable(bool enabled)
 {
@@ -7524,7 +7524,9 @@ void lcd_update_enable(bool enabled)
             encoderDiff = 0;
             // Enabling the normal LCD update procedure.
             // Reset the timeout interval.
-            lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+            lcd_timeoutToStatus.start();
+            // Force the keypad update now.
+            lcd_next_update_millis = millis() - 1;
             // Full update.
             lcd_implementation_clear();
       #if defined(LCD_PROGRESS_BAR) && defined(SDSUPPORT)
@@ -7542,6 +7544,24 @@ void lcd_update_enable(bool enabled)
         }
     }
 }
+static inline bool z_menu_expired()
+{
+    return (currentMenu == lcd_babystep_z
+         && lcd_timeoutToStatus.expired(LCD_TIMEOUT_TO_STATUS_BABYSTEP_Z));
+}
+static inline bool other_menu_expired()
+{
+    return (currentMenu != lcd_status_screen
+            && currentMenu != lcd_babystep_z
+            && lcd_timeoutToStatus.expired(LCD_TIMEOUT_TO_STATUS));
+}
+static inline bool forced_menu_expire()
+{
+    bool retval = (currentMenu != lcd_status_screen
+            && forceMenuExpire);
+    forceMenuExpire = false;
+    return retval;
+}
 
 static inline void debugBlink()
 {
@@ -7651,30 +7671,29 @@ void lcd_update(uint8_t lcdDrawUpdateOverride, bool forceRedraw)
             encoderPosition += encoderDiff / ENCODER_PULSES_PER_STEP;
             encoderDiff = 0;
             lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+            lcd_timeoutToStatus.start();
         }
-
-        if (LCD_CLICKED) lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
+    	  if (LCD_CLICKED) lcd_timeoutToStatus.start();
 #endif//ULTIPANEL
 
         (*currentMenu)();
         lcd_implementation_update_indicators();
 
 #ifdef ULTIPANEL
-        if (lcd_timeoutToStatus < millis() && currentMenu != lcd_status_screen)
-        {
-            // Exiting a menu. Let's call the menu function the last time with menuExiting flag set to true
-            // to give it a chance to save its state.
-            // This is useful for example, when the babystep value has to be written into EEPROM.
-            if (currentMenu != NULL)
-            {
-                menuExiting = true;
-                (*currentMenu)();
-                menuExiting = false;
-            }
-            lcd_implementation_clear();
-            lcd_return_to_status();
-            lcdDrawUpdate = 2;
-        }
+	  if (z_menu_expired() || other_menu_expired() || forced_menu_expire())
+	  {
+      // Exiting a menu. Let's call the menu function the last time with menuExiting flag set to true
+      // to give it a chance to save its state.
+      // This is useful for example, when the babystep value has to be written into EEPROM.
+      if (currentMenu != NULL) {
+        menuExiting = true;
+        (*currentMenu)();
+        menuExiting = false;
+      }
+	      lcd_implementation_clear();
+		  lcd_return_to_status();
+		  lcdDrawUpdate = 2;
+	  }
 #endif//ULTIPANEL
         if (lcdDrawUpdate == 2) lcd_implementation_clear();
         if (lcdDrawUpdate) lcdDrawUpdate--;
@@ -7830,9 +7849,9 @@ void lcd_buttons_update()
 #if BTN_ENC > 0
   if (lcd_update_enabled == true) { //if we are in non-modal mode, long press can be used and short press triggers with button release
 	  if (READ(BTN_ENC) == 0) { //button is pressed	  
-		  lcd_timeoutToStatus = millis() + LCD_TIMEOUT_TO_STATUS;
-		  if (millis() > button_blanking_time) {
-			  button_blanking_time = millis() + BUTTON_BLANKING_TIME;
+		  lcd_timeoutToStatus.start();
+		  if (!buttonBlanking.running() || buttonBlanking.expired(BUTTON_BLANKING_TIME)) {
+			  buttonBlanking.start();
 			  if (button_pressed == false && long_press_active == false) {
 			      longPressTimer.start();
 				  button_pressed = true;
@@ -7848,7 +7867,7 @@ void lcd_buttons_update()
 	  }
 	  else { //button not pressed
 		  if (button_pressed) { //button was released
-			  button_blanking_time = millis() + BUTTON_BLANKING_TIME;
+		      buttonBlanking.start();
 
 			  if (long_press_active == false) { //button released before long press gets activated
 					  newbutton |= EN_C;

+ 4 - 2
Firmware/ultralcd.h

@@ -3,6 +3,7 @@
 
 #include "Marlin.h"
 #include "mesh_bed_calibration.h"
+#include "Timer.h"
 
 extern int lcd_puts_P(const char* str);
 extern int lcd_printf_P(const char* format, ...);
@@ -72,7 +73,8 @@ extern int lcd_printf_P(const char* format, ...);
   #define LCD_ALERTMESSAGERPGM(x) lcd_setalertstatuspgm((x))
 
   #define LCD_UPDATE_INTERVAL 100
-  #define LCD_TIMEOUT_TO_STATUS 30000
+  #define LCD_TIMEOUT_TO_STATUS 30000ul //!< Generic timeout to status screen in ms, when no user action.
+  #define LCD_TIMEOUT_TO_STATUS_BABYSTEP_Z 90000ul //!< Specific timeout for lcd_babystep_z screen in ms.
 
   #ifdef ULTIPANEL
   void lcd_buttons_update();
@@ -95,7 +97,7 @@ extern int lcd_printf_P(const char* format, ...);
   #define LCD_COMMAND_PID_EXTRUDER 7 
   #define LCD_COMMAND_V2_CAL 8
 
-  extern unsigned long lcd_timeoutToStatus;
+  extern LongTimer lcd_timeoutToStatus;
   extern int lcd_commands_type;
   
   extern uint8_t farm_mode;