From 797367b9f5652df4103d7185567c8d749a8afbff Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 8 Jan 2019 10:55:18 -0500 Subject: [PATCH] klippy: Convert printer_state("ready") to an event handler Convert all users of the printer_state("ready") handler to register a "klippy:ready" event handler instead. Signed-off-by: Kevin O'Connor --- klippy/extras/display/display.py | 38 ++++++++++++++++---------------- klippy/extras/display/menu.py | 30 ++++++++++++------------- klippy/extras/heater_fan.py | 14 ++++++------ klippy/extras/idle_timeout.py | 13 +++++------ klippy/extras/servo.py | 12 +++++----- klippy/extras/statistics.py | 14 ++++++------ klippy/gcode.py | 5 ++--- klippy/klippy.py | 17 ++++++++------ 8 files changed, 72 insertions(+), 71 deletions(-) diff --git a/klippy/extras/display/display.py b/klippy/extras/display/display.py index 8e05cd2d..e83ed1a4 100644 --- a/klippy/extras/display/display.py +++ b/klippy/extras/display/display.py @@ -26,29 +26,29 @@ class PrinterLCD: # printer objects self.gcode = self.toolhead = self.sdcard = None self.fan = self.extruder0 = self.extruder1 = self.heater_bed = None + self.printer.register_event_handler("klippy:ready", self.handle_ready) # screen updating self.screen_update_timer = self.reactor.register_timer( self.screen_update_event) # Initialization - def printer_state(self, state): - if state == 'ready': - self.lcd_chip.init() - # Load printer objects - self.gcode = self.printer.lookup_object('gcode') - self.toolhead = self.printer.lookup_object('toolhead') - self.sdcard = self.printer.lookup_object('virtual_sdcard', None) - self.fan = self.printer.lookup_object('fan', None) - self.extruder0 = self.printer.lookup_object('extruder0', None) - self.extruder1 = self.printer.lookup_object('extruder1', None) - self.heater_bed = self.printer.lookup_object('heater_bed', None) - self.prg_time = .0 - self.progress = None - self.msg_time = None - self.message = None - self.gcode.register_command('M73', self.cmd_M73) - self.gcode.register_command('M117', self.cmd_M117) - # Start screen update timer - self.reactor.update_timer(self.screen_update_timer, self.reactor.NOW) + def handle_ready(self): + self.lcd_chip.init() + # Load printer objects + self.gcode = self.printer.lookup_object('gcode') + self.toolhead = self.printer.lookup_object('toolhead') + self.sdcard = self.printer.lookup_object('virtual_sdcard', None) + self.fan = self.printer.lookup_object('fan', None) + self.extruder0 = self.printer.lookup_object('extruder0', None) + self.extruder1 = self.printer.lookup_object('extruder1', None) + self.heater_bed = self.printer.lookup_object('heater_bed', None) + self.prg_time = .0 + self.progress = None + self.msg_time = None + self.message = None + self.gcode.register_command('M73', self.cmd_M73) + self.gcode.register_command('M117', self.cmd_M117) + # Start screen update timer + self.reactor.update_timer(self.screen_update_timer, self.reactor.NOW) # Get menu instance def get_menu(self): return self.menu diff --git a/klippy/extras/display/menu.py b/klippy/extras/display/menu.py index 1fc41fbf..470a2078 100644 --- a/klippy/extras/display/menu.py +++ b/klippy/extras/display/menu.py @@ -1007,8 +1007,9 @@ class MenuManager: self._last_encoder_ccw_eventtime = 0 # printer objects self.buttons = self.printer.try_load_module(config, "buttons") - # register itself for a printer_state callback - config.get_printer().add_object('menu', self) + # register itself for printer callbacks + self.printer.add_object('menu', self) + self.printer.register_event_handler("klippy:ready", self.handle_ready) # register buttons & encoder if self.buttons: if self.encoder_pins: @@ -1046,19 +1047,18 @@ class MenuManager: # Load menu root self.load_root() - def printer_state(self, state): - if state == 'ready': - # Load all available printer objects - for cfg_name in self.printer.objects: - obj = self.printer.lookup_object(cfg_name, None) - if obj is not None: - name = ".".join(str(cfg_name).split()) - self.objs[name] = obj - logging.debug("Load module '%s' -> %s" % ( - str(name), str(obj.__class__))) - # start timer - reactor = self.printer.get_reactor() - reactor.register_timer(self.timer_event, reactor.NOW) + def handle_ready(self): + # Load all available printer objects + for cfg_name in self.printer.objects: + obj = self.printer.lookup_object(cfg_name, None) + if obj is not None: + name = ".".join(str(cfg_name).split()) + self.objs[name] = obj + logging.debug("Load module '%s' -> %s" % ( + str(name), str(obj.__class__))) + # start timer + reactor = self.printer.get_reactor() + reactor.register_timer(self.timer_event, reactor.NOW) def timer_event(self, eventtime): # take next from sequence diff --git a/klippy/extras/heater_fan.py b/klippy/extras/heater_fan.py index 94b2679e..248f6c6b 100644 --- a/klippy/extras/heater_fan.py +++ b/klippy/extras/heater_fan.py @@ -10,19 +10,19 @@ PIN_MIN_TIME = 0.100 class PrinterHeaterFan: def __init__(self, config): self.printer = config.get_printer() + self.printer.register_event_handler("klippy:ready", self.handle_ready) self.heater_name = config.get("heater", "extruder0") self.heater_temp = config.getfloat("heater_temp", 50.0) self.heaters = [] self.fan = fan.PrinterFan(config, default_shutdown_speed=1.) self.mcu = self.fan.mcu_fan.get_mcu() self.fan_speed = config.getfloat("fan_speed", 1., minval=0., maxval=1.) - def printer_state(self, state): - if state == 'ready': - pheater = self.printer.lookup_object('heater') - self.heaters = [pheater.lookup_heater(n.strip()) - for n in self.heater_name.split(',')] - reactor = self.printer.get_reactor() - reactor.register_timer(self.callback, reactor.NOW) + def handle_ready(self): + pheater = self.printer.lookup_object('heater') + self.heaters = [pheater.lookup_heater(n.strip()) + for n in self.heater_name.split(',')] + reactor = self.printer.get_reactor() + reactor.register_timer(self.callback, reactor.NOW) def callback(self, eventtime): power = 0. for heater in self.heaters: diff --git a/klippy/extras/idle_timeout.py b/klippy/extras/idle_timeout.py index af3e773c..1a3795c8 100644 --- a/klippy/extras/idle_timeout.py +++ b/klippy/extras/idle_timeout.py @@ -18,16 +18,15 @@ class IdleTimeout: self.reactor = self.printer.get_reactor() self.gcode = self.printer.lookup_object('gcode') self.toolhead = self.timeout_timer = None + self.printer.register_event_handler("klippy:ready", self.handle_ready) self.state = "Idle" self.idle_timeout = config.getfloat('timeout', 600., above=0.) self.idle_gcode = config.get('gcode', DEFAULT_IDLE_GCODE).split('\n') - def printer_state(self, state): - if state == 'ready': - 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) + def handle_ready(self): + 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) def transition_idle_state(self, eventtime): self.state = "Printing" try: diff --git a/klippy/extras/servo.py b/klippy/extras/servo.py index 0a33c4d1..7f761a0b 100644 --- a/klippy/extras/servo.py +++ b/klippy/extras/servo.py @@ -44,12 +44,12 @@ class PrinterServo: if initial_pulse_width is not None: self.initial_pwm_value = self._get_pwm_from_pulse_width( initial_pulse_width) - def printer_state(self, state): - if state == 'ready': - if self.initial_pwm_value is not None: - toolhead = self.printer.lookup_object('toolhead') - print_time = toolhead.get_last_move_time() - self._set_pwm(print_time, self.initial_pwm_value) + self.printer.register_event_handler("klippy:ready", self.handle_ready) + def handle_ready(self): + if self.initial_pwm_value is not None: + toolhead = self.printer.lookup_object('toolhead') + print_time = toolhead.get_last_move_time() + self._set_pwm(print_time, self.initial_pwm_value) def _set_pwm(self, print_time, value): if value == self.last_value and self.enable == self.last_enable: return diff --git a/klippy/extras/statistics.py b/klippy/extras/statistics.py index 639bcad3..84186ed1 100644 --- a/klippy/extras/statistics.py +++ b/klippy/extras/statistics.py @@ -11,13 +11,13 @@ class PrinterStats: reactor = self.printer.get_reactor() self.stats_timer = reactor.register_timer(self.generate_stats) self.stats_cb = [] - def printer_state(self, state): - if state == 'ready': - self.stats_cb = [o.stats for n, o in self.printer.lookup_objects() - if hasattr(o, 'stats')] - if self.printer.get_start_args().get('debugoutput') is None: - reactor = self.printer.get_reactor() - reactor.update_timer(self.stats_timer, reactor.NOW) + self.printer.register_event_handler("klippy:ready", self.handle_ready) + def handle_ready(self): + self.stats_cb = [o.stats for n, o in self.printer.lookup_objects() + if hasattr(o, 'stats')] + if self.printer.get_start_args().get('debugoutput') is None: + reactor = self.printer.get_reactor() + reactor.update_timer(self.stats_timer, reactor.NOW) def generate_stats(self, eventtime): stats = [cb(eventtime) for cb in self.stats_cb] if max([s[0] for s in stats]): diff --git a/klippy/gcode.py b/klippy/gcode.py index 87b45f96..96fb2dcb 100644 --- a/klippy/gcode.py +++ b/klippy/gcode.py @@ -16,6 +16,7 @@ class GCodeParser: def __init__(self, printer, fd): self.printer = printer self.fd = fd + printer.register_event_handler("klippy:ready", self.handle_ready) printer.register_event_handler("klippy:shutdown", self.handle_shutdown) printer.register_event_handler("klippy:disconnect", self.handle_disconnect) @@ -126,9 +127,7 @@ class GCodeParser: self._respond_state("Shutdown") def handle_disconnect(self): self._respond_state("Disconnect") - def printer_state(self, state): - if state != 'ready': - return + def handle_ready(self): self.is_printer_ready = True self.gcode_handlers = self.ready_gcode_handlers # Lookup printer components diff --git a/klippy/klippy.py b/klippy/klippy.py index 0ac421ae..0a8f2852 100644 --- a/klippy/klippy.py +++ b/klippy/klippy.py @@ -138,11 +138,6 @@ class Printer: if self.state_message is not message_startup: return cb('connect') - self._set_state(message_ready) - for cb in self.state_cb: - if self.state_message is not message_ready: - return - cb('ready') except (self.config_error, pins.error) as e: logging.exception("Config error") self._set_state("%s%s" % (str(e), message_restart)) @@ -156,6 +151,15 @@ class Printer: logging.exception("Unhandled exception during connect") self._set_state("Internal error during connect.%s" % ( message_restart,)) + try: + self._set_state(message_ready) + for cb in self.event_handlers.get("klippy:ready", []): + if self.state_message is not message_ready: + return + cb() + except: + logging.exception("Unhandled exception during ready callback") + self.invoke_shutdown("Internal error during ready callback") def run(self): systime = time.time() monotime = self.reactor.monotonic() @@ -173,8 +177,7 @@ class Printer: if run_result == 'firmware_restart': for n, m in self.lookup_objects(module='mcu'): m.microcontroller_restart() - for cb in self.state_cb: - cb('disconnect') + self.send_event("klippy:disconnect") except: logging.exception("Unhandled exception during post run") return run_result