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:
parent
f4be0ac7be
commit
797367b9f5
|
@ -26,12 +26,12 @@ 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':
|
||||
def handle_ready(self):
|
||||
self.lcd_chip.init()
|
||||
# Load printer objects
|
||||
self.gcode = self.printer.lookup_object('gcode')
|
||||
|
|
|
@ -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,8 +1047,7 @@ class MenuManager:
|
|||
# Load menu root
|
||||
self.load_root()
|
||||
|
||||
def printer_state(self, state):
|
||||
if state == 'ready':
|
||||
def handle_ready(self):
|
||||
# Load all available printer objects
|
||||
for cfg_name in self.printer.objects:
|
||||
obj = self.printer.lookup_object(cfg_name, None)
|
||||
|
|
|
@ -10,14 +10,14 @@ 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':
|
||||
def handle_ready(self):
|
||||
pheater = self.printer.lookup_object('heater')
|
||||
self.heaters = [pheater.lookup_heater(n.strip())
|
||||
for n in self.heater_name.split(',')]
|
||||
|
|
|
@ -18,14 +18,13 @@ 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':
|
||||
def handle_ready(self):
|
||||
self.toolhead = self.printer.lookup_object('toolhead')
|
||||
self.timeout_timer = self.reactor.register_timer(
|
||||
self.timeout_handler)
|
||||
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):
|
||||
|
|
|
@ -44,8 +44,8 @@ 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':
|
||||
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()
|
||||
|
|
|
@ -11,8 +11,8 @@ 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.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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue