diff --git a/octoprint_filamentsensorng/__init__.py b/octoprint_filamentsensorng/__init__.py index 3edf1c2..1e08f8c 100644 --- a/octoprint_filamentsensorng/__init__.py +++ b/octoprint_filamentsensorng/__init__.py @@ -5,8 +5,7 @@ import octoprint.plugin from octoprint.events import Events import RPi.GPIO as GPIO -from time import sleep - +from time import sleep, time class filamentsensorngPlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.EventHandlerPlugin, @@ -52,6 +51,22 @@ def no_filament_gcode(self): def pause_print(self): return self._settings.get_boolean(["pause_print"]) + @property + def send_gcode_only_once(self): + return self._settings.get_boolean(["send_gcode_only_once"]) + + @property + def trigger_timeout(self): + return int(self._settings.get(["trigger_timeout"])) + + @property + def triggered_at(self): + return self._triggered_at + + @triggered_at.setter + def triggered_at(self, value): + self._triggered_at = int(value) + def _setup_sensor(self): if self.sensor_enabled(): self._logger.info("Setting up sensor.") @@ -80,6 +95,8 @@ def get_settings_defaults(self): 'no_filament_gcode':'', 'debug_mode':0, # Debug off! 'pause_print':True, + 'send_gcode_only_once':False, + 'trigger_timeout':300, }) def debug_only_output(self, string): @@ -112,6 +129,7 @@ def on_event(self, event, payload): ): self._logger.info("%s: Enabling filament sensor." % (event)) if self.sensor_enabled(): + self.triggered_at = 0 GPIO.remove_event_detect(self.pin) GPIO.add_event_detect( self.pin, GPIO.BOTH, @@ -138,14 +156,26 @@ def check_status(self): def sensor_callback(self, _): sleep(self.poll_time/1000) self.debug_only_output('Pin: '+str(GPIO.input(self.pin))) + if self.no_filament(): - self.filamentsensorngPlugin_confirmations_tracking+=1 + self.filamentsensorngPlugin_confirmations_tracking += 1 self.debug_only_output('Confirmations: '+str(self.filamentsensorngPlugin_confirmations_tracking)) - if self.confirmations<=self.filamentsensorngPlugin_confirmations_tracking: + + if self.confirmations <= self.filamentsensorngPlugin_confirmations_tracking: self._logger.info("Out of filament!") + + # Check if we want to just end it once + if self.send_gcode_only_once: + if self.triggered_at > time() - self.trigger_timeout: + self._logger.info("Sensor callback already executed, waiting for timeout") + return + else: + self.triggered_at = time() + if self.pause_print: self._logger.info("Pausing print.") self._printer.pause_print() + if self.no_filament_gcode: self._logger.info("Sending out of filament GCODE") self._printer.commands(self.no_filament_gcode) @@ -171,7 +201,7 @@ def get_update_information(self): ) __plugin_name__ = "Filament Sensor NG" -__plugin_version__ = "1.0.2" +__plugin_version__ = "1.0.3" def __plugin_load__(): global __plugin_implementation__ diff --git a/octoprint_filamentsensorng/templates/filamentsensorng_settings.jinja2 b/octoprint_filamentsensorng/templates/filamentsensorng_settings.jinja2 index a6bca93..c93dd50 100644 --- a/octoprint_filamentsensorng/templates/filamentsensorng_settings.jinja2 +++ b/octoprint_filamentsensorng/templates/filamentsensorng_settings.jinja2 @@ -59,4 +59,17 @@ +