From 68dc28580fd6e7500e5e8fa515763a25d6a03c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janar=20S=C3=B6=C3=B6t?= Date: Thu, 27 Sep 2018 19:08:01 +0300 Subject: [PATCH] gcode_macro: default parameter values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Janar Sööt Signed-off-by: Kevin O'Connor --- config/example-extras.cfg | 8 ++++++++ klippy/extras/gcode_macro.py | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/config/example-extras.cfg b/config/example-extras.cfg index 8b1cad33..1e5776f9 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -976,6 +976,14 @@ # a macro MY_DELAY with gcode "G4 P{DELAY}" then the command # "MY_DELAY DELAY=100" would evaluate to "G4 P100". This parameter # must be provided. +#default_parameter_: +# One may define any number of options with a "default_parameter_" +# prefix. Use this to define default values for g-code parameters. +# For example, if one were to define the macro MY_DELAY with gcode +# "G4 P{DELAY}" along with "default_paramter_DELAY = 50" then the +# command "MY_DELAY" would evaluate to "G4 P50". The default is to +# require that all parameters used in the gcode script be present in +# the command invoking the macro. # Replicape support - see the generic-replicape.cfg file for further diff --git a/klippy/extras/gcode_macro.py b/klippy/extras/gcode_macro.py index 5e93e360..7fe4bfed 100644 --- a/klippy/extras/gcode_macro.py +++ b/klippy/extras/gcode_macro.py @@ -5,6 +5,8 @@ # This file may be distributed under the terms of the GNU GPLv3 license. import logging +DEFAULT_PREFIX = 'default_parameter_' + class GCodeMacro: def __init__(self, config): self.alias = config.get_name().split()[1].upper() @@ -17,14 +19,18 @@ class GCodeMacro: except self.gcode.error as e: raise config.error(str(e)) self.in_script = False + self.kwparams = { o[len(DEFAULT_PREFIX):].upper(): config.get(o) + for o in config.get_prefix_options(DEFAULT_PREFIX) } cmd_desc = "G-Code macro" def cmd(self, params): if self.in_script: raise self.gcode.error( "Macro %s called recursively" % (self.alias,)) script = "" + kwparams = dict(self.kwparams) + kwparams.update(params) try: - script = self.script.format(**params) + script = self.script.format(**kwparams) except Exception: msg = "Macro %s script formatting failed" % (self.alias,) logging.exception(msg)