Added typing

CL-541
This commit is contained in:
Jaime van Kessel 2017-12-19 16:15:48 +01:00
parent b4c83814d9
commit 79add4ffd8
4 changed files with 21 additions and 17 deletions

View file

@ -10,6 +10,9 @@ from time import time, sleep
from serial import Serial, SerialException from serial import Serial, SerialException
# An async job that attempts to find the correct baud rate for a USB printer.
# It tries a pre-set list of baud rates. All these baud rates are validated by requesting the temperature a few times
# and checking if the results make sense. If getResult() is not None, it was able to find a correct baud rate.
class AutoDetectBaudJob(Job): class AutoDetectBaudJob(Job):
def __init__(self, serial_port): def __init__(self, serial_port):
super().__init__() super().__init__()
@ -43,7 +46,7 @@ class AutoDetectBaudJob(Job):
serial.baudrate = baud_rate serial.baudrate = baud_rate
except: except:
continue continue
sleep(1.5) # Ensure that we are not talking to the bootloader. 1.5 seconds seems to be the magic number sleep(1.5) # Ensure that we are not talking to the boot loader. 1.5 seconds seems to be the magic number
successful_responses = 0 successful_responses = 0
serial.write(b"\n") # Ensure we clear out previous responses serial.write(b"\n") # Ensure we clear out previous responses
@ -60,4 +63,4 @@ class AutoDetectBaudJob(Job):
return return
serial.write(b"M105\n") serial.write(b"M105\n")
self.setResult(None) # Unable to detect the correct baudrate. self.setResult(None) # Unable to detect the correct baudrate.

View file

@ -1,4 +1,4 @@
// Copyright (c) 2015 Ultimaker B.V. // Copyright (c) 2017 Ultimaker B.V.
// Cura is released under the terms of the LGPLv3 or higher. // Cura is released under the terms of the LGPLv3 or higher.
import QtQuick 2.2 import QtQuick 2.2
@ -68,7 +68,6 @@ UM.Dialog
left: parent.left; left: parent.left;
right: parent.right; right: parent.right;
} }
} }
SystemPalette SystemPalette

View file

@ -22,6 +22,7 @@ from threading import Thread
from time import time, sleep from time import time, sleep
from queue import Queue from queue import Queue
from enum import IntEnum from enum import IntEnum
from typing import Union, Optional, List
import re import re
import functools # Used for reduce import functools # Used for reduce
@ -34,20 +35,20 @@ class USBPrinterOutputDevice(PrinterOutputDevice):
firmwareProgressChanged = pyqtSignal() firmwareProgressChanged = pyqtSignal()
firmwareUpdateStateChanged = pyqtSignal() firmwareUpdateStateChanged = pyqtSignal()
def __init__(self, serial_port, baud_rate = None): def __init__(self, serial_port: str, baud_rate: Optional[int] = None):
super().__init__(serial_port) super().__init__(serial_port)
self.setName(catalog.i18nc("@item:inmenu", "USB printing")) self.setName(catalog.i18nc("@item:inmenu", "USB printing"))
self.setShortDescription(catalog.i18nc("@action:button Preceded by 'Ready to'.", "Print via USB")) self.setShortDescription(catalog.i18nc("@action:button Preceded by 'Ready to'.", "Print via USB"))
self.setDescription(catalog.i18nc("@info:tooltip", "Print via USB")) self.setDescription(catalog.i18nc("@info:tooltip", "Print via USB"))
self.setIconName("print") self.setIconName("print")
self._serial = None self._serial = None # type: Optional[Serial]
self._serial_port = serial_port self._serial_port = serial_port
self._timeout = 3 self._timeout = 3
# List of gcode lines to be printed # List of gcode lines to be printed
self._gcode = [] self._gcode = [] # type: List[str]
self._gcode_position = 0 self._gcode_position = 0
self._use_auto_detect = True self._use_auto_detect = True
@ -61,13 +62,13 @@ class USBPrinterOutputDevice(PrinterOutputDevice):
self._update_firmware_thread = Thread(target=self._updateFirmware, daemon = True) self._update_firmware_thread = Thread(target=self._updateFirmware, daemon = True)
self._last_temperature_request = None self._last_temperature_request = None # type: Optional[int]
self._is_printing = False # A print is being sent. self._is_printing = False # A print is being sent.
## Set when print is started in order to check running time. ## Set when print is started in order to check running time.
self._print_start_time = None self._print_start_time = None # type: Optional[int]
self._print_estimated_time = None self._print_estimated_time = None # type: Optional[int]
self._accepts_commands = True self._accepts_commands = True
@ -90,7 +91,7 @@ class USBPrinterOutputDevice(PrinterOutputDevice):
# \param kwargs Keyword arguments. # \param kwargs Keyword arguments.
def requestWrite(self, nodes, file_name = None, filter_by_machine = False, file_handler = None, **kwargs): def requestWrite(self, nodes, file_name = None, filter_by_machine = False, file_handler = None, **kwargs):
if self._is_printing: if self._is_printing:
return # Aleady printing return # Aleady printing
Application.getInstance().getController().setActiveStage("MonitorStage") Application.getInstance().getController().setActiveStage("MonitorStage")
@ -181,7 +182,7 @@ class USBPrinterOutputDevice(PrinterOutputDevice):
## Start a print based on a g-code. ## Start a print based on a g-code.
# \param gcode_list List with gcode (strings). # \param gcode_list List with gcode (strings).
def _printGCode(self, gcode_list): def _printGCode(self, gcode_list: List[str]):
self._gcode.clear() self._gcode.clear()
self._paused = False self._paused = False
@ -201,13 +202,13 @@ class USBPrinterOutputDevice(PrinterOutputDevice):
self.writeFinished.emit(self) self.writeFinished.emit(self)
def _autoDetectFinished(self, job): def _autoDetectFinished(self, job: AutoDetectBaudJob):
result = job.getResult() result = job.getResult()
if result is not None: if result is not None:
self.setBaudRate(result) self.setBaudRate(result)
self.connect() # Try to connect (actually create serial, etc) self.connect() # Try to connect (actually create serial, etc)
def setBaudRate(self, baud_rate): def setBaudRate(self, baud_rate: int):
if baud_rate not in self._all_baud_rates: if baud_rate not in self._all_baud_rates:
Logger.log("w", "Not updating baudrate to {baud_rate} as it's an unknown baudrate".format(baud_rate=baud_rate)) Logger.log("w", "Not updating baudrate to {baud_rate} as it's an unknown baudrate".format(baud_rate=baud_rate))
return return
@ -243,13 +244,14 @@ class USBPrinterOutputDevice(PrinterOutputDevice):
self._update_thread = Thread(target=self._update, daemon=True) self._update_thread = Thread(target=self._update, daemon=True)
self._serial = None self._serial = None
def sendCommand(self, command): ## Send a command to printer.
def sendCommand(self, command: Union[str, bytes]):
if self._is_printing: if self._is_printing:
self._command_queue.put(command) self._command_queue.put(command)
elif self._connection_state == ConnectionState.connected: elif self._connection_state == ConnectionState.connected:
self._sendCommand(command) self._sendCommand(command)
def _sendCommand(self, command): def _sendCommand(self, command: Union[str, bytes]):
if self._serial is None: if self._serial is None:
return return

View file

@ -22,7 +22,7 @@ import serial.tools.list_ports
i18n_catalog = i18nCatalog("cura") i18n_catalog = i18nCatalog("cura")
## Manager class that ensures that a usbPrinteroutput device is created for every connected USB printer. ## Manager class that ensures that an USBPrinterOutput device is created for every connected USB printer.
@signalemitter @signalemitter
class USBPrinterOutputDeviceManager(QObject, OutputDevicePlugin): class USBPrinterOutputDeviceManager(QObject, OutputDevicePlugin):
addUSBOutputDeviceSignal = Signal() addUSBOutputDeviceSignal = Signal()