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 <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-01-08 11:09:55 -05:00
parent 797367b9f5
commit 857e7ed5f1
8 changed files with 32 additions and 37 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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