diff --git a/klippy/extras/buttons.py b/klippy/extras/buttons.py index 8c5c2bd0..e0d3ee36 100644 --- a/klippy/extras/buttons.py +++ b/klippy/extras/buttons.py @@ -46,7 +46,7 @@ class MCU_buttons: def __init__(self, printer, mcu): self.reactor = printer.get_reactor() self.mcu = mcu - mcu.add_config_object(self) + self.mcu.register_config_callback(self.build_config) self.pin_list = [] self.callbacks = [] self.invert = self.last_button = 0 diff --git a/klippy/extras/display/hd44780.py b/klippy/extras/display/hd44780.py index 69fdcbb8..e4fd971e 100644 --- a/klippy/extras/display/hd44780.py +++ b/klippy/extras/display/hd44780.py @@ -33,7 +33,7 @@ class HD44780: self.pins = [pin_params['pin'] for pin_params in pins] self.mcu = mcu self.oid = self.mcu.create_oid() - self.mcu.add_config_object(self) + self.mcu.register_config_callback(self.build_config) self.send_data_cmd = self.send_cmds_cmd = None # framebuffers self.text_framebuffer = (bytearray(' '*80), bytearray('~'*80), 0x80) diff --git a/klippy/extras/display/st7920.py b/klippy/extras/display/st7920.py index 006b23f0..20787124 100644 --- a/klippy/extras/display/st7920.py +++ b/klippy/extras/display/st7920.py @@ -27,7 +27,7 @@ class ST7920: self.pins = [pin_params['pin'] for pin_params in pins] self.mcu = mcu self.oid = self.mcu.create_oid() - self.mcu.add_config_object(self) + self.mcu.register_config_callback(self.build_config) self.send_data_cmd = self.send_cmds_cmd = None self.is_extended = False # framebuffers diff --git a/klippy/extras/display/uc1701.py b/klippy/extras/display/uc1701.py index c2b86540..32ba7ebf 100644 --- a/klippy/extras/display/uc1701.py +++ b/klippy/extras/display/uc1701.py @@ -28,7 +28,7 @@ class UC1701: self.mcu = mcu self.spi_oid = self.mcu.create_oid() self.a0_oid = self.mcu.create_oid() - self.mcu.add_config_object(self) + self.mcu.register_config_callback(self.build_config) self.glyph_buffer = [] self.spi_xfer_cmd = self.set_pin_cmd = None self.vram = ([bytearray(128) for i in range(8)], diff --git a/klippy/extras/probe.py b/klippy/extras/probe.py index c826e03d..482cfb4a 100644 --- a/klippy/extras/probe.py +++ b/klippy/extras/probe.py @@ -31,7 +31,7 @@ class PrinterProbe: pin = config.get('pin') pin_params = ppins.lookup_pin(pin, can_invert=True, can_pullup=True) mcu = pin_params['chip'] - mcu.add_config_object(self) + mcu.register_config_callback(self.build_config) self.mcu_probe = mcu.setup_pin('endstop', pin_params) if (config.get('activate_gcode', None) is not None or config.get('deactivate_gcode', None) is not None): diff --git a/klippy/extras/replicape.py b/klippy/extras/replicape.py index 91e75550..3f5ee63f 100644 --- a/klippy/extras/replicape.py +++ b/klippy/extras/replicape.py @@ -20,7 +20,7 @@ class pca9685_pwm: if pin_type not in ['digital_out', 'pwm']: raise pins.error("Pin type not supported on replicape") self._mcu = replicape.host_mcu - self._mcu.add_config_object(self) + self._mcu.register_config_callback(self._build_config) self._bus = REPLICAPE_PCA9685_BUS self._address = REPLICAPE_PCA9685_ADDRESS self._cycle_time = REPLICAPE_PCA9685_CYCLE_TIME @@ -53,7 +53,7 @@ class pca9685_pwm: self._is_static = is_static self._replicape.note_pwm_start_value( self._channel, self._start_value, self._shutdown_value) - def build_config(self): + def _build_config(self): self._pwm_max = self._mcu.get_constant_float("PCA9685_MAX") cycle_ticks = self._mcu.seconds_to_clock(self._cycle_time) if self._is_static: @@ -175,7 +175,7 @@ class Replicape: and self.stepper_dacs): shift_registers[4] &= ~1 self.sr_enabled = tuple(reversed(shift_registers)) - self.host_mcu.add_config_object(self) + self.host_mcu.register_config_callback(self._build_config) self.sr_oid = self.host_mcu.create_oid() str_sr_disabled = "".join(["%02x" % (x,) for x in self.sr_disabled]) self.host_mcu.add_config_cmd( @@ -184,7 +184,7 @@ class Replicape: self.sr_oid, REPLICAPE_SHIFT_REGISTER_BUS, str_sr_disabled)) self.host_mcu.add_config_cmd("spi_send oid=%d data=%s" % ( self.sr_oid, str_sr_disabled), is_init=True) - def build_config(self): + def _build_config(self): cmd_queue = self.host_mcu.alloc_command_queue() self.spi_send_cmd = self.host_mcu.lookup_command( "spi_send oid=%c data=%*s", cq=cmd_queue) diff --git a/klippy/extras/spi_temperature.py b/klippy/extras/spi_temperature.py index 18e6dc69..9f16d74c 100644 --- a/klippy/extras/spi_temperature.py +++ b/klippy/extras/spi_temperature.py @@ -46,7 +46,7 @@ class SensorBase: oid, spi_oid, chip_type)) mcu.register_msg(self._handle_spi_response, "thermocouple_result", oid) - mcu.add_config_object(self) + mcu.register_config_callback(self._build_config) def setup_minmax(self, min_temp, max_temp): adc_range = [self.calc_adc(min_temp), self.calc_adc(max_temp)] self.min_sample_value = min(adc_range) @@ -55,7 +55,7 @@ class SensorBase: self._callback = cb def get_report_time_delta(self): return REPORT_TIME - def build_config(self): + def _build_config(self): clock = self.mcu.get_query_slot(self.oid) self._report_clock = self.mcu.seconds_to_clock(REPORT_TIME) self.mcu.add_config_cmd( diff --git a/klippy/extras/tmc2130.py b/klippy/extras/tmc2130.py index 775a685f..08c7c7f3 100644 --- a/klippy/extras/tmc2130.py +++ b/klippy/extras/tmc2130.py @@ -38,7 +38,7 @@ class TMC2130: "config_spi oid=%d bus=%d pin=%s mode=%d rate=%d shutdown_msg=" % ( self.oid, 0, cs_pin_params['pin'], 3, 4000000)) self.spi_send_cmd = self.spi_transfer_cmd = None - self.mcu.add_config_object(self) + self.mcu.register_config_callback(self.build_config) # Allow virtual endstop to be created self.diag1_pin = config.get('diag1_pin', None) ppins.register_chip("tmc2130_" + self.name, self) diff --git a/klippy/extras/tmc2208.py b/klippy/extras/tmc2208.py index 3fa40400..64202d7d 100644 --- a/klippy/extras/tmc2208.py +++ b/klippy/extras/tmc2208.py @@ -112,7 +112,7 @@ class TMC2208: self.tx_pin = tx_pin_params['pin'] self.oid = self.mcu.create_oid() self.tmcuart_send_cmd = None - self.mcu.add_config_object(self) + self.mcu.register_config_callback(self.build_config) # Add DUMP_TMC command gcode = self.printer.lookup_object("gcode") gcode.register_mux_command( diff --git a/klippy/mcu.py b/klippy/mcu.py index 5060dbeb..4387b84d 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -15,6 +15,7 @@ class MCU_stepper: def __init__(self, mcu, pin_params): self._mcu = mcu self._oid = oid = self._mcu.create_oid() + self._mcu.register_config_callback(self._build_config) self._step_pin = pin_params['pin'] self._invert_step = pin_params['invert'] self._dir_pin = self._invert_dir = None @@ -43,7 +44,7 @@ class MCU_stepper: ffi_main, ffi_lib = chelper.get_ffi() sk = ffi_main.gc(getattr(ffi_lib, alloc_func)(*params), ffi_lib.free) self.set_stepper_kinematics(sk) - def build_config(self): + def _build_config(self): max_error = self._mcu.get_max_stepper_error() min_stop_interval = max(0., self._min_stop_interval - max_error) self._mcu.add_config_cmd( @@ -142,6 +143,7 @@ class MCU_endstop: self._pullup = pin_params['pullup'] self._invert = pin_params['invert'] self._oid = self._home_cmd = self._query_cmd = None + self._mcu.register_config_callback(self._build_config) self._homing = False self._min_query_time = self._next_query_time = 0. self._last_state = {} @@ -155,7 +157,7 @@ class MCU_endstop: self._steppers.append(stepper) def get_steppers(self): return list(self._steppers) - def build_config(self): + def _build_config(self): self._oid = self._mcu.create_oid() self._mcu.add_config_cmd( "config_end_stop oid=%d pin=%s pull_up=%d stepper_count=%d" % ( @@ -236,6 +238,7 @@ class MCU_digital_out: def __init__(self, mcu, pin_params): self._mcu = mcu self._oid = None + self._mcu.register_config_callback(self._build_config) self._pin = pin_params['pin'] self._invert = pin_params['invert'] self._start_value = self._shutdown_value = self._invert @@ -253,7 +256,7 @@ class MCU_digital_out: self._start_value = (not not start_value) ^ self._invert self._shutdown_value = (not not shutdown_value) ^ self._invert self._is_static = is_static - def build_config(self): + def _build_config(self): if self._is_static: self._mcu.add_config_cmd("set_digital_out pin=%s value=%d" % ( self._pin, self._start_value)) @@ -282,6 +285,7 @@ class MCU_pwm: self._cycle_time = 0.100 self._max_duration = 2. self._oid = None + self._mcu.register_config_callback(self._build_config) self._pin = pin_params['pin'] self._invert = pin_params['invert'] self._start_value = self._shutdown_value = float(self._invert) @@ -305,7 +309,7 @@ class MCU_pwm: self._start_value = max(0., min(1., start_value)) self._shutdown_value = max(0., min(1., shutdown_value)) self._is_static = is_static - def build_config(self): + def _build_config(self): cmd_queue = self._mcu.alloc_command_queue() cycle_ticks = self._mcu.seconds_to_clock(self._cycle_time) if self._hardware_pwm: @@ -363,6 +367,7 @@ class MCU_adc: self._sample_count = self._range_check_count = 0 self._report_clock = 0 self._oid = self._callback = None + self._mcu.register_config_callback(self._build_config) self._inv_max_adc = 0. def get_mcu(self): return self._mcu @@ -376,7 +381,7 @@ class MCU_adc: def setup_adc_callback(self, report_time, callback): self._report_time = report_time self._callback = callback - def build_config(self): + def _build_config(self): if not self._sample_count: return self._oid = self._mcu.create_oid() @@ -437,7 +442,7 @@ class MCU: # Config building self._printer.lookup_object('pins').register_chip(self._name, self) self._oid_count = 0 - self._config_objects = [] + self._config_callbacks = [] self._init_cmds = [] self._config_cmds = [] self._pin_map = config.get('pin_map', None) @@ -516,8 +521,8 @@ class MCU: self.add_config_cmd(line) def _send_config(self, prev_crc): # Build config commands - for co in self._config_objects: - co.build_config() + for cb in self._config_callbacks: + cb() self._add_custom() self._config_cmds.insert(0, "allocate_oids count=%d" % ( self._oid_count,)) @@ -622,14 +627,12 @@ class MCU: 'digital_out': MCU_digital_out, 'pwm': MCU_pwm, 'adc': MCU_adc} if pin_type not in pcs: raise pins.error("pin type %s not supported on mcu" % (pin_type,)) - co = pcs[pin_type](self, pin_params) - self.add_config_object(co) - return co + return pcs[pin_type](self, pin_params) def create_oid(self): self._oid_count += 1 return self._oid_count - 1 - def add_config_object(self, co): - self._config_objects.append(co) + def register_config_callback(self, cb): + self._config_callbacks.append(cb) def add_config_cmd(self, cmd, is_init=False): if is_init: self._init_cmds.append(cmd)