mirror of https://github.com/Desuuuu/klipper.git
gcode_macro: Parse variable_X parameters using ast.literal_eval()
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
01f3b50e73
commit
8b00580884
|
@ -1454,11 +1454,11 @@
|
||||||
# the command invoking the macro.
|
# the command invoking the macro.
|
||||||
#variable_<name>:
|
#variable_<name>:
|
||||||
# One may specify any number of options with a "variable_" prefix.
|
# One may specify any number of options with a "variable_" prefix.
|
||||||
# The given variable name will then be available during macro
|
# The given variable name will be assigned the given value (parsed
|
||||||
# expansion. For example, a config with "variable_my_name = zebra"
|
# as a Python literal) and will be available during macro expansion.
|
||||||
# might have a gcode config containing "M117 My name is {my_name}".
|
# For example, a config with "variable_fan_speed = 75" might have
|
||||||
# Variables can be changed at run-time using the SET_GCODE_VARIABLE
|
# gcode commands containing "M106 S{ fan_speed * 255 }". Variables
|
||||||
# command.
|
# can be changed at run-time using the SET_GCODE_VARIABLE command.
|
||||||
|
|
||||||
|
|
||||||
# Enable the "M118" and "RESPOND" extended commands.
|
# Enable the "M118" and "RESPOND" extended commands.
|
||||||
|
|
|
@ -6,6 +6,11 @@ All dates in this document are approximate.
|
||||||
|
|
||||||
# Changes
|
# Changes
|
||||||
|
|
||||||
|
20190607: The "variable_X" parameters of gcode_macro (along with the
|
||||||
|
VALUE parameter of SET_GCODE_VARIABLE) are now parsed as Python
|
||||||
|
literals. If a value needs to be assigned a string then wrap the value
|
||||||
|
in quotes so that it is evaluated as a string.
|
||||||
|
|
||||||
20190606: The "samples", "samples_result", and "sample_retract_dist"
|
20190606: The "samples", "samples_result", and "sample_retract_dist"
|
||||||
config options have been moved to the "probe" config section. These
|
config options have been moved to the "probe" config section. These
|
||||||
options are no longer supported in the "delta_calibrate", "bed_tilt",
|
options are no longer supported in the "delta_calibrate", "bed_tilt",
|
||||||
|
|
|
@ -182,7 +182,8 @@ The following command is available when a "gcode_macro" config section
|
||||||
is enabled:
|
is enabled:
|
||||||
- `SET_GCODE_VARIABLE MACRO=<macro_name> VARIABLE=<name>
|
- `SET_GCODE_VARIABLE MACRO=<macro_name> VARIABLE=<name>
|
||||||
VALUE=<value>`: This command allows one to change the value of a
|
VALUE=<value>`: This command allows one to change the value of a
|
||||||
gcode_macro variable at run-time.
|
gcode_macro variable at run-time. The provided VALUE is parsed as a
|
||||||
|
Python literal.
|
||||||
|
|
||||||
## Custom Pin Commands
|
## Custom Pin Commands
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
# Copyright (C) 2018-2019 Kevin O'Connor <kevin@koconnor.net>
|
# Copyright (C) 2018-2019 Kevin O'Connor <kevin@koconnor.net>
|
||||||
#
|
#
|
||||||
# This file may be distributed under the terms of the GNU GPLv3 license.
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
||||||
import traceback, logging
|
import traceback, logging, ast
|
||||||
import jinja2
|
import jinja2
|
||||||
|
|
||||||
|
|
||||||
|
@ -100,9 +100,16 @@ class GCodeMacro:
|
||||||
prefix = 'default_parameter_'
|
prefix = 'default_parameter_'
|
||||||
self.kwparams = { o[len(prefix):].upper(): config.get(o)
|
self.kwparams = { o[len(prefix):].upper(): config.get(o)
|
||||||
for o in config.get_prefix_options(prefix) }
|
for o in config.get_prefix_options(prefix) }
|
||||||
|
self.variables = {}
|
||||||
prefix = 'variable_'
|
prefix = 'variable_'
|
||||||
self.variables = { o[len(prefix):]: config.get(o)
|
for option in config.get_prefix_options(prefix):
|
||||||
for o in config.get_prefix_options(prefix) }
|
try:
|
||||||
|
self.variables[option[len(prefix):]] = ast.literal_eval(
|
||||||
|
config.get(option))
|
||||||
|
except ValueError as e:
|
||||||
|
raise config.error(
|
||||||
|
"Option '%s' in section '%s' is not a valid literal" % (
|
||||||
|
option, config.get_name()))
|
||||||
def get_status(self, eventtime):
|
def get_status(self, eventtime):
|
||||||
return dict(self.variables)
|
return dict(self.variables)
|
||||||
cmd_SET_GCODE_VARIABLE_help = "Set the value of a G-Code macro variable"
|
cmd_SET_GCODE_VARIABLE_help = "Set the value of a G-Code macro variable"
|
||||||
|
@ -115,7 +122,12 @@ class GCodeMacro:
|
||||||
return
|
return
|
||||||
raise self.gcode.error("Unknown gcode_macro variable '%s'" % (
|
raise self.gcode.error("Unknown gcode_macro variable '%s'" % (
|
||||||
variable,))
|
variable,))
|
||||||
self.variables[variable] = value
|
try:
|
||||||
|
literal = ast.literal_eval(value)
|
||||||
|
except ValueError as e:
|
||||||
|
raise self.gcode.error("Unable to parse '%s' as a literal" % (
|
||||||
|
value,))
|
||||||
|
self.variables[variable] = literal
|
||||||
cmd_desc = "G-Code macro"
|
cmd_desc = "G-Code macro"
|
||||||
def cmd(self, params):
|
def cmd(self, params):
|
||||||
if self.in_script:
|
if self.in_script:
|
||||||
|
|
|
@ -80,13 +80,13 @@ gcode:
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
[gcode_macro TEST_variable]
|
[gcode_macro TEST_variable]
|
||||||
variable_t: 12
|
variable_t: 12.0
|
||||||
gcode:
|
gcode:
|
||||||
{ printer.gcode.action_respond_info("TEST_variable") }
|
{ printer.gcode.action_respond_info("TEST_variable") }
|
||||||
{% if t|float - 12.0 != printer.toolhead.position.y %}
|
{% if t - 12.0 != printer.toolhead.position.y %}
|
||||||
M112
|
M112
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if printer["gcode_macro TEST_variable"].t|float - 12.0 != 0.0 %}
|
{% if printer["gcode_macro TEST_variable"].t - 12.0 != 0.0 %}
|
||||||
M112
|
M112
|
||||||
{% endif %}
|
{% endif %}
|
||||||
SET_GCODE_VARIABLE MACRO=TEST_variable VARIABLE=t VALUE=17
|
SET_GCODE_VARIABLE MACRO=TEST_variable VARIABLE=t VALUE=17
|
||||||
|
@ -95,7 +95,7 @@ gcode:
|
||||||
[gcode_macro TEST_variable_part2]
|
[gcode_macro TEST_variable_part2]
|
||||||
gcode:
|
gcode:
|
||||||
{ printer.gcode.action_respond_info("TEST_variable_part2") }
|
{ printer.gcode.action_respond_info("TEST_variable_part2") }
|
||||||
{% if printer["gcode_macro TEST_variable"].t|float != 17.0 %}
|
{% if printer["gcode_macro TEST_variable"].t != 17.0 %}
|
||||||
M112
|
M112
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue