From 6086ca19a7fc102a4086477f658fba0aa2eda9b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Doma=C5=84ski?= Date: Wed, 21 Feb 2024 00:48:37 +0100 Subject: [PATCH] tmc2240: add current formatters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Run current, hold current and current range will be shown in Amperes. Each 2240 driver now has a separate instance of FieldFormatters, since the current formatters need to access a few fields in order to calculate the current. Signed-off-by: Kamil DomaƄski --- klippy/extras/tmc2240.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/klippy/extras/tmc2240.py b/klippy/extras/tmc2240.py index aef2280f279a..943eb9aa53bb 100644 --- a/klippy/extras/tmc2240.py +++ b/klippy/extras/tmc2240.py @@ -313,13 +313,15 @@ def _calc_current(self, run_current, hold_current): irun = self._calc_current_bits(run_current, gscaler) ihold = self._calc_current_bits(min(hold_current, run_current), gscaler) return gscaler, irun, ihold - def _calc_current_from_field(self, field_name): + def _calc_current_from_field_value(self, value): ifs_rms = self._get_ifs_rms() globalscaler = self.fields.get_field("globalscaler") if not globalscaler: globalscaler = 256 + return globalscaler * (value + 1) * ifs_rms / (256. * 32.) + def _calc_current_from_field(self, field_name): bits = self.fields.get_field(field_name) - return globalscaler * (bits + 1) * ifs_rms / (256. * 32.) + return self._calc_current_from_field_value(bits) def get_current(self): ifs_rms = self._get_ifs_rms() run_current = self._calc_current_from_field("irun") @@ -342,7 +344,8 @@ def set_current(self, run_current, hold_current, print_time): class TMC2240: def __init__(self, config): # Setup mcu communication - self.fields = tmc.FieldHelper(Fields, SignedFields, FieldFormatters) + self.fields = tmc.FieldHelper(Fields, SignedFields, + FieldFormatters.copy()) if config.get("uart_pin", None) is not None: # use UART for communication self.mcu_tmc = tmc_uart.MCU_TMC_uart(config, Registers, self.fields, @@ -355,6 +358,14 @@ def __init__(self, config): tmc.TMCVirtualPinHelper(config, self.mcu_tmc) # Register commands current_helper = TMC2240CurrentHelper(config, self.mcu_tmc) + self.fields.field_formatters.update({ + "current_range": (lambda v: "%d (%.2fA RMS)" + % (v, current_helper._get_ifs_rms(v))), + "ihold": (lambda v: "%d (%.2fA RMS)" + % (v, current_helper._calc_current_from_field_value(v))), + "irun": (lambda v: "%d (%.2fA RMS)" + % (v, current_helper._calc_current_from_field_value(v))), + }) cmdhelper = tmc.TMCCommandHelper(config, self.mcu_tmc, current_helper) cmdhelper.setup_register_dump(ReadRegisters) self.get_phase_offset = cmdhelper.get_phase_offset