2019-06-22 01:06:31 +03:00
|
|
|
# Support for executing gcode when a hardware button is pressed or released.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2019 Alec Plumb <alec@etherwalker.com>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
import logging
|
|
|
|
|
|
|
|
class GCodeButton:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
|
|
|
self.name = config.get_name().split(' ')[-1]
|
|
|
|
self.pin = config.get('pin')
|
|
|
|
self.last_state = 0
|
2020-05-05 21:10:30 +03:00
|
|
|
buttons = self.printer.load_object(config, "buttons")
|
2019-06-22 01:06:31 +03:00
|
|
|
buttons.register_buttons([self.pin], self.button_callback)
|
2020-05-05 21:10:30 +03:00
|
|
|
gcode_macro = self.printer.load_object(config, 'gcode_macro')
|
2019-06-22 01:06:31 +03:00
|
|
|
self.press_template = gcode_macro.load_template(config, 'press_gcode')
|
2020-04-25 19:09:03 +03:00
|
|
|
self.release_template = gcode_macro.load_template(config,
|
|
|
|
'release_gcode', '')
|
2019-06-22 01:06:31 +03:00
|
|
|
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"
|
2020-04-25 06:50:41 +03:00
|
|
|
def cmd_QUERY_BUTTON(self, gcmd):
|
|
|
|
gcmd.respond_info(self.name + ": " + self.get_status()['state'])
|
2019-06-22 01:06:31 +03:00
|
|
|
|
|
|
|
def button_callback(self, eventtime, state):
|
|
|
|
self.last_state = state
|
2020-04-25 19:09:03 +03:00
|
|
|
template = self.press_template
|
|
|
|
if not state:
|
|
|
|
template = self.release_template
|
|
|
|
try:
|
|
|
|
self.gcode.run_script(template.render())
|
|
|
|
except:
|
|
|
|
logging.exception("Script running error")
|
2019-06-22 01:06:31 +03:00
|
|
|
|
|
|
|
def get_status(self, eventtime=None):
|
|
|
|
if self.last_state:
|
|
|
|
return {'state': "PRESSED"}
|
|
|
|
return {'state': "RELEASED"}
|
|
|
|
|
|
|
|
def load_config_prefix(config):
|
|
|
|
return GCodeButton(config)
|