From f39fd7d5e02f33aa419b4911d21fb648b1576cae Mon Sep 17 00:00:00 2001 From: David Holdeman Date: Sat, 15 Jun 2024 15:23:22 -0500 Subject: [PATCH] don't use spi temp until connect has happened print stacktrace if clock is used before frequency is set --- klippy/clocksync.py | 10 +++++++++- klippy/extras/spi_temperature.py | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/klippy/clocksync.py b/klippy/clocksync.py index 5da85af3f..291481005 100644 --- a/klippy/clocksync.py +++ b/klippy/clocksync.py @@ -3,7 +3,7 @@ # Copyright (C) 2016-2018 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. -import logging, math +import logging, math, traceback RTT_AGE = 0.000010 / (60.0 * 60.0) DECAY = 1.0 / 30.0 @@ -237,10 +237,18 @@ def connect_file(self, serial, pace=False): # clock frequency conversions def print_time_to_clock(self, print_time): + if self.clock_adj[1] == 1.0: + logging.warning("Clock not yet synchronized, clock is untrustworthy") + for line in traceback.format_stack(): + logging.warning(line.strip()) adjusted_offset, adjusted_freq = self.clock_adj return int((print_time - adjusted_offset) * adjusted_freq) def clock_to_print_time(self, clock): + if self.clock_adj[1] == 1.0: + logging.warning("Clock not yet synchronized, print time is untrustworthy") + for line in traceback.format_stack(): + logging.warning(line.strip()) adjusted_offset, adjusted_freq = self.clock_adj return clock / adjusted_freq + adjusted_offset diff --git a/klippy/extras/spi_temperature.py b/klippy/extras/spi_temperature.py index 5556ced37..53d0fc934 100644 --- a/klippy/extras/spi_temperature.py +++ b/klippy/extras/spi_temperature.py @@ -32,9 +32,13 @@ def __init__(self, config, chip_type, config_cmd=None, spi_mode=1): self.mcu = mcu = self.spi.get_mcu() # Reader chip configuration self.oid = oid = mcu.create_oid() + self.printer.register_event_handler( + "klippy:connect", self._handle_connect + ) mcu.register_response( self._handle_spi_response, "thermocouple_result", oid ) + self._is_connected = False mcu.register_config_callback(self._build_config) def setup_minmax(self, min_temp, max_temp): @@ -48,6 +52,9 @@ def setup_callback(self, cb): def get_report_time_delta(self): return REPORT_TIME + def _handle_connect(self): + self._is_connected = True + def _build_config(self): self.mcu.add_config_cmd( "config_thermocouple oid=%u spi_oid=%u thermocouple_type=%s" @@ -76,6 +83,8 @@ def _build_config(self): ) def _handle_spi_response(self, params): + if not self._is_connected: + return if params["fault"]: self.handle_fault(params["value"], params["fault"]) return