diff options
Diffstat (limited to 'klippy/extras/idle_timeout.py')
-rw-r--r-- | klippy/extras/idle_timeout.py | 66 |
1 files changed, 39 insertions, 27 deletions
diff --git a/klippy/extras/idle_timeout.py b/klippy/extras/idle_timeout.py index 6ab2a34a..cc722b82 100644 --- a/klippy/extras/idle_timeout.py +++ b/klippy/extras/idle_timeout.py @@ -13,34 +13,40 @@ M84 """ PIN_MIN_TIME = 0.100 -READY_TIMEOUT = .500 +READY_TIMEOUT = 0.500 + class IdleTimeout: def __init__(self, config): self.printer = config.get_printer() self.reactor = self.printer.get_reactor() - self.gcode = self.printer.lookup_object('gcode') + self.gcode = self.printer.lookup_object("gcode") self.toolhead = self.timeout_timer = None self.printer.register_event_handler("klippy:ready", self.handle_ready) - self.idle_timeout = config.getfloat('timeout', 600., above=0.) - gcode_macro = self.printer.load_object(config, 'gcode_macro') - self.idle_gcode = gcode_macro.load_template(config, 'gcode', - DEFAULT_IDLE_GCODE) - self.gcode.register_command('SET_IDLE_TIMEOUT', - self.cmd_SET_IDLE_TIMEOUT, - desc=self.cmd_SET_IDLE_TIMEOUT_help) + self.idle_timeout = config.getfloat("timeout", 600.0, above=0.0) + gcode_macro = self.printer.load_object(config, "gcode_macro") + self.idle_gcode = gcode_macro.load_template(config, "gcode", DEFAULT_IDLE_GCODE) + self.gcode.register_command( + "SET_IDLE_TIMEOUT", + self.cmd_SET_IDLE_TIMEOUT, + desc=self.cmd_SET_IDLE_TIMEOUT_help, + ) self.state = "Idle" - self.last_print_start_systime = 0. + self.last_print_start_systime = 0.0 + def get_status(self, eventtime): - printing_time = 0. + printing_time = 0.0 if self.state == "Printing": printing_time = eventtime - self.last_print_start_systime - return { "state": self.state, "printing_time": printing_time } + return {"state": self.state, "printing_time": printing_time} + def handle_ready(self): - self.toolhead = self.printer.lookup_object('toolhead') + self.toolhead = self.printer.lookup_object("toolhead") self.timeout_timer = self.reactor.register_timer(self.timeout_handler) - self.printer.register_event_handler("toolhead:sync_print_time", - self.handle_sync_print_time) + self.printer.register_event_handler( + "toolhead:sync_print_time", self.handle_sync_print_time + ) + def transition_idle_state(self, eventtime): self.state = "Printing" try: @@ -49,17 +55,19 @@ class IdleTimeout: except: logging.exception("idle timeout gcode execution") self.state = "Ready" - return eventtime + 1. + return eventtime + 1.0 print_time = self.toolhead.get_last_move_time() self.state = "Idle" self.printer.send_event("idle_timeout:idle", print_time) return self.reactor.NEVER + def check_idle_timeout(self, eventtime): # Make sure toolhead class isn't busy print_time, est_print_time, lookahead_empty = self.toolhead.check_busy( - eventtime) + eventtime + ) idle_time = est_print_time - print_time - if not lookahead_empty or idle_time < 1.: + if not lookahead_empty or idle_time < 1.0: # Toolhead is busy return eventtime + self.idle_timeout if idle_time < self.idle_timeout: @@ -67,9 +75,10 @@ class IdleTimeout: return eventtime + self.idle_timeout - idle_time if self.gcode.get_mutex().test(): # Gcode class busy - return eventtime + 1. + return eventtime + 1.0 # Idle timeout has elapsed return self.transition_idle_state(eventtime) + def timeout_handler(self, eventtime): if self.printer.is_shutdown(): return self.reactor.NEVER @@ -77,11 +86,12 @@ class IdleTimeout: return self.check_idle_timeout(eventtime) # Check if need to transition to "ready" state print_time, est_print_time, lookahead_empty = self.toolhead.check_busy( - eventtime) - buffer_time = min(2., print_time - est_print_time) + eventtime + ) + buffer_time = min(2.0, print_time - est_print_time) if not lookahead_empty: # Toolhead is busy - return eventtime + READY_TIMEOUT + max(0., buffer_time) + return eventtime + READY_TIMEOUT + max(0.0, buffer_time) if buffer_time > -READY_TIMEOUT: # Wait for ready timeout return eventtime + READY_TIMEOUT + buffer_time @@ -90,9 +100,9 @@ class IdleTimeout: return eventtime + READY_TIMEOUT # Transition to "ready" state self.state = "Ready" - self.printer.send_event("idle_timeout:ready", - est_print_time + PIN_MIN_TIME) + self.printer.send_event("idle_timeout:ready", est_print_time + PIN_MIN_TIME) return eventtime + self.idle_timeout + def handle_sync_print_time(self, curtime, print_time, est_print_time): if self.state == "Printing": return @@ -101,16 +111,18 @@ class IdleTimeout: self.last_print_start_systime = curtime check_time = READY_TIMEOUT + print_time - est_print_time self.reactor.update_timer(self.timeout_timer, curtime + check_time) - self.printer.send_event("idle_timeout:printing", - est_print_time + PIN_MIN_TIME) + self.printer.send_event("idle_timeout:printing", est_print_time + PIN_MIN_TIME) + cmd_SET_IDLE_TIMEOUT_help = "Set the idle timeout in seconds" + def cmd_SET_IDLE_TIMEOUT(self, gcmd): - timeout = gcmd.get_float('TIMEOUT', self.idle_timeout, above=0.) + timeout = gcmd.get_float("TIMEOUT", self.idle_timeout, above=0.0) self.idle_timeout = timeout gcmd.respond_info("idle_timeout: Timeout set to %.2f s" % (timeout,)) if self.state == "Ready": checktime = self.reactor.monotonic() + timeout self.reactor.update_timer(self.timeout_timer, checktime) + def load_config(config): return IdleTimeout(config) |