aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--docs/G-Codes.md14
-rw-r--r--klippy/extras/fan.py2
-rw-r--r--klippy/extras/output_pin.py22
3 files changed, 35 insertions, 3 deletions
diff --git a/docs/G-Codes.md b/docs/G-Codes.md
index 2444c53b..f855f364 100644
--- a/docs/G-Codes.md
+++ b/docs/G-Codes.md
@@ -857,6 +857,20 @@ output `VALUE`. VALUE should be 0 or 1 for "digital" output pins. For
PWM pins, set to a value between 0.0 and 1.0, or between 0.0 and
`scale` if a scale is configured in the output_pin config section.
+`SET_PIN 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
+pin. For example, if one defined a `[display_template
+my_pin_template]` config section then one could assign
+`TEMPLATE=my_pin_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 pin will be
+automatically set to the resulting value. 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_PIN` commands to manage the values directly).
+
### [palette2]
The following commands are available when the
diff --git a/klippy/extras/fan.py b/klippy/extras/fan.py
index 14769473..0fc88a37 100644
--- a/klippy/extras/fan.py
+++ b/klippy/extras/fan.py
@@ -67,7 +67,7 @@ class Fan:
self.last_fan_value = self.last_req_value = value
self.mcu_fan.set_pwm(print_time, value)
def set_speed(self, print_time, value):
- self.gcrq.send_async_request(print_time, value)
+ self.gcrq.send_async_request(value, print_time)
def set_speed_from_command(self, value):
self.gcrq.queue_gcode_request(value)
def _handle_request_restart(self, print_time):
diff --git a/klippy/extras/output_pin.py b/klippy/extras/output_pin.py
index 524b1844..24d27a62 100644
--- a/klippy/extras/output_pin.py
+++ b/klippy/extras/output_pin.py
@@ -58,7 +58,10 @@ class GCodeRequestQueue:
def queue_gcode_request(self, value):
self.toolhead.register_lookahead_callback(
(lambda pt: self._queue_request(pt, value)))
- def send_async_request(self, print_time, value):
+ def send_async_request(self, value, print_time=None):
+ if print_time is None:
+ systime = self.printer.get_reactor().monotonic()
+ print_time = self.mcu.estimated_print_time(systime + PIN_MIN_TIME)
while 1:
next_time = max(print_time, self.next_min_flush_time)
# Invoke callback for the request
@@ -202,6 +205,8 @@ class PrinterOutputPin:
# Create gcode request queue
self.gcrq = GCodeRequestQueue(config, self.mcu_pin.get_mcu(),
self._set_pin)
+ # Template handling
+ self.template_eval = lookup_template_eval(config)
# Register commands
pin_name = config.get_name().split()[1]
gcode = self.printer.lookup_object('gcode')
@@ -218,10 +223,23 @@ class PrinterOutputPin:
self.mcu_pin.set_pwm(print_time, value)
else:
self.mcu_pin.set_digital(print_time, value)
+ def _template_update(self, text):
+ try:
+ value = float(text)
+ except ValueError as e:
+ logging.exception("output_pin template render error")
+ self.gcrq.send_async_request(value)
cmd_SET_PIN_help = "Set the value of an output pin"
def cmd_SET_PIN(self, gcmd):
+ value = gcmd.get_float('VALUE', None, minval=0., maxval=self.scale)
+ template = gcmd.get('TEMPLATE', None)
+ if (value is None) == (template is None):
+ raise gcmd.error("SET_PIN command must specify VALUE or TEMPLATE")
+ # Check for template setting
+ if template is not None:
+ self.template_eval.set_template(gcmd, self._template_update)
+ return
# Read requested value
- value = gcmd.get_float('VALUE', minval=0., maxval=self.scale)
value /= self.scale
if not self.is_pwm and value not in [0., 1.]:
raise gcmd.error("Invalid pin value")