From 9a1ac45d195a08c785b3f931806a117207cea82e Mon Sep 17 00:00:00 2001 From: Timofey Titovets Date: Wed, 13 Aug 2025 18:55:54 +0200 Subject: [PATCH] sx1509: migrate i2c write to connect phase Signed-off-by: Timofey Titovets --- klippy/extras/sx1509.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/klippy/extras/sx1509.py b/klippy/extras/sx1509.py index 070b9133..82b41d81 100644 --- a/klippy/extras/sx1509.py +++ b/klippy/extras/sx1509.py @@ -29,7 +29,6 @@ class SX1509(object): self._ppins = self._printer.lookup_object("pins") self._ppins.register_chip("sx1509_" + self._name, self) self._mcu = self._i2c.get_mcu() - self._mcu.register_config_callback(self._build_config) self._oid = self._i2c.get_oid() self._last_clock = 0 # Set up registers default values @@ -37,22 +36,22 @@ class SX1509(object): REG_PULLUP : 0, REG_PULLDOWN : 0, REG_INPUT_DISABLE : 0, REG_ANALOG_DRIVER_ENABLE : 0} self.reg_i_on_dict = {reg : 0 for reg in REG_I_ON} - def _build_config(self): + config.get_printer().register_event_handler("klippy:connect", + self.handle_connect) + def handle_connect(self): # Reset the chip, Default RegClock/RegMisc 0x0 - self._mcu.add_config_cmd("i2c_write oid=%d data=%02x%02x" % ( - self._oid, REG_RESET, 0x12)) - self._mcu.add_config_cmd("i2c_write oid=%d data=%02x%02x" % ( - self._oid, REG_RESET, 0x34)) + self._i2c.i2c_write([REG_RESET, 0x12]) + self._i2c.i2c_write([REG_RESET, 0x34]) # Enable Oscillator - self._mcu.add_config_cmd("i2c_write oid=%d data=%02x%02x" % ( - self._oid, REG_CLOCK, (1 << 6))) + self._i2c.i2c_write([REG_CLOCK, (1 << 6)]) # Setup Clock Divider - self._mcu.add_config_cmd("i2c_write oid=%d data=%02x%02x" % ( - self._oid, REG_MISC, (1 << 4))) + self._i2c.i2c_write([REG_MISC, (1 << 4)]) # Transfer all regs with their initial cached state - for _reg, _data in self.reg_dict.items(): - self._mcu.add_config_cmd("i2c_write oid=%d data=%02x%04x" % ( - self._oid, _reg, _data), is_init=True) + reactor = self._mcu.get_printer().get_reactor() + for _reg in self.reg_dict: + curtime = reactor.monotonic() + printtime = self._mcu.estimated_print_time(curtime) + self.send_register(_reg, printtime) def setup_pin(self, pin_type, pin_params): if pin_type == 'digital_out' and pin_params['pin'][0:4] == "PIN_": return SX1509_digital_out(self, pin_params)