From 443b04efa83d927954395d36dee75aa9adc44733 Mon Sep 17 00:00:00 2001 From: Vitor de Miranda Henrique Date: Mon, 26 Feb 2018 00:42:40 -0600 Subject: [PATCH] gcode partial implementation --- octoprint_enclosure/__init__.py | 49 +++++++++++++------ octoprint_enclosure/static/js/enclosure.js | 9 +++- .../templates/enclosure_settings.jinja2 | 24 +++++++-- .../templates/enclosure_tab.jinja2 | 2 +- 4 files changed, 63 insertions(+), 21 deletions(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index cd0ba4e..70b05c0 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -161,6 +161,13 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, rpi_output['gpio_pin']), self.to_int(pwm_val)) return flask.jsonify(success=True) + @octoprint.plugin.BlueprintPlugin.route("/sendGcodeCommand", methods=["GET"]) + def requested_gcode_command(self): + gpio_index = self.to_int(flask.request.values["index_id"]) + rpi_output = [r_out for r_out in self.rpi_outputs if self.to_int(r_out['index_id']) == gpio_index].pop() + self.send_gcode_command(rpi_output['gcode']) + return flask.jsonify(success=True) + @octoprint.plugin.BlueprintPlugin.route("/setNeopixel", methods=["GET"]) def set_neopixel(self): """ set_neopixel method get request from octoprint and send the comand to arduino or neopixel""" @@ -647,7 +654,7 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, gpio_pin = self.to_int(rpi_input['gpio_pin']) GPIO.setup(gpio_pin, GPIO.IN, pullResistor) edge = GPIO.RISING if rpi_input['edge'] == 'rise' else GPIO.FALLING - if rpi_input['action_type'] == 'gpio_control': + if rpi_input['action_type'] == 'output_control': self._logger.info( "Adding GPIO event detect on pin %s with edge: %s", gpio_pin, edge) GPIO.add_event_detect(gpio_pin, edge, callback=self.handle_gpio_control, bouncetime=200) @@ -748,19 +755,25 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, controlled_io = self.to_int(rpi_input['controlled_io']) if ((rpi_input['edge'] == 'fall') ^ GPIO.input(gpio_pin)): rpi_output = [r_out for r_out in self.rpi_outputs if self.to_int(r_out['index_id']) == controlled_io].pop() - - if rpi_input['controlled_io_set_value'] == 'toggle': - val = GPIO.LOW if GPIO.input(self.to_int( - rpi_output['gpio_pin'])) == GPIO.HIGH else GPIO.HIGH - else: - val = GPIO.LOW if rpi_input['controlled_io_set_value'] == 'low' else GPIO.HIGH - self.write_gpio(self.to_int( - rpi_output['gpio_pin']), val) - for notification in self.notifications: - if notification['gpioAction']: - msg = "GPIO control action caused by input " + str(rpi_input['label']) + ". Setting GPIO" + str( - rpi_input['controlled_io']) + " to: " + str(rpi_input['controlled_io_set_value']) - self.send_notification(msg) + if rpi_output['output_type'] == 'regular': + if rpi_input['controlled_io_set_value'] == 'toggle': + val = GPIO.LOW if GPIO.input(self.to_int( + rpi_output['gpio_pin'])) == GPIO.HIGH else GPIO.HIGH + else: + val = GPIO.LOW if rpi_input['controlled_io_set_value'] == 'low' else GPIO.HIGH + self.write_gpio(self.to_int( + rpi_output['gpio_pin']), val) + for notification in self.notifications: + if notification['gpioAction']: + msg = "GPIO control action caused by input " + str(rpi_input['label']) + ". Setting GPIO" + str( + rpi_input['controlled_io']) + " to: " + str(rpi_input['controlled_io_set_value']) + self.send_notification(msg) + if rpi_output['output_type'] == 'gcode_output': + self.send_gcode_command(rpi_output['gcode']) + for notification in self.notifications: + if notification['gpioAction']: + msg = "GPIO control action caused by input " + str(rpi_input['label']) + ". Sending GCODE command" + self.send_notification(msg) except Exception as ex: template = "An exception of type {0} occurred on {1}. Arguments:\n{2!r}" message = template.format( @@ -768,6 +781,14 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, self._logger.warn(message) pass + def send_gcode_command(self, command): + for line in command.split('\n'): + if line: + self._printer.commands(line.strip().upper()) + self._logger.info( + "Sending GCODE command: %s", line.strip().upper()) + time.sleep(0.2) + def handle_printer_action(self, channel): try: for rpi_input in self.rpi_inputs: diff --git a/octoprint_enclosure/static/js/enclosure.js b/octoprint_enclosure/static/js/enclosure.js index 7ca4aaa..ff7b086 100644 --- a/octoprint_enclosure/static/js/enclosure.js +++ b/octoprint_enclosure/static/js/enclosure.js @@ -17,6 +17,12 @@ $(function () { }); }); + self.rpi_possible_outputs = ko.pureComputed(function () { + return ko.utils.arrayFilter(self.rpi_outputs(), function (item) { + return (item.output_type() === "regular" || item.output_type() === "gcode_output"); + }); + }); + self.rpi_outputs_pwm = ko.pureComputed(function () { return ko.utils.arrayFilter(self.rpi_outputs(), function (item) { return (item.output_type() === "pwm"); @@ -280,6 +286,7 @@ $(function () { output_type: ko.observable("regular"), gpio_pin: ko.observable(0), gpio_status: ko.observable(false), + hide_btn_ui: ko.observable(false), active_low: ko.observable(true), auto_startup: ko.observable(false), controlled_io: ko.observable(0), @@ -334,7 +341,7 @@ $(function () { temp_sensor_humidity: ko.observable(""), ds18b20_serial: ko.observable(""), use_fahrenheit: ko.observable(false), - action_type: ko.observable("gpio_control"), + action_type: ko.observable("output_control"), controlled_io: ko.observable(""), controlled_io_set_value: ko.observable("low"), edge: ko.observable("fall"), diff --git a/octoprint_enclosure/templates/enclosure_settings.jinja2 b/octoprint_enclosure/templates/enclosure_settings.jinja2 index c35ada2..781fa4f 100644 --- a/octoprint_enclosure/templates/enclosure_settings.jinja2 +++ b/octoprint_enclosure/templates/enclosure_settings.jinja2 @@ -87,6 +87,20 @@ + + + +
+
+ + If you plan to use a physical button (INPUT) and want to hide the button from enclosure tab check this. +
+
+ + +
@@ -411,7 +425,7 @@
- {{ _('GPIO Control') }} + {{ _('Output Control') }}
{{ _('Printer') }} @@ -426,7 +440,7 @@ sensor conects to ground when detects the end of the filament, you should choose PULL UP resistors and detect the event on the falling edge. - + Info: Action will control GPIO outputs when a condition is met, for example detect a press of a button. You can use this to control any previous configured OUTPUTS, basically beeing able to control your lights / fan @@ -471,14 +485,14 @@
- +
- - When the event happen, you want control which IO? + When the event happen, you want control which OUTPUT?
diff --git a/octoprint_enclosure/templates/enclosure_tab.jinja2 b/octoprint_enclosure/templates/enclosure_tab.jinja2 index 3e09b5d..7612e5f 100644 --- a/octoprint_enclosure/templates/enclosure_tab.jinja2 +++ b/octoprint_enclosure/templates/enclosure_tab.jinja2 @@ -101,7 +101,7 @@ - +