Skip to content

Commit

Permalink
refactor with wait_while util
Browse files Browse the repository at this point in the history
  • Loading branch information
bwnance committed Dec 21, 2023
1 parent 6f03204 commit d6ab92a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
38 changes: 14 additions & 24 deletions klippy/extras/heaters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion klippy/gcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
16 changes: 16 additions & 0 deletions klippy/klippy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d6ab92a

Please sign in to comment.