Skip to content

Commit

Permalink
gcode: expose status with available commands
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Lamas <pedrolamas@gmail.com>
  • Loading branch information
pedrolamas authored and KevinOConnor committed Dec 11, 2023
1 parent 2c2bb72 commit 6676c1d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/Status_Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ The following information is available in the
module. These settings may differ from the config file if a
`SET_RETRACTION` command alters them.

## gcode

The following information is available in the `gcode` object:
- `commands`: Returns a list of all currently available commands. For each
command, if a help string is defined it will also be provided.

## gcode_button

The following information is available in
Expand Down
13 changes: 13 additions & 0 deletions klippy/gcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def __init__(self, printer):
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']
Expand All @@ -126,6 +127,7 @@ def register_command(self, cmd, func, when_not_ready=False, desc=None):
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(
Expand All @@ -138,6 +140,7 @@ def register_command(self, cmd, func, when_not_ready=False, desc=None):
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:
Expand All @@ -156,19 +159,29 @@ def register_mux_command(self, cmd, key, value, func, desc=None):
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._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*/])')
Expand Down

0 comments on commit 6676c1d

Please sign in to comment.