2016-05-25 18:37:40 +03:00
|
|
|
# Parse gcode commands
|
|
|
|
#
|
2021-01-08 20:07:45 +03:00
|
|
|
# Copyright (C) 2016-2021 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 18:37:40 +03:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
2019-01-03 01:45:35 +03:00
|
|
|
import os, re, logging, collections, shlex
|
2021-01-08 20:07:45 +03:00
|
|
|
|
|
|
|
class CommandError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
Coord = collections.namedtuple('Coord', ('x', 'y', 'z', 'e'))
|
2016-05-25 18:37:40 +03:00
|
|
|
|
2020-04-22 19:40:32 +03:00
|
|
|
class GCodeCommand:
|
2021-01-08 20:07:45 +03:00
|
|
|
error = CommandError
|
2020-04-25 22:30:24 +03:00
|
|
|
def __init__(self, gcode, command, commandline, params, need_ack):
|
2020-04-22 19:40:32 +03:00
|
|
|
self._command = command
|
|
|
|
self._commandline = commandline
|
|
|
|
self._params = params
|
2020-04-25 22:30:24 +03:00
|
|
|
self._need_ack = need_ack
|
2020-04-22 19:40:32 +03: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 22:30:24 +03: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 19:40:32 +03: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 18:43:45 +03:00
|
|
|
# Parse and dispatch G-Code commands
|
|
|
|
class GCodeDispatch:
|
2021-01-08 20:07:45 +03:00
|
|
|
error = CommandError
|
|
|
|
Coord = Coord
|
2020-08-04 22:47:25 +03:00
|
|
|
def __init__(self, printer):
|
2016-05-25 18:37:40 +03:00
|
|
|
self.printer = printer
|
2020-08-04 23:32:19 +03:00
|
|
|
self.is_fileinput = not not printer.get_start_args().get("debuginput")
|
2019-05-25 01:45:18 +03:00
|
|
|
printer.register_event_handler("klippy:ready", self._handle_ready)
|
|
|
|
printer.register_event_handler("klippy:shutdown", self._handle_shutdown)
|
2019-01-08 18:58:35 +03:00
|
|
|
printer.register_event_handler("klippy:disconnect",
|
2019-05-25 01:45:18 +03:00
|
|
|
self._handle_disconnect)
|
2016-05-25 18:37:40 +03:00
|
|
|
# Command handling
|
2016-11-23 03:38:51 +03:00
|
|
|
self.is_printer_ready = False
|
2020-08-04 23:32:19 +03:00
|
|
|
self.mutex = printer.get_reactor().mutex()
|
2020-08-04 21:45:21 +03:00
|
|
|
self.output_callbacks = []
|
2017-12-04 02:53:42 +03:00
|
|
|
self.base_gcode_handlers = self.gcode_handlers = {}
|
|
|
|
self.ready_gcode_handlers = {}
|
2018-05-20 19:33:43 +03:00
|
|
|
self.mux_commands = {}
|
2017-12-04 02:53:42 +03:00
|
|
|
self.gcode_help = {}
|
2020-08-05 18:43:45 +03: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 02:53:42 +03:00
|
|
|
func = getattr(self, 'cmd_' + cmd)
|
|
|
|
desc = getattr(self, 'cmd_' + cmd + '_help', None)
|
2020-08-05 18:43:45 +03:00
|
|
|
self.register_command(cmd, func, True, desc)
|
2020-01-08 03:06:55 +03: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 02:53:42 +03:00
|
|
|
def register_command(self, cmd, func, when_not_ready=False, desc=None):
|
2018-01-17 19:26:09 +03:00
|
|
|
if func is None:
|
2020-02-13 04:19:18 +03:00
|
|
|
old_cmd = self.ready_gcode_handlers.get(cmd)
|
2018-01-17 19:26:09 +03: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 04:19:18 +03:00
|
|
|
return old_cmd
|
2018-05-20 20:04:52 +03:00
|
|
|
if cmd in self.ready_gcode_handlers:
|
2019-02-19 02:04:42 +03:00
|
|
|
raise self.printer.config_error(
|
|
|
|
"gcode command %s already registered" % (cmd,))
|
2020-01-08 03:06:55 +03:00
|
|
|
if not self.is_traditional_gcode(cmd):
|
2017-12-04 04:22:44 +03:00
|
|
|
origfunc = func
|
2019-05-25 01:45:18 +03:00
|
|
|
func = lambda params: origfunc(self._get_extended_params(params))
|
2017-12-04 02:53:42 +03: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 19:33:43 +03: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 01:45:18 +03:00
|
|
|
self.register_command(cmd, self._cmd_mux, desc=desc)
|
2018-05-20 19:33:43 +03:00
|
|
|
self.mux_commands[cmd] = prev = (key, {})
|
|
|
|
prev_key, prev_values = prev
|
|
|
|
if prev_key != key:
|
2019-02-19 02:04:42 +03: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 19:33:43 +03:00
|
|
|
if value in prev_values:
|
2019-02-19 02:04:42 +03:00
|
|
|
raise self.printer.config_error(
|
|
|
|
"mux command %s %s %s already registered (%s)" % (
|
|
|
|
cmd, key, value, prev_values))
|
2018-05-20 19:33:43 +03:00
|
|
|
prev_values[value] = func
|
2020-08-12 04:21:41 +03:00
|
|
|
def get_command_help(self):
|
|
|
|
return dict(self.gcode_help)
|
2020-08-04 21:45:21 +03:00
|
|
|
def register_output_handler(self, cb):
|
|
|
|
self.output_callbacks.append(cb)
|
2019-05-25 01:45:18 +03:00
|
|
|
def _handle_shutdown(self):
|
2019-01-08 17:15:40 +03:00
|
|
|
if not self.is_printer_ready:
|
2018-01-20 06:49:27 +03:00
|
|
|
return
|
2019-01-08 17:15:40 +03:00
|
|
|
self.is_printer_ready = False
|
|
|
|
self.gcode_handlers = self.base_gcode_handlers
|
|
|
|
self._respond_state("Shutdown")
|
2019-05-25 01:45:18 +03:00
|
|
|
def _handle_disconnect(self):
|
2019-01-08 18:58:35 +03:00
|
|
|
self._respond_state("Disconnect")
|
2019-05-25 01:45:18 +03:00
|
|
|
def _handle_ready(self):
|
2017-10-12 22:15:14 +03:00
|
|
|
self.is_printer_ready = True
|
2017-12-04 02:53:42 +03:00
|
|
|
self.gcode_handlers = self.ready_gcode_handlers
|
2018-09-13 01:46:25 +03:00
|
|
|
self._respond_state("Ready")
|
2016-05-25 18:37:40 +03:00
|
|
|
# Parse input into commands
|
2017-10-10 18:12:15 +03:00
|
|
|
args_r = re.compile('([A-Z_]+|[A-Z*/])')
|
2019-05-25 01:45:18 +03:00
|
|
|
def _process_commands(self, commands, need_ack=True):
|
2017-05-20 04:05:46 +03:00
|
|
|
for line in commands:
|
2016-05-25 18:37:40 +03:00
|
|
|
# Ignore comments and leading/trailing spaces
|
|
|
|
line = origline = line.strip()
|
|
|
|
cpos = line.find(';')
|
|
|
|
if cpos >= 0:
|
|
|
|
line = line[:cpos]
|
2020-04-22 19:40:32 +03: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 22:04:01 +03:00
|
|
|
params = { parts[i]: parts[i+1].strip()
|
2020-04-22 19:40:32 +03:00
|
|
|
for i in range(1, numparts, 2) }
|
2020-04-25 22:30:24 +03:00
|
|
|
gcmd = GCodeCommand(self, cmd, origline, params, need_ack)
|
2016-05-25 18:37:40 +03:00
|
|
|
# Invoke handler for command
|
|
|
|
handler = self.gcode_handlers.get(cmd, self.cmd_default)
|
|
|
|
try:
|
2020-04-22 19:40:32 +03:00
|
|
|
handler(gcmd)
|
2019-06-03 19:48:35 +03:00
|
|
|
except self.error as e:
|
2020-04-24 21:59:25 +03:00
|
|
|
self._respond_error(str(e))
|
2020-02-12 21:20:12 +03:00
|
|
|
self.printer.send_event("gcode:command_error")
|
2018-02-01 20:13:48 +03:00
|
|
|
if not need_ack:
|
|
|
|
raise
|
2016-05-25 18:37:40 +03:00
|
|
|
except:
|
2017-10-12 22:15:14 +03:00
|
|
|
msg = 'Internal error on command:"%s"' % (cmd,)
|
|
|
|
logging.exception(msg)
|
|
|
|
self.printer.invoke_shutdown(msg)
|
2020-04-24 21:59:25 +03:00
|
|
|
self._respond_error(msg)
|
2018-02-01 20:13:48 +03:00
|
|
|
if not need_ack:
|
|
|
|
raise
|
2020-04-25 22:30:24 +03:00
|
|
|
gcmd.ack()
|
2018-06-30 21:08:02 +03:00
|
|
|
def run_script_from_command(self, script):
|
2020-04-25 22:30:24 +03:00
|
|
|
self._process_commands(script.split('\n'), need_ack=False)
|
2018-06-30 21:10:59 +03:00
|
|
|
def run_script(self, script):
|
2019-06-09 19:01:58 +03:00
|
|
|
with self.mutex:
|
|
|
|
self._process_commands(script.split('\n'), need_ack=False)
|
2019-06-09 20:33:21 +03:00
|
|
|
def get_mutex(self):
|
|
|
|
return self.mutex
|
2020-04-22 19:40:32 +03:00
|
|
|
def create_gcode_command(self, command, commandline, params):
|
2020-04-25 22:30:24 +03:00
|
|
|
return GCodeCommand(self, command, commandline, params, False)
|
2016-05-25 18:37:40 +03:00
|
|
|
# Response handling
|
2020-04-24 22:54:18 +03:00
|
|
|
def respond_raw(self, msg):
|
2020-08-04 21:45:21 +03:00
|
|
|
for cb in self.output_callbacks:
|
|
|
|
cb(msg)
|
2019-03-04 21:04:18 +03:00
|
|
|
def respond_info(self, msg, log=True):
|
|
|
|
if log:
|
|
|
|
logging.info(msg)
|
2016-12-21 19:33:03 +03:00
|
|
|
lines = [l.strip() for l in msg.strip().split('\n')]
|
2020-04-24 22:54:18 +03:00
|
|
|
self.respond_raw("// " + "\n// ".join(lines))
|
2020-04-24 21:59:25 +03:00
|
|
|
def _respond_error(self, msg):
|
2017-07-17 18:24:15 +03:00
|
|
|
logging.warning(msg)
|
2016-11-30 22:30:45 +03:00
|
|
|
lines = msg.strip().split('\n')
|
2016-12-21 19:33:03 +03:00
|
|
|
if len(lines) > 1:
|
2019-03-04 21:04:18 +03:00
|
|
|
self.respond_info("\n".join(lines), log=False)
|
2020-04-24 22:54:18 +03:00
|
|
|
self.respond_raw('!! %s' % (lines[0].strip(),))
|
2018-06-16 22:15:17 +03:00
|
|
|
if self.is_fileinput:
|
|
|
|
self.printer.request_exit('error_exit')
|
2018-09-13 01:46:25 +03:00
|
|
|
def _respond_state(self, state):
|
2019-03-04 21:04:18 +03:00
|
|
|
self.respond_info("Klipper state: %s" % (state,), log=False)
|
2017-03-16 06:40:46 +03:00
|
|
|
# Parameter parsing helpers
|
2017-06-06 22:04:01 +03:00
|
|
|
extended_r = re.compile(
|
|
|
|
r'^\s*(?:N[0-9]+\s*)?'
|
2020-01-08 03:06:55 +03:00
|
|
|
r'(?P<cmd>[a-zA-Z_][a-zA-Z0-9_]+)(?:\s+|$)'
|
2017-06-06 22:04:01 +03:00
|
|
|
r'(?P<args>[^#*;]*?)'
|
|
|
|
r'\s*(?:[#*;].*)?$')
|
2020-04-22 19:40:32 +03:00
|
|
|
def _get_extended_params(self, gcmd):
|
|
|
|
m = self.extended_r.match(gcmd.get_commandline())
|
2017-06-06 22:04:01 +03:00
|
|
|
if m is None:
|
2020-04-22 19:40:32 +03:00
|
|
|
raise self.error("Malformed command '%s'"
|
|
|
|
% (gcmd.get_commandline(),))
|
2017-06-06 22:04:01 +03:00
|
|
|
eargs = m.group('args')
|
|
|
|
try:
|
2019-01-03 01:45:35 +03:00
|
|
|
eparams = [earg.split('=', 1) for earg in shlex.split(eargs)]
|
2017-10-03 04:48:45 +03:00
|
|
|
eparams = { k.upper(): v for k, v in eparams }
|
2020-04-22 19:40:32 +03:00
|
|
|
gcmd._params.clear()
|
|
|
|
gcmd._params.update(eparams)
|
|
|
|
return gcmd
|
2017-06-06 22:04:01 +03:00
|
|
|
except ValueError as e:
|
2020-04-22 19:40:32 +03:00
|
|
|
raise self.error("Malformed command '%s'"
|
|
|
|
% (gcmd.get_commandline(),))
|
2017-12-22 04:45:07 +03:00
|
|
|
# G-Code special command handlers
|
2020-04-25 02:27:35 +03:00
|
|
|
def cmd_default(self, gcmd):
|
|
|
|
cmd = gcmd.get_command()
|
2020-04-25 20:06:51 +03:00
|
|
|
if cmd == 'M105':
|
|
|
|
# Don't warn about temperature requests when not ready
|
2020-04-25 22:30:24 +03:00
|
|
|
gcmd.ack("T:0")
|
2020-04-25 20:06:51 +03:00
|
|
|
return
|
2020-08-05 18:43:45 +03:00
|
|
|
if cmd == 'M21':
|
|
|
|
# Don't warn about sd card init when not ready
|
|
|
|
return
|
2016-11-23 03:38:51 +03:00
|
|
|
if not self.is_printer_ready:
|
2020-06-23 14:56:50 +03:00
|
|
|
raise gcmd.error(self.printer.get_state_message()[0])
|
2016-05-25 18:37:40 +03:00
|
|
|
return
|
|
|
|
if not cmd:
|
2020-09-17 09:16:29 +03:00
|
|
|
cmdline = gcmd.get_commandline()
|
|
|
|
if cmdline:
|
|
|
|
logging.debug(cmdline)
|
2016-05-25 18:37:40 +03:00
|
|
|
return
|
2019-11-25 03:59:36 +03:00
|
|
|
if cmd.startswith("M117 "):
|
2018-08-22 03:59:51 +03:00
|
|
|
# Handle M117 gcode with numeric and special characters
|
|
|
|
handler = self.gcode_handlers.get("M117", None)
|
|
|
|
if handler is not None:
|
2020-04-25 02:27:35 +03:00
|
|
|
handler(gcmd)
|
2018-08-22 03:59:51 +03:00
|
|
|
return
|
2020-04-25 02:27:35 +03:00
|
|
|
elif cmd in ['M140', 'M104'] and not gcmd.get_float('S', 0.):
|
2019-12-17 03:35:49 +03:00
|
|
|
# Don't warn about requests to turn off heaters when not present
|
|
|
|
return
|
2019-11-13 19:29:27 +03:00
|
|
|
elif cmd == 'M107' or (cmd == 'M106' and (
|
2020-04-25 02:27:35 +03:00
|
|
|
not gcmd.get_float('S', 1.) or self.is_fileinput)):
|
2019-11-13 19:29:27 +03:00
|
|
|
# Don't warn about requests to turn off fan when fan not present
|
|
|
|
return
|
2020-04-25 02:27:35 +03:00
|
|
|
gcmd.respond_info('Unknown command:"%s"' % (cmd,))
|
|
|
|
def _cmd_mux(self, gcmd):
|
|
|
|
key, values = self.mux_commands[gcmd.get_command()]
|
2018-05-20 19:33:43 +03:00
|
|
|
if None in values:
|
2020-04-25 02:27:35 +03:00
|
|
|
key_param = gcmd.get(key, None)
|
2018-05-20 19:33:43 +03:00
|
|
|
else:
|
2020-04-25 02:27:35 +03:00
|
|
|
key_param = gcmd.get(key)
|
2018-05-20 19:33:43 +03:00
|
|
|
if key_param not in values:
|
2020-04-25 02:27:35 +03:00
|
|
|
raise gcmd.error("The value '%s' is not valid for %s"
|
|
|
|
% (key_param, key))
|
|
|
|
values[key_param](gcmd)
|
2020-08-05 18:43:45 +03: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}
|
|
|
|
gcmd.ack(" ".join(["%s:%s" % (k, v) for k, v in kw.items()]))
|
|
|
|
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)
|
|
|
|
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 23:32:19 +03: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 18:43:45 +03:00
|
|
|
out.append("Dumping gcode input %d blocks" % (len(self.input_log),))
|
2020-08-04 23:32:19 +03: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:
|
|
|
|
data = os.read(self.fd, 4096)
|
|
|
|
except os.error:
|
|
|
|
logging.exception("Read g-code")
|
|
|
|
return
|
|
|
|
self.input_log.append((eventtime, data))
|
|
|
|
self.bytes_read += len(data)
|
|
|
|
lines = data.split('\n')
|
|
|
|
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 20:18:30 +03:00
|
|
|
self.gcode.cmd_M112(None)
|
2020-08-04 23:32:19 +03: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:
|
|
|
|
os.write(self.fd, msg+"\n")
|
|
|
|
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 22:49:40 +03:00
|
|
|
|
|
|
|
def add_early_printer_objects(printer):
|
2020-08-05 18:43:45 +03:00
|
|
|
printer.add_object('gcode', GCodeDispatch(printer))
|
2020-08-04 23:32:19 +03:00
|
|
|
printer.add_object('gcode_io', GCodeIO(printer))
|