diff options
Diffstat (limited to 'klippy/extras/filament_motion_sensor.py')
-rw-r--r-- | klippy/extras/filament_motion_sensor.py | 61 |
1 files changed, 36 insertions, 25 deletions
diff --git a/klippy/extras/filament_motion_sensor.py b/klippy/extras/filament_motion_sensor.py index dd476d28..71a01949 100644 --- a/klippy/extras/filament_motion_sensor.py +++ b/klippy/extras/filament_motion_sensor.py @@ -6,18 +6,18 @@ import logging from . import filament_switch_sensor -CHECK_RUNOUT_TIMEOUT = .250 +CHECK_RUNOUT_TIMEOUT = 0.250 + class EncoderSensor: def __init__(self, config): # Read config self.printer = config.get_printer() - switch_pin = config.get('switch_pin') - self.extruder_name = config.get('extruder') - self.detection_length = config.getfloat( - 'detection_length', 7., above=0.) + switch_pin = config.get("switch_pin") + self.extruder_name = config.get("extruder") + self.detection_length = config.getfloat("detection_length", 7.0, above=0.0) # Configure pins - buttons = self.printer.load_object(config, 'buttons') + buttons = self.printer.load_object(config, "buttons") buttons.register_buttons([switch_pin], self.encoder_event) # Get printer objects self.reactor = self.printer.get_reactor() @@ -28,44 +28,54 @@ class EncoderSensor: # Initialise internal state self.filament_runout_pos = None # Register commands and event handlers - self.printer.register_event_handler('klippy:ready', - self._handle_ready) - self.printer.register_event_handler('idle_timeout:printing', - self._handle_printing) - self.printer.register_event_handler('idle_timeout:ready', - self._handle_not_printing) - self.printer.register_event_handler('idle_timeout:idle', - self._handle_not_printing) + self.printer.register_event_handler("klippy:ready", self._handle_ready) + self.printer.register_event_handler( + "idle_timeout:printing", self._handle_printing + ) + self.printer.register_event_handler( + "idle_timeout:ready", self._handle_not_printing + ) + self.printer.register_event_handler( + "idle_timeout:idle", self._handle_not_printing + ) + def _update_filament_runout_pos(self, eventtime=None): if eventtime is None: eventtime = self.reactor.monotonic() self.filament_runout_pos = ( - self._get_extruder_pos(eventtime) + - self.detection_length) + self._get_extruder_pos(eventtime) + self.detection_length + ) + def _handle_ready(self): self.extruder = self.printer.lookup_object(self.extruder_name) - self.estimated_print_time = ( - self.printer.lookup_object('mcu').estimated_print_time) + self.estimated_print_time = self.printer.lookup_object( + "mcu" + ).estimated_print_time self._update_filament_runout_pos() self._extruder_pos_update_timer = self.reactor.register_timer( - self._extruder_pos_update_event) + self._extruder_pos_update_event + ) + def _handle_printing(self, print_time): - self.reactor.update_timer(self._extruder_pos_update_timer, - self.reactor.NOW) + self.reactor.update_timer(self._extruder_pos_update_timer, self.reactor.NOW) + def _handle_not_printing(self, print_time): - self.reactor.update_timer(self._extruder_pos_update_timer, - self.reactor.NEVER) + self.reactor.update_timer(self._extruder_pos_update_timer, self.reactor.NEVER) + def _get_extruder_pos(self, eventtime=None): if eventtime is None: eventtime = self.reactor.monotonic() print_time = self.estimated_print_time(eventtime) return self.extruder.find_past_position(print_time) + def _extruder_pos_update_event(self, eventtime): extruder_pos = self._get_extruder_pos(eventtime) # Check for filament runout - self.runout_helper.note_filament_present(eventtime, - extruder_pos < self.filament_runout_pos) + self.runout_helper.note_filament_present( + eventtime, extruder_pos < self.filament_runout_pos + ) return eventtime + CHECK_RUNOUT_TIMEOUT + def encoder_event(self, eventtime, state): if self.extruder is not None: self._update_filament_runout_pos(eventtime) @@ -73,5 +83,6 @@ class EncoderSensor: # Filament is always assumed to be present on an encoder event self.runout_helper.note_filament_present(eventtime, True) + def load_config_prefix(config): return EncoderSensor(config) |