gcode_macro: Support overriding of builtin g-code commands

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2020-02-12 20:34:20 -05:00
parent 0cfab84e1a
commit 9818055ac3
2 changed files with 30 additions and 2 deletions

View File

@ -420,6 +420,13 @@
# can be changed at run-time using the SET_GCODE_VARIABLE command
# (see docs/Command_Templates.md for details). Variable names may
# not use upper case characters.
#rename_existing:
# This option will cause the macro to override an existing G-Code
# command, and the existing definition for the command will be
# renamed to the name provided here. This can be used to override
# builtin G-Code commands. Care should be taken when overriding
# commands as that can cause complex and unexpected results. The
# default is to not override an existing G-Code command.
# Execute a gcode on a set delay.
#[delayed_gcode my_delayed_gcode]

View File

@ -92,11 +92,22 @@ class GCodeMacro:
def __init__(self, config):
name = config.get_name().split()[1]
self.alias = name.upper()
printer = config.get_printer()
self.printer = printer = config.get_printer()
gcode_macro = printer.try_load_module(config, 'gcode_macro')
self.template = gcode_macro.load_template(config, 'gcode')
self.gcode = printer.lookup_object('gcode')
self.gcode.register_command(self.alias, self.cmd, desc=self.cmd_desc)
self.rename_existing = config.get("rename_existing", None)
if self.rename_existing is not None:
if (self.gcode.is_traditional_gcode(self.alias)
!= self.gcode.is_traditional_gcode(self.rename_existing)):
raise config.error(
"G-Code macro rename of different types ('%s' vs '%s')"
% (self.alias, self.rename_existing))
printer.register_event_handler("klippy:connect",
self.handle_connect)
else:
self.gcode.register_command(self.alias, self.cmd,
desc=self.cmd_desc)
self.gcode.register_mux_command("SET_GCODE_VARIABLE", "MACRO",
name, self.cmd_SET_GCODE_VARIABLE,
desc=self.cmd_SET_GCODE_VARIABLE_help)
@ -114,6 +125,16 @@ class GCodeMacro:
raise config.error(
"Option '%s' in section '%s' is not a valid literal" % (
option, config.get_name()))
def handle_connect(self):
prev_cmd = self.gcode.register_command(self.alias, None)
if prev_cmd is None:
raise self.printer.config_error(
"Existing command '%s' not found in gcode_macro rename"
% (self.alias,))
pdesc = "Renamed builtin of '%s'" % (self.alias,)
self.gcode.register_command(self.rename_existing, prev_cmd, desc=pdesc)
self.gcode.register_command(self.alias, self.cmd, desc=self.cmd_desc)
return dict(self.variables)
def get_status(self, eventtime):
return dict(self.variables)
cmd_SET_GCODE_VARIABLE_help = "Set the value of a G-Code macro variable"