From c54c81dc2af44fbc797283670ba18d91d565711d Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 25 Feb 2019 20:30:37 -0500 Subject: [PATCH] heater: Return heaters with a gcode_id from new get_gcode_sensors() The gcode.py code should not peek into the heater classes to obtain the gcode_id. Instead, introduce a new get_gcode_sensors() so that the gcode.py code does not need to. Signed-off-by: Kevin O'Connor --- klippy/gcode.py | 6 +++--- klippy/heater.py | 14 ++++++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/klippy/gcode.py b/klippy/gcode.py index d6662152..184f3a81 100644 --- a/klippy/gcode.py +++ b/klippy/gcode.py @@ -373,9 +373,9 @@ class GCodeParser: # Tn:XXX /YYY B:XXX /YYY out = [] if self.heaters is not None: - for heater in self.heaters.get_all_heaters(): - cur, target = heater.get_temp(eventtime) - out.append("%s:%.1f /%.1f" % (heater.gcode_id, cur, target)) + for gcode_id, sensor in sorted(self.heaters.get_gcode_sensors()): + cur, target = sensor.get_temp(eventtime) + out.append("%s:%.1f /%.1f" % (gcode_id, cur, target)) if not out: return "T:0" return " ".join(out) diff --git a/klippy/heater.py b/klippy/heater.py index 7165582d..1d48c5a3 100644 --- a/klippy/heater.py +++ b/klippy/heater.py @@ -20,11 +20,10 @@ class error(Exception): class Heater: error = error - def __init__(self, config, sensor, gcode_id): + def __init__(self, config, sensor): self.printer = config.get_printer() self.gcode = self.printer.lookup_object("gcode") self.name = config.get_name().split()[-1] - self.gcode_id = gcode_id # Setup sensor self.sensor = sensor self.min_temp = config.getfloat('min_temp', minval=KELVIN_TO_CELCIUS) @@ -230,14 +229,14 @@ class PrinterHeaters: self.printer = config.get_printer() self.sensor_factories = {} self.heaters = {} - self.heaters_gcode_id = {} + self.gcode_id_to_sensor = {} # Register TURN_OFF_HEATERS command gcode = self.printer.lookup_object('gcode') gcode.register_command("TURN_OFF_HEATERS", self.cmd_TURN_OFF_HEATERS, desc=self.cmd_TURN_OFF_HEATERS_help) def add_sensor_factory(self, sensor_type, sensor_factory): self.sensor_factories[sensor_type] = sensor_factory - def setup_heater(self, config, gcode_id): + def setup_heater(self, config, gcode_id=None): heater_name = config.get_name().split()[-1] if heater_name == 'extruder': heater_name = 'extruder0' @@ -246,8 +245,9 @@ class PrinterHeaters: # Setup sensor sensor = self.setup_sensor(config) # Create heater - self.heaters[heater_name] = heater = Heater(config, sensor, gcode_id) - self.heaters_gcode_id[heater.gcode_id] = heater_name + self.heaters[heater_name] = heater = Heater(config, sensor) + if gcode_id is not None: + self.gcode_id_to_sensor[gcode_id] = heater return heater def lookup_heater(self, heater_name): if heater_name == 'extruder': @@ -267,6 +267,8 @@ class PrinterHeaters: return self.sensor_factories[sensor_type](config) def get_all_heaters(self): return self.heaters.values() + def get_gcode_sensors(self): + return self.gcode_id_to_sensor.items() cmd_TURN_OFF_HEATERS_help = "Turn off all heaters" def cmd_TURN_OFF_HEATERS(self, params): print_time = self.printer.lookup_object('toolhead').get_last_move_time()