filament_switch_sensor: Execute events using reactor callbacks

This removes the possibility of re-entry to callers of  note_filament_present().

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2020-02-16 07:18:56 -05:00 committed by KevinOConnor
parent 740c0ae636
commit 9415071482
1 changed files with 5 additions and 4 deletions

View File

@ -76,6 +76,7 @@ class RunoutHelper:
self.gcode.run_script(prefix + template.render() + "\nM400") self.gcode.run_script(prefix + template.render() + "\nM400")
except Exception: except Exception:
logging.exception("Script running error") logging.exception("Script running error")
self.event_running = False
def set_enable(self, runout, insert): def set_enable(self, runout, insert):
if runout and insert: if runout and insert:
# both cannot be enabled # both cannot be enabled
@ -93,23 +94,23 @@ class RunoutHelper:
self.filament_present = state self.filament_present = state
return return
self.filament_present = state self.filament_present = state
self.event_running = True
if state: if state:
if self.insert_enabled: if self.insert_enabled:
# insert detected # insert detected
self.event_running = True
self.last_event_time = eventtime self.last_event_time = eventtime
logging.info( logging.info(
"Filament Sensor %s: insert event detected, Time %.2f" % "Filament Sensor %s: insert event detected, Time %.2f" %
(self.name, eventtime)) (self.name, eventtime))
self._insert_event_handler(eventtime) self.reactor.register_callback(self._insert_event_handler)
elif self.runout_enabled: elif self.runout_enabled:
# runout detected # runout detected
self.event_running = True
self.last_event_time = eventtime self.last_event_time = eventtime
logging.info( logging.info(
"Filament Sensor %s: runout event detected, Time %.2f" % "Filament Sensor %s: runout event detected, Time %.2f" %
(self.name, eventtime)) (self.name, eventtime))
self._runout_event_handler(eventtime) self.reactor.register_callback(self._runout_event_handler)
self.event_running = False
cmd_QUERY_FILAMENT_SENSOR_help = "Query the status of the Filament Sensor" cmd_QUERY_FILAMENT_SENSOR_help = "Query the status of the Filament Sensor"
def cmd_QUERY_FILAMENT_SENSOR(self, params): def cmd_QUERY_FILAMENT_SENSOR(self, params):
if self.filament_present: if self.filament_present: