From 3721b411ae9598128118bf06cc2e9632f733ce19 Mon Sep 17 00:00:00 2001 From: emouty Date: Sat, 20 Mar 2021 21:42:12 +0100 Subject: [PATCH 01/13] feat: add update script to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index ecfcd6f..ad68f1a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ dist *.egg* .DS_Store *.zip +temp +update.sh From 7df1e56cec103905eafb6ca6deb4f01a3f966ee8 Mon Sep 17 00:00:00 2001 From: emouty Date: Sat, 20 Mar 2021 21:43:26 +0100 Subject: [PATCH 02/13] feat: ignore vscode settings --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ad68f1a..43166f9 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ dist *.zip temp update.sh +.vscode/settings.json From 949b7675c0a826a49b7036dfd8ae092ec4667edc Mon Sep 17 00:00:00 2001 From: emouty Date: Sat, 20 Mar 2021 21:49:47 +0100 Subject: [PATCH 03/13] feat: add led strip and physical button support - allow control of the light from a physical button connected to a GPIO pin - added support for led strip --- octoprint_octolight/__init__.py | 195 ++++++++++++------ .../templates/octolight_settings.jinja2 | 20 ++ setup.py | 4 +- 3 files changed, 152 insertions(+), 67 deletions(-) diff --git a/octoprint_octolight/__init__.py b/octoprint_octolight/__init__.py index 39e4828..f71a1bb 100755 --- a/octoprint_octolight/__init__.py +++ b/octoprint_octolight/__init__.py @@ -4,12 +4,29 @@ import octoprint.plugin from octoprint.events import Events import flask - +import board +from rpi_ws281x import Color, PixelStrip, ws import RPi.GPIO as GPIO -GPIO.setmode(GPIO.BOARD) +# GPIO.cleanup() +# GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) +# constants +BUTTON_PIN = "button_pin" +LIGHT_PIN = "light_pin" +INVERTED_OUTPUT = "inverted_output" +PREVIOUS_BUTTON_PIN = "previous_button_pin" +IS_LED_STRIP = "is_led_strip" +NB_LEDS = "nb_leds" + +LED_DMA = 10 # DMA channel to use for generating signal (try 10) +LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest +LED_CHANNEL = 0 +LED_STRIP = ws.WS2812_STRIP +LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz) + + class OctoLightPlugin( octoprint.plugin.AssetPlugin, octoprint.plugin.StartupPlugin, @@ -18,15 +35,22 @@ class OctoLightPlugin( octoprint.plugin.SettingsPlugin, octoprint.plugin.EventHandlerPlugin, octoprint.plugin.RestartNeedingPlugin + octoprint.plugin.ShutdownPlugin ): - light_state = False + # variables + light_state = False + pixels = None - def get_settings_defaults(self): - return dict( - light_pin = 13, - inverted_output = False - ) + def get_settings_defaults(self): + return dict( + light_pin=19, + button_pin=2, + previous_button_pin=2, + inverted_output=False, + is_led_strip=False, + nb_leds=20, + ) def get_template_configs(self): return [ @@ -43,84 +67,125 @@ def get_assets(self): #less=["less/octolight.less"] ) - def on_after_startup(self): - self.light_state = False - self._logger.info("--------------------------------------------") - self._logger.info("OctoLight started, listening for GET request") - self._logger.info("Light pin: {}, inverted_input: {}".format( - self._settings.get(["light_pin"]), - self._settings.get(["inverted_output"]) - )) - self._logger.info("--------------------------------------------") - - # Setting the default state of pin - GPIO.setup(int(self._settings.get(["light_pin"])), GPIO.OUT) - if bool(self._settings.get(["inverted_output"])): - GPIO.output(int(self._settings.get(["light_pin"])), GPIO.HIGH) - else: - GPIO.output(int(self._settings.get(["light_pin"])), GPIO.LOW) - - #Because light is set to ff on startup we don't need to retrieve the current state - """ - r = self.light_state = GPIO.input(int(self._settings.get(["light_pin"]))) - if r==1: - self.light_state = False - else: - self.light_state = True - - self._logger.info("After Startup. Light state: {}".format( - self.light_state + def on_after_startup(self): + self.light_state = False + self._logger.info("--------------------------------------------") + self._logger.info("OctoLight started, listening for GET request") + self._logger.debug("GPIO Mode: {}".format( + GPIO.getmode() + )) + self._logger.info("Light pin: {}, inverted_input: {}, button pin: {}".format( + self._settings.get([LIGHT_PIN]), + self._settings.get([INVERTED_OUTPUT]), + self._settings.get([BUTTON_PIN]) )) - """ + self._logger.info("--------------------------------------------") + self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state)) - def on_api_get(self, request): - # Sets the GPIO every time, if user changed it in the settings. - GPIO.setup(int(self._settings.get(["light_pin"])), GPIO.OUT) + # Enabling watch to the default button pin + self.enable_watch_button(self._settings.get([BUTTON_PIN])) + def on_api_get(self, request): + self._logger.info("Got request. Light state: {}".format( + self.light_state + )) - self.light_state = not self.light_state + if self._settings.get([BUTTON_PIN]) != self._settings.get([PREVIOUS_BUTTON_PIN]): + # stop watching on the previous pin + GPIO.remove_event_detect( + self._settings.get([PREVIOUS_BUTTON_PIN])) + # enable watching on the new pin + self.enable_watch_button(self._settings.get([BUTTON_PIN])) + self._settings.set([PREVIOUS_BUTTON_PIN], + self._settings.get([BUTTON_PIN])) + + if bool(self._settings.get([IS_LED_STRIP])): + + # enabling led strip on selected pin + self.pixels = PixelStrip(int(self._settings.get([NB_LEDS])), int(self._settings.get( + [LIGHT_PIN])), LED_FREQ_HZ, LED_DMA, bool(self._settings.get([INVERTED_OUTPUT])), LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP) + self.pixels.begin() + else: - # Sets the light state depending on the inverted output setting (XOR) - if self.light_state ^ self._settings.get(["inverted_output"]): - GPIO.output(int(self._settings.get(["light_pin"])), GPIO.HIGH) - else: - GPIO.output(int(self._settings.get(["light_pin"])), GPIO.LOW) + # Sets the GPIO every time, if user changed it in the settings. + GPIO.setup(int(self._settings.get([LIGHT_PIN])), GPIO.OUT) - self._logger.info("Got request. Light state: {}".format( - self.light_state - )) + self.change_light_state(None) - self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state)) + self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state)) return flask.jsonify(status="ok") def on_event(self, event, payload): - if event == Events.CLIENT_OPENED: self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state)) + if event == Events.CLIENT_OPENED: return - def get_update_information(self): - return dict( - octolight=dict( - displayName="OctoLight", - displayVersion=self._plugin_version, - type="github_release", - current=self._plugin_version, + def enable_watch_button(self, button): + self._logger.info("watching events on pin : {}".format( + button + )) + GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP) + GPIO.add_event_detect(button, GPIO.FALLING, + callback=self.change_light_state, bouncetime=200) + + def change_light_state(self, channel): + self.light_state = not self.light_state + + # Sets the light state depending on the inverted output setting (XOR) + if bool(self._settings.get([IS_LED_STRIP])): + if self.light_state: + self._logger.debug( + "Led strip mode is enabled, will turn on the led strip") + self.colorWipe(self.pixels, Color(255, 255, 255)) + else: + self._logger.info( + "Led strip mode is enabled, will turn off the led strip") + self.colorWipe(self.pixels, Color(0,0,0)) + elif self.light_state ^ self._settings.get([INVERTED_OUTPUT]): + GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.HIGH) + else: + GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.LOW) - user="gigibu5", - repo="OctoLight", - pip="https://github.com/gigibu5/OctoLight/archive/{target}.zip" - ) - ) + self._logger.debug("Light state switched to : {}".format( + self.light_state + )) + + + def get_update_information(self): + return dict( + octolight=dict( + displayName="OctoLight", + displayVersion=self._plugin_version, + + type="github_release", + current=self._plugin_version, + + user="emouty", + repo="OctoLight", + pip="https://github.com/emouty/OctoLight/archive/{target}.zip" + ) + ) + + def on_shutdown(self): + # release GPIO pin on shutdown + GPIO.cleanup() + + # Define functions which animate LEDs in various ways. + def colorWipe(self, strip, color): + """Wipe color across display a pixel at a time.""" + for i in range(strip.numPixels()): + strip.setPixelColor(i, color) + strip.show() __plugin_pythoncompat__ = ">=2.7,<4" __plugin_implementation__ = OctoLightPlugin() __plugin_hooks__ = { - "octoprint.plugin.softwareupdate.check_config": - __plugin_implementation__.get_update_information -} \ No newline at end of file + "octoprint.plugin.softwareupdate.check_config": + __plugin_implementation__.get_update_information +} diff --git a/octoprint_octolight/templates/octolight_settings.jinja2 b/octoprint_octolight/templates/octolight_settings.jinja2 index 05415af..68214fb 100755 --- a/octoprint_octolight/templates/octolight_settings.jinja2 +++ b/octoprint_octolight/templates/octolight_settings.jinja2 @@ -12,5 +12,25 @@ {{ _('Inverted output') }} +
+ +
+ +
+ +
+
+ +
+ +
+ +
\ No newline at end of file diff --git a/setup.py b/setup.py index 6b978c9..70a0314 100755 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ plugin_name = "OctoLight" # The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module -plugin_version = "0.1.2" +plugin_version = "0.2.0-SNAPSHOT" # The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin # module @@ -33,7 +33,7 @@ plugin_license = "AGPLv3" # Any additional requirements besides OctoPrint should be listed here -plugin_requires = ["RPi.GPIO"] +plugin_requires = ["RPi.GPIO","rpi_ws281x"] ### -------------------------------------------------------------------------------------------------------------------- ### More advanced options that you usually shouldn't have to touch follow after this point From 420275472dab9295dc35d256da47f3b2cc3e428b Mon Sep 17 00:00:00 2001 From: emouty Date: Sun, 21 Mar 2021 09:36:29 +0100 Subject: [PATCH 04/13] fix: init when led is already enabled --- octoprint_octolight/__init__.py | 37 ++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/octoprint_octolight/__init__.py b/octoprint_octolight/__init__.py index f71a1bb..06245e7 100755 --- a/octoprint_octolight/__init__.py +++ b/octoprint_octolight/__init__.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, unicode_literals + import octoprint.plugin from octoprint.events import Events import flask @@ -81,6 +82,13 @@ def on_after_startup(self): )) self._logger.info("--------------------------------------------") + # Setting the default state of the light pin + self.setup_pin() + if not bool(self._settings.get([IS_LED_STRIP])): + if bool(self._settings.get([INVERTED_OUTPUT]))): + GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.HIGH) + else: + GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.LOW) self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state)) @@ -101,16 +109,7 @@ def on_api_get(self, request): self._settings.set([PREVIOUS_BUTTON_PIN], self._settings.get([BUTTON_PIN])) - if bool(self._settings.get([IS_LED_STRIP])): - - # enabling led strip on selected pin - self.pixels = PixelStrip(int(self._settings.get([NB_LEDS])), int(self._settings.get( - [LIGHT_PIN])), LED_FREQ_HZ, LED_DMA, bool(self._settings.get([INVERTED_OUTPUT])), LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP) - self.pixels.begin() - else: - - # Sets the GPIO every time, if user changed it in the settings. - GPIO.setup(int(self._settings.get([LIGHT_PIN])), GPIO.OUT) + self.setup_pin() self.change_light_state(None) @@ -143,9 +142,9 @@ def change_light_state(self, channel): "Led strip mode is enabled, will turn on the led strip") self.colorWipe(self.pixels, Color(255, 255, 255)) else: - self._logger.info( + self._logger.debug( "Led strip mode is enabled, will turn off the led strip") - self.colorWipe(self.pixels, Color(0,0,0)) + self.colorWipe(self.pixels, Color(0, 0, 0)) elif self.light_state ^ self._settings.get([INVERTED_OUTPUT]): GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.HIGH) else: @@ -155,7 +154,6 @@ def change_light_state(self, channel): self.light_state )) - def get_update_information(self): return dict( octolight=dict( @@ -170,7 +168,7 @@ def get_update_information(self): pip="https://github.com/emouty/OctoLight/archive/{target}.zip" ) ) - + def on_shutdown(self): # release GPIO pin on shutdown GPIO.cleanup() @@ -182,6 +180,17 @@ def colorWipe(self, strip, color): strip.setPixelColor(i, color) strip.show() + def setup_pin(self): + if bool(self._settings.get([IS_LED_STRIP])): + + # enabling led strip on selected pin + self.pixels = PixelStrip(int(self._settings.get([NB_LEDS])), int(self._settings.get( + [LIGHT_PIN])), LED_FREQ_HZ, LED_DMA, bool(self._settings.get([INVERTED_OUTPUT])), LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP) + self.pixels.begin() + else: + # Sets the GPIO every time, if user changed it in the settings. + GPIO.setup(int(self._settings.get([LIGHT_PIN])), GPIO.OUT) + __plugin_pythoncompat__ = ">=2.7,<4" __plugin_implementation__ = OctoLightPlugin() From 2b58f26db79c8a10994509918c388b296ebaf0ce Mon Sep 17 00:00:00 2001 From: emouty Date: Sun, 21 Mar 2021 09:42:13 +0100 Subject: [PATCH 05/13] fix: remove extra parentheses --- octoprint_octolight/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/octoprint_octolight/__init__.py b/octoprint_octolight/__init__.py index 06245e7..6dee2f5 100755 --- a/octoprint_octolight/__init__.py +++ b/octoprint_octolight/__init__.py @@ -85,7 +85,7 @@ def on_after_startup(self): # Setting the default state of the light pin self.setup_pin() if not bool(self._settings.get([IS_LED_STRIP])): - if bool(self._settings.get([INVERTED_OUTPUT]))): + if bool(self._settings.get([INVERTED_OUTPUT])): GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.HIGH) else: GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.LOW) From 25dec649531024a50b720170d518911efb2a2bdf Mon Sep 17 00:00:00 2001 From: emouty Date: Sun, 21 Mar 2021 13:03:51 +0100 Subject: [PATCH 06/13] chore: add precisions to the settings page --- octoprint_octolight/templates/octolight_settings.jinja2 | 1 + 1 file changed, 1 insertion(+) diff --git a/octoprint_octolight/templates/octolight_settings.jinja2 b/octoprint_octolight/templates/octolight_settings.jinja2 index 68214fb..9019dd6 100755 --- a/octoprint_octolight/templates/octolight_settings.jinja2 +++ b/octoprint_octolight/templates/octolight_settings.jinja2 @@ -1,6 +1,7 @@

OctoLight settings

+

If LED strip has been selected ensure that the pin selected is either a PWM, PCM or SPI pin

From 68c81a0883fb445439eb8bd6ad442c8ae28de694 Mon Sep 17 00:00:00 2001 From: emouty Date: Sun, 21 Mar 2021 13:04:27 +0100 Subject: [PATCH 07/13] chore: update readme with new parameters --- README.md | 58 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ea50d92..30ae1fe 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # OctoLight -A simple plugin that adds a button to the navigation bar for toggleing a GPIO pin on the Raspberry Pi. +A simple plugin that adds a button to the navigation bar for toggling a GPIO pin on the Raspberry Pi. ![WebUI interface](img/screenshoot.png) @@ -12,18 +12,60 @@ or manually using this URL: ## Configuration ![Settings panel](img/settings.png) -Curently, you can configure two settings: +Currently, you can configure two settings: - `Light PIN`: The pin on the Raspberry Pi that the button controls. - - Default value: 13 - - The pin number is saved in the **board layout naming** scheme (gray labels on the pinout image below). - - **!! IMPORTANT !!** The Raspberry Pi can only controll the **GPIO** pins (orange labels on the pinout image below) + - Default value: 19 + - The pin number is saved in the **BCM layout naming** scheme (orange labels on the pinout image below). + - **!! IMPORTANT !!** The Raspberry Pi can only control th **GPIO** pins (orange labels on the pinout image below) ![Raspberry Pi GPIO](img/rpi_gpio.png) - `Inverted output`: If true, the output will be inverted - - Usage: if you have a light, that is turned off when voltage is applied to the pin (wired in negative logic), you should turn on this option, so the light isn't on when you reboot your Raspberry Pi. - + - Usage: if you have a light, that is turned off when voltage is applied to the pin (wired in negative logic), you + should turn on this option, so the light isn't on when you reboot your Raspberry Pi. +- `Enable physical button`: If true, you will be able to turn on and off the light from a physical button wired to +the Raspberry Pi GPIO + - Default value: False +- `Button PIN`: The pin on the Raspberry Pi to read value from for the physical button. + - Default value: 2 +- `Led Strip`: if checked, enable the support for a led strip on the `Light PIN` + - Default value: False + - Before using this option, you should follow the [guide bellow](#setup-spi) + - Usage: theoretically it should work on a PWM, PCM or SPI pin but it has only been tested on the SPI pin. +- `Number of leds`: Number of leds on the led strip. + - Default value: 20 + +## Setup SPI +**Only needed if you use the LED strip option** +1. Add the pi user to the gpio group. + ```shell + sudo adduser pi gpio + ``` +2. Enable SPI. The plugin uses SPI to drive the LEDs, which is disabled by default and needs to be turned on. + - `Adds dtparam=spi=on to /boot/config.txt` +3. Increase SPI buffer size. Whilst the plugin will work without this, it will only work well with a handful of LEDs. + - `Adds spidev.bufsize=32768 to the end of /boot/cmdline.txt` +4. Set compatible clock frequency Raspberry Pi 3 or earlier only, not required for a Pi 4 The Pi 3's default internal +clock frequency is not compatible with SPI, so it needs to be set to 250 to be compatible. + - `Adds core_freq=250 to /boot/config.txt` +5. Set a minimum clock frequency Raspberry Pi 4 only On a Raspberry Pi 4, the clock frequency is dynamic and can change +when the pi is 'idle' vs. 'working', which causes LEDs to flicker, change colour, or stop working completely. By setting + a minimum the same as the max, we stop this dynamic clocking. + - `Adds core_freq_min=500 to /boot/config.txt` +6. reboot the pi + ```shell + sudo reboot + ``` + +_[source](https://cp2004.gitbook.io/ws281x-led-status/guides/setup-guide-1/spi-setup)_ + ## TO DO - [x] Update interface if Light is turned on or off +- [ ] Use wizard to setup the led strip library like on [OctoPrint-WS281x_LED_Status](https://github.com/cp2004/OctoPrint-WS281x_LED_Status) -Maybe in the distant future: +### Maybe in the distant future: - [ ] Turn off on finish print + +## Thanks + +Thanks to [cp2004](https://github.com/cp2004) for its documentation to setup the SPI interface to be used by the + [rpi_ws281x](https://github.com/jgarff/rpi_ws281x) library \ No newline at end of file From b1f34ecee9e41ff113a3e79bab4dd477bacb43fb Mon Sep 17 00:00:00 2001 From: emouty Date: Sun, 21 Mar 2021 13:04:41 +0100 Subject: [PATCH 08/13] feat: release 0.2.0 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 70a0314..4a0fe91 100755 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ plugin_name = "OctoLight" # The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module -plugin_version = "0.2.0-SNAPSHOT" +plugin_version = "0.2.0" # The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin # module From 1ca08c408f1718848055d7531c53f7582f1e5856 Mon Sep 17 00:00:00 2001 From: emouty Date: Sun, 21 Mar 2021 13:24:29 +0100 Subject: [PATCH 09/13] fix: force BCM mode --- octoprint_octolight/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/octoprint_octolight/__init__.py b/octoprint_octolight/__init__.py index 6dee2f5..2bced12 100755 --- a/octoprint_octolight/__init__.py +++ b/octoprint_octolight/__init__.py @@ -9,8 +9,8 @@ from rpi_ws281x import Color, PixelStrip, ws import RPi.GPIO as GPIO -# GPIO.cleanup() -# GPIO.setmode(GPIO.BOARD) + +GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) # constants From 056533aaf55ca85d2839a50bb0a7b89be16ca7e8 Mon Sep 17 00:00:00 2001 From: emouty Date: Sun, 21 Mar 2021 13:27:52 +0100 Subject: [PATCH 10/13] fix: readme number of settings --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 30ae1fe..6995ce7 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ or manually using this URL: ## Configuration ![Settings panel](img/settings.png) -Currently, you can configure two settings: +Currently, you can configure theses settings: - `Light PIN`: The pin on the Raspberry Pi that the button controls. - Default value: 19 - The pin number is saved in the **BCM layout naming** scheme (orange labels on the pinout image below). From 1a03ec9078b9717bce9eae6010a6515d87e61506 Mon Sep 17 00:00:00 2001 From: emouty Date: Sat, 3 Apr 2021 16:47:48 +0200 Subject: [PATCH 11/13] fix: missing comma --- octoprint_octolight/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/octoprint_octolight/__init__.py b/octoprint_octolight/__init__.py index 2bced12..72b3db6 100755 --- a/octoprint_octolight/__init__.py +++ b/octoprint_octolight/__init__.py @@ -35,8 +35,8 @@ class OctoLightPlugin( octoprint.plugin.SimpleApiPlugin, octoprint.plugin.SettingsPlugin, octoprint.plugin.EventHandlerPlugin, - octoprint.plugin.RestartNeedingPlugin - octoprint.plugin.ShutdownPlugin + octoprint.plugin.RestartNeedingPlugin, + octoprint.plugin.ShutdownPlugin ): # variables From 81cd2d11260fe02bc63edca8bb16cd6d710940ae Mon Sep 17 00:00:00 2001 From: emouty Date: Sat, 3 Apr 2021 16:52:13 +0200 Subject: [PATCH 12/13] chore: use tabs instead of space for indentation --- octoprint_octolight/__init__.py | 240 ++++++++++++++++---------------- 1 file changed, 120 insertions(+), 120 deletions(-) diff --git a/octoprint_octolight/__init__.py b/octoprint_octolight/__init__.py index 72b3db6..1dd4198 100755 --- a/octoprint_octolight/__init__.py +++ b/octoprint_octolight/__init__.py @@ -36,22 +36,22 @@ class OctoLightPlugin( octoprint.plugin.SettingsPlugin, octoprint.plugin.EventHandlerPlugin, octoprint.plugin.RestartNeedingPlugin, - octoprint.plugin.ShutdownPlugin + octoprint.plugin.ShutdownPlugin ): - # variables - light_state = False - pixels = None + # variables + light_state = False + pixels = None - def get_settings_defaults(self): - return dict( - light_pin=19, - button_pin=2, - previous_button_pin=2, - inverted_output=False, - is_led_strip=False, - nb_leds=20, - ) + def get_settings_defaults(self): + return dict( + light_pin=19, + button_pin=2, + previous_button_pin=2, + inverted_output=False, + is_led_strip=False, + nb_leds=20, + ) def get_template_configs(self): return [ @@ -64,56 +64,56 @@ def get_assets(self): # core UI here. return dict( js=["js/octolight.js"], - css=["css/octolight.css"], + css=["css/octolight.css"] #less=["less/octolight.less"] ) - def on_after_startup(self): - self.light_state = False - self._logger.info("--------------------------------------------") - self._logger.info("OctoLight started, listening for GET request") - self._logger.debug("GPIO Mode: {}".format( - GPIO.getmode() - )) - self._logger.info("Light pin: {}, inverted_input: {}, button pin: {}".format( - self._settings.get([LIGHT_PIN]), - self._settings.get([INVERTED_OUTPUT]), - self._settings.get([BUTTON_PIN]) - )) - self._logger.info("--------------------------------------------") - - # Setting the default state of the light pin - self.setup_pin() - if not bool(self._settings.get([IS_LED_STRIP])): - if bool(self._settings.get([INVERTED_OUTPUT])): - GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.HIGH) - else: - GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.LOW) + def on_after_startup(self): + self.light_state = False + self._logger.info("--------------------------------------------") + self._logger.info("OctoLight started, listening for GET request") + self._logger.debug("GPIO Mode: {}".format( + GPIO.getmode() + )) + self._logger.info("Light pin: {}, inverted_input: {}, button pin: {}".format( + self._settings.get([LIGHT_PIN]), + self._settings.get([INVERTED_OUTPUT]), + self._settings.get([BUTTON_PIN]) + )) + self._logger.info("--------------------------------------------") + + # Setting the default state of the light pin + self.setup_pin() + if not bool(self._settings.get([IS_LED_STRIP])): + if bool(self._settings.get([INVERTED_OUTPUT])): + GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.HIGH) + else: + GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.LOW) self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state)) - # Enabling watch to the default button pin - self.enable_watch_button(self._settings.get([BUTTON_PIN])) + # Enabling watch to the default button pin + self.enable_watch_button(self._settings.get([BUTTON_PIN])) - def on_api_get(self, request): - self._logger.info("Got request. Light state: {}".format( - self.light_state - )) + def on_api_get(self, request): + self._logger.info("Got request. Light state: {}".format( + self.light_state + )) - if self._settings.get([BUTTON_PIN]) != self._settings.get([PREVIOUS_BUTTON_PIN]): - # stop watching on the previous pin - GPIO.remove_event_detect( - self._settings.get([PREVIOUS_BUTTON_PIN])) - # enable watching on the new pin - self.enable_watch_button(self._settings.get([BUTTON_PIN])) - self._settings.set([PREVIOUS_BUTTON_PIN], - self._settings.get([BUTTON_PIN])) + if self._settings.get([BUTTON_PIN]) != self._settings.get([PREVIOUS_BUTTON_PIN]): + # stop watching on the previous pin + GPIO.remove_event_detect( + self._settings.get([PREVIOUS_BUTTON_PIN])) + # enable watching on the new pin + self.enable_watch_button(self._settings.get([BUTTON_PIN])) + self._settings.set([PREVIOUS_BUTTON_PIN], + self._settings.get([BUTTON_PIN])) - self.setup_pin() + self.setup_pin() - self.change_light_state(None) + self.change_light_state(None) - self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state)) + self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state)) return flask.jsonify(status="ok") @@ -124,77 +124,77 @@ def on_event(self, event, payload): return - def enable_watch_button(self, button): - self._logger.info("watching events on pin : {}".format( - button - )) - GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP) - GPIO.add_event_detect(button, GPIO.FALLING, - callback=self.change_light_state, bouncetime=200) - - def change_light_state(self, channel): - self.light_state = not self.light_state - - # Sets the light state depending on the inverted output setting (XOR) - if bool(self._settings.get([IS_LED_STRIP])): - if self.light_state: - self._logger.debug( - "Led strip mode is enabled, will turn on the led strip") - self.colorWipe(self.pixels, Color(255, 255, 255)) - else: - self._logger.debug( - "Led strip mode is enabled, will turn off the led strip") - self.colorWipe(self.pixels, Color(0, 0, 0)) - elif self.light_state ^ self._settings.get([INVERTED_OUTPUT]): - GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.HIGH) - else: - GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.LOW) - - self._logger.debug("Light state switched to : {}".format( - self.light_state - )) - - def get_update_information(self): - return dict( - octolight=dict( - displayName="OctoLight", - displayVersion=self._plugin_version, - - type="github_release", - current=self._plugin_version, - - user="emouty", - repo="OctoLight", - pip="https://github.com/emouty/OctoLight/archive/{target}.zip" - ) - ) - - def on_shutdown(self): - # release GPIO pin on shutdown - GPIO.cleanup() - - # Define functions which animate LEDs in various ways. - def colorWipe(self, strip, color): - """Wipe color across display a pixel at a time.""" - for i in range(strip.numPixels()): - strip.setPixelColor(i, color) - strip.show() - - def setup_pin(self): - if bool(self._settings.get([IS_LED_STRIP])): - - # enabling led strip on selected pin - self.pixels = PixelStrip(int(self._settings.get([NB_LEDS])), int(self._settings.get( - [LIGHT_PIN])), LED_FREQ_HZ, LED_DMA, bool(self._settings.get([INVERTED_OUTPUT])), LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP) - self.pixels.begin() - else: - # Sets the GPIO every time, if user changed it in the settings. - GPIO.setup(int(self._settings.get([LIGHT_PIN])), GPIO.OUT) + def enable_watch_button(self, button): + self._logger.info("watching events on pin : {}".format( + button + )) + GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP) + GPIO.add_event_detect(button, GPIO.FALLING, + callback=self.change_light_state, bouncetime=200) + + def change_light_state(self, channel): + self.light_state = not self.light_state + + # Sets the light state depending on the inverted output setting (XOR) + if bool(self._settings.get([IS_LED_STRIP])): + if self.light_state: + self._logger.debug( + "Led strip mode is enabled, will turn on the led strip") + self.colorWipe(self.pixels, Color(255, 255, 255)) + else: + self._logger.debug( + "Led strip mode is enabled, will turn off the led strip") + self.colorWipe(self.pixels, Color(0, 0, 0)) + elif self.light_state ^ self._settings.get([INVERTED_OUTPUT]): + GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.HIGH) + else: + GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.LOW) + + self._logger.debug("Light state switched to : {}".format( + self.light_state + )) + + def get_update_information(self): + return dict( + octolight=dict( + displayName="OctoLight", + displayVersion=self._plugin_version, + + type="github_release", + current=self._plugin_version, + + user="emouty", + repo="OctoLight", + pip="https://github.com/emouty/OctoLight/archive/{target}.zip" + ) + ) + + def on_shutdown(self): + # release GPIO pin on shutdown + GPIO.cleanup() + + # Define functions which animate LEDs in various ways. + def colorWipe(self, strip, color): + """Wipe color across display a pixel at a time.""" + for i in range(strip.numPixels()): + strip.setPixelColor(i, color) + strip.show() + + def setup_pin(self): + if bool(self._settings.get([IS_LED_STRIP])): + + # enabling led strip on selected pin + self.pixels = PixelStrip(int(self._settings.get([NB_LEDS])), int(self._settings.get( + [LIGHT_PIN])), LED_FREQ_HZ, LED_DMA, bool(self._settings.get([INVERTED_OUTPUT])), LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP) + self.pixels.begin() + else: + # Sets the GPIO every time, if user changed it in the settings. + GPIO.setup(int(self._settings.get([LIGHT_PIN])), GPIO.OUT) __plugin_pythoncompat__ = ">=2.7,<4" __plugin_implementation__ = OctoLightPlugin() __plugin_hooks__ = { - "octoprint.plugin.softwareupdate.check_config": - __plugin_implementation__.get_update_information + "octoprint.plugin.softwareupdate.check_config": + __plugin_implementation__.get_update_information } From 9f5005d18b3cdc3107f36c8eab419f380556b850 Mon Sep 17 00:00:00 2001 From: emouty Date: Sat, 3 Apr 2021 17:04:41 +0200 Subject: [PATCH 13/13] feat: adapt UI light state to change when external button is pressed --- octoprint_octolight/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/octoprint_octolight/__init__.py b/octoprint_octolight/__init__.py index 1dd4198..38da51c 100755 --- a/octoprint_octolight/__init__.py +++ b/octoprint_octolight/__init__.py @@ -31,12 +31,12 @@ class OctoLightPlugin( octoprint.plugin.AssetPlugin, octoprint.plugin.StartupPlugin, + octoprint.plugin.ShutdownPlugin, octoprint.plugin.TemplatePlugin, octoprint.plugin.SimpleApiPlugin, octoprint.plugin.SettingsPlugin, octoprint.plugin.EventHandlerPlugin, - octoprint.plugin.RestartNeedingPlugin, - octoprint.plugin.ShutdownPlugin + octoprint.plugin.RestartNeedingPlugin ): # variables @@ -113,13 +113,10 @@ def on_api_get(self, request): self.change_light_state(None) - self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state)) - - return flask.jsonify(status="ok") def on_event(self, event, payload): - self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state)) + self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state)) if event == Events.CLIENT_OPENED: return @@ -153,6 +150,9 @@ def change_light_state(self, channel): self._logger.debug("Light state switched to : {}".format( self.light_state )) + # message the ui to change the button color + self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state)) + def get_update_information(self): return dict(