From 857e7ed5f1a725e1342359b6c071586a3d495702 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 8 Jan 2019 11:09:55 -0500 Subject: [PATCH] klippy: Convert printer_state("connect") to an event handler Convert all users of the printer_state("connect") handler to register a "klippy:connect" event handler instead. Signed-off-by: Kevin O'Connor --- klippy/extras/bed_mesh.py | 9 +++++---- klippy/extras/bed_tilt.py | 7 ++++--- klippy/extras/quad_gantry_level.py | 5 ++--- klippy/extras/tmc2208.py | 9 +++++---- klippy/extras/verify_heater.py | 22 +++++++++++----------- klippy/extras/z_tilt.py | 5 ++--- klippy/klippy.py | 8 ++------ klippy/mcu.py | 4 +--- 8 files changed, 32 insertions(+), 37 deletions(-) diff --git a/klippy/extras/bed_mesh.py b/klippy/extras/bed_mesh.py index 1625bbcc..dc6eba33 100644 --- a/klippy/extras/bed_mesh.py +++ b/klippy/extras/bed_mesh.py @@ -53,6 +53,8 @@ class BedMesh: FADE_DISABLE = 0x7FFFFFFF def __init__(self, config): self.printer = config.get_printer() + self.printer.register_event_handler("klippy:connect", + self.handle_connect) self.last_position = [0., 0., 0., 0.] self.calibrate = BedMeshCalibrate(config, self) self.z_mesh = None @@ -74,10 +76,9 @@ class BedMesh: 'BED_MESH_CLEAR', self.cmd_BED_MESH_CLEAR, desc=self.cmd_BED_MESH_CLEAR_help) self.gcode.set_move_transform(self) - def printer_state(self, state): - if state == 'connect': - self.toolhead = self.printer.lookup_object('toolhead') - self.calibrate.load_default_profile() + def handle_connect(self): + self.toolhead = self.printer.lookup_object('toolhead') + self.calibrate.load_default_profile() def set_mesh(self, mesh): if mesh is not None: if self.base_fade_target is None: diff --git a/klippy/extras/bed_tilt.py b/klippy/extras/bed_tilt.py index 50e0bcff..b94bf189 100644 --- a/klippy/extras/bed_tilt.py +++ b/klippy/extras/bed_tilt.py @@ -9,6 +9,8 @@ import probe, mathutil class BedTilt: def __init__(self, config): self.printer = config.get_printer() + self.printer.register_event_handler("klippy:connect", + self.handle_connect) self.x_adjust = config.getfloat('x_adjust', 0.) self.y_adjust = config.getfloat('y_adjust', 0.) self.z_adjust = config.getfloat('z_adjust', 0.) @@ -18,9 +20,8 @@ class BedTilt: # Register move transform with g-code class gcode = self.printer.lookup_object('gcode') gcode.set_move_transform(self) - def printer_state(self, state): - if state == 'connect': - self.toolhead = self.printer.lookup_object('toolhead') + def handle_connect(self): + self.toolhead = self.printer.lookup_object('toolhead') def get_position(self): x, y, z, e = self.toolhead.get_position() return [x, y, z - x*self.x_adjust - y*self.y_adjust - self.z_adjust, e] diff --git a/klippy/extras/quad_gantry_level.py b/klippy/extras/quad_gantry_level.py index b9f91f1f..0b1768e5 100644 --- a/klippy/extras/quad_gantry_level.py +++ b/klippy/extras/quad_gantry_level.py @@ -9,6 +9,8 @@ import probe class QuadGantryLevel: def __init__(self, config): self.printer = config.get_printer() + self.printer.register_event_handler("klippy:connect", + self.handle_connect) self.probe_helper = probe.ProbePointsHelper(config, self.probe_finalize) gantry_corners = config.get('gantry_corners').split('\n') try: @@ -27,9 +29,6 @@ class QuadGantryLevel: self.gcode.register_command( 'QUAD_GANTRY_LEVEL', self.cmd_QUAD_GANTRY_LEVEL, desc=self.cmd_QUAD_GANTRY_LEVEL_help) - def printer_state(self, state): - if state == 'connect': - self.handle_connect() def handle_connect(self): kin = self.printer.lookup_object('toolhead').get_kinematics() z_steppers = kin.get_steppers('Z') diff --git a/klippy/extras/tmc2208.py b/klippy/extras/tmc2208.py index a86a86d8..3058f291 100644 --- a/klippy/extras/tmc2208.py +++ b/klippy/extras/tmc2208.py @@ -95,6 +95,8 @@ class TMC2208: def __init__(self, config): self.printer = config.get_printer() self.name = config.get_name().split()[1] + self.printer.register_event_handler("klippy:connect", + self.handle_connect) # pin setup ppins = self.printer.lookup_object("pins") rx_pin_params = ppins.lookup_pin( @@ -191,10 +193,9 @@ class TMC2208: cmd_queue = self.mcu.alloc_command_queue() self.tmcuart_send_cmd = self.mcu.lookup_command( "tmcuart_send oid=%c write=%*s read=%c", cq=cmd_queue) - def printer_state(self, state): - if state == 'connect': - for reg_name, val in self.init_regs.items(): - self.set_register(reg_name, val) + def handle_connect(self): + for reg_name, val in self.init_regs.items(): + self.set_register(reg_name, val) def get_register(self, reg_name): reg = Registers[reg_name] msg = encode_tmc2208_read(0xf5, 0x00, reg) diff --git a/klippy/extras/verify_heater.py b/klippy/extras/verify_heater.py index 378a8b94..507ee33b 100644 --- a/klippy/extras/verify_heater.py +++ b/klippy/extras/verify_heater.py @@ -13,6 +13,8 @@ for the parameters that control this check. class HeaterCheck: def __init__(self, config): self.printer = config.get_printer() + self.printer.register_event_handler("klippy:connect", + self.handle_connect) self.printer.register_event_handler("klippy:shutdown", self.handle_shutdown) self.heater_name = config.get_name().split()[1] @@ -29,17 +31,15 @@ class HeaterCheck: self.last_target = self.goal_temp = self.error = 0. self.fault_systime = self.printer.get_reactor().NEVER self.check_timer = None - def printer_state(self, state): - if state == 'connect': - if self.printer.get_start_args().get('debugoutput') is not None: - # Disable verify_heater if outputting to a debug file - return - pheater = self.printer.lookup_object('heater') - self.heater = pheater.lookup_heater(self.heater_name) - logging.info("Starting heater checks for %s", self.heater_name) - reactor = self.printer.get_reactor() - self.check_timer = reactor.register_timer(self.check_event, - reactor.NOW) + def handle_connect(self): + if self.printer.get_start_args().get('debugoutput') is not None: + # Disable verify_heater if outputting to a debug file + return + pheater = self.printer.lookup_object('heater') + self.heater = pheater.lookup_heater(self.heater_name) + logging.info("Starting heater checks for %s", self.heater_name) + reactor = self.printer.get_reactor() + self.check_timer = reactor.register_timer(self.check_event, reactor.NOW) def handle_shutdown(self): if self.check_timer is not None: reactor = self.printer.get_reactor() diff --git a/klippy/extras/z_tilt.py b/klippy/extras/z_tilt.py index 4e748dd0..04b7d4c1 100644 --- a/klippy/extras/z_tilt.py +++ b/klippy/extras/z_tilt.py @@ -9,6 +9,8 @@ import probe, mathutil class ZTilt: def __init__(self, config): self.printer = config.get_printer() + self.printer.register_event_handler("klippy:connect", + self.handle_connect) z_positions = config.get('z_positions').split('\n') try: z_positions = [line.split(',', 1) @@ -27,9 +29,6 @@ class ZTilt: self.gcode.register_command( 'Z_TILT_ADJUST', self.cmd_Z_TILT_ADJUST, desc=self.cmd_Z_TILT_ADJUST_help) - def printer_state(self, state): - if state == 'connect': - self.handle_connect() def handle_connect(self): kin = self.printer.lookup_object('toolhead').get_kinematics() z_steppers = kin.get_steppers('Z') diff --git a/klippy/klippy.py b/klippy/klippy.py index 0a8f2852..51ec2bd6 100644 --- a/klippy/klippy.py +++ b/klippy/klippy.py @@ -58,7 +58,6 @@ class Printer: self.event_handlers = {} gc = gcode.GCodeParser(self, input_fd) self.objects = collections.OrderedDict({'gcode': gc}) - self.state_cb = [gc.printer_state] def get_start_args(self): return self.start_args def get_reactor(self): @@ -128,16 +127,13 @@ class Printer: m.add_printer_objects(config) # Validate that there are no undefined parameters in the config file pconfig.check_unused_options(config) - # Determine which printer objects have state callbacks - self.state_cb = [o.printer_state for o in self.objects.values() - if hasattr(o, 'printer_state')] def _connect(self, eventtime): try: self._read_config() - for cb in self.state_cb: + for cb in self.event_handlers.get("klippy:connect", []): if self.state_message is not message_startup: return - cb('connect') + cb() except (self.config_error, pins.error) as e: logging.exception("Config error") self._set_state("%s%s" % (str(e), message_restart)) diff --git a/klippy/mcu.py b/klippy/mcu.py index ee0cbd1a..febc69b7 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -422,6 +422,7 @@ class MCU: self._name = config.get_name() if self._name.startswith('mcu '): self._name = self._name[4:] + self._printer.register_event_handler("klippy:connect", self._connect) self._printer.register_event_handler("klippy:shutdown", self._shutdown) self._printer.register_event_handler("klippy:disconnect", self._disconnect) @@ -767,9 +768,6 @@ class MCU: self._mcu_tick_stddev) return False, ' '.join([msg, self._serial.stats(eventtime), self._clocksync.stats(eventtime)]) - def printer_state(self, state): - if state == 'connect': - self._connect() def __del__(self): self._disconnect()