Skip to content

Commit

Permalink
gcode_macro: Add "rawparams" pseudo-variable
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Lamas <pedrolamas@gmail.com>
  • Loading branch information
pedrolamas committed Nov 19, 2021
1 parent a5ec751 commit d7b6499
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
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

0 comments on commit d7b6499

Please sign in to comment.