diff --git a/PrinterConnection.py b/PrinterConnection.py index 86ea0fbf60..b155700843 100644 --- a/PrinterConnection.py +++ b/PrinterConnection.py @@ -7,7 +7,11 @@ import queue import re import functools -class PrinterConnection(): +from UM.Application import Application +from UM.Signal import Signal, SignalEmitter + + +class PrinterConnection(SignalEmitter): def __init__(self, serial_port): super().__init__() @@ -156,15 +160,27 @@ class PrinterConnection(): except Exception as e: return False + + def setIsConnected(self, state): self._is_connecting = False if self._is_connected != state: self._is_connected = state + self.connectionStateChanged.emit(self._serial_port) + if self._is_connected: + self._listen_thread.start() #Start listening + '''Application.getInstance().addOutputDevice(self._serial_port, { + 'id': self._serial_port, + 'function': self.printGCode, + 'description': 'Print with USB {0}'.format(self._serial_port), + 'icon': 'print_usb', + 'priority': 1 + })''' + else: Logger.log('w', "Printer connection state was not changed") - if self._is_connected: - self._listen_thread.start() #Start listening + connectionStateChanged = Signal() ## Close the printer connection def close(self): @@ -251,7 +267,8 @@ class PrinterConnection(): pass if b'B:' in line: #Check if it's a bed temperature try: - print("BED TEMPERATURE" ,float(re.search(b"B: *([0-9\.]*)", line).group(1))) + #print("BED TEMPERATURE" ,float(re.search(b"B: *([0-9\.]*)", line).group(1))) + pass except: pass #TODO: temperature changed callback @@ -306,7 +323,7 @@ class PrinterConnection(): if self._current_z != z: self._current_z = z except Exception as e: - self._log("Unexpected error: %s" % e) + Logger.log('e', "Unexpected error: %s" % e) checksum = functools.reduce(lambda x,y: x^y, map(ord, 'N%d%s' % (self._gcode_position, line))) self._sendCommand("N%d%s*%d" % (self._gcode_position, line, checksum)) diff --git a/USBPrinterManager.py b/USBPrinterManager.py index 15db396b72..e4526ef095 100644 --- a/USBPrinterManager.py +++ b/USBPrinterManager.py @@ -1,7 +1,9 @@ from UM.Signal import Signal, SignalEmitter from UM.PluginObject import PluginObject from . import PrinterConnection - +from UM.Application import Application +from UM.Scene.Iterator.DepthFirstIterator import DepthFirstIterator +from UM.Scene.SceneNode import SceneNode import threading import platform import glob @@ -41,11 +43,15 @@ class USBPrinterManager(SignalEmitter,PluginObject): for serial_port in self._serial_port_list: if self.getConnectionByPort(serial_port) is None: #If it doesn't already exist, add it if not os.path.islink(serial_port): #Only add the connection if it's a non symbolic link - self._printer_connections.append(PrinterConnection.PrinterConnection(serial_port)) + connection = PrinterConnection.PrinterConnection(serial_port) + connection.connect() + connection.connectionStateChanged.connect(self.serialConectionStateCallback) + self._printer_connections.append(connection) for serial_port in disconnected_ports: # Close connections and remove them from list. connection = self.getConnectionByPort(serial_port) - connection.close() + if connection != None: + connection.close() self._printer_connections.remove(connection) time.sleep(5) #Throttle, as we don't need this information to be updated every single second. @@ -93,7 +99,28 @@ class USBPrinterManager(SignalEmitter,PluginObject): return True else: return False + + + def serialConectionStateCallback(self,serial_port): + connection = self.getConnectionByPort(serial_port) + if connection.isConnected(): + Application.getInstance().addOutputDevice(serial_port, { + 'id': serial_port, + 'function': self._writeToSerial, + 'description': 'Write to USB {0}'.format(serial_port), + 'icon': 'print_usb', + 'priority': 1 + }) + def _writeToSerial(self, serial_port): + for node in DepthFirstIterator(Application.getInstance().getController().getScene().getRoot()): + if type(node) is not SceneNode or not node.getMeshData(): + continue + + gcode = getattr(node.getMeshData(), 'gcode', False) + self.sendGCodeByPort(serial_port,gcode.split('\n')) + + ## Get a list of printer connection objects that are connected. def getActiveConnections(self): return [connection for connection in self._printer_connections if connection.isConnected()]