diff options
author | Kevin O'Connor <kevin@koconnor.net> | 2018-07-12 19:07:54 -0400 |
---|---|---|
committer | Kevin O'Connor <kevin@koconnor.net> | 2018-07-16 10:32:48 -0400 |
commit | acefe26e0fa725b40c09f51d652289e5ff203656 (patch) | |
tree | bffb32c31b8d17512bb8cb2ebea975ffc162edaa /klippy/extras | |
parent | 0025fbf10d81555b0c417941d70650d7c625045c (diff) | |
download | kutter-acefe26e0fa725b40c09f51d652289e5ff203656.tar.gz kutter-acefe26e0fa725b40c09f51d652289e5ff203656.tar.xz kutter-acefe26e0fa725b40c09f51d652289e5ff203656.zip |
idle_timeout: Move timeout handling from toolhead.py to new extras module
Move the "motor_off_timeout" tracking to a new module in the extras/
directory. This makes it easier to customize the idle timeout
behavior.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'klippy/extras')
-rw-r--r-- | klippy/extras/idle_timeout.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/klippy/extras/idle_timeout.py b/klippy/extras/idle_timeout.py new file mode 100644 index 00000000..ac91cbcf --- /dev/null +++ b/klippy/extras/idle_timeout.py @@ -0,0 +1,48 @@ +# Support for disabling the printer on an idle timeout +# +# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net> +# +# This file may be distributed under the terms of the GNU GPLv3 license. + +DEFAULT_IDLE_GCODE = """ +M84 +""" + +class IdleTimeout: + def __init__(self, config): + self.printer = config.get_printer() + self.toolhead = None + self.last_timeout = 0. + self.idle_timeout = config.getfloat('timeout', 600., above=0.) + self.idle_script = config.get('gcode', DEFAULT_IDLE_GCODE) + def printer_state(self, state): + if state == 'ready': + self.toolhead = self.printer.lookup_object('toolhead') + reactor = self.printer.get_reactor() + reactor.register_timer(self.timeout_handler, reactor.NOW) + def run_idle_script(self): + gcode = self.printer.lookup_object('gcode') + for line in self.idle_script.split('\n'): + try: + res = gcode.process_batch(line) + except: + break + if not res: + # Raced with incoming g-code commands + return + self.last_timeout = self.toolhead.get_last_move_time() + def timeout_handler(self, eventtime): + info = self.toolhead.get_status(eventtime) + print_time = info['print_time'] + status = info['status'] + if print_time <= self.last_timeout or status == 'Printing': + return eventtime + self.idle_timeout + estimated_print_time = info['estimated_print_time'] + elapsed_time = estimated_print_time - print_time + if elapsed_time < self.idle_timeout: + return eventtime + self.idle_timeout - elapsed_time + self.run_idle_script() + return eventtime + 1. + +def load_config(config): + return IdleTimeout(config) |