2020-12-03 18:35:37 +01:00
|
|
|
# Support for Raspberry Pi temperature sensor
|
|
|
|
#
|
|
|
|
# Copyright (C) 2020 Al Crate <al3ph@users.noreply.github.com>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2021-02-17 20:59:57 +01:00
|
|
|
HOST_REPORT_TIME = 1.0
|
|
|
|
RPI_PROC_TEMP_FILE = "/sys/class/thermal/thermal_zone0/temp"
|
2020-12-03 18:35:37 +01:00
|
|
|
|
2021-02-17 20:59:57 +01:00
|
|
|
class Temperature_HOST:
|
2020-12-03 18:35:37 +01:00
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
|
|
|
self.reactor = self.printer.get_reactor()
|
|
|
|
self.name = config.get_name().split()[-1]
|
2021-02-20 15:21:51 +01:00
|
|
|
self.path = config.get("sensor_path", RPI_PROC_TEMP_FILE)
|
2020-12-03 18:35:37 +01:00
|
|
|
|
2020-12-03 18:37:56 +01:00
|
|
|
self.temp = self.min_temp = self.max_temp = 0.0
|
2020-12-03 18:35:37 +01:00
|
|
|
|
2021-02-20 00:51:42 +01:00
|
|
|
if config.get("sensor_type", "", note_valid=False).startswith('rpi'):
|
|
|
|
# Temporary backwards compatibility
|
|
|
|
self.printer.add_object("rpi_temperature " + self.name, self)
|
|
|
|
else:
|
|
|
|
self.printer.add_object("temperature_host " + self.name, self)
|
2021-01-24 19:59:09 +01:00
|
|
|
if self.printer.get_start_args().get('debugoutput') is not None:
|
|
|
|
return
|
2020-12-03 18:35:37 +01:00
|
|
|
self.sample_timer = self.reactor.register_timer(
|
2021-01-24 19:57:58 +01:00
|
|
|
self._sample_pi_temperature)
|
2020-12-03 18:35:37 +01:00
|
|
|
try:
|
2021-02-17 20:59:57 +01:00
|
|
|
self.file_handle = open(self.path, "r")
|
2020-12-03 18:35:37 +01:00
|
|
|
except:
|
2021-01-24 19:57:58 +01:00
|
|
|
raise config.error("Unable to open temperature file '%s'"
|
2021-02-17 20:59:57 +01:00
|
|
|
% (self.path,))
|
2020-12-03 18:35:37 +01:00
|
|
|
|
2021-01-24 19:57:58 +01:00
|
|
|
self.printer.register_event_handler("klippy:connect",
|
|
|
|
self.handle_connect)
|
2020-12-03 18:35:37 +01:00
|
|
|
|
|
|
|
def handle_connect(self):
|
|
|
|
self.reactor.update_timer(self.sample_timer, self.reactor.NOW)
|
|
|
|
|
|
|
|
def setup_minmax(self, min_temp, max_temp):
|
2020-12-03 18:37:56 +01:00
|
|
|
self.min_temp = min_temp
|
|
|
|
self.max_temp = max_temp
|
2020-12-03 18:35:37 +01:00
|
|
|
|
|
|
|
def setup_callback(self, cb):
|
|
|
|
self._callback = cb
|
|
|
|
|
2021-01-24 20:00:41 +01:00
|
|
|
def get_report_time_delta(self):
|
2021-02-17 20:59:57 +01:00
|
|
|
return HOST_REPORT_TIME
|
2021-01-24 20:00:41 +01:00
|
|
|
|
2020-12-03 18:35:37 +01:00
|
|
|
def _sample_pi_temperature(self, eventtime):
|
|
|
|
try:
|
|
|
|
self.file_handle.seek(0)
|
|
|
|
self.temp = float(self.file_handle.read())/1000.0
|
|
|
|
except Exception:
|
2021-02-17 20:59:57 +01:00
|
|
|
logging.exception("temperature_host: Error reading data")
|
2020-12-03 18:35:37 +01:00
|
|
|
self.temp = 0.0
|
|
|
|
return self.reactor.NEVER
|
|
|
|
|
|
|
|
if self.temp < self.min_temp:
|
|
|
|
self.printer.invoke_shutdown(
|
2021-02-17 20:59:57 +01:00
|
|
|
"HOST temperature %0.1f below minimum temperature of %0.1f."
|
2021-01-24 19:57:58 +01:00
|
|
|
% (self.temp, self.min_temp,))
|
2020-12-03 18:35:37 +01:00
|
|
|
if self.temp > self.max_temp:
|
|
|
|
self.printer.invoke_shutdown(
|
2021-02-17 20:59:57 +01:00
|
|
|
"HOST temperature %0.1f above maximum temperature of %0.1f."
|
2021-01-24 19:57:58 +01:00
|
|
|
% (self.temp, self.max_temp,))
|
2020-12-03 18:35:37 +01:00
|
|
|
|
2021-01-24 21:37:37 +01:00
|
|
|
mcu = self.printer.lookup_object('mcu')
|
2020-12-03 18:35:37 +01:00
|
|
|
measured_time = self.reactor.monotonic()
|
2021-01-24 21:37:37 +01:00
|
|
|
self._callback(mcu.estimated_print_time(measured_time), self.temp)
|
2021-02-17 20:59:57 +01:00
|
|
|
return measured_time + HOST_REPORT_TIME
|
2020-12-03 18:35:37 +01:00
|
|
|
|
|
|
|
def get_status(self, eventtime):
|
|
|
|
return {
|
|
|
|
'temperature': self.temp,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def load_config(config):
|
|
|
|
# Register sensor
|
|
|
|
pheaters = config.get_printer().load_object(config, "heaters")
|
2021-02-17 20:59:57 +01:00
|
|
|
pheaters.add_sensor_factory("temperature_host", Temperature_HOST)
|
|
|
|
pheaters.add_sensor_factory("rpi_temperature", Temperature_HOST) # XXX
|