From ec5719585e022492ba89f8f409a2c96740eb16d5 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 19 Sep 2022 14:04:18 -0400 Subject: [PATCH] spi_temperature: Separate out fault handling from temperature calculation Introduce a new handle_fault() method to handle processing of sensor faults. This simplifies the calc_temp() methods. Signed-off-by: Kevin O'Connor --- klippy/extras/spi_temperature.py | 61 ++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/klippy/extras/spi_temperature.py b/klippy/extras/spi_temperature.py index be8cc11e..29df3536 100644 --- a/klippy/extras/spi_temperature.py +++ b/klippy/extras/spi_temperature.py @@ -51,12 +51,15 @@ class SensorBase: self.oid, clock, self._report_clock, self.min_sample_value, self.max_sample_value), is_init=True) def _handle_spi_response(self, params): - temp = self.calc_temp(params['value'], params['fault']) + if params['fault']: + self.handle_fault(params['value'], params['fault']) + return + temp = self.calc_temp(params['value']) next_clock = self.mcu.clock32_to_clock64(params['next_clock']) last_read_clock = next_clock - self._report_clock last_read_time = self.mcu.clock_to_print_time(last_read_clock) self._callback(last_read_time, temp) - def fault(self, msg): + def report_fault(self, msg): self.printer.invoke_async_shutdown(msg) @@ -120,23 +123,24 @@ class MAX31856(SensorBase): def __init__(self, config): SensorBase.__init__(self, config, "MAX31856", self.build_spi_init(config)) - def calc_temp(self, adc, fault): + def handle_fault(self, adc, fault): if fault & MAX31856_FAULT_CJRANGE: - self.fault("Max31856: Cold Junction Range Fault") + self.report_fault("Max31856: Cold Junction Range Fault") if fault & MAX31856_FAULT_TCRANGE: - self.fault("Max31856: Thermocouple Range Fault") + self.report_fault("Max31856: Thermocouple Range Fault") if fault & MAX31856_FAULT_CJHIGH: - self.fault("Max31856: Cold Junction High Fault") + self.report_fault("Max31856: Cold Junction High Fault") if fault & MAX31856_FAULT_CJLOW: - self.fault("Max31856: Cold Junction Low Fault") + self.report_fault("Max31856: Cold Junction Low Fault") if fault & MAX31856_FAULT_TCHIGH: - self.fault("Max31856: Thermocouple High Fault") + self.report_fault("Max31856: Thermocouple High Fault") if fault & MAX31856_FAULT_TCLOW: - self.fault("Max31856: Thermocouple Low Fault") + self.report_fault("Max31856: Thermocouple Low Fault") if fault & MAX31856_FAULT_OVUV: - self.fault("Max31856: Over/Under Voltage Fault") + self.report_fault("Max31856: Over/Under Voltage Fault") if fault & MAX31856_FAULT_OPEN: - self.fault("Max31856: Thermocouple Open Fault") + self.report_fault("Max31856: Thermocouple Open Fault") + def calc_temp(self, adc): adc = adc >> MAX31856_SCALE # Fix sign bit: if adc & 0x40000: @@ -192,13 +196,14 @@ MAX31855_MULT = 0.25 class MAX31855(SensorBase): def __init__(self, config): SensorBase.__init__(self, config, "MAX31855", spi_mode=0) - def calc_temp(self, adc, fault): + def handle_fault(self, adc, fault): if fault & 0x1: - self.fault("MAX31855 : Open Circuit") + self.report_fault("MAX31855 : Open Circuit") if fault & 0x2: - self.fault("MAX31855 : Short to GND") + self.report_fault("MAX31855 : Short to GND") if fault & 0x4: - self.fault("MAX31855 : Short to Vcc") + self.report_fault("MAX31855 : Short to Vcc") + def calc_temp(self, adc): adc = adc >> MAX31855_SCALE # Fix sign bit: if adc & 0x2000: @@ -221,11 +226,12 @@ MAX6675_MULT = 0.25 class MAX6675(SensorBase): def __init__(self, config): SensorBase.__init__(self, config, "MAX6675", spi_mode=0) - def calc_temp(self, adc, fault): + def handle_fault(self, adc, fault): if fault & 0x02: - self.fault("Max6675 : Device ID error") + self.report_fault("Max6675 : Device ID error") if fault & 0x04: - self.fault("Max6675 : Thermocouple Open Fault") + self.report_fault("Max6675 : Thermocouple Open Fault") + def calc_temp(self, adc): adc = adc >> MAX6675_SCALE # Fix sign bit: if adc & 0x2000: @@ -279,22 +285,25 @@ class MAX31865(SensorBase): self.adc_to_resist_div_nominal = adc_to_resist / rtd_nominal_r SensorBase.__init__(self, config, "MAX31865", self.build_spi_init(config)) - def calc_temp(self, adc, fault): + def handle_fault(self, adc, fault): if fault & 0x80: - self.fault("Max31865 RTD input is disconnected") + self.report_fault("Max31865 RTD input is disconnected") if fault & 0x40: - self.fault("Max31865 RTD input is shorted") + self.report_fault("Max31865 RTD input is shorted") if fault & 0x20: - self.fault( + self.report_fault( "Max31865 VREF- is greater than 0.85 * VBIAS, FORCE- open") if fault & 0x10: - self.fault("Max31865 VREF- is less than 0.85 * VBIAS, FORCE- open") + self.report_fault( + "Max31865 VREF- is less than 0.85 * VBIAS, FORCE- open") if fault & 0x08: - self.fault("Max31865 VRTD- is less than 0.85 * VBIAS, FORCE- open") + self.report_fault( + "Max31865 VRTD- is less than 0.85 * VBIAS, FORCE- open") if fault & 0x04: - self.fault("Max31865 Overvoltage or undervoltage fault") + self.report_fault("Max31865 Overvoltage or undervoltage fault") if not fault & 0xfc: - self.fault("Max31865 Unspecified error") + self.report_fault("Max31865 Unspecified error") + def calc_temp(self, adc): adc = adc >> 1 # remove fault bit R_div_nominal = adc * self.adc_to_resist_div_nominal # Resistance (relative to rtd_nominal_r) is calculated using: