1
1
# coding=utf-8
2
2
from __future__ import absolute_import
3
+ from flask import jsonify
3
4
4
5
import octoprint .plugin
5
6
from octoprint .events import Events
@@ -17,14 +18,15 @@ def initialize(self):
17
18
if GPIO .VERSION < "0.6" : # Need at least 0.6 for edge detection
18
19
raise Exception ("RPi.GPIO must be greater than 0.6" )
19
20
GPIO .setwarnings (False ) # Disable GPIO warnings
21
+ self .filamentsensorngPlugin_confirmations_tracking = 0
20
22
21
23
@property
22
24
def pin (self ):
23
25
return int (self ._settings .get (["pin" ]))
24
26
25
27
@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 " ]))
28
30
29
31
@property
30
32
def switch (self ):
@@ -65,15 +67,15 @@ def on_after_startup(self):
65
67
self ._setup_sensor ()
66
68
67
69
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
+ } )
77
79
78
80
def on_settings_save (self , data ):
79
81
octoprint .plugin .SettingsPlugin .on_settings_save (self , data )
@@ -100,13 +102,16 @@ def on_event(self, event, payload):
100
102
Events .PRINT_RESUMED
101
103
):
102
104
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 )
110
115
# Disable sensor
111
116
elif event in (
112
117
Events .PRINT_DONE ,
@@ -117,42 +122,54 @@ def on_event(self, event, payload):
117
122
self ._logger .info ("%s: Disabling filament sensor." % (event ))
118
123
GPIO .remove_event_detect (self .pin )
119
124
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
+
120
132
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 )))
122
135
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
130
147
else :
131
- self ._logger . info ( "Filament detected!" )
148
+ self .filamentsensorngPlugin_confirmations_tracking = 0
132
149
133
150
def get_update_information (self ):
134
151
return dict (
135
152
octoprint_filament = dict (
136
- displayName = "Filament Sensor Reloaded " ,
153
+ displayName = "Filament Sensor NG " ,
137
154
displayVersion = self ._plugin_version ,
138
155
139
156
# version check: github repository
140
157
type = "github_release" ,
141
- user = "kontakt " ,
142
- repo = "Octoprint-Filament-Reloaded " ,
158
+ user = "Red-M " ,
159
+ repo = "Octoprint-Filament-Sensor-ng " ,
143
160
current = self ._plugin_version ,
144
161
145
162
# 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"
147
164
)
148
165
)
149
166
150
- __plugin_name__ = "Filament Sensor Reloaded "
151
- __plugin_version__ = "1.0.1 "
167
+ __plugin_name__ = "Filament Sensor NG "
168
+ __plugin_version__ = "1.0.0 "
152
169
153
170
def __plugin_load__ ():
154
171
global __plugin_implementation__
155
- __plugin_implementation__ = FilamentReloadedPlugin ()
172
+ __plugin_implementation__ = filamentsensorngPlugin ()
156
173
157
174
global __plugin_hooks__
158
175
__plugin_hooks__ = {
0 commit comments