2018-07-16 16:06:30 +02:00
|
|
|
# Utility for querying the current state of all endstops
|
|
|
|
#
|
2019-06-26 16:45:06 +02:00
|
|
|
# Copyright (C) 2018-2019 Kevin O'Connor <kevin@koconnor.net>
|
2018-07-16 16:06:30 +02:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
|
|
|
|
class QueryEndstops:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
|
|
|
self.endstops = []
|
2021-01-15 04:13:50 +01:00
|
|
|
self.last_state = []
|
2020-06-19 12:26:09 +02:00
|
|
|
# Register webhook if server is available
|
|
|
|
webhooks = self.printer.lookup_object('webhooks')
|
|
|
|
webhooks.register_endpoint(
|
|
|
|
"query_endstops/status", self._handle_web_request)
|
2018-07-16 16:06:30 +02:00
|
|
|
gcode = self.printer.lookup_object('gcode')
|
|
|
|
gcode.register_command("QUERY_ENDSTOPS", self.cmd_QUERY_ENDSTOPS,
|
|
|
|
desc=self.cmd_QUERY_ENDSTOPS_help)
|
|
|
|
gcode.register_command("M119", self.cmd_QUERY_ENDSTOPS)
|
|
|
|
def register_endstop(self, mcu_endstop, name):
|
|
|
|
self.endstops.append((mcu_endstop, name))
|
2020-02-13 01:11:40 +01:00
|
|
|
def get_status(self, eventtime):
|
|
|
|
return {'last_query': {name: value for name, value in self.last_state}}
|
2020-06-19 12:26:09 +02:00
|
|
|
def _handle_web_request(self, web_request):
|
|
|
|
gc_mutex = self.printer.lookup_object('gcode').get_mutex()
|
|
|
|
toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
with gc_mutex:
|
|
|
|
print_time = toolhead.get_last_move_time()
|
|
|
|
self.last_state = [(name, mcu_endstop.query_endstop(print_time))
|
|
|
|
for mcu_endstop, name in self.endstops]
|
|
|
|
web_request.send({name: ["open", "TRIGGERED"][not not t]
|
|
|
|
for name, t in self.last_state})
|
2018-07-16 16:06:30 +02:00
|
|
|
cmd_QUERY_ENDSTOPS_help = "Report on the status of each endstop"
|
2020-04-25 05:06:07 +02:00
|
|
|
def cmd_QUERY_ENDSTOPS(self, gcmd):
|
2018-07-16 16:06:30 +02:00
|
|
|
# Query the endstops
|
2019-06-26 16:45:06 +02:00
|
|
|
print_time = self.printer.lookup_object('toolhead').get_last_move_time()
|
2020-02-13 01:11:40 +01:00
|
|
|
self.last_state = [(name, mcu_endstop.query_endstop(print_time))
|
|
|
|
for mcu_endstop, name in self.endstops]
|
2018-07-16 16:06:30 +02:00
|
|
|
# Report results
|
|
|
|
msg = " ".join(["%s:%s" % (name, ["open", "TRIGGERED"][not not t])
|
2020-02-13 01:11:40 +01:00
|
|
|
for name, t in self.last_state])
|
2020-04-25 05:06:07 +02:00
|
|
|
gcmd.respond_raw(msg)
|
2018-07-16 16:06:30 +02:00
|
|
|
|
|
|
|
def load_config(config):
|
|
|
|
return QueryEndstops(config)
|