mirror of
https://github.com/QIDITECH/klipper.git
synced 2026-01-31 07:58:42 +03:00
plus4的klipper版本
This commit is contained in:
@@ -9,6 +9,11 @@ class CommandError(Exception):
|
||||
pass
|
||||
|
||||
Coord = collections.namedtuple('Coord', ('x', 'y', 'z', 'e'))
|
||||
# priority_queue = []
|
||||
# set_gcode_offset_r = re.compile(
|
||||
# r'^\s*SET_GCODE_OFFSET(?:\s+[a-zA-Z_]+=[+-]?\d*\.?\d+)*\s+MOVE=1(?:\s+[a-zA-Z_]+=[+-]?\d*\.?\d+)*(?:\s|$)',
|
||||
# re.IGNORECASE
|
||||
# )
|
||||
|
||||
class GCodeCommand:
|
||||
error = CommandError
|
||||
@@ -104,13 +109,16 @@ class GCodeDispatch:
|
||||
self.ready_gcode_handlers = {}
|
||||
self.mux_commands = {}
|
||||
self.gcode_help = {}
|
||||
self.status_commands = {}
|
||||
# Register commands needed before config file is loaded
|
||||
handlers = ['M110', 'M112', 'M115',
|
||||
'RESTART', 'FIRMWARE_RESTART', 'ECHO', 'STATUS', 'HELP', 'CLOSE_MCU_PORT']
|
||||
'RESTART', 'FIRMWARE_RESTART', 'ECHO', 'STATUS', 'HELP']
|
||||
for cmd in handlers:
|
||||
func = getattr(self, 'cmd_' + cmd)
|
||||
desc = getattr(self, 'cmd_' + cmd + '_help', None)
|
||||
self.register_command(cmd, func, True, desc)
|
||||
# Gcode macro interupt
|
||||
self.break_flag=False
|
||||
def is_traditional_gcode(self, cmd):
|
||||
# A "traditional" g-code command is a letter and followed by a number
|
||||
try:
|
||||
@@ -126,6 +134,7 @@ class GCodeDispatch:
|
||||
del self.ready_gcode_handlers[cmd]
|
||||
if cmd in self.base_gcode_handlers:
|
||||
del self.base_gcode_handlers[cmd]
|
||||
self._build_status_commands()
|
||||
return old_cmd
|
||||
if cmd in self.ready_gcode_handlers:
|
||||
raise self.printer.config_error(
|
||||
@@ -138,6 +147,7 @@ class GCodeDispatch:
|
||||
self.base_gcode_handlers[cmd] = func
|
||||
if desc is not None:
|
||||
self.gcode_help[cmd] = desc
|
||||
self._build_status_commands()
|
||||
def register_mux_command(self, cmd, key, value, func, desc=None):
|
||||
prev = self.mux_commands.get(cmd)
|
||||
if prev is None:
|
||||
@@ -156,19 +166,29 @@ class GCodeDispatch:
|
||||
prev_values[value] = func
|
||||
def get_command_help(self):
|
||||
return dict(self.gcode_help)
|
||||
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
|
||||
def register_output_handler(self, cb):
|
||||
self.output_callbacks.append(cb)
|
||||
def _handle_shutdown(self):
|
||||
if not self.is_printer_ready:
|
||||
return
|
||||
self.is_printer_ready = False
|
||||
# self.gcode_handlers = self.base_gcode_handlers
|
||||
self.gcode_handlers = self.base_gcode_handlers
|
||||
self._build_status_commands()
|
||||
self._respond_state("Shutdown")
|
||||
def _handle_disconnect(self):
|
||||
self._respond_state("Disconnect")
|
||||
def _handle_ready(self):
|
||||
self.is_printer_ready = True
|
||||
self.gcode_handlers = self.ready_gcode_handlers
|
||||
self._build_status_commands()
|
||||
self._respond_state("Ready")
|
||||
# Parse input into commands
|
||||
args_r = re.compile('([A-Z_]+|[A-Z*/])')
|
||||
@@ -176,6 +196,45 @@ class GCodeDispatch:
|
||||
for line in commands:
|
||||
# Ignore comments and leading/trailing spaces
|
||||
line = origline = line.strip()
|
||||
# Gcode macro interupt
|
||||
if self.break_flag:
|
||||
if str(line) != "CANCEL_PRINT":
|
||||
continue
|
||||
else:
|
||||
self.break_flag=False
|
||||
self.printer.lookup_object("heaters").break_flag=False
|
||||
# while priority_queue:
|
||||
# priority_command = priority_queue.pop(0)
|
||||
# # self.respond_info("priority: {}".format(priority_command))
|
||||
# cpos = priority_command.find(';')
|
||||
# if cpos >= 0:
|
||||
# priority_command = priority_command[:cpos]
|
||||
# parts = self.args_r.split(priority_command.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':
|
||||
# cmd = parts[3] + parts[4].strip()
|
||||
# params = { parts[i]: parts[i+1].strip()
|
||||
# for i in range(1, numparts, 2) }
|
||||
# gcmd = GCodeCommand(self, cmd, priority_command, params, need_ack)
|
||||
# handler = self.gcode_handlers.get(cmd, self.cmd_default)
|
||||
# try:
|
||||
# handler(gcmd)
|
||||
# except self.error as e:
|
||||
# self._respond_error(str(e))
|
||||
# self.printer.send_event("gcode:command_error")
|
||||
# if not need_ack:
|
||||
# raise
|
||||
# except:
|
||||
# msg = 'Internal error on command:"%s"' % (cmd,)
|
||||
# logging.exception(msg)
|
||||
# self.printer.invoke_shutdown(msg)
|
||||
# self._respond_error(msg)
|
||||
# if not need_ack:
|
||||
# raise
|
||||
# gcmd.ack()
|
||||
cpos = line.find(';')
|
||||
if cpos >= 0:
|
||||
line = line[:cpos]
|
||||
@@ -212,6 +271,16 @@ class GCodeDispatch:
|
||||
def run_script_from_command(self, script):
|
||||
self._process_commands(script.split('\n'), need_ack=False)
|
||||
def run_script(self, script):
|
||||
# lines = script.split('\n')
|
||||
# temp_lines = []
|
||||
# for line in lines:
|
||||
# if set_gcode_offset_r.match(line) is not None:
|
||||
# priority_queue.append(line)
|
||||
# else:
|
||||
# temp_lines.append(line)
|
||||
# lines = temp_lines
|
||||
# with self.mutex:
|
||||
# self._process_commands(lines, need_ack=False)
|
||||
with self.mutex:
|
||||
self._process_commands(script.split('\n'), need_ack=False)
|
||||
def get_mutex(self):
|
||||
@@ -329,9 +398,6 @@ class GCodeDispatch:
|
||||
def cmd_RESTART(self, gcmd):
|
||||
self.request_restart('restart')
|
||||
cmd_FIRMWARE_RESTART_help = "Restart firmware, host, and reload config"
|
||||
def cmd_CLOSE_MCU_PORT(self, gcmd):
|
||||
self.request_restart('close_mcu_port')
|
||||
cmd_CLOSE_MCU_PORT_help = "Close the port of mcu"
|
||||
def cmd_FIRMWARE_RESTART(self, gcmd):
|
||||
self.request_restart('firmware_restart')
|
||||
def cmd_ECHO(self, gcmd):
|
||||
|
||||
Reference in New Issue
Block a user