-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for a physical button and led strip #26
Open
emouty
wants to merge
14
commits into
gigibu5:master
Choose a base branch
from
emouty:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3721b41
feat: add update script to gitignore
emouty 7df1e56
feat: ignore vscode settings
emouty 949b767
feat: add led strip and physical button support
emouty 4202754
fix: init when led is already enabled
emouty 2b58f26
fix: remove extra parentheses
emouty 25dec64
chore: add precisions to the settings page
emouty 68c81a0
chore: update readme with new parameters
emouty b1f34ec
feat: release 0.2.0
emouty 1ca08c4
fix: force BCM mode
emouty 056533a
fix: readme number of settings
emouty 1a03ec9
fix: missing comma
emouty 81cd2d1
chore: use tabs instead of space for indentation
emouty 9f5005d
feat: adapt UI light state to change when external button is pressed
emouty 082796c
Merge branch 'rebase_on_upstream'
emouty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,6 @@ dist | |
*.egg* | ||
.DS_Store | ||
*.zip | ||
temp | ||
update.sh | ||
.vscode/settings.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,56 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
|
||
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.setmode(GPIO.BCM) | ||
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, | ||
octoprint.plugin.ShutdownPlugin, | ||
octoprint.plugin.TemplatePlugin, | ||
octoprint.plugin.SimpleApiPlugin, | ||
octoprint.plugin.SettingsPlugin, | ||
octoprint.plugin.EventHandlerPlugin, | ||
octoprint.plugin.RestartNeedingPlugin | ||
): | ||
|
||
# variables | ||
light_state = False | ||
pixels = None | ||
|
||
def get_settings_defaults(self): | ||
return dict( | ||
light_pin = 13, | ||
inverted_output = False | ||
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): | ||
|
@@ -39,69 +64,96 @@ 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.info("Light pin: {}, inverted_input: {}".format( | ||
self._settings.get(["light_pin"]), | ||
self._settings.get(["inverted_output"]) | ||
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 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) | ||
# 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)) | ||
|
||
#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 | ||
# Enabling watch to the default button pin | ||
self.enable_watch_button(self._settings.get([BUTTON_PIN])) | ||
|
||
self._logger.info("After Startup. 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])) | ||
|
||
self.setup_pin() | ||
|
||
self.change_light_state(None) | ||
|
||
return flask.jsonify(status="ok") | ||
|
||
def on_event(self, event, payload): | ||
self._plugin_manager.send_plugin_message(self._identifier, dict(isLightOn=self.light_state)) | ||
if event == Events.CLIENT_OPENED: | ||
return | ||
|
||
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) | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @emouty you need to wrap button in this line and below (or in L96 and 108) with int() otherwise an exception is thrown
|
||
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 self.light_state ^ self._settings.get(["inverted_output"]): | ||
GPIO.output(int(self._settings.get(["light_pin"])), GPIO.HIGH) | ||
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) | ||
GPIO.output(int(self._settings.get([LIGHT_PIN])), GPIO.LOW) | ||
|
||
self._logger.info("Got request. Light state: {}".format( | ||
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)) | ||
|
||
|
||
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)) | ||
return | ||
|
||
def get_update_information(self): | ||
return dict( | ||
octolight=dict( | ||
|
@@ -111,16 +163,38 @@ def get_update_information(self): | |
type="github_release", | ||
current=self._plugin_version, | ||
|
||
user="gigibu5", | ||
user="emouty", | ||
repo="OctoLight", | ||
pip="https://github.com/gigibu5/OctoLight/archive/{target}.zip" | ||
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 | ||
} | ||
__plugin_implementation__.get_update_information | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also here we need to wrap with int()