diff --git a/klippy/extras/controller_fan.py b/klippy/extras/controller_fan.py index 562ad2bc..63ed3e5f 100644 --- a/klippy/extras/controller_fan.py +++ b/klippy/extras/controller_fan.py @@ -15,10 +15,9 @@ class ControllerFan: self.stepper_enable = self.printer.load_object(config, 'stepper_enable') self.printer.load_object(config, 'heaters') self.heaters = [] - self.fan = fan.PrinterFan(config) - self.mcu = self.fan.mcu_fan.get_mcu() - self.fan_speed = config.getfloat( - 'fan_speed', default=1., minval=0., maxval=1.) + self.fan = fan.Fan(config) + self.fan_speed = config.getfloat('fan_speed', default=1., + minval=0., maxval=1.) self.idle_speed = config.getfloat( 'idle_speed', default=self.fan_speed, minval=0., maxval=1.) self.idle_timeout = config.getint("idle_timeout", default=30, minval=0) @@ -47,8 +46,8 @@ class ControllerFan: elif self.last_on < self.idle_timeout: power = self.idle_speed self.last_on += 1 - print_time = self.mcu.estimated_print_time(eventtime) + PIN_MIN_TIME - self.fan.set_speed(print_time, power) + print_time = self.fan.get_mcu().estimated_print_time(eventtime) + self.fan.set_speed(print_time + PIN_MIN_TIME, power) return eventtime + 1. def load_config_prefix(config): diff --git a/klippy/extras/fan.py b/klippy/extras/fan.py index 438da84b..664c1b31 100644 --- a/klippy/extras/fan.py +++ b/klippy/extras/fan.py @@ -1,40 +1,38 @@ # Printer cooling fan # -# Copyright (C) 2016-2018 Kevin O'Connor +# Copyright (C) 2016-2020 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. FAN_MIN_TIME = 0.100 -class PrinterFan: +class Fan: def __init__(self, config, default_shutdown_speed=0.): + self.printer = config.get_printer() self.last_fan_value = 0. self.last_fan_time = 0. - self.printer = config.get_printer() - self.printer.register_event_handler("gcode:request_restart", - self.handle_request_restart) + # Read config self.max_power = config.getfloat('max_power', 1., above=0., maxval=1.) self.kick_start_time = config.getfloat('kick_start_time', 0.1, minval=0.) - self.off_below = config.getfloat( - 'off_below', default=0., minval=0., maxval=1.) + self.off_below = config.getfloat('off_below', default=0., + minval=0., maxval=1.) + cycle_time = config.getfloat('cycle_time', 0.010, above=0.) + hardware_pwm = config.getboolean('hardware_pwm', False) + shutdown_speed = config.getfloat( + 'shutdown_speed', default_shutdown_speed, minval=0., maxval=1.) + # Setup pwm object ppins = self.printer.lookup_object('pins') self.mcu_fan = ppins.setup_pin('pwm', config.get('pin')) self.mcu_fan.setup_max_duration(0.) - cycle_time = config.getfloat('cycle_time', 0.010, above=0.) - hardware_pwm = config.getboolean('hardware_pwm', False) self.mcu_fan.setup_cycle_time(cycle_time, hardware_pwm) - shutdown_speed = config.getfloat( - 'shutdown_speed', default_shutdown_speed, minval=0., maxval=1.) - self.mcu_fan.setup_start_value( - 0., max(0., min(self.max_power, shutdown_speed))) - # Register commands - if config.get_name() == 'fan': - gcode = self.printer.lookup_object('gcode') - gcode.register_command("M106", self.cmd_M106) - gcode.register_command("M107", self.cmd_M107) - def handle_request_restart(self, print_time): - self.set_speed(print_time, 0.) + shutdown_power = max(0., min(self.max_power, shutdown_speed)) + self.mcu_fan.setup_start_value(0., shutdown_power) + # Register callbacks + self.printer.register_event_handler("gcode:request_restart", + self._handle_request_restart) + def get_mcu(self): + return self.mcu_fan.get_mcu() def set_speed(self, print_time, value): if value < self.off_below: value = 0. @@ -50,19 +48,31 @@ class PrinterFan: self.mcu_fan.set_pwm(print_time, value) self.last_fan_time = print_time self.last_fan_value = value - def get_status(self, eventtime): - return {'speed': self.last_fan_value} - def _delayed_set_speed(self, value): + def set_speed_from_command(self, value): toolhead = self.printer.lookup_object('toolhead') toolhead.register_lookahead_callback((lambda pt: self.set_speed(pt, value))) + def _handle_request_restart(self, print_time): + self.set_speed(print_time, 0.) + def get_status(self, eventtime): + return {'speed': self.last_fan_value} + +class PrinterFan: + def __init__(self, config): + self.fan = Fan(config) + # Register commands + gcode = config.get_printer().lookup_object('gcode') + gcode.register_command("M106", self.cmd_M106) + gcode.register_command("M107", self.cmd_M107) + def get_status(self, eventtime): + return self.fan.get_status(eventtime) def cmd_M106(self, gcmd): # Set fan speed value = gcmd.get_float('S', 255., minval=0.) / 255. - self._delayed_set_speed(value) + self.fan.set_speed_from_command(value) def cmd_M107(self, gcmd): # Turn fan off - self._delayed_set_speed(0.) + self.fan.set_speed_from_command(0.) def load_config(config): return PrinterFan(config) diff --git a/klippy/extras/heater_fan.py b/klippy/extras/heater_fan.py index 5d64160d..d69a7cf4 100644 --- a/klippy/extras/heater_fan.py +++ b/klippy/extras/heater_fan.py @@ -1,6 +1,6 @@ # Support fans that are enabled when a heater is on # -# Copyright (C) 2016-2018 Kevin O'Connor +# Copyright (C) 2016-2020 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. from . import fan @@ -15,8 +15,7 @@ class PrinterHeaterFan: self.heater_name = config.get("heater", "extruder") self.heater_temp = config.getfloat("heater_temp", 50.0) self.heaters = [] - self.fan = fan.PrinterFan(config, default_shutdown_speed=1.) - self.mcu = self.fan.mcu_fan.get_mcu() + self.fan = fan.Fan(config, default_shutdown_speed=1.) self.fan_speed = config.getfloat("fan_speed", 1., minval=0., maxval=1.) def handle_ready(self): pheaters = self.printer.lookup_object('heaters') @@ -32,8 +31,8 @@ class PrinterHeaterFan: current_temp, target_temp = heater.get_temp(eventtime) if target_temp or current_temp > self.heater_temp: power = self.fan_speed - print_time = self.mcu.estimated_print_time(eventtime) + PIN_MIN_TIME - self.fan.set_speed(print_time, power) + print_time = self.fan.get_mcu().estimated_print_time(eventtime) + self.fan.set_speed(print_time + PIN_MIN_TIME, power) return eventtime + 1. def load_config_prefix(config): diff --git a/klippy/extras/temperature_fan.py b/klippy/extras/temperature_fan.py index 3a568113..8309f096 100644 --- a/klippy/extras/temperature_fan.py +++ b/klippy/extras/temperature_fan.py @@ -1,6 +1,6 @@ # Support fans that are enabled when temperature exceeds a set threshold # -# Copyright (C) 2016-2018 Kevin O'Connor +# Copyright (C) 2016-2020 Kevin O'Connor # # This file may be distributed under the terms of the GNU GPLv3 license. from . import fan @@ -14,7 +14,7 @@ class TemperatureFan: def __init__(self, config): self.name = config.get_name().split()[1] self.printer = config.get_printer() - self.fan = fan.PrinterFan(config, default_shutdown_speed=1.) + self.fan = fan.Fan(config, default_shutdown_speed=1.) self.min_temp = config.getfloat('min_temp', minval=KELVIN_TO_CELSIUS) self.max_temp = config.getfloat('max_temp', above=self.min_temp) pheaters = self.printer.load_object(config, 'heaters')