Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kontakt committed Jan 6, 2017
0 parents commit 3bb69d0
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This file is for unifying the coding style for different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true

[**.py]
indent_style = tab

[**.js]
indent_style = space
indent_size = 4
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.pyc
*.swp
.idea
*.iml
build
dist
*.egg*
.DS_Store
*.zip
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include README.md
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# OctoPrint-FilamentReloaded

Based on the Octoprint-Filament plugin by MoonshineSG (https://github.com/MoonshineSG/Octoprint-Filament), this modification adds the ability to modify your configuration through OctoPrint settings, as well as adding configurations for both NO and NC switches.

Future developments are planned to include multiple filament sensors, pop-ups, pre-print validation and custom filament run-out scripting.

## Setup

Install via the bundled [Plugin Manager](https://github.com/foosel/OctoPrint/wiki/Plugin:-Plugin-Manager)
or manually using this URL:

https://github.com/kontakt/Octoprint-Filament-Reloaded/archive/master.zip

Using this plugin requires a filament sensor. The code is set to use the Raspberry Pi's internal Pull-Up resistors, so the switch should be between your detection pin and a ground pin.
6 changes: 6 additions & 0 deletions babel.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[python: */**.py]
[jinja2: */**.jinja2]
extensions=jinja2.ext.autoescape, jinja2.ext.with_

[javascript: */**.js]
extract_messages = gettext, ngettext
88 changes: 88 additions & 0 deletions octoprint_filamentreload/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# coding=utf-8
from __future__ import absolute_import

import octoprint.plugin
from octoprint.events import eventManager, Events
from flask import jsonify, make_response
import RPi.GPIO as GPIO

class FilamentReloadedPlugin(octoprint.plugin.StartupPlugin,
octoprint.plugin.EventHandlerPlugin,
octoprint.plugin.TemplatePlugin,
octoprint.plugin.SettingsPlugin):

def initialize(self):
self._logger.info("Running RPi.GPIO version '{0}'".format(GPIO.VERSION))
if GPIO.VERSION < "0.6": # Need at least 0.6 for edge detection
raise Exception("RPi.GPIO must be greater than 0.6")
GPIO.setmode(GPIO.BOARD) # Use the board numbering scheme
GPIO.setwarnings(False) # Disable GPIO warnings

def on_after_startup(self):
self._logger.info("Filament Sensor Reloaded started")
self.pin = int(self._settings.get(["pin"]))
self.bounce = int(self._settings.get(["bounce"]))
self.switch = int(self._settings.get(["switch"]))

if self._settings.get(["pin"]) != -1: # If a pin is defined
self._logger.info("Filament Sensor active on GPIO Pin [%s]"%self.pin)
GPIO.setup(self.pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Initialize GPIO as INPUT

def get_settings_defaults(self):
return dict(
pin = -1, # Default is no pin
bounce = 250, # Debounce 250ms
switch = 0 # Normally Open
)

def get_template_configs(self):
return [dict(type="settings", custom_bindings=False)]

def on_event(self, event, payload):
if event == Events.PRINT_STARTED: # If a new print is beginning
self._logger.info("Printing started: Filament sensor enabled")
if self.pin != -1:
GPIO.add_event_detect(self.pin, GPIO.BOTH, callback=self.check_gpio, bouncetime=self.bounce)
elif event in (Events.PRINT_DONE, Events.PRINT_FAILED, Events.PRINT_CANCELLED):
self._logger.info("Printing stopped: Filament sensor disabled")
try:
GPIO.remove_event_detect(self.pin)
except Exception:
pass

def check_gpio(self, channel):
state = GPIO.input(self.pin)
self._logger.debug("Detected sensor [%s] state [%s]"%(channel, state))
if state != self.switch: # If the sensor is tripped
self._logger.debug("Sensor [%s]"%state)
if self._printer.is_printing():
self._printer.toggle_pause_print()

def get_update_information(self):
return dict(
octoprint_filament=dict(
displayName="Filament Sensor Reloaded",
displayVersion=self._plugin_version,

# version check: github repository
type="github_release",
user="kontakt",
repo="Octoprint-Filament-Reloaded",
current=self._plugin_version,

# update method: pip
pip="https://github.com/kontakt/Octoprint-Filament-Reloaded/archive/{target_version}.zip"
)
)

__plugin_name__ = "Filament Sensor Reloaded"
__plugin_version__ = "1.0.0"

def __plugin_load__():
global __plugin_implementation__
__plugin_implementation__ = FilamentReloadedPlugin()

global __plugin_hooks__
__plugin_hooks__ = {
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information
}
27 changes: 27 additions & 0 deletions octoprint_filamentreload/templates/filamentreload_settings.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<h4>{{ _('Filament Sensor') }}</h4>
Define the pin used by your sensor and the debounce timing for false positive prevention.
<br>
<form class="form-horizontal">
<div class="control-group">
<label class="control-label">{{ _('Pin:') }}</label>
<div class="controls" data-toggle="tooltip" title="{{ _('Which GPIO pin your switch is attached to') }}">
<input type="number" step="any" min="0" class="input-mini text-right" data-bind="value: settings.plugins.filamentreload.pin">
</div>
</div>
<div class="control-group">
<label class="control-label">{{ _('Debounce Time:') }}</label>
<div class="controls" data-toggle="tooltip" title="{{ _('The amount of time a signal needs to stay constant to be considered valid') }}">
<input type="number" step="any" min="0" class="input-mini text-right" data-bind="value: settings.plugins.filamentreload.bounce">
<span class="add-on">ms</span>
</div>
</div>
<div class="control-group">
<label class="control-label">{{ _('Switch Type:') }}</label>
<div class="controls" data-toggle="tooltip" title="{{ _('Whether the sensor should trip when the switch goes HIGH or LOW, Normally Open is LOW when filament is present, and Normally Closed is HIGH when filament is present') }}">
<select class="select-mini" data-bind="value: settings.plugins.filamentreload.switch">
<option value=0>{{ _('Normally Open') }}</option>
<option value=1>{{ _('Normally Closed') }}</option>
</select>
</div>
</div>
</form>
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
###
# This file is only here to make sure that something like
#
# pip install -e .
#
# works as expected. Requirements can be found in setup.py.
###

.
94 changes: 94 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# coding=utf-8

########################################################################################################################
### Do not forget to adjust the following variables to your own plugin.

# The plugin's identifier, has to be unique
plugin_identifier = "filamentreload"

# The plugin's python package, should be "octoprint_<plugin identifier>", has to be unique
plugin_package = "octoprint_filamentreload"

# The plugin's human readable name. Can be overwritten within OctoPrint's internal data via __plugin_name__ in the
# plugin module
plugin_name = "Octoprint-FilamentReload"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "1.0.0"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
plugin_description = """A revamped and rewritten filament monitor that pauses the print when your filament runs out"""

# The plugin's author. Can be overwritten within OctoPrint's internal data via __plugin_author__ in the plugin module
plugin_author = "Connor Huffine"

# The plugin's author's mail address.
plugin_author_email = "[email protected]"

# The plugin's homepage URL. Can be overwritten within OctoPrint's internal data via __plugin_url__ in the plugin module
plugin_url = "https://github.com/kontakt/Octoprint-Filament-Reloaded"

# The plugin's license. Can be overwritten within OctoPrint's internal data via __plugin_license__ in the plugin module
plugin_license = "AGPLv3"

# Any additional requirements besides OctoPrint should be listed here
plugin_requires = []

### --------------------------------------------------------------------------------------------------------------------
### More advanced options that you usually shouldn't have to touch follow after this point
### --------------------------------------------------------------------------------------------------------------------

# Additional package data to install for this plugin. The subfolders "templates", "static" and "translations" will
# already be installed automatically if they exist.
plugin_additional_data = []

# Any additional python packages you need to install with your plugin that are not contains in <plugin_package>.*
plugin_addtional_packages = []

# Any python packages within <plugin_package>.* you do NOT want to install with your plugin
plugin_ignored_packages = []

# Additional parameters for the call to setuptools.setup. If your plugin wants to register additional entry points,
# define dependency links or other things like that, this is the place to go. Will be merged recursively with the
# default setup parameters as provided by octoprint_setuptools.create_plugin_setup_parameters using
# octoprint.util.dict_merge.
#
# Example:
# plugin_requires = ["someDependency==dev"]
# additional_setup_parameters = {"dependency_links": ["https://github.com/someUser/someRepo/archive/master.zip#egg=someDependency-dev"]}
additional_setup_parameters = {}

########################################################################################################################

from setuptools import setup

try:
import octoprint_setuptools
except:
print("Could not import OctoPrint's setuptools, are you sure you are running that under "
"the same python installation that OctoPrint is installed under?")
import sys
sys.exit(-1)

setup_parameters = octoprint_setuptools.create_plugin_setup_parameters(
identifier=plugin_identifier,
package=plugin_package,
name=plugin_name,
version=plugin_version,
description=plugin_description,
author=plugin_author,
mail=plugin_author_email,
url=plugin_url,
license=plugin_license,
requires=plugin_requires,
additional_packages=plugin_addtional_packages,
ignored_packages=plugin_ignored_packages,
additional_data=plugin_additional_data
)

if len(additional_setup_parameters):
from octoprint.util import dict_merge
setup_parameters = dict_merge(setup_parameters, additional_setup_parameters)

setup(**setup_parameters)

0 comments on commit 3bb69d0

Please sign in to comment.