gcode: Rework gcode handler setup to allow dynamic command registration
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
64a091fb98
commit
b5a41d0dd1
|
@ -27,8 +27,17 @@ class GCodeParser:
|
||||||
self.input_log = collections.deque([], 50)
|
self.input_log = collections.deque([], 50)
|
||||||
# Command handling
|
# Command handling
|
||||||
self.is_printer_ready = False
|
self.is_printer_ready = False
|
||||||
self.gcode_handlers = {}
|
self.base_gcode_handlers = self.gcode_handlers = {}
|
||||||
self.build_handlers()
|
self.ready_gcode_handlers = {}
|
||||||
|
self.gcode_help = {}
|
||||||
|
for cmd in self.all_handlers:
|
||||||
|
func = getattr(self, 'cmd_' + cmd)
|
||||||
|
wnr = getattr(self, 'cmd_' + cmd + '_when_not_ready', False)
|
||||||
|
desc = getattr(self, 'cmd_' + cmd + '_help', None)
|
||||||
|
self.register_command(cmd, func, wnr, desc)
|
||||||
|
for a in getattr(self, 'cmd_' + cmd + '_aliases', []):
|
||||||
|
self.register_command(a, func, wnr)
|
||||||
|
# G-Code state
|
||||||
self.need_ack = False
|
self.need_ack = False
|
||||||
self.toolhead = self.fan = self.extruder = None
|
self.toolhead = self.fan = self.extruder = None
|
||||||
self.heaters = []
|
self.heaters = []
|
||||||
|
@ -38,21 +47,17 @@ class GCodeParser:
|
||||||
self.last_position = [0.0, 0.0, 0.0, 0.0]
|
self.last_position = [0.0, 0.0, 0.0, 0.0]
|
||||||
self.homing_add = [0.0, 0.0, 0.0, 0.0]
|
self.homing_add = [0.0, 0.0, 0.0, 0.0]
|
||||||
self.axis2pos = {'X': 0, 'Y': 1, 'Z': 2, 'E': 3}
|
self.axis2pos = {'X': 0, 'Y': 1, 'Z': 2, 'E': 3}
|
||||||
def build_handlers(self):
|
def register_command(self, cmd, func, when_not_ready=False, desc=None):
|
||||||
handlers = self.all_handlers
|
self.ready_gcode_handlers[cmd] = func
|
||||||
if not self.is_printer_ready:
|
if when_not_ready:
|
||||||
handlers = [h for h in handlers
|
self.base_gcode_handlers[cmd] = func
|
||||||
if getattr(self, 'cmd_'+h+'_when_not_ready', False)]
|
if desc is not None:
|
||||||
gcode_handlers = { h: getattr(self, 'cmd_'+h) for h in handlers }
|
self.gcode_help[cmd] = desc
|
||||||
for h, f in list(gcode_handlers.items()):
|
|
||||||
aliases = getattr(self, 'cmd_'+h+'_aliases', [])
|
|
||||||
gcode_handlers.update({ a: f for a in aliases })
|
|
||||||
self.gcode_handlers = gcode_handlers
|
|
||||||
def stats(self, eventtime):
|
def stats(self, eventtime):
|
||||||
return "gcodein=%d" % (self.bytes_read,)
|
return "gcodein=%d" % (self.bytes_read,)
|
||||||
def connect(self):
|
def connect(self):
|
||||||
self.is_printer_ready = True
|
self.is_printer_ready = True
|
||||||
self.build_handlers()
|
self.gcode_handlers = self.ready_gcode_handlers
|
||||||
# Lookup printer components
|
# Lookup printer components
|
||||||
self.toolhead = self.printer.objects.get('toolhead')
|
self.toolhead = self.printer.objects.get('toolhead')
|
||||||
extruders = extruder.get_printer_extruders(self.printer)
|
extruders = extruder.get_printer_extruders(self.printer)
|
||||||
|
@ -71,7 +76,7 @@ class GCodeParser:
|
||||||
if not self.is_printer_ready:
|
if not self.is_printer_ready:
|
||||||
return
|
return
|
||||||
self.is_printer_ready = False
|
self.is_printer_ready = False
|
||||||
self.build_handlers()
|
self.gcode_handlers = self.base_gcode_handlers
|
||||||
self.dump_debug()
|
self.dump_debug()
|
||||||
if self.is_fileinput:
|
if self.is_fileinput:
|
||||||
self.printer.request_exit()
|
self.printer.request_exit()
|
||||||
|
@ -508,7 +513,6 @@ class GCodeParser:
|
||||||
cmdhelp.append("Printer is not ready - not all commands available.")
|
cmdhelp.append("Printer is not ready - not all commands available.")
|
||||||
cmdhelp.append("Available extended commands:")
|
cmdhelp.append("Available extended commands:")
|
||||||
for cmd in sorted(self.gcode_handlers):
|
for cmd in sorted(self.gcode_handlers):
|
||||||
desc = getattr(self, 'cmd_'+cmd+'_help', None)
|
if cmd in self.gcode_help:
|
||||||
if desc is not None:
|
cmdhelp.append("%-10s: %s" % (cmd, self.gcode_help[cmd]))
|
||||||
cmdhelp.append("%-10s: %s" % (cmd, desc))
|
|
||||||
self.respond_info("\n".join(cmdhelp))
|
self.respond_info("\n".join(cmdhelp))
|
||||||
|
|
Loading…
Reference in New Issue