From 86a99cf38ed9c041a332505e413805e582a361db Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sat, 25 Apr 2020 12:09:03 -0400 Subject: [PATCH] gcode_button: Simplify script execution It's no longer necessary to queue gcode scripts - gcode.run_script() now does that automatically. Also, make release_gcode config optional. Signed-off-by: Kevin O'Connor --- config/example-extras.cfg | 5 +++-- klippy/extras/gcode_button.py | 34 +++++++++------------------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/config/example-extras.cfg b/config/example-extras.cfg index a81ab014..830381ce 100644 --- a/config/example-extras.cfg +++ b/config/example-extras.cfg @@ -1208,10 +1208,11 @@ # provided. #press_gcode: # A list of G-Code commands to execute when the button is pressed. -# G-Code templates are supported. +# G-Code templates are supported. This parameter must be provided. #release_gcode: # A list of G-Code commands to execute when the button is released. -# G-Code templates are supported. +# G-Code templates are supported. The default is to not run any +# commands on a button release. # Run-time configurable output pins (one may define any number of # sections with an "output_pin" prefix). Pins configured here will be diff --git a/klippy/extras/gcode_button.py b/klippy/extras/gcode_button.py index 757d8f7a..bb2bb458 100644 --- a/klippy/extras/gcode_button.py +++ b/klippy/extras/gcode_button.py @@ -11,51 +11,35 @@ class GCodeButton: self.name = config.get_name().split(' ')[-1] self.pin = config.get('pin') self.last_state = 0 - self.template_queue = [] buttons = self.printer.try_load_module(config, "buttons") buttons.register_buttons([self.pin], self.button_callback) gcode_macro = self.printer.try_load_module(config, 'gcode_macro') self.press_template = gcode_macro.load_template(config, 'press_gcode') - self.release_template = gcode_macro.load_template( - config, 'release_gcode') + self.release_template = gcode_macro.load_template(config, + 'release_gcode', '') self.gcode = self.printer.lookup_object('gcode') self.gcode.register_mux_command("QUERY_BUTTON", "BUTTON", self.name, self.cmd_QUERY_BUTTON, desc=self.cmd_QUERY_BUTTON_help) cmd_QUERY_BUTTON_help = "Report on the state of a button" - def cmd_QUERY_BUTTON(self, params): self.gcode.respond_info(self.name + ": " + self.get_status()['state']) def button_callback(self, eventtime, state): self.last_state = state - if state and bool(self.press_template): - self.queue_template(self.press_template) - if (not state) and bool(self.release_template): - self.queue_template(self.release_template) + template = self.press_template + if not state: + template = self.release_template + try: + self.gcode.run_script(template.render()) + except: + logging.exception("Script running error") def get_status(self, eventtime=None): if self.last_state: return {'state': "PRESSED"} return {'state': "RELEASED"} - def queue_template(self, template): - if template is None: - return - if not self.template_queue: - reactor = self.printer.get_reactor() - reactor.register_callback(self.dispatch_templates) - self.template_queue.append(template) - - def dispatch_templates(self, eventtime): - while self.template_queue: - template = self.template_queue[0] - try: - self.gcode.run_script(template.render()) - except Exception: - logging.exception("Script running error") - self.template_queue.pop(0) - def load_config_prefix(config): return GCodeButton(config)