Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gcode_macro: Add "rawparams" pseudo-variable #4727

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/Command_Templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,20 @@ gcode:
M140 S{bed_temp}
```

### The "rawparams" variable

The full unparsed parameters for the running macro can be access via the `rawparams` pseudo-variable.

This is quite useful if you want to change the behavior of certain commands like the `M117`. For example:

```
[gcode_macro M117]
rename_existing: M117.1
gcode:
M117.1 { rawparams }
M118 { rawparams }
```

### The "printer" Variable

It is possible to inspect (and alter) the current state of the printer
Expand Down
15 changes: 2 additions & 13 deletions klippy/extras/display_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,8 @@ def cmd_M73(self, gcmd):
curtime = self.printer.get_reactor().monotonic()
self.expire_progress = curtime + M73_TIMEOUT
def cmd_M117(self, gcmd):
msg = gcmd.get_commandline()
umsg = msg.upper()
if not umsg.startswith('M117'):
# Parse out additional info if M117 recd during a print
start = umsg.find('M117')
end = msg.rfind('*')
if end >= 0:
msg = msg[:end]
msg = msg[start:]
if len(msg) > 5:
self.message = msg[5:]
else:
self.message = None
msg = gcmd.get_raw_command_parameters() or None
self.message = msg

def load_config(config):
return DisplayStatus(config)
1 change: 1 addition & 0 deletions klippy/extras/gcode_macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def cmd(self, gcmd):
kwparams = dict(self.variables)
kwparams.update(self.template.create_template_context())
kwparams['params'] = gcmd.get_command_parameters()
kwparams['rawparams'] = gcmd.get_raw_command_parameters()
self.in_script = True
try:
self.template.run_gcode_from_command(kwparams)
Expand Down
12 changes: 1 addition & 11 deletions klippy/extras/respond.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,7 @@ def __init__(self, config):
gcode.register_command('RESPOND', self.cmd_RESPOND, True,
desc=self.cmd_RESPOND_help)
def cmd_M118(self, gcmd):
msg = gcmd.get_commandline()
umsg = msg.upper()
if not umsg.startswith('M118'):
# Parse out additional info if M118 recd during a print
start = umsg.find('M118')
end = msg.rfind('*')
msg = msg[start:end]
if len(msg) > 5:
msg = msg[5:]
else:
msg = ''
msg = gcmd.get_raw_command_parameters()
gcmd.respond_raw("%s %s" % (self.default_prefix, msg))
cmd_RESPOND_help = ("Echo the message prepended with a prefix")
def cmd_RESPOND(self, gcmd):
Expand Down
16 changes: 16 additions & 0 deletions klippy/gcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ def get_commandline(self):
return self._commandline
def get_command_parameters(self):
return self._params
def get_raw_command_parameters(self):
rawparams = self._commandline
command = self._command
urawparams = rawparams.upper()
if not urawparams.startswith(command):
start = urawparams.find(command)
end = rawparams.rfind('*')
if end >= 0:
rawparams = rawparams[:end]
rawparams = rawparams[start:]
commandlen = len(command) + 1
if len(rawparams) > commandlen:
rawparams = rawparams[commandlen:]
else:
rawparams = ''
return rawparams
def ack(self, msg=None):
if not self._need_ack:
return False
Expand Down