USBPrinterOutputController.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. MYPY = False
  6. if MYPY:
  7. from cura.PrinterOutput.PrintJobOutputModel import PrintJobOutputModel
  8. from cura.PrinterOutput.PrinterOutputModel import PrinterOutputModel
  9. class USBPrinterOutputController(PrinterOutputController):
  10. def __init__(self, output_device):
  11. super().__init__(output_device)
  12. self._preheat_bed_timer = QTimer()
  13. self._preheat_bed_timer.setSingleShot(True)
  14. self._preheat_bed_timer.timeout.connect(self._onPreheatBedTimerFinished)
  15. self._preheat_printer = None
  16. def moveHead(self, printer: "PrinterOutputModel", x, y, z, speed):
  17. self._output_device.sendCommand("G91")
  18. self._output_device.sendCommand("G0 X%s Y%s Z%s F%s" % (x, y, z, speed))
  19. self._output_device.sendCommand("G90")
  20. def homeHead(self, printer):
  21. self._output_device.sendCommand("G28 X")
  22. self._output_device.sendCommand("G28 Y")
  23. def homeBed(self, printer):
  24. self._output_device.sendCommand("G28 Z")
  25. def sendCustomCommand(self, printer, command):
  26. self._output_device.sendCommand(str(command))
  27. def setJobState(self, job: "PrintJobOutputModel", state: str):
  28. if state == "pause":
  29. self._output_device.pausePrint()
  30. job.updateState("paused")
  31. elif state == "print":
  32. self._output_device.resumePrint()
  33. job.updateState("printing")
  34. elif state == "abort":
  35. self._output_device.cancelPrint()
  36. pass
  37. def preheatBed(self, printer: "PrinterOutputModel", temperature, duration):
  38. try:
  39. temperature = round(temperature) # The API doesn't allow floating point.
  40. duration = round(duration)
  41. except ValueError:
  42. return # Got invalid values, can't pre-heat.
  43. self.setTargetBedTemperature(printer, temperature=temperature)
  44. self._preheat_bed_timer.setInterval(duration * 1000)
  45. self._preheat_bed_timer.start()
  46. self._preheat_printer = printer
  47. printer.updateIsPreheating(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 setTargetBedTemperature(self, printer: "PrinterOutputModel", temperature: int):
  53. self._output_device.sendCommand("M140 S%s" % temperature)
  54. def _onPreheatBedTimerFinished(self):
  55. self.setTargetBedTemperature(self._preheat_printer, 0)
  56. self._preheat_printer.updateIsPreheating(False)