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,29 +26,29 @@ class PrinterLCD:
|
||||||
# printer objects
|
# printer objects
|
||||||
self.gcode = self.toolhead = self.sdcard = None
|
self.gcode = self.toolhead = self.sdcard = None
|
||||||
self.fan = self.extruder0 = self.extruder1 = self.heater_bed = None
|
self.fan = self.extruder0 = self.extruder1 = self.heater_bed = None
|
||||||
|
self.printer.register_event_handler("klippy:ready", self.handle_ready)
|
||||||
# screen updating
|
# screen updating
|
||||||
self.screen_update_timer = self.reactor.register_timer(
|
self.screen_update_timer = self.reactor.register_timer(
|
||||||
self.screen_update_event)
|
self.screen_update_event)
|
||||||
# Initialization
|
# Initialization
|
||||||
def printer_state(self, state):
|
def handle_ready(self):
|
||||||
if state == 'ready':
|
self.lcd_chip.init()
|
||||||
self.lcd_chip.init()
|
# Load printer objects
|
||||||
# Load printer objects
|
self.gcode = self.printer.lookup_object('gcode')
|
||||||
self.gcode = self.printer.lookup_object('gcode')
|
self.toolhead = self.printer.lookup_object('toolhead')
|
||||||
self.toolhead = self.printer.lookup_object('toolhead')
|
self.sdcard = self.printer.lookup_object('virtual_sdcard', None)
|
||||||
self.sdcard = self.printer.lookup_object('virtual_sdcard', None)
|
self.fan = self.printer.lookup_object('fan', None)
|
||||||
self.fan = self.printer.lookup_object('fan', None)
|
self.extruder0 = self.printer.lookup_object('extruder0', None)
|
||||||
self.extruder0 = self.printer.lookup_object('extruder0', None)
|
self.extruder1 = self.printer.lookup_object('extruder1', None)
|
||||||
self.extruder1 = self.printer.lookup_object('extruder1', None)
|
self.heater_bed = self.printer.lookup_object('heater_bed', None)
|
||||||
self.heater_bed = self.printer.lookup_object('heater_bed', None)
|
self.prg_time = .0
|
||||||
self.prg_time = .0
|
self.progress = None
|
||||||
self.progress = None
|
self.msg_time = None
|
||||||
self.msg_time = None
|
self.message = None
|
||||||
self.message = None
|
self.gcode.register_command('M73', self.cmd_M73)
|
||||||
self.gcode.register_command('M73', self.cmd_M73)
|
self.gcode.register_command('M117', self.cmd_M117)
|
||||||
self.gcode.register_command('M117', self.cmd_M117)
|
# Start screen update timer
|
||||||
# Start screen update timer
|
self.reactor.update_timer(self.screen_update_timer, self.reactor.NOW)
|
||||||
self.reactor.update_timer(self.screen_update_timer, self.reactor.NOW)
|
|
||||||
# Get menu instance
|
# Get menu instance
|
||||||
def get_menu(self):
|
def get_menu(self):
|
||||||
return self.menu
|
return self.menu
|
||||||
|
|
|
@ -1007,8 +1007,9 @@ class MenuManager:
|
||||||
self._last_encoder_ccw_eventtime = 0
|
self._last_encoder_ccw_eventtime = 0
|
||||||
# printer objects
|
# printer objects
|
||||||
self.buttons = self.printer.try_load_module(config, "buttons")
|
self.buttons = self.printer.try_load_module(config, "buttons")
|
||||||
# register itself for a printer_state callback
|
# register itself for printer callbacks
|
||||||
config.get_printer().add_object('menu', self)
|
self.printer.add_object('menu', self)
|
||||||
|
self.printer.register_event_handler("klippy:ready", self.handle_ready)
|
||||||
# register buttons & encoder
|
# register buttons & encoder
|
||||||
if self.buttons:
|
if self.buttons:
|
||||||
if self.encoder_pins:
|
if self.encoder_pins:
|
||||||
|
@ -1046,19 +1047,18 @@ class MenuManager:
|
||||||
# Load menu root
|
# Load menu root
|
||||||
self.load_root()
|
self.load_root()
|
||||||
|
|
||||||
def printer_state(self, state):
|
def handle_ready(self):
|
||||||
if state == 'ready':
|
# Load all available printer objects
|
||||||
# Load all available printer objects
|
for cfg_name in self.printer.objects:
|
||||||
for cfg_name in self.printer.objects:
|
obj = self.printer.lookup_object(cfg_name, None)
|
||||||
obj = self.printer.lookup_object(cfg_name, None)
|
if obj is not None:
|
||||||
if obj is not None:
|
name = ".".join(str(cfg_name).split())
|
||||||
name = ".".join(str(cfg_name).split())
|
self.objs[name] = obj
|
||||||
self.objs[name] = obj
|
logging.debug("Load module '%s' -> %s" % (
|
||||||
logging.debug("Load module '%s' -> %s" % (
|
str(name), str(obj.__class__)))
|
||||||
str(name), str(obj.__class__)))
|
# start timer
|
||||||
# start timer
|
reactor = self.printer.get_reactor()
|
||||||
reactor = self.printer.get_reactor()
|
reactor.register_timer(self.timer_event, reactor.NOW)
|
||||||
reactor.register_timer(self.timer_event, reactor.NOW)
|
|
||||||
|
|
||||||
def timer_event(self, eventtime):
|
def timer_event(self, eventtime):
|
||||||
# take next from sequence
|
# take next from sequence
|
||||||
|
|
|
@ -10,19 +10,19 @@ PIN_MIN_TIME = 0.100
|
||||||
class PrinterHeaterFan:
|
class PrinterHeaterFan:
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.printer = config.get_printer()
|
self.printer = config.get_printer()
|
||||||
|
self.printer.register_event_handler("klippy:ready", self.handle_ready)
|
||||||
self.heater_name = config.get("heater", "extruder0")
|
self.heater_name = config.get("heater", "extruder0")
|
||||||
self.heater_temp = config.getfloat("heater_temp", 50.0)
|
self.heater_temp = config.getfloat("heater_temp", 50.0)
|
||||||
self.heaters = []
|
self.heaters = []
|
||||||
self.fan = fan.PrinterFan(config, default_shutdown_speed=1.)
|
self.fan = fan.PrinterFan(config, default_shutdown_speed=1.)
|
||||||
self.mcu = self.fan.mcu_fan.get_mcu()
|
self.mcu = self.fan.mcu_fan.get_mcu()
|
||||||
self.fan_speed = config.getfloat("fan_speed", 1., minval=0., maxval=1.)
|
self.fan_speed = config.getfloat("fan_speed", 1., minval=0., maxval=1.)
|
||||||
def printer_state(self, state):
|
def handle_ready(self):
|
||||||
if state == 'ready':
|
pheater = self.printer.lookup_object('heater')
|
||||||
pheater = self.printer.lookup_object('heater')
|
self.heaters = [pheater.lookup_heater(n.strip())
|
||||||
self.heaters = [pheater.lookup_heater(n.strip())
|
for n in self.heater_name.split(',')]
|
||||||
for n in self.heater_name.split(',')]
|
reactor = self.printer.get_reactor()
|
||||||
reactor = self.printer.get_reactor()
|
reactor.register_timer(self.callback, reactor.NOW)
|
||||||
reactor.register_timer(self.callback, reactor.NOW)
|
|
||||||
def callback(self, eventtime):
|
def callback(self, eventtime):
|
||||||
power = 0.
|
power = 0.
|
||||||
for heater in self.heaters:
|
for heater in self.heaters:
|
||||||
|
|
|
@ -18,16 +18,15 @@ class IdleTimeout:
|
||||||
self.reactor = self.printer.get_reactor()
|
self.reactor = self.printer.get_reactor()
|
||||||
self.gcode = self.printer.lookup_object('gcode')
|
self.gcode = self.printer.lookup_object('gcode')
|
||||||
self.toolhead = self.timeout_timer = None
|
self.toolhead = self.timeout_timer = None
|
||||||
|
self.printer.register_event_handler("klippy:ready", self.handle_ready)
|
||||||
self.state = "Idle"
|
self.state = "Idle"
|
||||||
self.idle_timeout = config.getfloat('timeout', 600., above=0.)
|
self.idle_timeout = config.getfloat('timeout', 600., above=0.)
|
||||||
self.idle_gcode = config.get('gcode', DEFAULT_IDLE_GCODE).split('\n')
|
self.idle_gcode = config.get('gcode', DEFAULT_IDLE_GCODE).split('\n')
|
||||||
def printer_state(self, state):
|
def handle_ready(self):
|
||||||
if state == 'ready':
|
self.toolhead = self.printer.lookup_object('toolhead')
|
||||||
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.printer.register_event_handler("toolhead:sync_print_time",
|
||||||
self.timeout_handler)
|
self.handle_sync_print_time)
|
||||||
self.printer.register_event_handler("toolhead:sync_print_time",
|
|
||||||
self.handle_sync_print_time)
|
|
||||||
def transition_idle_state(self, eventtime):
|
def transition_idle_state(self, eventtime):
|
||||||
self.state = "Printing"
|
self.state = "Printing"
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -44,12 +44,12 @@ class PrinterServo:
|
||||||
if initial_pulse_width is not None:
|
if initial_pulse_width is not None:
|
||||||
self.initial_pwm_value = self._get_pwm_from_pulse_width(
|
self.initial_pwm_value = self._get_pwm_from_pulse_width(
|
||||||
initial_pulse_width)
|
initial_pulse_width)
|
||||||
def printer_state(self, state):
|
self.printer.register_event_handler("klippy:ready", self.handle_ready)
|
||||||
if state == 'ready':
|
def handle_ready(self):
|
||||||
if self.initial_pwm_value is not None:
|
if self.initial_pwm_value is not None:
|
||||||
toolhead = self.printer.lookup_object('toolhead')
|
toolhead = self.printer.lookup_object('toolhead')
|
||||||
print_time = toolhead.get_last_move_time()
|
print_time = toolhead.get_last_move_time()
|
||||||
self._set_pwm(print_time, self.initial_pwm_value)
|
self._set_pwm(print_time, self.initial_pwm_value)
|
||||||
def _set_pwm(self, print_time, value):
|
def _set_pwm(self, print_time, value):
|
||||||
if value == self.last_value and self.enable == self.last_enable:
|
if value == self.last_value and self.enable == self.last_enable:
|
||||||
return
|
return
|
||||||
|
|
|
@ -11,13 +11,13 @@ class PrinterStats:
|
||||||
reactor = self.printer.get_reactor()
|
reactor = self.printer.get_reactor()
|
||||||
self.stats_timer = reactor.register_timer(self.generate_stats)
|
self.stats_timer = reactor.register_timer(self.generate_stats)
|
||||||
self.stats_cb = []
|
self.stats_cb = []
|
||||||
def printer_state(self, state):
|
self.printer.register_event_handler("klippy:ready", self.handle_ready)
|
||||||
if state == 'ready':
|
def handle_ready(self):
|
||||||
self.stats_cb = [o.stats for n, o in self.printer.lookup_objects()
|
self.stats_cb = [o.stats for n, o in self.printer.lookup_objects()
|
||||||
if hasattr(o, 'stats')]
|
if hasattr(o, 'stats')]
|
||||||
if self.printer.get_start_args().get('debugoutput') is None:
|
if self.printer.get_start_args().get('debugoutput') is None:
|
||||||
reactor = self.printer.get_reactor()
|
reactor = self.printer.get_reactor()
|
||||||
reactor.update_timer(self.stats_timer, reactor.NOW)
|
reactor.update_timer(self.stats_timer, reactor.NOW)
|
||||||
def generate_stats(self, eventtime):
|
def generate_stats(self, eventtime):
|
||||||
stats = [cb(eventtime) for cb in self.stats_cb]
|
stats = [cb(eventtime) for cb in self.stats_cb]
|
||||||
if max([s[0] for s in stats]):
|
if max([s[0] for s in stats]):
|
||||||
|
|
|
@ -16,6 +16,7 @@ class GCodeParser:
|
||||||
def __init__(self, printer, fd):
|
def __init__(self, printer, fd):
|
||||||
self.printer = printer
|
self.printer = printer
|
||||||
self.fd = fd
|
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:shutdown", self.handle_shutdown)
|
||||||
printer.register_event_handler("klippy:disconnect",
|
printer.register_event_handler("klippy:disconnect",
|
||||||
self.handle_disconnect)
|
self.handle_disconnect)
|
||||||
|
@ -126,9 +127,7 @@ class GCodeParser:
|
||||||
self._respond_state("Shutdown")
|
self._respond_state("Shutdown")
|
||||||
def handle_disconnect(self):
|
def handle_disconnect(self):
|
||||||
self._respond_state("Disconnect")
|
self._respond_state("Disconnect")
|
||||||
def printer_state(self, state):
|
def handle_ready(self):
|
||||||
if state != 'ready':
|
|
||||||
return
|
|
||||||
self.is_printer_ready = True
|
self.is_printer_ready = True
|
||||||
self.gcode_handlers = self.ready_gcode_handlers
|
self.gcode_handlers = self.ready_gcode_handlers
|
||||||
# Lookup printer components
|
# Lookup printer components
|
||||||
|
|
|
@ -138,11 +138,6 @@ class Printer:
|
||||||
if self.state_message is not message_startup:
|
if self.state_message is not message_startup:
|
||||||
return
|
return
|
||||||
cb('connect')
|
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:
|
except (self.config_error, pins.error) as e:
|
||||||
logging.exception("Config error")
|
logging.exception("Config error")
|
||||||
self._set_state("%s%s" % (str(e), message_restart))
|
self._set_state("%s%s" % (str(e), message_restart))
|
||||||
|
@ -156,6 +151,15 @@ class Printer:
|
||||||
logging.exception("Unhandled exception during connect")
|
logging.exception("Unhandled exception during connect")
|
||||||
self._set_state("Internal error during connect.%s" % (
|
self._set_state("Internal error during connect.%s" % (
|
||||||
message_restart,))
|
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):
|
def run(self):
|
||||||
systime = time.time()
|
systime = time.time()
|
||||||
monotime = self.reactor.monotonic()
|
monotime = self.reactor.monotonic()
|
||||||
|
@ -173,8 +177,7 @@ class Printer:
|
||||||
if run_result == 'firmware_restart':
|
if run_result == 'firmware_restart':
|
||||||
for n, m in self.lookup_objects(module='mcu'):
|
for n, m in self.lookup_objects(module='mcu'):
|
||||||
m.microcontroller_restart()
|
m.microcontroller_restart()
|
||||||
for cb in self.state_cb:
|
self.send_event("klippy:disconnect")
|
||||||
cb('disconnect')
|
|
||||||
except:
|
except:
|
||||||
logging.exception("Unhandled exception during post run")
|
logging.exception("Unhandled exception during post run")
|
||||||
return run_result
|
return run_result
|
||||||
|
|
Loading…
Reference in New Issue