output_pin: Support setting a TEMPLATE on SET_PIN commands
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
8a7a39530e
commit
1c0adb9af8
@ -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
|
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.
|
`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]
|
### [palette2]
|
||||||
|
|
||||||
The following commands are available when the
|
The following commands are available when the
|
||||||
|
@ -67,7 +67,7 @@ class Fan:
|
|||||||
self.last_fan_value = self.last_req_value = value
|
self.last_fan_value = self.last_req_value = value
|
||||||
self.mcu_fan.set_pwm(print_time, value)
|
self.mcu_fan.set_pwm(print_time, value)
|
||||||
def set_speed(self, 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):
|
def set_speed_from_command(self, value):
|
||||||
self.gcrq.queue_gcode_request(value)
|
self.gcrq.queue_gcode_request(value)
|
||||||
def _handle_request_restart(self, print_time):
|
def _handle_request_restart(self, print_time):
|
||||||
|
@ -58,7 +58,10 @@ class GCodeRequestQueue:
|
|||||||
def queue_gcode_request(self, value):
|
def queue_gcode_request(self, value):
|
||||||
self.toolhead.register_lookahead_callback(
|
self.toolhead.register_lookahead_callback(
|
||||||
(lambda pt: self._queue_request(pt, value)))
|
(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:
|
while 1:
|
||||||
next_time = max(print_time, self.next_min_flush_time)
|
next_time = max(print_time, self.next_min_flush_time)
|
||||||
# Invoke callback for the request
|
# Invoke callback for the request
|
||||||
@ -202,6 +205,8 @@ class PrinterOutputPin:
|
|||||||
# Create gcode request queue
|
# Create gcode request queue
|
||||||
self.gcrq = GCodeRequestQueue(config, self.mcu_pin.get_mcu(),
|
self.gcrq = GCodeRequestQueue(config, self.mcu_pin.get_mcu(),
|
||||||
self._set_pin)
|
self._set_pin)
|
||||||
|
# Template handling
|
||||||
|
self.template_eval = lookup_template_eval(config)
|
||||||
# Register commands
|
# Register commands
|
||||||
pin_name = config.get_name().split()[1]
|
pin_name = config.get_name().split()[1]
|
||||||
gcode = self.printer.lookup_object('gcode')
|
gcode = self.printer.lookup_object('gcode')
|
||||||
@ -218,10 +223,23 @@ class PrinterOutputPin:
|
|||||||
self.mcu_pin.set_pwm(print_time, value)
|
self.mcu_pin.set_pwm(print_time, value)
|
||||||
else:
|
else:
|
||||||
self.mcu_pin.set_digital(print_time, value)
|
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"
|
cmd_SET_PIN_help = "Set the value of an output pin"
|
||||||
def cmd_SET_PIN(self, gcmd):
|
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
|
# Read requested value
|
||||||
value = gcmd.get_float('VALUE', minval=0., maxval=self.scale)
|
|
||||||
value /= self.scale
|
value /= self.scale
|
||||||
if not self.is_pwm and value not in [0., 1.]:
|
if not self.is_pwm and value not in [0., 1.]:
|
||||||
raise gcmd.error("Invalid pin value")
|
raise gcmd.error("Invalid pin value")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user