From 36de6118c159638c5f1e77ad554d3488884ebb48 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Tue, 5 Sep 2017 18:19:36 -0400 Subject: [PATCH] mcu: Limit ADC min/max range to a 16bit integer Make sure the ADC range sent to the MCU can be encoded into a 16bit integer. Otherwise, if the provided min_temp/max_temp was outside the range of possible values it could result in a spurious mcu shutdown. In particular, the AD595 could not properly encode a min_temp of zero. Signed-off-by: Kevin O'Connor --- klippy/mcu.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/klippy/mcu.py b/klippy/mcu.py index c72b4ee8..7724e63e 100644 --- a/klippy/mcu.py +++ b/klippy/mcu.py @@ -396,8 +396,9 @@ class MCU_adc: max_adc = self._sample_count * mcu_adc_max self._inv_max_adc = 1.0 / max_adc self._report_clock = int(self._report_time * self._mcu_freq) - min_sample = int(self._min_sample * max_adc) - max_sample = min(0xffff, int(math.ceil(self._max_sample * max_adc))) + min_sample = max(0, min(0xffff, int(self._min_sample * max_adc))) + max_sample = max(0, min(0xffff, int( + math.ceil(self._max_sample * max_adc)))) self._mcu.add_config_cmd( "query_analog_in oid=%d clock=%d sample_ticks=%d sample_count=%d" " rest_ticks=%d min_value=%d max_value=%d" % (