From 900bf2be55f912d5e9804fe31d9e65da175c4e0c Mon Sep 17 00:00:00 2001
From: Kevin O'Connor <kevin@koconnor.net>
Date: Fri, 13 Sep 2024 14:30:56 -0400
Subject: [PATCH] Revert "fan: Use GCodeRequestQueue to queue updates"

This reverts commit 7940a6a728be8f6b4ec36eecfd7c5291da4fdc81.

Queing of fan updates via GCodeRequestQueue is only valid if updates
originate from gcode commands.  The heater_fan, controller_fan, and
temperature_fan modules could send updates asynchronously.  Revert the
fan queuing changes until this issue can be resolved.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
---
 klippy/extras/fan.py | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)

diff --git a/klippy/extras/fan.py b/klippy/extras/fan.py
index ac0e6d5f4..d6e687218 100644
--- a/klippy/extras/fan.py
+++ b/klippy/extras/fan.py
@@ -1,14 +1,17 @@
 # Printer cooling fan
 #
-# Copyright (C) 2016-2024  Kevin O'Connor <kevin@koconnor.net>
+# Copyright (C) 2016-2020  Kevin O'Connor <kevin@koconnor.net>
 #
 # This file may be distributed under the terms of the GNU GPLv3 license.
-from . import pulse_counter, output_pin
+from . import pulse_counter
+
+FAN_MIN_TIME = 0.100
 
 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.
         # 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,
@@ -33,10 +36,6 @@ class Fan:
             self.enable_pin = ppins.setup_pin('digital_out', enable_pin)
             self.enable_pin.setup_max_duration(0.)
 
-        # Create gcode request queue
-        self.gcrq = output_pin.GCodeRequestQueue(config, self.mcu_fan.get_mcu(),
-                                                 self._apply_speed)
-
         # Setup tachometer
         self.tachometer = FanTachometer(config)
 
@@ -46,12 +45,13 @@ class Fan:
 
     def get_mcu(self):
         return self.mcu_fan.get_mcu()
-    def _apply_speed(self, print_time, value):
+    def set_speed(self, print_time, value):
         if value < self.off_below:
             value = 0.
         value = max(0., min(self.max_power, value * self.max_power))
         if value == self.last_fan_value:
-            return (True, 0.)
+            return
+        print_time = max(self.last_fan_time + FAN_MIN_TIME, print_time)
         if self.enable_pin:
             if value > 0 and self.last_fan_value == 0:
                 self.enable_pin.set_digital(print_time, 1)
@@ -60,16 +60,15 @@ class Fan:
         if (value and value < self.max_power and self.kick_start_time
             and (not self.last_fan_value or value - self.last_fan_value > .5)):
             # Run fan at full speed for specified kick_start_time
-            self.last_fan_value = self.max_power
             self.mcu_fan.set_pwm(print_time, self.max_power)
-            return (False, self.kick_start_time)
-        self.last_fan_value = value
+            print_time += self.kick_start_time
         self.mcu_fan.set_pwm(print_time, value)
-        return (True, 0.)
-    def set_speed(self, print_time, value):
-        self.gcrq.queue_request(print_time, value)
+        self.last_fan_time = print_time
+        self.last_fan_value = value
     def set_speed_from_command(self, value):
-        self.gcrq.queue_gcode_request(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.)