Added window for usb printing

This commit is contained in:
Jaime van Kessel 2015-04-10 11:25:16 +02:00
parent c0d39ff287
commit 5e8f767b78
2 changed files with 82 additions and 14 deletions

View file

@ -32,6 +32,8 @@ class PrinterConnection(SignalEmitter):
# response. If the baudrate is correct, this should make sense, else we get giberish. # response. If the baudrate is correct, this should make sense, else we get giberish.
self._required_responses_auto_baud = 10 self._required_responses_auto_baud = 10
self._progress = 0
self._listen_thread = threading.Thread(target=self._listen) self._listen_thread = threading.Thread(target=self._listen)
self._listen_thread.daemon = True self._listen_thread.daemon = True
@ -328,6 +330,23 @@ class PrinterConnection(SignalEmitter):
self._sendCommand("N%d%s*%d" % (self._gcode_position, line, checksum)) self._sendCommand("N%d%s*%d" % (self._gcode_position, line, checksum))
self._gcode_position += 1 self._gcode_position += 1
self.setProgress(( self._gcode_position / len(self._gcode)) * 100)
self.progressChanged.emit(self._progress, self._serial_port)
progressChanged = Signal()
def setProgress(self, progress):
self._progress = progress
self.progressChanged.emit(self._progress, self._serial_port)
def cancelPrint(self):
self._gcode_position = 0
self.setProgress(0)
self._gcode = []
# Turn of temperatures
self._sendCommand("M140 S0")
self._sendCommand("M109 S0")
self._is_printing = False
def hasError(self): def hasError(self):
return False return False

View file

@ -10,16 +10,27 @@ import glob
import time import time
import os import os
class USBPrinterManager(SignalEmitter,PluginObject): from PyQt5.QtQuick import QQuickView
def __init__(self): from PyQt5.QtCore import QUrl, QObject,pyqtSlot , pyqtProperty,pyqtSignal
super().__init__()
class USBPrinterManager(QObject, SignalEmitter,PluginObject):
def __init__(self, parent = None):
super().__init__(parent)
self._serial_port_list = [] self._serial_port_list = []
self._printer_connections = [] self._printer_connections = []
self._check_ports_thread = threading.Thread(target=self._updateConnectionList) self._check_ports_thread = threading.Thread(target=self._updateConnectionList)
self._check_ports_thread.daemon = True self._check_ports_thread.daemon = True
self._check_ports_thread.start() self._check_ports_thread.start()
self._progress = 50
## DEBUG CODE ## DEBUG CODE
self.view = QQuickView()
self.view.setSource(QUrl("plugins/USBPrinting/ControlWindow.qml"))
self.view.show()
self.view.engine().rootContext().setContextProperty('manager',self)
#time.sleep(1) #time.sleep(1)
#self.connectAllConnections() #self.connectAllConnections()
#time.sleep(5) #time.sleep(5)
@ -31,9 +42,27 @@ class USBPrinterManager(SignalEmitter,PluginObject):
#print("sending heat " , self.sendCommandToAllActive("M104 S190")) #print("sending heat " , self.sendCommandToAllActive("M104 S190"))
#def spawnInterface(self):
#view = QQuickView()
#view.setSource(QUrl("plugins/USBPrinting/ControlWindow.qml"))
#view.show()
## Check all serial ports and create a PrinterConnection object for them. ## Check all serial ports and create a PrinterConnection object for them.
# Note that this does not validate if the serial ports are actually usable! # Note that this does not validate if the serial ports are actually usable!
# This is only done when the connect function is called. # This is only done when the connect function is called.
processingProgress = pyqtSignal(float, arguments = ['amount'])
#@pyqtProperty(float, notify = processingProgress)
@pyqtProperty(float,notify = processingProgress)
def progress(self):
return self._progress
@pyqtSlot()
def test(self):
print("derp")
def _updateConnectionList(self): def _updateConnectionList(self):
while True: while True:
temp_serial_port_list = self.getSerialPortList(only_list_usb = True) temp_serial_port_list = self.getSerialPortList(only_list_usb = True)
@ -46,6 +75,7 @@ class USBPrinterManager(SignalEmitter,PluginObject):
connection = PrinterConnection.PrinterConnection(serial_port) connection = PrinterConnection.PrinterConnection(serial_port)
connection.connect() connection.connect()
connection.connectionStateChanged.connect(self.serialConectionStateCallback) connection.connectionStateChanged.connect(self.serialConectionStateCallback)
connection.progressChanged.connect(self.onProgress)
self._printer_connections.append(connection) self._printer_connections.append(connection)
for serial_port in disconnected_ports: # Close connections and remove them from list. for serial_port in disconnected_ports: # Close connections and remove them from list.
@ -55,6 +85,11 @@ class USBPrinterManager(SignalEmitter,PluginObject):
connection.close() connection.close()
time.sleep(5) #Throttle, as we don't need this information to be updated every single second. time.sleep(5) #Throttle, as we don't need this information to be updated every single second.
def onProgress(self, progress, serial_port):
self._progress = progress
self.processingProgress.emit(progress)
pass
## Attempt to connect with all possible connections. ## Attempt to connect with all possible connections.
def connectAllConnections(self): def connectAllConnections(self):
for connection in self._printer_connections: for connection in self._printer_connections:
@ -67,6 +102,10 @@ class USBPrinterManager(SignalEmitter,PluginObject):
printer_connection.printGCode(gcode_list) printer_connection.printGCode(gcode_list)
return True return True
return False return False
@pyqtSlot()
def cancelPrint(self):
for printer_connection in self.getActiveConnections():
printer_connection.cancelPrint()
## Send gcode to all active printers. ## Send gcode to all active printers.
# \return True if there was at least one active connection. # \return True if there was at least one active connection.
@ -103,6 +142,7 @@ class USBPrinterManager(SignalEmitter,PluginObject):
def serialConectionStateCallback(self,serial_port): def serialConectionStateCallback(self,serial_port):
connection = self.getConnectionByPort(serial_port) connection = self.getConnectionByPort(serial_port)
if connection is not None:
if connection.isConnected(): if connection.isConnected():
Application.getInstance().addOutputDevice(serial_port, { Application.getInstance().addOutputDevice(serial_port, {
'id': serial_port, 'id': serial_port,
@ -113,7 +153,16 @@ class USBPrinterManager(SignalEmitter,PluginObject):
}) })
else: else:
Application.getInstance().removeOutputDevice(serial_port) Application.getInstance().removeOutputDevice(serial_port)
def _writeToSerial(self, serial_port): def _writeToSerial(self, serial_port):
## Create USB control window
#self.view = QQuickView()
#self.view.setSource(QUrl("plugins/USBPrinting/ControlWindow.qml"))
#self.view.show()
#self.view.engine().rootContext().setContextProperty('manager',self)
for node in DepthFirstIterator(Application.getInstance().getController().getScene().getRoot()): for node in DepthFirstIterator(Application.getInstance().getController().getScene().getRoot()):
if type(node) is not SceneNode or not node.getMeshData(): if type(node) is not SceneNode or not node.getMeshData():
continue continue