LegacyUM3PrinterOutputController.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright (c) 2017 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from cura.PrinterOutput.PrinterOutputController import PrinterOutputController
  4. from PyQt5.QtCore import QTimer
  5. from UM.Version import Version
  6. MYPY = False
  7. if MYPY:
  8. from cura.PrinterOutput.PrintJobOutputModel import PrintJobOutputModel
  9. from cura.PrinterOutput.PrinterOutputModel import PrinterOutputModel
  10. class LegacyUM3PrinterOutputController(PrinterOutputController):
  11. def __init__(self, output_device):
  12. super().__init__(output_device)
  13. self._preheat_bed_timer = QTimer()
  14. self._preheat_bed_timer.setSingleShot(True)
  15. self._preheat_bed_timer.timeout.connect(self._onPreheatBedTimerFinished)
  16. self._preheat_printer = None
  17. self.can_control_manually = False
  18. # Are we still waiting for a response about preheat?
  19. # We need this so we can already update buttons, so it feels more snappy.
  20. self._preheat_request_in_progress = False
  21. def isPreheatRequestInProgress(self):
  22. return self._preheat_request_in_progress
  23. def setJobState(self, job: "PrintJobOutputModel", state: str):
  24. data = "{\"target\": \"%s\"}" % state
  25. self._output_device.put("print_job/state", data, onFinished=None)
  26. def setTargetBedTemperature(self, printer: "PrinterOutputModel", temperature: int):
  27. data = str(temperature)
  28. self._output_device.put("printer/bed/temperature/target", data, onFinished=self._onPutBedTemperatureCompleted)
  29. def _onPutBedTemperatureCompleted(self, reply):
  30. if Version(self._preheat_printer.firmwareVersion) < Version("3.5.92"):
  31. # If it was handling a preheat, it isn't anymore.
  32. self._preheat_request_in_progress = False
  33. def _onPutPreheatBedCompleted(self, reply):
  34. self._preheat_request_in_progress = False
  35. def moveHead(self, printer: "PrinterOutputModel", x, y, z, speed):
  36. head_pos = printer._head_position
  37. new_x = head_pos.x + x
  38. new_y = head_pos.y + y
  39. new_z = head_pos.z + z
  40. data = "{\n\"x\":%s,\n\"y\":%s,\n\"z\":%s\n}" %(new_x, new_y, new_z)
  41. self._output_device.put("printer/heads/0/position", data, onFinished=None)
  42. def homeBed(self, printer):
  43. self._output_device.put("printer/heads/0/position/z", "0", onFinished=None)
  44. def _onPreheatBedTimerFinished(self):
  45. self.setTargetBedTemperature(self._preheat_printer, 0)
  46. self._preheat_printer.updateIsPreheating(False)
  47. self._preheat_request_in_progress = True
  48. def cancelPreheatBed(self, printer: "PrinterOutputModel"):
  49. self.preheatBed(printer, temperature=0, duration=0)
  50. self._preheat_bed_timer.stop()
  51. printer.updateIsPreheating(False)
  52. def preheatBed(self, printer: "PrinterOutputModel", temperature, duration):
  53. try:
  54. temperature = round(temperature) # The API doesn't allow floating point.
  55. duration = round(duration)
  56. except ValueError:
  57. return # Got invalid values, can't pre-heat.
  58. if duration > 0:
  59. data = """{"temperature": "%i", "timeout": "%i"}""" % (temperature, duration)
  60. else:
  61. data = """{"temperature": "%i"}""" % temperature
  62. # Real bed pre-heating support is implemented from 3.5.92 and up.
  63. if Version(printer.firmwareVersion) < Version("3.5.92"):
  64. # No firmware-side duration support then, so just set target bed temp and set a timer.
  65. self.setTargetBedTemperature(printer, temperature=temperature)
  66. self._preheat_bed_timer.setInterval(duration * 1000)
  67. self._preheat_bed_timer.start()
  68. self._preheat_printer = printer
  69. printer.updateIsPreheating(True)
  70. return
  71. self._output_device.put("printer/bed/pre_heat", data, onFinished = self._onPutPreheatBedCompleted)
  72. printer.updateIsPreheating(True)
  73. self._preheat_request_in_progress = True