Skip to content

Commit e4c26e1

Browse files
committed
No more segfaults.
Add status page in the API from original plugin. Replace dict conversion with actual dict.... Swap circuit state settings, that isn't how circuits work...
1 parent 8c5daf8 commit e4c26e1

File tree

2 files changed

+58
-42
lines changed

2 files changed

+58
-42
lines changed

octoprint_filamentsensorng/__init__.py

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# coding=utf-8
22
from __future__ import absolute_import
3+
from flask import jsonify
34

45
import octoprint.plugin
56
from octoprint.events import Events
@@ -17,14 +18,15 @@ def initialize(self):
1718
if GPIO.VERSION < "0.6": # Need at least 0.6 for edge detection
1819
raise Exception("RPi.GPIO must be greater than 0.6")
1920
GPIO.setwarnings(False) # Disable GPIO warnings
21+
self.filamentsensorngPlugin_confirmations_tracking = 0
2022

2123
@property
2224
def pin(self):
2325
return int(self._settings.get(["pin"]))
2426

2527
@property
26-
def bounce(self):
27-
return int(self._settings.get(["bounce"]))
28+
def poll_time(self):
29+
return int(self._settings.get(["poll_time"]))
2830

2931
@property
3032
def switch(self):
@@ -65,15 +67,15 @@ def on_after_startup(self):
6567
self._setup_sensor()
6668

6769
def get_settings_defaults(self):
68-
return dict(
69-
pin = -1, # Default is no pin
70-
bounce = 250, # Debounce 250ms
71-
switch = 0, # Normally Open
72-
mode = 0, # Board Mode
73-
confirmations=0,# Confirm that we're actually out of filament
74-
no_filament_gcode = '',
75-
pause_print = True,
76-
)
70+
return({
71+
'pin':-1, # Default is no pin
72+
'poll_time':250, # Debounce 250ms
73+
'switch':0, # Normally Open
74+
'mode':0, # Board Mode
75+
'confirmations':5,# Confirm that we're actually out of filament
76+
'no_filament_gcode':'',
77+
'pause_print':True,
78+
})
7779

7880
def on_settings_save(self, data):
7981
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
@@ -100,13 +102,16 @@ def on_event(self, event, payload):
100102
Events.PRINT_RESUMED
101103
):
102104
self._logger.info("%s: Enabling filament sensor." % (event))
103-
# if self.sensor_enabled():
104-
# GPIO.remove_event_detect(self.pin)
105-
# GPIO.add_event_detect(
106-
# self.pin, GPIO.BOTH,
107-
# callback=self.sensor_callback,
108-
# bouncetime=self.bounce
109-
# )
105+
if self.sensor_enabled():
106+
self._logger.info(1)
107+
GPIO.remove_event_detect(self.pin)
108+
self._logger.info(2)
109+
GPIO.add_event_detect(
110+
self.pin, GPIO.BOTH,
111+
callback=self.sensor_callback,
112+
bouncetime=self.poll_time
113+
)
114+
self._logger.info(3)
110115
# Disable sensor
111116
elif event in (
112117
Events.PRINT_DONE,
@@ -117,42 +122,54 @@ def on_event(self, event, payload):
117122
self._logger.info("%s: Disabling filament sensor." % (event))
118123
GPIO.remove_event_detect(self.pin)
119124

125+
@octoprint.plugin.BlueprintPlugin.route("/status", methods=["GET"])
126+
def check_status(self):
127+
status = "-1"
128+
if self.pin != -1:
129+
status = str(self.no_filament())
130+
return jsonify( status = status )
131+
120132
def sensor_callback(self, _):
121-
sleep(self.bounce/1000)
133+
sleep(self.poll_time/1000)
134+
self._logger.info('Pin: '+str(GPIO.input(self.pin)))
122135
if self.no_filament():
123-
self._logger.info("Out of filament!")
124-
if self.pause_print:
125-
self._logger.info("Pausing print.")
126-
self._printer.pause_print()
127-
if self.no_filament_gcode:
128-
self._logger.info("Sending out of filament GCODE")
129-
self._printer.commands(self.no_filament_gcode)
136+
self._logger.info('Confirmations: '+str(self.filamentsensorngPlugin_confirmations_tracking))
137+
self.filamentsensorngPlugin_confirmations_tracking+=1
138+
if self.confirmations<=self.filamentsensorngPlugin_confirmations_tracking:
139+
self._logger.info("Out of filament!")
140+
if self.pause_print:
141+
self._logger.info("Pausing print.")
142+
self._printer.pause_print()
143+
if self.no_filament_gcode:
144+
self._logger.info("Sending out of filament GCODE")
145+
self._printer.commands(self.no_filament_gcode)
146+
self.filamentsensorngPlugin_confirmations_tracking = 0
130147
else:
131-
self._logger.info("Filament detected!")
148+
self.filamentsensorngPlugin_confirmations_tracking = 0
132149

133150
def get_update_information(self):
134151
return dict(
135152
octoprint_filament=dict(
136-
displayName="Filament Sensor Reloaded",
153+
displayName="Filament Sensor NG",
137154
displayVersion=self._plugin_version,
138155

139156
# version check: github repository
140157
type="github_release",
141-
user="kontakt",
142-
repo="Octoprint-Filament-Reloaded",
158+
user="Red-M",
159+
repo="Octoprint-Filament-Sensor-ng",
143160
current=self._plugin_version,
144161

145162
# update method: pip
146-
pip="https://github.com/kontakt/Octoprint-Filament-Reloaded/archive/{target_version}.zip"
163+
pip="https://github.com/Red-M/Octoprint-Filament-Sensor-ng/archive/{target_version}.zip"
147164
)
148165
)
149166

150-
__plugin_name__ = "Filament Sensor Reloaded"
151-
__plugin_version__ = "1.0.1"
167+
__plugin_name__ = "Filament Sensor NG"
168+
__plugin_version__ = "1.0.0"
152169

153170
def __plugin_load__():
154171
global __plugin_implementation__
155-
__plugin_implementation__ = FilamentReloadedPlugin()
172+
__plugin_implementation__ = filamentsensorngPlugin()
156173

157174
global __plugin_hooks__
158175
__plugin_hooks__ = {

octoprint_filamentsensorng/templates/filamentsensorng_settings.jinja2

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h4>{{ _('Filament Sensor Reloaded') }}</h4>
1+
<h4>{{ _('Filament Sensor NG') }}</h4>
22
<form class="form-horizontal">
33
<div class="control-group">
44
<label class="control-label">{{ _('Pin:') }}</label>
@@ -7,25 +7,24 @@
77
</div>
88
</div>
99
<div class="control-group">
10-
<label class="control-label">{{ _('Debounce Time:') }}</label>
11-
<div class="controls" data-toggle="tooltip" title="{{ _('When a sensor state change happens, wait for this amount of time to wait for the signal to stabilize.') }}">
12-
<input type="number" step="any" min="0" class="input-mini text-right" data-bind="value: settings.plugins.filamentsensorng.bounce">
10+
<label class="control-label">{{ _('Poll Time:') }}</label>
11+
<div class="controls" data-toggle="tooltip" title="{{ _('Poll the sensor this many milliseconds a part.') }}">
12+
<input type="number" step="any" min="0" class="input-mini text-right" data-bind="value: settings.plugins.filamentsensorng.poll_time">
1313
<span class="add-on">ms</span>
1414
</div>
1515
</div>
1616
<div class="control-group">
17-
<label class="control-label">{{ _('Debounce Time:') }}</label>
17+
<label class="control-label">{{ _('Event Confirmations:') }}</label>
1818
<div class="controls" data-toggle="tooltip" title="{{ _('When a sensor state change happens, wait for this amount of continous reads of the sensor not having filament to trigger.') }}">
1919
<input type="number" step="any" min="0" class="input-mini text-right" data-bind="value: settings.plugins.filamentsensorng.confirmations">
20-
<span class="add-on">ms</span>
2120
</div>
2221
</div>
2322
<div class="control-group">
2423
<label class="control-label">{{ _('Switch Type:') }}</label>
2524
<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.') }}">
2625
<select class="select-mini" data-bind="value: settings.plugins.filamentsensorng.switch">
27-
<option value=0>{{ _('Normally Open') }}</option>
28-
<option value=1>{{ _('Normally Closed') }}</option>
26+
<option value=1>{{ _('Normally Open') }}</option>
27+
<option value=0>{{ _('Normally Closed') }}</option>
2928
</select>
3029
</div>
3130
</div>

0 commit comments

Comments
 (0)