2016-05-25 17:37:40 +02:00
|
|
|
# Parse gcode commands
|
|
|
|
#
|
2021-01-08 18:07:45 +01:00
|
|
|
# Copyright (C) 2016-2021 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 17:37:40 +02:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2019-01-02 23:45:35 +01:00
|
|
|
import os, re, logging, collections, shlex
|
2021-01-08 18:07:45 +01:00
|
|
|
|
|
|
|
class CommandError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
Coord = collections.namedtuple('Coord', ('x', 'y', 'z', 'e'))
|
2016-05-25 17:37:40 +02:00
|
|
|
|
2020-04-22 18:40:32 +02:00
|
|
|
class GCodeCommand:
|
2021-01-08 18:07:45 +01:00
|
|
|
error = CommandError
|
2020-04-25 21:30:24 +02:00
|
|
|
def __init__(self, gcode, command, commandline, params, need_ack):
|
2020-04-22 18:40:32 +02:00
|
|
|
self._command = command
|
|
|
|
self._commandline = commandline
|
|
|
|
self._params = params
|
2020-04-25 21:30:24 +02:00
|
|
|
self._need_ack = need_ack
|
2020-04-22 18:40:32 +02:00
|
|
|
# Method wrappers
|
|
|
|
self.respond_info = gcode.respond_info
|
|
|
|
self.respond_raw = gcode.respond_raw
|
|
|
|
def get_command(self):
|
|
|
|
return self._command
|
|
|
|
def get_commandline(self):
|
|
|
|
return self._commandline
|
|
|
|
def get_command_parameters(self):
|
|
|
|
return self._params
|
2020-04-25 21:30:24 +02:00
|
|
|
def ack(self, msg=None):
|
|
|
|
if not self._need_ack:
|
|
|
|
return False
|
|
|
|
ok_msg = "ok"
|
|
|
|
if msg:
|
|
|
|
ok_msg = "ok %s" % (msg,)
|
|
|
|
self.respond_raw(ok_msg)
|
|
|
|
self._need_ack = False
|
|
|
|
return True
|
2020-04-22 18:40:32 +02:00
|
|
|
# Parameter parsing helpers
|
|
|
|
class sentinel: pass
|
|
|
|
def get(self, name, default=sentinel, parser=str, minval=None, maxval=None,
|
|
|
|
above=None, below=None):
|
|
|
|
value = self._params.get(name)
|
|
|
|
if value is None:
|
|
|
|
if default is self.sentinel:
|
|
|
|
raise self.error("Error on '%s': missing %s"
|
|
|
|
% (self._commandline, name))
|
|
|
|
return default
|
|
|
|
try:
|
|
|
|
value = parser(value)
|
|
|
|
except:
|
|
|
|
raise self.error("Error on '%s': unable to parse %s"
|
|
|
|
% (self._commandline, value))
|
|
|
|
if minval is not None and value < minval:
|
|
|
|
raise self.error("Error on '%s': %s must have minimum of %s"
|
|
|
|
% (self._commandline, name, minval))
|
|
|
|
if maxval is not None and value > maxval:
|
|
|
|
raise self.error("Error on '%s': %s must have maximum of %s"
|
|
|
|
% (self._commandline, name, maxval))
|
|
|
|
if above is not None and value <= above:
|
|
|
|
raise self.error("Error on '%s': %s must be above %s"
|
|
|
|
% (self._commandline, name, above))
|
|
|
|
if below is not None and value >= below:
|
|
|
|
raise self.error("Error on '%s': %s must be below %s"
|
|
|
|
% (self._commandline, name, below))
|
|
|
|
return value
|
|
|
|
def get_int(self, name, default=sentinel, minval=None, maxval=None):
|
|
|
|
return self.get(name, default, parser=int, minval=minval, maxval=maxval)
|
|
|
|
def get_float(self, name, default=sentinel, minval=None, maxval=None,
|
|
|
|
above=None, below=None):
|
|
|
|
return self.get(name, default, parser=float, minval=minval,
|
|
|
|
maxval=maxval, above=above, below=below)
|
|
|
|
|
2020-08-05 17:43:45 +02:00
|
|
|
# Parse and dispatch G-Code commands
|
|
|
|
class GCodeDispatch:
|
2021-01-08 18:07:45 +01:00
|
|
|
error = CommandError
|
|
|
|
Coord = Coord
|
2020-08-04 21:47:25 +02:00
|
|
|
def __init__(self, printer):
|
2016-05-25 17:37:40 +02:00
|
|
|
self.printer = printer
|
2020-08-04 22:32:19 +02:00
|
|
|
self.is_fileinput = not not printer.get_start_args().get("debuginput")
|
2019-05-25 00:45:18 +02:00
|
|
|
printer.register_event_handler("klippy:ready", self._handle_ready)
|
|
|
|
printer.register_event_handler("klippy:shutdown", self._handle_shutdown)
|
2019-01-08 16:58:35 +01:00
|
|
|
printer.register_event_handler("klippy:disconnect",
|
2019-05-25 00:45:18 +02:00
|
|
|
self._handle_disconnect)
|
2016-05-25 17:37:40 +02:00
|
|
|
# Command handling
|
2016-11-23 01:38:51 +01:00
|
|
|
self.is_printer_ready = False
|
2020-08-04 22:32:19 +02:00
|
|
|
self.mutex = printer.get_reactor().mutex()
|
2020-08-04 20:45:21 +02:00
|
|
|
self.output_callbacks = []
|
2017-12-04 00:53:42 +01:00
|
|
|
self.base_gcode_handlers = self.gcode_handlers = {}
|
|
|
|
self.ready_gcode_handlers = {}
|
2018-05-20 18:33:43 +02:00
|
|
|
self.mux_commands = {}
|
2017-12-04 00:53:42 +01:00
|
|
|
self.gcode_help = {}
|
2020-08-05 17:43:45 +02:00
|
|
|
# Register commands needed before config file is loaded
|
|
|
|
handlers = ['M110', 'M112', 'M115',
|
|
|
|
'RESTART', 'FIRMWARE_RESTART', 'ECHO', 'STATUS', 'HELP']
|
|
|
|
for cmd in handlers:
|
2017-12-04 00:53:42 +01:00
|
|
|
func = getattr(self, 'cmd_' + cmd)
|
|
|
|
desc = getattr(self, 'cmd_' + cmd + '_help', None)
|
2020-08-05 17:43:45 +02:00
|
|
|
self.register_command(cmd, func, True, desc)
|
2020-01-08 01:06:55 +01:00
|
|
|
def is_traditional_gcode(self, cmd):
|
|
|
|
# A "traditional" g-code command is a letter and followed by a number
|
|
|
|
try:
|
|
|
|
cmd = cmd.upper().split()[0]
|
|
|
|
val = float(cmd[1:])
|
|
|
|
return cmd[0].isupper() and cmd[1].isdigit()
|
|
|
|
except:
|
|
|
|
return False
|
2017-12-04 00:53:42 +01:00
|
|
|
def register_command(self, cmd, func, when_not_ready=False, desc=None):
|
2018-01-17 17:26:09 +01:00
|
|
|
if func is None:
|
2020-02-13 02:19:18 +01:00
|
|
|
old_cmd = self.ready_gcode_handlers.get(cmd)
|
2018-01-17 17:26:09 +01:00
|
|
|
if cmd in self.ready_gcode_handlers:
|
|
|
|
del self.ready_gcode_handlers[cmd]
|
|
|
|
if cmd in self.base_gcode_handlers:
|
|
|
|
del self.base_gcode_handlers[cmd]
|
2020-02-13 02:19:18 +01:00
|
|
|
return old_cmd
|
2018-05-20 19:04:52 +02:00
|
|
|
if cmd in self.ready_gcode_handlers:
|
2019-02-19 00:04:42 +01:00
|
|
|
raise self.printer.config_error(
|
|
|
|
"gcode command %s already registered" % (cmd,))
|
2020-01-08 01:06:55 +01:00
|
|
|
if not self.is_traditional_gcode(cmd):
|
2017-12-04 02:22:44 +01:00
|
|
|
origfunc = func
|
2019-05-25 00:45:18 +02:00
|
|
|
func = lambda params: origfunc(self._get_extended_params(params))
|
2017-12-04 00:53:42 +01:00
|
|
|
self.ready_gcode_handlers[cmd] = func
|
|
|
|
if when_not_ready:
|
|
|
|
self.base_gcode_handlers[cmd] = func
|
|
|
|
if desc is not None:
|
|
|
|
self.gcode_help[cmd] = desc
|
2018-05-20 18:33:43 +02:00
|
|
|
def register_mux_command(self, cmd, key, value, func, desc=None):
|
|
|
|
prev = self.mux_commands.get(cmd)
|
|
|
|
if prev is None:
|
2019-05-25 00:45:18 +02:00
|
|
|
self.register_command(cmd, self._cmd_mux, desc=desc)
|
2018-05-20 18:33:43 +02:00
|
|
|
self.mux_commands[cmd] = prev = (key, {})
|
|
|
|
prev_key, prev_values = prev
|
|
|
|
if prev_key != key:
|
2019-02-19 00:04:42 +01:00
|
|
|
raise self.printer.config_error(
|
|
|
|
"mux command %s %s %s may have only one key (%s)" % (
|
|
|
|
cmd, key, value, prev_key))
|
2018-05-20 18:33:43 +02:00
|
|
|
if value in prev_values:
|
2019-02-19 00:04:42 +01:00
|
|
|
raise self.printer.config_error(
|
|
|
|
"mux command %s %s %s already registered (%s)" % (
|
|
|
|
cmd, key, value, prev_values))
|
2018-05-20 18:33:43 +02:00
|
|
|
prev_values[value] = func
|
2020-08-12 03:21:41 +02:00
|
|
|
def get_command_help(self):
|
|
|
|
return dict(self.gcode_help)
|
2020-08-04 20:45:21 +02:00
|
|
|
def register_output_handler(self, cb):
|
|
|
|
self.output_callbacks.append(cb)
|
2019-05-25 00:45:18 +02:00
|
|
|
def _handle_shutdown(self):
|
2019-01-08 15:15:40 +01:00
|
|
|
if not self.is_printer_ready:
|
2018-01-20 04:49:27 +01:00
|
|
|
return
|
2019-01-08 15:15:40 +01:00
|
|
|
self.is_printer_ready = False
|
|
|
|
self.gcode_handlers = self.base_gcode_handlers
|
|
|
|
self._respond_state("Shutdown")
|
2019-05-25 00:45:18 +02:00
|
|
|
def _handle_disconnect(self):
|
2019-01-08 16:58:35 +01:00
|
|
|
self._respond_state("Disconnect")
|
2019-05-25 00:45:18 +02:00
|
|
|
def _handle_ready(self):
|
2017-10-12 21:15:14 +02:00
|
|
|
self.is_printer_ready = True
|
2017-12-04 00:53:42 +01:00
|
|
|
self.gcode_handlers = self.ready_gcode_handlers
|
2018-09-13 00:46:25 +02:00
|
|
|
self._respond_state("Ready")
|
2016-05-25 17:37:40 +02:00
|
|
|
# Parse input into commands
|
2017-10-10 17:12:15 +02:00
|
|
|
args_r = re.compile('([A-Z_]+|[A-Z*/])')
|
2019-05-25 00:45:18 +02:00
|
|
|
def _process_commands(self, commands, need_ack=True):
|
2017-05-20 03:05:46 +02:00
|
|
|
for line in commands:
|
2016-05-25 17:37:40 +02:00
|
|
|
# Ignore comments and leading/trailing spaces
|
|
|
|
line = origline = line.strip()
|
|
|
|
cpos = line.find(';')
|
|
|
|
if cpos >= 0:
|
|
|
|
line = line[:cpos]
|
2020-04-22 18:40:32 +02:00
|
|
|
# Break line into parts and determine command
|
|
|
|
parts = self.args_r.split(line.upper())
|
|
|
|
numparts = len(parts)
|
|
|
|
cmd = ""
|
|
|
|
if numparts >= 3 and parts[1] != 'N':
|
|
|
|
cmd = parts[1] + parts[2].strip()
|
|
|
|
elif numparts >= 5 and parts[1] == 'N':
|
|
|
|
# Skip line number at start of command
|
|
|
|
cmd = parts[3] + parts[4].strip()
|
|
|
|
# Build gcode "params" dictionary
|
2017-06-06 21:04:01 +02:00
|
|
|
params = { parts[i]: parts[i+1].strip()
|
2020-04-22 18:40:32 +02:00
|
|
|
for i in range(1, numparts, 2) }
|
2020-04-25 21:30:24 +02:00
|
|
|
gcmd = GCodeCommand(self, cmd, origline, params, need_ack)
|
2016-05-25 17:37:40 +02:00
|
|
|
# Invoke handler for command
|
|
|
|
handler = self.gcode_handlers.get(cmd, self.cmd_default)
|
|
|
|
try:
|
2020-04-22 18:40:32 +02:00
|
|
|
handler(gcmd)
|
2019-06-03 18:48:35 +02:00
|
|
|
except self.error as e:
|
2020-04-24 20:59:25 +02:00
|
|
|
self._respond_error(str(e))
|
2020-02-12 19:20:12 +01:00
|
|
|
self.printer.send_event("gcode:command_error")
|
2018-02-01 18:13:48 +01:00
|
|
|
if not need_ack:
|
|
|
|
raise
|
2016-05-25 17:37:40 +02:00
|
|
|
except:
|
2017-10-12 21:15:14 +02:00
|
|
|
msg = 'Internal error on command:"%s"' % (cmd,)
|
|
|
|
logging.exception(msg)
|
|
|
|
self.printer.invoke_shutdown(msg)
|
2020-04-24 20:59:25 +02:00
|
|
|
self._respond_error(msg)
|
2018-02-01 18:13:48 +01:00
|
|
|
if not need_ack:
|
|
|
|
raise
|
2020-04-25 21:30:24 +02:00
|
|
|
gcmd.ack()
|
2018-06-30 20:08:02 +02:00
|
|
|
def run_script_from_command(self, script):
|
2020-04-25 21:30:24 +02:00
|
|
|
self._process_commands(script.split('\n'), need_ack=False)
|
2018-06-30 20:10:59 +02:00
|
|
|
def run_script(self, script):
|
2019-06-09 18:01:58 +02:00
|
|
|
with self.mutex:
|
|
|
|
self._process_commands(script.split('\n'), need_ack=False)
|
2019-06-09 19:33:21 +02:00
|
|
|
def get_mutex(self):
|
|
|
|
return self.mutex
|
2020-04-22 18:40:32 +02:00
|
|
|
def create_gcode_command(self, command, commandline, params):
|
2020-04-25 21:30:24 +02:00
|
|
|
return GCodeCommand(self, command, commandline, params, False)
|
2016-05-25 17:37:40 +02:00
|
|
|
# Response handling
|
2020-04-24 21:54:18 +02:00
|
|
|
def respond_raw(self, msg):
|
2020-08-04 20:45:21 +02:00
|
|
|
for cb in self.output_callbacks:
|
|
|
|
cb(msg)
|
2019-03-04 19:04:18 +01:00
|
|
|
def respond_info(self, msg, log=True):
|
|
|
|
if log:
|
|
|
|
logging.info(msg)
|
2016-12-21 17:33:03 +01:00
|
|
|
lines = [l.strip() for l in msg.strip().split('\n')]
|
2020-04-24 21:54:18 +02:00
|
|
|
self.respond_raw("// " + "\n// ".join(lines))
|
2020-04-24 20:59:25 +02:00
|
|
|
def _respond_error(self, msg):
|
2017-07-17 17:24:15 +02:00
|
|
|
logging.warning(msg)
|
2016-11-30 20:30:45 +01:00
|
|
|
lines = msg.strip().split('\n')
|
2016-12-21 17:33:03 +01:00
|
|
|
if len(lines) > 1:
|
2019-03-04 19:04:18 +01:00
|
|
|
self.respond_info("\n".join(lines), log=False)
|
2020-04-24 21:54:18 +02:00
|
|
|
self.respond_raw('!! %s' % (lines[0].strip(),))
|
2018-06-16 21:15:17 +02:00
|
|
|
if self.is_fileinput:
|
|
|
|
self.printer.request_exit('error_exit')
|
2018-09-13 00:46:25 +02:00
|
|
|
def _respond_state(self, state):
|
2019-03-04 19:04:18 +01:00
|
|
|
self.respond_info("Klipper state: %s" % (state,), log=False)
|
2017-03-16 04:40:46 +01:00
|
|
|
# Parameter parsing helpers
|
2017-06-06 21:04:01 +02:00
|
|
|
extended_r = re.compile(
|
|
|
|
r'^\s*(?:N[0-9]+\s*)?'
|
2020-01-08 01:06:55 +01:00
|
|
|
r'(?P<cmd>[a-zA-Z_][a-zA-Z0-9_]+)(?:\s+|$)'
|
2017-06-06 21:04:01 +02:00
|
|
|
r'(?P<args>[^#*;]*?)'
|
|
|
|
r'\s*(?:[#*;].*)?$')
|
2020-04-22 18:40:32 +02:00
|
|
|
def _get_extended_params(self, gcmd):
|
|
|
|
m = self.extended_r.match(gcmd.get_commandline())
|
2017-06-06 21:04:01 +02:00
|
|
|
if m is None:
|
2020-04-22 18:40:32 +02:00
|
|
|
raise self.error("Malformed command '%s'"
|
|
|
|
% (gcmd.get_commandline(),))
|
2017-06-06 21:04:01 +02:00
|
|
|
eargs = m.group('args')
|
|
|
|
try:
|
2019-01-02 23:45:35 +01:00
|
|
|
eparams = [earg.split('=', 1) for earg in shlex.split(eargs)]
|
2017-10-03 03:48:45 +02:00
|
|
|
eparams = { k.upper(): v for k, v in eparams }
|
2020-04-22 18:40:32 +02:00
|
|
|
gcmd._params.clear()
|
|
|
|
gcmd._params.update(eparams)
|
|
|
|
return gcmd
|
2017-06-06 21:04:01 +02:00
|
|
|
except ValueError as e:
|
2020-04-22 18:40:32 +02:00
|
|
|
raise self.error("Malformed command '%s'"
|
|
|
|
% (gcmd.get_commandline(),))
|
2017-12-22 02:45:07 +01:00
|
|
|
# G-Code special command handlers
|
2020-04-25 01:27:35 +02:00
|
|
|
def cmd_default(self, gcmd):
|
|
|
|
cmd = gcmd.get_command()
|
2020-04-25 19:06:51 +02:00
|
|
|
if cmd == 'M105':
|
|
|
|
# Don't warn about temperature requests when not ready
|
2020-04-25 21:30:24 +02:00
|
|
|
gcmd.ack("T:0")
|
2020-04-25 19:06:51 +02:00
|
|
|
return
|
2020-08-05 17:43:45 +02:00
|
|
|
if cmd == 'M21':
|
|
|
|
# Don't warn about sd card init when not ready
|
|
|
|
return
|
2016-11-23 01:38:51 +01:00
|
|
|
if not self.is_printer_ready:
|
2020-06-23 13:56:50 +02:00
|
|
|
raise gcmd.error(self.printer.get_state_message()[0])
|
2016-05-25 17:37:40 +02:00
|
|
|
return
|
|
|
|
if not cmd:
|
2020-09-17 08:16:29 +02:00
|
|
|
cmdline = gcmd.get_commandline()
|
|
|
|
if cmdline:
|
|
|
|
logging.debug(cmdline)
|
2016-05-25 17:37:40 +02:00
|
|
|
return
|
2019-11-25 01:59:36 +01:00
|
|
|
if cmd.startswith("M117 "):
|
2018-08-22 02:59:51 +02:00
|
|
|
# Handle M117 gcode with numeric and special characters
|
|
|
|
handler = self.gcode_handlers.get("M117", None)
|
|
|
|
if handler is not None:
|
2020-04-25 01:27:35 +02:00
|
|
|
handler(gcmd)
|
2018-08-22 02:59:51 +02:00
|
|
|
return
|
2020-04-25 01:27:35 +02:00
|
|
|
elif cmd in ['M140', 'M104'] and not gcmd.get_float('S', 0.):
|
2019-12-17 01:35:49 +01:00
|
|
|
# Don't warn about requests to turn off heaters when not present
|
|
|
|
return
|
2019-11-13 17:29:27 +01:00
|
|
|
elif cmd == 'M107' or (cmd == 'M106' and (
|
2020-04-25 01:27:35 +02:00
|
|
|
not gcmd.get_float('S', 1.) or self.is_fileinput)):
|
2019-11-13 17:29:27 +01:00
|
|
|
# Don't warn about requests to turn off fan when fan not present
|
|
|
|
return
|
2020-04-25 01:27:35 +02:00
|
|
|
gcmd.respond_info('Unknown command:"%s"' % (cmd,))
|
|
|
|
def _cmd_mux(self, gcmd):
|
|
|
|
key, values = self.mux_commands[gcmd.get_command()]
|
2018-05-20 18:33:43 +02:00
|
|
|
if None in values:
|
2020-04-25 01:27:35 +02:00
|
|
|
key_param = gcmd.get(key, None)
|
2018-05-20 18:33:43 +02:00
|
|
|
else:
|
2020-04-25 01:27:35 +02:00
|
|
|
key_param = gcmd.get(key)
|
2018-05-20 18:33:43 +02:00
|
|
|
if key_param not in values:
|
2020-04-25 01:27:35 +02:00
|
|
|
raise gcmd.error("The value '%s' is not valid for %s"
|
|
|
|
% (key_param, key))
|
|
|
|
values[key_param](gcmd)
|
2020-08-05 17:43:45 +02:00
|
|
|
# Low-level G-Code commands that are needed before the config file is loaded
|
|
|
|
def cmd_M110(self, gcmd):
|
|
|
|
# Set Current Line Number
|
|
|
|
pass
|
|
|
|
def cmd_M112(self, gcmd):
|
|
|
|
# Emergency Stop
|
|
|
|
self.printer.invoke_shutdown("Shutdown due to M112 command")
|
|
|
|
def cmd_M115(self, gcmd):
|
|
|
|
# Get Firmware Version and Capabilities
|
|
|
|
software_version = self.printer.get_start_args().get('software_version')
|
|
|
|
kw = {"FIRMWARE_NAME": "Klipper", "FIRMWARE_VERSION": software_version}
|
2021-04-29 03:07:48 +02:00
|
|
|
msg = " ".join(["%s:%s" % (k, v) for k, v in kw.items()])
|
|
|
|
did_ack = gcmd.ack(msg)
|
|
|
|
if not did_ack:
|
|
|
|
gcmd.respond_info(msg)
|
2020-08-05 17:43:45 +02:00
|
|
|
def request_restart(self, result):
|
|
|
|
if self.is_printer_ready:
|
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
print_time = toolhead.get_last_move_time()
|
|
|
|
if result == 'exit':
|
|
|
|
logging.info("Exiting (print time %.3fs)" % (print_time,))
|
|
|
|
self.printer.send_event("gcode:request_restart", print_time)
|
|
|
|
toolhead.dwell(0.500)
|
|
|
|
toolhead.wait_moves()
|
|
|
|
self.printer.request_exit(result)
|
|
|
|
cmd_RESTART_help = "Reload config file and restart host software"
|
|
|
|
def cmd_RESTART(self, gcmd):
|
|
|
|
self.request_restart('restart')
|
|
|
|
cmd_FIRMWARE_RESTART_help = "Restart firmware, host, and reload config"
|
|
|
|
def cmd_FIRMWARE_RESTART(self, gcmd):
|
|
|
|
self.request_restart('firmware_restart')
|
|
|
|
def cmd_ECHO(self, gcmd):
|
|
|
|
gcmd.respond_info(gcmd.get_commandline(), log=False)
|
|
|
|
cmd_STATUS_help = "Report the printer status"
|
|
|
|
def cmd_STATUS(self, gcmd):
|
|
|
|
if self.is_printer_ready:
|
|
|
|
self._respond_state("Ready")
|
|
|
|
return
|
|
|
|
msg = self.printer.get_state_message()[0]
|
|
|
|
msg = msg.rstrip() + "\nKlipper state: Not ready"
|
|
|
|
raise gcmd.error(msg)
|
2021-06-02 16:45:27 +02:00
|
|
|
cmd_HELP_help = "Report the list of available extended G-Code commands"
|
2020-08-05 17:43:45 +02:00
|
|
|
def cmd_HELP(self, gcmd):
|
|
|
|
cmdhelp = []
|
|
|
|
if not self.is_printer_ready:
|
|
|
|
cmdhelp.append("Printer is not ready - not all commands available.")
|
|
|
|
cmdhelp.append("Available extended commands:")
|
|
|
|
for cmd in sorted(self.gcode_handlers):
|
|
|
|
if cmd in self.gcode_help:
|
|
|
|
cmdhelp.append("%-10s: %s" % (cmd, self.gcode_help[cmd]))
|
|
|
|
gcmd.respond_info("\n".join(cmdhelp), log=False)
|
|
|
|
|
2020-08-04 22:32:19 +02:00
|
|
|
# Support reading gcode from a pseudo-tty interface
|
|
|
|
class GCodeIO:
|
|
|
|
def __init__(self, printer):
|
|
|
|
self.printer = printer
|
|
|
|
printer.register_event_handler("klippy:ready", self._handle_ready)
|
|
|
|
printer.register_event_handler("klippy:shutdown", self._handle_shutdown)
|
|
|
|
self.gcode = printer.lookup_object('gcode')
|
|
|
|
self.gcode_mutex = self.gcode.get_mutex()
|
|
|
|
self.fd = printer.get_start_args().get("gcode_fd")
|
|
|
|
self.reactor = printer.get_reactor()
|
|
|
|
self.is_printer_ready = False
|
|
|
|
self.is_processing_data = False
|
|
|
|
self.is_fileinput = not not printer.get_start_args().get("debuginput")
|
|
|
|
self.pipe_is_active = True
|
|
|
|
self.fd_handle = None
|
|
|
|
if not self.is_fileinput:
|
|
|
|
self.gcode.register_output_handler(self._respond_raw)
|
|
|
|
self.fd_handle = self.reactor.register_fd(self.fd,
|
|
|
|
self._process_data)
|
|
|
|
self.partial_input = ""
|
|
|
|
self.pending_commands = []
|
|
|
|
self.bytes_read = 0
|
|
|
|
self.input_log = collections.deque([], 50)
|
|
|
|
def _handle_ready(self):
|
|
|
|
self.is_printer_ready = True
|
|
|
|
if self.is_fileinput and self.fd_handle is None:
|
|
|
|
self.fd_handle = self.reactor.register_fd(self.fd,
|
|
|
|
self._process_data)
|
|
|
|
def _dump_debug(self):
|
|
|
|
out = []
|
2020-08-05 17:43:45 +02:00
|
|
|
out.append("Dumping gcode input %d blocks" % (len(self.input_log),))
|
2020-08-04 22:32:19 +02:00
|
|
|
for eventtime, data in self.input_log:
|
|
|
|
out.append("Read %f: %s" % (eventtime, repr(data)))
|
|
|
|
logging.info("\n".join(out))
|
|
|
|
def _handle_shutdown(self):
|
|
|
|
if not self.is_printer_ready:
|
|
|
|
return
|
|
|
|
self.is_printer_ready = False
|
|
|
|
self._dump_debug()
|
|
|
|
if self.is_fileinput:
|
|
|
|
self.printer.request_exit('error_exit')
|
|
|
|
m112_r = re.compile('^(?:[nN][0-9]+)?\s*[mM]112(?:\s|$)')
|
|
|
|
def _process_data(self, eventtime):
|
|
|
|
# Read input, separate by newline, and add to pending_commands
|
|
|
|
try:
|
2021-10-31 19:15:32 +01:00
|
|
|
data = str(os.read(self.fd, 4096).decode())
|
|
|
|
except (os.error, UnicodeDecodeError):
|
2020-08-04 22:32:19 +02:00
|
|
|
logging.exception("Read g-code")
|
|
|
|
return
|
|
|
|
self.input_log.append((eventtime, data))
|
|
|
|
self.bytes_read += len(data)
|
2021-10-31 18:39:01 +01:00
|
|
|
lines = data.split('\n')
|
2020-08-04 22:32:19 +02:00
|
|
|
lines[0] = self.partial_input + lines[0]
|
|
|
|
self.partial_input = lines.pop()
|
|
|
|
pending_commands = self.pending_commands
|
|
|
|
pending_commands.extend(lines)
|
|
|
|
self.pipe_is_active = True
|
|
|
|
# Special handling for debug file input EOF
|
|
|
|
if not data and self.is_fileinput:
|
|
|
|
if not self.is_processing_data:
|
|
|
|
self.reactor.unregister_fd(self.fd_handle)
|
|
|
|
self.fd_handle = None
|
|
|
|
self.gcode.request_restart('exit')
|
|
|
|
pending_commands.append("")
|
|
|
|
# Handle case where multiple commands pending
|
|
|
|
if self.is_processing_data or len(pending_commands) > 1:
|
|
|
|
if len(pending_commands) < 20:
|
|
|
|
# Check for M112 out-of-order
|
|
|
|
for line in lines:
|
|
|
|
if self.m112_r.match(line) is not None:
|
2020-08-23 19:18:30 +02:00
|
|
|
self.gcode.cmd_M112(None)
|
2020-08-04 22:32:19 +02:00
|
|
|
if self.is_processing_data:
|
|
|
|
if len(pending_commands) >= 20:
|
|
|
|
# Stop reading input
|
|
|
|
self.reactor.unregister_fd(self.fd_handle)
|
|
|
|
self.fd_handle = None
|
|
|
|
return
|
|
|
|
# Process commands
|
|
|
|
self.is_processing_data = True
|
|
|
|
while pending_commands:
|
|
|
|
self.pending_commands = []
|
|
|
|
with self.gcode_mutex:
|
|
|
|
self.gcode._process_commands(pending_commands)
|
|
|
|
pending_commands = self.pending_commands
|
|
|
|
self.is_processing_data = False
|
|
|
|
if self.fd_handle is None:
|
|
|
|
self.fd_handle = self.reactor.register_fd(self.fd,
|
|
|
|
self._process_data)
|
|
|
|
def _respond_raw(self, msg):
|
|
|
|
if self.pipe_is_active:
|
|
|
|
try:
|
2020-06-12 15:59:04 +02:00
|
|
|
os.write(self.fd, (msg+"\n").encode())
|
2020-08-04 22:32:19 +02:00
|
|
|
except os.error:
|
|
|
|
logging.exception("Write g-code response")
|
|
|
|
self.pipe_is_active = False
|
|
|
|
def stats(self, eventtime):
|
|
|
|
return False, "gcodein=%d" % (self.bytes_read,)
|
2020-08-04 21:49:40 +02:00
|
|
|
|
|
|
|
def add_early_printer_objects(printer):
|
2020-08-05 17:43:45 +02:00
|
|
|
printer.add_object('gcode', GCodeDispatch(printer))
|
2020-08-04 22:32:19 +02:00
|
|
|
printer.add_object('gcode_io', GCodeIO(printer))
|