Skip to content

Commit

Permalink
temperature_mcu: Enhance "ADC out of range" error reports
Browse files Browse the repository at this point in the history
Try to report which ADC is reporting out of range.

Signed-off-by: Kevin O'Connor <[email protected]>
  • Loading branch information
KevinOConnor committed Jun 21, 2024
1 parent 2d73211 commit 6d70050
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions klippy/extras/temperature_mcu.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Support for micro-controller chip based temperature sensors
#
# Copyright (C) 2020 Kevin O'Connor <[email protected]>
# Copyright (C) 2020-2024 Kevin O'Connor <[email protected]>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import logging
import mcu
from . import adc_temperature

SAMPLE_TIME = 0.001
SAMPLE_COUNT = 8
Expand All @@ -31,8 +32,8 @@ def __init__(self, config):
self.mcu_adc = ppins.setup_pin('adc',
'%s:ADC_TEMPERATURE' % (mcu_name,))
self.mcu_adc.setup_adc_callback(REPORT_TIME, self.adc_callback)
query_adc = config.get_printer().load_object(config, 'query_adc')
query_adc.register_adc(config.get_name(), self.mcu_adc)
self.diag_helper = adc_temperature.HelperTemperatureDiagnostics(
config, self.mcu_adc, self.calc_temp)
# Register callbacks
if self.printer.get_start_args().get('debugoutput') is not None:
self.mcu_adc.setup_adc_sample(SAMPLE_TIME, SAMPLE_COUNT)
Expand All @@ -51,6 +52,8 @@ def setup_minmax(self, min_temp, max_temp):
def adc_callback(self, read_time, read_value):
temp = self.base_temperature + read_value * self.slope
self.temperature_callback(read_time + SAMPLE_COUNT * SAMPLE_TIME, temp)
def calc_temp(self, adc):
return self.base_temperature + adc * self.slope
def calc_adc(self, temp):
return (temp - self.base_temperature) / self.slope
def calc_base(self, temp, adc):
Expand Down Expand Up @@ -91,9 +94,12 @@ def handle_mcu_identify(self):
self.base_temperature = self.calc_base(self.temp1, self.adc1)
# Setup min/max checks
arange = [self.calc_adc(t) for t in [self.min_temp, self.max_temp]]
min_adc, max_adc = sorted(arange)
self.mcu_adc.setup_adc_sample(SAMPLE_TIME, SAMPLE_COUNT,
minval=min(arange), maxval=max(arange),
minval=min_adc, maxval=max_adc,
range_check_count=RANGE_CHECK_COUNT)
self.diag_helper.setup_diag_minmax(self.min_temp, self.max_temp,
min_adc, max_adc)
def config_unknown(self):
raise self.printer.config_error("MCU temperature not supported on %s"
% (self.mcu_type,))
Expand Down

0 comments on commit 6d70050

Please sign in to comment.