aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2024-09-21 15:47:19 -0400
committerKevin O'Connor <kevin@koconnor.net>2024-09-30 12:23:24 -0400
commit8f361a15b2c13f532f6ea16db448f749dcc408d3 (patch)
treec505f135417fd06aba8073aed12b786414afe971
parentf4143af4fa26822db2e1c4321de7377b808ce16f (diff)
downloadkutter-8f361a15b2c13f532f6ea16db448f749dcc408d3.tar.gz
kutter-8f361a15b2c13f532f6ea16db448f749dcc408d3.tar.xz
kutter-8f361a15b2c13f532f6ea16db448f749dcc408d3.zip
fan_generic: Support setting a TEMPLATE on SET_FAN_SPEED commands
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
-rw-r--r--docs/G-Codes.md14
-rw-r--r--klippy/extras/fan_generic.py22
2 files changed, 33 insertions, 3 deletions
diff --git a/docs/G-Codes.md b/docs/G-Codes.md
index f855f364..63c84f56 100644
--- a/docs/G-Codes.md
+++ b/docs/G-Codes.md
@@ -476,6 +476,20 @@ enabled.
`SET_FAN_SPEED FAN=config_name SPEED=<speed>` This command sets the
speed of a fan. "speed" must be between 0.0 and 1.0.
+`SET_FAN_SPEED PIN=config_name TEMPLATE=<template_name>
+[<param_x>=<literal>]`: If `TEMPLATE` is specified then it assigns a
+[display_template](Config_Reference.md#display_template) to the given
+fan. For example, if one defined a `[display_template
+my_fan_template]` config section then one could assign
+`TEMPLATE=my_fan_template` here. The display_template should produce a
+string containing a floating point number with the desired value. The
+template will be continuously evaluated and the fan will be
+automatically set to the resulting speed. One may set display_template
+parameters to use during template evaluation (parameters will be
+parsed as Python literals). If TEMPLATE is an empty string then this
+command will clear any previous template assigned to the pin (one can
+then use `SET_FAN_SPEED` commands to manage the values directly).
+
### [filament_switch_sensor]
The following command is available when a
diff --git a/klippy/extras/fan_generic.py b/klippy/extras/fan_generic.py
index def9a77e..7584974f 100644
--- a/klippy/extras/fan_generic.py
+++ b/klippy/extras/fan_generic.py
@@ -1,9 +1,9 @@
# Support fans that are controlled by gcode
#
-# Copyright (C) 2016-2020 Kevin O'Connor <kevin@koconnor.net>
+# Copyright (C) 2016-2024 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
-from . import fan
+from . import fan, output_pin
class PrinterFanGeneric:
cmd_SET_FAN_SPEED_help = "Sets the speed of a fan"
@@ -12,6 +12,9 @@ class PrinterFanGeneric:
self.fan = fan.Fan(config, default_shutdown_speed=0.)
self.fan_name = config.get_name().split()[-1]
+ # Template handling
+ self.template_eval = output_pin.lookup_template_eval(config)
+
gcode = self.printer.lookup_object("gcode")
gcode.register_mux_command("SET_FAN_SPEED", "FAN",
self.fan_name,
@@ -20,8 +23,21 @@ class PrinterFanGeneric:
def get_status(self, eventtime):
return self.fan.get_status(eventtime)
+ def _template_update(self, text):
+ try:
+ value = float(text)
+ except ValueError as e:
+ logging.exception("fan_generic template render error")
+ self.fan.set_speed(value)
def cmd_SET_FAN_SPEED(self, gcmd):
- speed = gcmd.get_float('SPEED', 0.)
+ speed = gcmd.get_float('SPEED', None, 0.)
+ template = gcmd.get('TEMPLATE', None)
+ if (speed is None) == (template is None):
+ raise gcmd.error("SET_FAN_SPEED must specify SPEED or TEMPLATE")
+ # Check for template setting
+ if template is not None:
+ self.template_eval.set_template(gcmd, self._template_update)
+ return
self.fan.set_speed_from_command(speed)
def load_config_prefix(config):