diff --git a/klippy/extras/heaters.py b/klippy/extras/heaters.py index 3c09c50fb..6f4893c8c 100644 --- a/klippy/extras/heaters.py +++ b/klippy/extras/heaters.py @@ -507,21 +507,17 @@ def cmd_M105(self, gcmd): def _wait_for_temperature(self, heater): # Helper to wait on heater.check_busy() and report M105 temperatures + if self.printer.get_start_args().get("debugoutput") is not None: return - toolhead = self.printer.lookup_object("toolhead") + gcode = self.printer.lookup_object("gcode") - reactor = self.printer.get_reactor() - eventtime = reactor.monotonic() - initial_interrupt_counter = gcode.check_interrupt_counter() - while ( - not self.printer.is_shutdown() - and heater.check_busy(eventtime) - and initial_interrupt_counter == gcode.check_interrupt_counter() - ): - print_time = toolhead.get_last_move_time() + + def check(eventtime): gcode.respond_raw(self._get_temp(eventtime)) - eventtime = reactor.pause(eventtime + 1.0) + return heater.check_busy(eventtime) + + self.printer.wait_while(check) def set_temperature(self, heater, temp, wait=False): toolhead = self.printer.lookup_object("toolhead") @@ -548,21 +544,15 @@ def cmd_TEMPERATURE_WAIT(self, gcmd): sensor = self.heaters[sensor_name] else: sensor = self.printer.lookup_object(sensor_name) - toolhead = self.printer.lookup_object("toolhead") - reactor = self.printer.get_reactor() - eventtime = reactor.monotonic() - initial_interrupt_counter = self.gcode.check_interrupt_counter() - while ( - not self.printer.is_shutdown() - and initial_interrupt_counter - == self.gcode.check_interrupt_counter() - ): - temp, target = sensor.get_temp(eventtime) + + def check(eventtime): + temp, _ = sensor.get_temp(eventtime) if temp >= min_temp and temp <= max_temp: - return - print_time = toolhead.get_last_move_time() + return False gcmd.respond_raw(self._get_temp(eventtime)) - eventtime = reactor.pause(eventtime + 1.0) + return True + + self.printer.wait_while(check) def load_config(config): diff --git a/klippy/gcode.py b/klippy/gcode.py index 7efbae9b6..8d233bdff 100644 --- a/klippy/gcode.py +++ b/klippy/gcode.py @@ -172,7 +172,7 @@ def __init__(self, printer): desc = getattr(self, "cmd_" + cmd + "_help", None) self.register_command(cmd, func, True, desc) - def check_interrupt_counter(self): + def get_interrupt_counter(self): return self._interrupt_counter def increment_interrupt_counter(self): diff --git a/klippy/klippy.py b/klippy/klippy.py index 801536fc6..6fe244a5d 100644 --- a/klippy/klippy.py +++ b/klippy/klippy.py @@ -373,6 +373,22 @@ def request_exit(self, result): self.run_result = result self.reactor.end() + def wait_while(self, condition_cb): + """ + receives a callback + waits until callback returns False + (or is interrupted, or printer shuts down) + """ + counter = self.gcode.get_interrupt_counter() + eventtime = self.reactor.monotonic() + while condition_cb(eventtime): + if ( + self.is_shutdown() + or counter != self.gcode.get_interrupt_counter() + ): + return + eventtime = self.reactor.pause(eventtime + 1.0) + ###################################################################### # Startup