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 <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-01-08 10:55:18 -05:00
parent f4be0ac7be
commit 797367b9f5
8 changed files with 72 additions and 71 deletions

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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:

View File

@ -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

View File

@ -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]):

View File

@ -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

View File

@ -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