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