Skip to content

Commit

Permalink
don't use spi temp until connect has happened
Browse files Browse the repository at this point in the history
print stacktrace if clock is used before frequency is set
  • Loading branch information
chuckwagoncomputing committed Jun 18, 2024
1 parent d634597 commit f39fd7d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion klippy/clocksync.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
#
# 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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions klippy/extras/spi_temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f39fd7d

Please sign in to comment.