Browse Source

Merge pull request #951 from PavelSindler/use_timer

Use timer
PavelSindler 5 years ago
parent
commit
77cc55ed76
5 changed files with 56 additions and 38 deletions
  1. 1 1
      Firmware/cardreader.cpp
  2. 7 9
      Firmware/lcd.cpp
  3. 5 2
      Firmware/lcd.h
  4. 43 25
      Firmware/ultralcd.cpp
  5. 0 1
      Firmware/ultralcd.h

+ 1 - 1
Firmware/cardreader.cpp

@@ -979,7 +979,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() {

+ 7 - 9
Firmware/lcd.cpp

@@ -664,7 +664,6 @@ int8_t lcd_encoder_diff = 0;
 uint8_t lcd_buttons = 0;
 uint8_t lcd_button_pressed = 0;
 uint8_t lcd_update_enabled = 1;
-uint32_t lcd_timeoutToStatus = 0;
 
 uint32_t lcd_next_update_millis = 0;
 uint8_t lcd_status_update_delay = 0;
@@ -677,8 +676,9 @@ lcd_charsetup_func_t lcd_charsetup_func = 0;
 
 lcd_lcdupdate_func_t lcd_lcdupdate_func = 0;
 
-uint32_t lcd_button_blanking_time = millis();
+static ShortTimer buttonBlanking;
 ShortTimer longPressTimer;
+LongTimer lcd_timeoutToStatus;
 
 
 uint8_t lcd_clicked(void)
@@ -718,7 +718,6 @@ void lcd_quick_feedback(void)
 
 
 
-
 void lcd_update(uint8_t lcdDrawUpdateOverride)
 {
 	if (lcd_draw_update < lcdDrawUpdateOverride)
@@ -741,7 +740,7 @@ void lcd_update_enable(uint8_t enabled)
 			lcd_encoder_diff = 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.
@@ -768,10 +767,9 @@ void lcd_buttons_update(void)
 	{ //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() > lcd_button_blanking_time)
-			{
-				lcd_button_blanking_time = millis() + BUTTON_BLANKING_TIME;
+			lcd_timeoutToStatus.start();
+			if (!buttonBlanking.running() || buttonBlanking.expired(BUTTON_BLANKING_TIME)) {
+				buttonBlanking.start();
 				if ((lcd_button_pressed == 0) && (lcd_long_press_active == 0))
 				{
 					longPressTimer.start();
@@ -792,7 +790,7 @@ void lcd_buttons_update(void)
 		{ //button not pressed
 			if (lcd_button_pressed)
 			{ //button was released
-				lcd_button_blanking_time = millis() + BUTTON_BLANKING_TIME;
+				buttonBlanking.start();
 				if (lcd_long_press_active == 0)
 				{ //button released before long press gets activated
 					newbutton |= EN_C;

+ 5 - 2
Firmware/lcd.h

@@ -4,6 +4,7 @@
 
 #include <inttypes.h>
 #include <stdio.h>
+#include "Timer.h"
 
 
 
@@ -80,7 +81,9 @@ extern void lcd_print(double, int = 2);
 
 
 #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.
+
 
 
 typedef void (*lcd_longpress_func_t)(void);
@@ -106,7 +109,7 @@ extern uint8_t lcd_button_pressed;
 
 extern uint8_t lcd_update_enabled;
 
-extern uint32_t lcd_timeoutToStatus;
+extern LongTimer lcd_timeoutToStatus;
 
 extern uint32_t lcd_next_update_millis;
 

+ 43 - 25
Firmware/ultralcd.cpp

@@ -8,7 +8,6 @@
 #include "stepper.h"
 #include "ConfigurationStore.h"
 #include <string.h>
-#include "Timer.h"
 
 
 #include "lcd.h"
@@ -175,7 +174,7 @@ bool printer_connected = true;
 unsigned long display_time; //just timer for showing pid finished message on lcd;
 float pid_temp = DEFAULT_PID_TEMP;
 
-
+static bool forceMenuExpire = false;
 bool menuExiting = false;
 
 
@@ -1236,7 +1235,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;
@@ -1262,7 +1261,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"));
@@ -1286,7 +1285,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"));
@@ -1311,7 +1310,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);
@@ -1344,7 +1343,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));
@@ -1373,7 +1372,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));
@@ -1402,7 +1401,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));
@@ -1431,7 +1430,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"));
@@ -1454,7 +1453,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 X0 Y1 E3.0000"));
 			enquecommand_P(PSTR("G1 X50 Y1 E-5.0000"));
@@ -1500,7 +1499,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;
@@ -1540,7 +1539,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
@@ -1588,7 +1587,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");
@@ -1618,7 +1617,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));
@@ -1647,7 +1646,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));
@@ -1676,7 +1675,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));
@@ -1705,7 +1704,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("M107")); //turn off printer fan			
 			#ifdef SNMM_V2
 			enquecommand_P(PSTR("M702 C"));
@@ -1717,7 +1716,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())
@@ -2901,7 +2900,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 (lcd_encoder != 0) 
@@ -4210,7 +4209,7 @@ void menu_setlang(unsigned char lang)
 		lcd_update_enable(true);
 		lcd_clear();
 		menu_goto(lcd_language_menu, 0, true, true);
-		lcd_timeoutToStatus = -1; //infinite timeout
+		lcd_timeoutToStatus.stop(); //infinite timeout
 		lcd_draw_update = 2;
 	}
 }
@@ -4445,7 +4444,7 @@ void lcd_language()
 	lcd_update_enable(true);
 	lcd_clear();
 	menu_goto(lcd_language_menu, 0, true, true);
-	lcd_timeoutToStatus = -1; //infinite timeout
+	lcd_timeoutToStatus.stop(); //infinite timeout
 	lcd_draw_update = 2;
 	while ((menu_menu != lcd_status_screen) && (!lang_is_selected()))
 	{
@@ -7606,6 +7605,25 @@ void menu_lcd_charsetup_func(void)
 		lcd_set_custom_characters_arrows();
 }
 
+static inline bool z_menu_expired()
+{
+    return (menu_menu == lcd_babystep_z
+         && lcd_timeoutToStatus.expired(LCD_TIMEOUT_TO_STATUS_BABYSTEP_Z));
+}
+static inline bool other_menu_expired()
+{
+    return (menu_menu != lcd_status_screen
+            && menu_menu != lcd_babystep_z
+            && lcd_timeoutToStatus.expired(LCD_TIMEOUT_TO_STATUS));
+}
+static inline bool forced_menu_expire()
+{
+    bool retval = (menu_menu != lcd_status_screen
+            && forceMenuExpire);
+    forceMenuExpire = false;
+    return retval;
+}
+
 void menu_lcd_lcdupdate_func(void)
 {
 #if (SDCARDDETECT > 0)
@@ -7635,14 +7653,14 @@ void menu_lcd_lcdupdate_func(void)
 			lcd_draw_update = 1;
 			lcd_encoder += lcd_encoder_diff / ENCODER_PULSES_PER_STEP;
 			lcd_encoder_diff = 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();
 
 		(*menu_menu)();
 
-		if (lcd_timeoutToStatus < millis() && menu_menu != lcd_status_screen)
+		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.

+ 0 - 1
Firmware/ultralcd.h

@@ -82,7 +82,6 @@ extern void menu_lcd_lcdupdate_func(void);
   #define LCD_COMMAND_PID_EXTRUDER 7 
   #define LCD_COMMAND_V2_CAL 8
 
-  extern unsigned long lcd_timeoutToStatus;
   extern int lcd_commands_type;
   
   extern uint8_t farm_mode;