Skip to content

Commit

Permalink
Implement show_macros_in_webui
Browse files Browse the repository at this point in the history
  • Loading branch information
thetic committed Oct 7, 2024
1 parent 7421e0d commit b3fb0fc
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
18 changes: 18 additions & 0 deletions autospeed/dummy_macros.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file is used when the `show_macros_in_webui` option is enabled. It wraps
# the native Python commands in macros so that they will show up in web UIs
# like Mainsail and Fluidd.

[gcode_macro AUTO_SPEED]
gcode: _AUTO_SPEED {rawparams}

[gcode_macro AUTO_SPEED_VELOCITY]
gcode: _AUTO_SPEED_VELOCITY {rawparams}

[gcode_macro AUTO_SPEED_ACCEL]
gcode: _AUTO_SPEED_ACCEL {rawparams}

[gcode_macro AUTO_SPEED_VALIDATE]
gcode: _AUTO_SPEED_VALIDATE {rawparams}

[gcode_macro AUTO_SPEED_GRAPH]
gcode: _AUTO_SPEED_GRAPH
63 changes: 46 additions & 17 deletions autospeed/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ def __init__(self, config):
self.validate_inner_margin = config.getfloat('validate_inner_margin', default=20.0, above=0.0)
self.validate_iterations = config.getint( 'validate_iterations', default=50, minval=1)

show_macros = config.getboolean('show_macros_in_webui', default=False)

for path in ( # Could be problematic if neither of these paths work
os.path.dirname(self.printer.start_args['log_file']),
os.path.expanduser('~/printer_data/config')
Expand All @@ -62,22 +64,49 @@ def __init__(self, config):
self.printer.register_event_handler("klippy:connect", self.handle_connect)
self.printer.register_event_handler("homing:home_rails_end", self.handle_home_rails_end)

self.gcode.register_command('AUTO_SPEED',
self.cmd_AUTO_SPEED,
desc=self.cmd_AUTO_SPEED_help)
self.gcode.register_command('AUTO_SPEED_VELOCITY',
self.cmd_AUTO_SPEED_VELOCITY,
desc=self.cmd_AUTO_SPEED_VELOCITY_help)
self.gcode.register_command('AUTO_SPEED_ACCEL',
self.cmd_AUTO_SPEED_ACCEL,
desc=self.cmd_AUTO_SPEED_ACCEL_help)
self.gcode.register_command('AUTO_SPEED_VALIDATE',
self.cmd_AUTO_SPEED_VALIDATE,
desc=self.cmd_AUTO_SPEED_VALIDATE_help)
self.gcode.register_command('AUTO_SPEED_GRAPH',
self.cmd_AUTO_SPEED_GRAPH,
desc=self.cmd_AUTO_SPEED_GRAPH_help)

measurement_commands = [
('AUTO_SPEED', self.cmd_AUTO_SPEED, self.cmd_AUTO_SPEED_help),
('AUTO_SPEED_VELOCITY', self.cmd_AUTO_SPEED_VELOCITY, self.cmd_AUTO_SPEED_VELOCITY_help),
('AUTO_SPEED_ACCEL', self.cmd_AUTO_SPEED_ACCEL, self.cmd_AUTO_SPEED_ACCEL_help),
('AUTO_SPEED_VALIDATE', self.cmd_AUTO_SPEED_VALIDATE, self.cmd_AUTO_SPEED_VALIDATE_help),
('AUTO_SPEED_GRAPH', self.cmd_AUTO_SPEED_GRAPH, self.cmd_AUTO_SPEED_GRAPH_help),
]
command_descriptions = {name: desc for name, _, desc in measurement_commands}
gcode = self.printer.lookup_object('gcode')
for name, command, description in measurement_commands:
gcode.register_command(f'_{name}' if show_macros else name, command, desc=description)

# Load the dummy macros with their description in order to show them in the web interfaces
if show_macros:
pconfig = self.printer.lookup_object('configfile')
dirname = os.path.dirname(os.path.realpath(__file__))
filename = os.path.join(dirname, 'dummy_macros.cfg')
try:
dummy_macros_cfg = pconfig.read_config(filename)
except Exception as err:
raise config.error(f'Cannot load AutoSpeed dummy macro {filename}') from err

for gcode_macro in dummy_macros_cfg.get_prefix_sections('gcode_macro '):
gcode_macro_name = gcode_macro.get_name()

# Replace the dummy description by the one here (to avoid code duplication and define it in only one place)
command = gcode_macro_name.split(' ', 1)[1]
description = command_descriptions.get(command, 'AutoSpeed macro')
gcode_macro.fileconfig.set(gcode_macro_name, 'description', description)

# Add the section to the Klipper configuration object with all its options
if not config.fileconfig.has_section(gcode_macro_name.lower()):
config.fileconfig.add_section(gcode_macro_name.lower())
for option in gcode_macro.fileconfig.options(gcode_macro_name):
value = gcode_macro.fileconfig.get(gcode_macro_name, option)
config.fileconfig.set(gcode_macro_name.lower(), option, value)

# Small trick to ensure the new injected sections are considered valid by Klipper config system
config.access_tracking[(gcode_macro_name.lower(), option.lower())] = 1

# Finally, load the section within the printer objects
self.printer.load_object(config, gcode_macro_name.lower())

self.level = None

self.steppers = {}
Expand Down Expand Up @@ -752,4 +781,4 @@ def _set_velocity(self, velocity: float, accel: float):
self.toolhead.max_velocity = velocity
self.toolhead.max_accel = accel
self.toolhead.requested_accel_to_decel = accel/2
self.toolhead._calc_junction_deviation()
self.toolhead._calc_junction_deviation()

0 comments on commit b3fb0fc

Please sign in to comment.