From adf6040e9eeb09299379ad980f8382538c163750 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 25 Feb 2019 21:26:27 -0500 Subject: [PATCH] gcode: Use an event to handle restart request actions Instead of directly turning off motors, heaters, and fans from gcode.py, raise a new event and allow the heater, fan, and toolhead to handle the event as needed. Signed-off-by: Kevin O'Connor --- klippy/extras/fan.py | 7 ++++++- klippy/gcode.py | 7 +------ klippy/heater.py | 10 ++++++---- klippy/toolhead.py | 4 ++++ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/klippy/extras/fan.py b/klippy/extras/fan.py index d69c1127..1796f636 100644 --- a/klippy/extras/fan.py +++ b/klippy/extras/fan.py @@ -10,9 +10,12 @@ class PrinterFan: def __init__(self, config, default_shutdown_speed=0.): self.last_fan_value = 0. self.last_fan_time = 0. + printer = config.get_printer() + printer.register_event_handler("gcode:request_restart", + self.handle_request_restart) self.max_power = config.getfloat('max_power', 1., above=0., maxval=1.) self.kick_start_time = config.getfloat('kick_start_time', 0.1, minval=0.) - ppins = config.get_printer().lookup_object('pins') + ppins = printer.lookup_object('pins') self.mcu_fan = ppins.setup_pin('pwm', config.get('pin')) self.mcu_fan.setup_max_duration(0.) cycle_time = config.getfloat('cycle_time', 0.010, above=0.) @@ -22,6 +25,8 @@ class PrinterFan: 'shutdown_speed', default_shutdown_speed, minval=0., maxval=1.) self.mcu_fan.setup_start_value( 0., max(0., min(self.max_power, shutdown_speed))) + def handle_request_restart(self, print_time): + self.set_speed(print_time, 0.) def set_speed(self, print_time, value): value = max(0., min(self.max_power, value * self.max_power)) if value == self.last_fan_value: diff --git a/klippy/gcode.py b/klippy/gcode.py index b7d737c6..519d8e0a 100644 --- a/klippy/gcode.py +++ b/klippy/gcode.py @@ -673,13 +673,8 @@ class GCodeParser: gcode_pos, base_pos, homing_pos)) def request_restart(self, result): if self.is_printer_ready: - self.toolhead.motor_off() print_time = self.toolhead.get_last_move_time() - if self.heaters is not None: - for heater in self.heaters.get_all_heaters(): - heater.set_temp(print_time, 0.) - if self.fan is not None: - self.fan.set_speed(print_time, 0.) + self.printer.send_event("gcode:request_restart", print_time) self.toolhead.dwell(0.500) self.toolhead.wait_moves() self.printer.request_exit(result) diff --git a/klippy/heater.py b/klippy/heater.py index 1d48c5a3..18a1d67b 100644 --- a/klippy/heater.py +++ b/klippy/heater.py @@ -230,6 +230,8 @@ class PrinterHeaters: self.sensor_factories = {} self.heaters = {} self.gcode_id_to_sensor = {} + self.printer.register_event_handler("gcode:request_restart", + self.turn_off_all_heaters) # Register TURN_OFF_HEATERS command gcode = self.printer.lookup_object('gcode') gcode.register_command("TURN_OFF_HEATERS", self.cmd_TURN_OFF_HEATERS, @@ -265,15 +267,15 @@ class PrinterHeaters: raise self.printer.config_error( "Unknown temperature sensor '%s'" % (sensor_type,)) return self.sensor_factories[sensor_type](config) - def get_all_heaters(self): - return self.heaters.values() def get_gcode_sensors(self): return self.gcode_id_to_sensor.items() + def turn_off_all_heaters(self, print_time): + for heater in self.heaters.values(): + heater.set_temp(print_time, 0.) cmd_TURN_OFF_HEATERS_help = "Turn off all heaters" def cmd_TURN_OFF_HEATERS(self, params): print_time = self.printer.lookup_object('toolhead').get_last_move_time() - for heater in self.heaters.values(): - heater.set_temp(print_time, 0.) + self.turn_off_all_heaters(print_time) def add_printer_objects(config): config.get_printer().add_object('heater', PrinterHeaters(config)) diff --git a/klippy/toolhead.py b/klippy/toolhead.py index 32bda0fc..9629dc8f 100644 --- a/klippy/toolhead.py +++ b/klippy/toolhead.py @@ -205,6 +205,8 @@ class ToolHead: self.mcu = self.all_mcus[0] self.move_queue = MoveQueue() self.commanded_pos = [0., 0., 0., 0.] + self.printer.register_event_handler("gcode:request_restart", + self._handle_request_restart) self.printer.register_event_handler("klippy:shutdown", self._handle_shutdown) # Velocity and acceleration control @@ -416,6 +418,8 @@ class ToolHead: return { 'status': status, 'print_time': print_time, 'estimated_print_time': estimated_print_time, 'printing_time': print_time - last_print_start_time } + def _handle_request_restart(self, print_time): + self.motor_off() def _handle_shutdown(self): self.move_queue.reset() self.reset_print_time()