Mon, 27 Feb 2023 17:43:51 +0100
Fixed some code formatting issues.
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
1 | # -*- coding: utf-8 -*- |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
2 | |
9653
e67609152c5e
Updated copyright for 2023.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
3 | # Copyright (c) 2019 - 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
4 | # |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
5 | |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
6 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
7 | Module implementing some file system commands for MicroPython. |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
8 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
9 | |
8318
962bce857696
Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
10 | from PyQt6.QtCore import ( |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
11 | QCoreApplication, |
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
12 | QEventLoop, |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
13 | QObject, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
14 | QThread, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
15 | QTimer, |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
16 | pyqtSignal, |
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
17 | pyqtSlot, |
7113
04ac3f9a87e6
MicroPythonCommandsInterface: fixed a code formatting issue and simplified the 'is_visible()' function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7112
diff
changeset
|
18 | ) |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
19 | |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
20 | from eric7 import Preferences |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
21 | |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
22 | from .MicroPythonSerialPort import MicroPythonSerialPort |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
23 | |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
24 | |
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9764
diff
changeset
|
25 | class MicroPythonDeviceInterface(QObject): |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
26 | """ |
9766
f0e22f3a5878
Fixed a few issue introduced during the recent changes to the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9765
diff
changeset
|
27 | Class implementing an interface to talk to a connected MicroPython device. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
28 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
29 | @signal executeAsyncFinished() emitted to indicate the end of an |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
30 | asynchronously executed list of commands (e.g. a script) |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
31 | @signal dataReceived(data) emitted to send data received via the serial |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
32 | connection for further processing |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
33 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
34 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
35 | executeAsyncFinished = pyqtSignal() |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
36 | dataReceived = pyqtSignal(bytes) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
37 | |
9799 | 38 | PasteModePrompt = b"=== " |
39 | TracebackMarker = b"Traceback (most recent call last):" | |
40 | ||
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
41 | def __init__(self, parent=None): |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
42 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
43 | Constructor |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
44 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
45 | @param parent reference to the parent object |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
46 | @type QObject |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
47 | """ |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8143
diff
changeset
|
48 | super().__init__(parent) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
49 | |
7126
376deb7fefe7
microbit: added the minimal filesystem commands which are supported by the BBC micro:bit.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7124
diff
changeset
|
50 | self.__repl = parent |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
51 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
52 | self.__blockReadyRead = False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
53 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
54 | self.__serial = MicroPythonSerialPort( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
55 | timeout=Preferences.getMicroPython("SerialTimeout"), parent=self |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
56 | ) |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
57 | self.__serial.readyRead.connect(self.__readSerial) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
58 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
59 | @pyqtSlot() |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
60 | def __readSerial(self): |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
61 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
62 | Private slot to read all available serial data and emit it with the |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
63 | "dataReceived" signal for further processing. |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
64 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
65 | if not self.__blockReadyRead: |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
66 | data = bytes(self.__serial.readAll()) |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
67 | self.dataReceived.emit(data) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
68 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
69 | @pyqtSlot() |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
70 | def connectToDevice(self, port): |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
71 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
72 | Public slot to start the manager. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
73 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
74 | @param port name of the port to be used |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
75 | @type str |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
76 | @return flag indicating success |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
77 | @rtype bool |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
78 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
79 | return self.__serial.openSerialLink(port) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
80 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
81 | @pyqtSlot() |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
82 | def disconnectFromDevice(self): |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
83 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
84 | Public slot to stop the thread. |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
85 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
86 | self.__serial.closeSerialLink() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
87 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
88 | def isConnected(self): |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
89 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
90 | Public method to get the connection status. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
91 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
92 | @return flag indicating the connection status |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
93 | @rtype bool |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
94 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
95 | return self.__serial.isConnected() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
96 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
97 | @pyqtSlot() |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
98 | def handlePreferencesChanged(self): |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
99 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
100 | Public slot to handle a change of the preferences. |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
101 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
102 | self.__serial.setTimeout(Preferences.getMicroPython("SerialTimeout")) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
103 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
104 | def write(self, data): |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
105 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
106 | Public method to write data to the connected device. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
107 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
108 | @param data data to be written |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
109 | @type bytes or bytearray |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
110 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
111 | self.__serial.isConnected() and self.__serial.write(data) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
112 | |
9799 | 113 | def __pasteOn(self): |
114 | """ | |
115 | Private method to switch the connected device to 'paste' mode. | |
116 | ||
117 | Note: switching to paste mode is done with synchronous writes. | |
118 | ||
119 | @return flag indicating success | |
120 | @rtype bool | |
121 | """ | |
122 | if not self.__serial: | |
123 | return False | |
124 | ||
125 | pasteMessage = b"paste mode; Ctrl-C to cancel, Ctrl-D to finish\r\n=== " | |
9810
39d3b227358c
Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9799
diff
changeset
|
126 | |
39d3b227358c
Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9799
diff
changeset
|
127 | self.__serial.clear() # clear any buffered output before entering paste mode |
9799 | 128 | self.__serial.write(b"\x02") # end raw mode if required |
129 | written = self.__serial.waitForBytesWritten(500) | |
130 | # time out after 500ms if device is not responding | |
131 | if not written: | |
132 | return False | |
133 | for _i in range(3): | |
134 | # CTRL-C three times to break out of loops | |
135 | self.__serial.write(b"\r\x03") | |
136 | written = self.__serial.waitForBytesWritten(500) | |
137 | # time out after 500ms if device is not responding | |
138 | if not written: | |
139 | return False | |
140 | QThread.msleep(10) | |
141 | self.__serial.readAll() # read all data and discard it | |
142 | self.__serial.write(b"\r\x05") # send CTRL-E to enter paste mode | |
143 | self.__serial.readUntil(pasteMessage) | |
144 | ||
145 | if self.__serial.hasTimedOut(): | |
146 | # it timed out; try it again and than fail | |
147 | self.__serial.write(b"\r\x05") # send CTRL-E again | |
148 | self.__serial.readUntil(pasteMessage) | |
149 | if self.__serial.hasTimedOut(): | |
150 | return False | |
151 | ||
152 | QCoreApplication.processEvents( | |
153 | QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents | |
154 | ) | |
155 | self.__serial.readAll() # read all data and discard it | |
156 | return True | |
157 | ||
158 | def __pasteOff(self): | |
159 | """ | |
160 | Private method to switch 'paste' mode off. | |
161 | """ | |
162 | if self.__serial: | |
163 | self.__serial.write(b"\x04") # send CTRL-D to cancel paste mode | |
164 | ||
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
165 | def __rawOn(self): |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
166 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
167 | Private method to switch the connected device to 'raw' mode. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
168 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
169 | Note: switching to raw mode is done with synchronous writes. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
170 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
171 | @return flag indicating success |
9799 | 172 | @rtype bool |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
173 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
174 | if not self.__serial: |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
175 | return False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
176 | |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7102
diff
changeset
|
177 | rawReplMessage = b"raw REPL; CTRL-B to exit\r\n>" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
178 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
179 | self.__serial.write(b"\x02") # end raw mode if required |
8061
979562f350bf
MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8058
diff
changeset
|
180 | written = self.__serial.waitForBytesWritten(500) |
979562f350bf
MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8058
diff
changeset
|
181 | # time out after 500ms if device is not responding |
979562f350bf
MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8058
diff
changeset
|
182 | if not written: |
979562f350bf
MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8058
diff
changeset
|
183 | return False |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
184 | for _i in range(3): |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
185 | # CTRL-C three times to break out of loops |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
186 | self.__serial.write(b"\r\x03") |
8061
979562f350bf
MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8058
diff
changeset
|
187 | written = self.__serial.waitForBytesWritten(500) |
979562f350bf
MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8058
diff
changeset
|
188 | # time out after 500ms if device is not responding |
979562f350bf
MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8058
diff
changeset
|
189 | if not written: |
979562f350bf
MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8058
diff
changeset
|
190 | return False |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
191 | QThread.msleep(10) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
192 | self.__serial.readAll() # read all data and discard it |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
193 | self.__serial.write(b"\r\x01") # send CTRL-A to enter raw mode |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
194 | self.__serial.readUntil(rawReplMessage) |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
195 | if self.__serial.hasTimedOut(): |
7112
701cdc76f887
MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7108
diff
changeset
|
196 | # it timed out; try it again and than fail |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
197 | self.__serial.write(b"\r\x01") # send CTRL-A again |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
198 | self.__serial.readUntil(rawReplMessage) |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
199 | if self.__serial.hasTimedOut(): |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
200 | return False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
201 | |
8143
2c730d5fd177
Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8117
diff
changeset
|
202 | QCoreApplication.processEvents( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
203 | QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
204 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
205 | self.__serial.readAll() # read all data and discard it |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
206 | return True |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
207 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
208 | def __rawOff(self): |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
209 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
210 | Private method to switch 'raw' mode off. |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
211 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
212 | if self.__serial: |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
213 | self.__serial.write(b"\x02") # send CTRL-B to cancel raw mode |
7102
5e77aa4671e6
MicroPythonCommandsInterface: fixed an issue causing the prompt being shown in the REPL pane for each file manager command sequence.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7095
diff
changeset
|
214 | self.__serial.readUntil(b">>> ") # read until Python prompt |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
215 | self.__serial.readAll() # read all data and discard it |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
216 | |
9749 | 217 | def probeDevice(self): |
218 | """ | |
219 | Public method to check the device is responding. | |
220 | ||
221 | If the device has not been flashed with a MicroPython formware, the | |
222 | probe will fail. | |
223 | ||
224 | @return flag indicating a communicating MicroPython device | |
225 | @rtype bool | |
226 | """ | |
227 | if not self.__serial: | |
228 | return False | |
229 | ||
230 | if not self.__serial.isConnected(): | |
231 | return False | |
232 | ||
233 | # switch on raw mode | |
234 | self.__blockReadyRead = True | |
235 | ok = self.__rawOn() | |
236 | if not ok: | |
237 | self.__blockReadyRead = False | |
238 | return False | |
239 | ||
240 | # switch off raw mode | |
241 | QThread.msleep(10) | |
242 | self.__rawOff() | |
243 | self.__blockReadyRead = False | |
244 | ||
245 | return True | |
246 | ||
9799 | 247 | def execute(self, commands, *, mode="raw", timeout=0): |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
248 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
249 | Public method to send commands to the connected device and return the |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
250 | result. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
251 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
252 | If no serial connection is available, empty results will be returned. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
253 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
254 | @param commands list of commands to be executed |
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9764
diff
changeset
|
255 | @type str or list of str |
9799 | 256 | @keyparam mode submit mode to be used (one of 'raw' or 'paste') (defaults to |
257 | 'raw') | |
258 | @type str | |
259 | @keyparam timeout per command timeout in milliseconds (0 for configured default) | |
260 | (defaults to 0) | |
261 | @type int (optional) | |
262 | @return tuple containing stdout and stderr output of the device | |
263 | @rtype tuple of (bytes, bytes) | |
264 | @exception ValueError raised in case of an unsupported submit mode | |
265 | """ | |
266 | if mode not in ("paste", "raw"): | |
267 | raise ValueError("Unsupported submit mode given ('{0}').".format(mode)) | |
268 | ||
269 | if mode == "raw": | |
270 | return self.__execute_raw(commands, timeout=timeout) | |
271 | elif mode == "paste": | |
272 | return self.__execute_paste(commands, timeout=timeout) | |
273 | else: | |
274 | # just in case | |
275 | return b"", b"" | |
276 | ||
277 | def __execute_raw(self, commands, timeout=0): | |
278 | """ | |
279 | Private method to send commands to the connected device using 'raw REPL' mode | |
280 | and return the result. | |
281 | ||
282 | If no serial connection is available, empty results will be returned. | |
283 | ||
284 | @param commands list of commands to be executed | |
285 | @type str or list of str | |
9775
c6806d24468b
Created new branch <mpy_network>.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9766
diff
changeset
|
286 | @param timeout per command timeout in milliseconds (0 for configured default) |
c6806d24468b
Created new branch <mpy_network>.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9766
diff
changeset
|
287 | (defaults to 0) |
c6806d24468b
Created new branch <mpy_network>.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9766
diff
changeset
|
288 | @type int (optional) |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
289 | @return tuple containing stdout and stderr output of the device |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
290 | @rtype tuple of (bytes, bytes) |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
291 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
292 | if not self.__serial: |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
293 | return b"", b"" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
294 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
295 | if not self.__serial.isConnected(): |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
296 | return b"", b"Device not connected or not switched on." |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
297 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
298 | result = bytearray() |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
299 | err = b"" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
300 | |
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9764
diff
changeset
|
301 | if isinstance(commands, str): |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9764
diff
changeset
|
302 | commands = [commands] |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9764
diff
changeset
|
303 | |
7112
701cdc76f887
MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7108
diff
changeset
|
304 | # switch on raw mode |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
305 | self.__blockReadyRead = True |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
306 | ok = self.__rawOn() |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
307 | if not ok: |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
308 | self.__blockReadyRead = False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
309 | return (b"", b"Could not switch to raw mode. Is the device switched on?") |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
310 | |
7112
701cdc76f887
MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7108
diff
changeset
|
311 | # send commands |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
312 | QThread.msleep(10) |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
313 | for command in commands: |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
314 | if command: |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
315 | commandBytes = command.encode("utf-8") |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
316 | self.__serial.write(commandBytes + b"\x04") |
7112
701cdc76f887
MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7108
diff
changeset
|
317 | QCoreApplication.processEvents( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
318 | QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
319 | ) |
7112
701cdc76f887
MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7108
diff
changeset
|
320 | ok = self.__serial.readUntil(b"OK") |
701cdc76f887
MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7108
diff
changeset
|
321 | if ok != b"OK": |
9799 | 322 | self.__blockReadyRead = False |
7112
701cdc76f887
MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7108
diff
changeset
|
323 | return ( |
701cdc76f887
MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7108
diff
changeset
|
324 | b"", |
701cdc76f887
MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7108
diff
changeset
|
325 | "Expected 'OK', got '{0}', followed by '{1}'".format( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
326 | ok, self.__serial.readAll() |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
327 | ).encode("utf-8"), |
7112
701cdc76f887
MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7108
diff
changeset
|
328 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
329 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
330 | # read until prompt |
9775
c6806d24468b
Created new branch <mpy_network>.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9766
diff
changeset
|
331 | response = self.__serial.readUntil(b"\x04>", timeout=timeout) |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
332 | if self.__serial.hasTimedOut(): |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
333 | self.__blockReadyRead = False |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
334 | return b"", b"Timeout while processing commands." |
7112
701cdc76f887
MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7108
diff
changeset
|
335 | if b"\x04" in response[:-2]: |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
336 | # split stdout, stderr |
7112
701cdc76f887
MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7108
diff
changeset
|
337 | out, err = response[:-2].split(b"\x04") |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
338 | result += out |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
339 | else: |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
340 | err = b"invalid response received: " + response |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
341 | if err: |
9764
57496966803c
Fixed a few issues in the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9760
diff
changeset
|
342 | result = b"" |
57496966803c
Fixed a few issues in the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9760
diff
changeset
|
343 | break |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
344 | |
7112
701cdc76f887
MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7108
diff
changeset
|
345 | # switch off raw mode |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
346 | QThread.msleep(10) |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
347 | self.__rawOff() |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
348 | self.__blockReadyRead = False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
349 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
350 | return bytes(result), err |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
351 | |
9799 | 352 | def __execute_paste(self, commands, timeout=0): |
353 | """ | |
354 | Private method to send commands to the connected device using 'paste' mode | |
355 | and return the result. | |
356 | ||
357 | If no serial connection is available, empty results will be returned. | |
358 | ||
359 | @param commands list of commands to be executed | |
360 | @type str or list of str | |
361 | @param timeout per command timeout in milliseconds (0 for configured default) | |
362 | (defaults to 0) | |
363 | @type int (optional) | |
364 | @return tuple containing stdout and stderr output of the device | |
365 | @rtype tuple of (bytes, bytes) | |
366 | """ | |
367 | if not self.__serial: | |
368 | return b"", b"" | |
369 | ||
370 | if not self.__serial.isConnected(): | |
371 | return b"", b"Device not connected or not switched on." | |
372 | ||
373 | if isinstance(commands, list): | |
374 | commands = "\n".join(commands) | |
375 | ||
9810
39d3b227358c
Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9799
diff
changeset
|
376 | # switch on paste mode |
9799 | 377 | self.__blockReadyRead = True |
378 | ok = self.__pasteOn() | |
379 | if not ok: | |
380 | self.__blockReadyRead = False | |
9810
39d3b227358c
Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9799
diff
changeset
|
381 | return (b"", b"Could not switch to paste mode. Is the device switched on?") |
9799 | 382 | |
383 | # send commands | |
384 | QThread.msleep(10) | |
385 | for command in commands.splitlines(keepends=True): | |
386 | # send the data as single lines | |
387 | commandBytes = command.encode("utf-8") | |
388 | self.__serial.write(commandBytes) | |
389 | QCoreApplication.processEvents( | |
390 | QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents | |
391 | ) | |
392 | QThread.msleep(10) | |
393 | ok = self.__serial.readUntil(commandBytes) | |
394 | if ok != commandBytes: | |
395 | self.__blockReadyRead = False | |
396 | return ( | |
397 | b"", | |
398 | "Expected '{0}', got '{1}', followed by '{2}'".format( | |
399 | commandBytes, ok, self.__serial.readAll() | |
400 | ).encode("utf-8"), | |
401 | ) | |
402 | ||
403 | # switch off paste mode causing the commands to be executed | |
404 | self.__pasteOff() | |
405 | QThread.msleep(10) | |
406 | # read until Python prompt | |
9821
6b1b06d74532
Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9810
diff
changeset
|
407 | result = ( |
6b1b06d74532
Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9810
diff
changeset
|
408 | self.__serial.readUntil(b">>> ", timeout=timeout) |
6b1b06d74532
Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9810
diff
changeset
|
409 | .replace(b">>> ", b"") |
6b1b06d74532
Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9810
diff
changeset
|
410 | .strip() |
6b1b06d74532
Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9810
diff
changeset
|
411 | ) |
9799 | 412 | if self.__serial.hasTimedOut(): |
413 | self.__blockReadyRead = False | |
414 | return b"", b"Timeout while processing commands." | |
415 | ||
9810
39d3b227358c
Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9799
diff
changeset
|
416 | # get rid of any OSD string |
39d3b227358c
Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9799
diff
changeset
|
417 | if result.startswith(b"\x1b]0;"): |
39d3b227358c
Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9799
diff
changeset
|
418 | result = result.split(b"\x1b\\")[-1] |
39d3b227358c
Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9799
diff
changeset
|
419 | |
9799 | 420 | if self.TracebackMarker in result: |
421 | errorIndex = result.find(self.TracebackMarker) | |
422 | out, err = result[:errorIndex], result[errorIndex:] | |
423 | else: | |
9810
39d3b227358c
Changed code to clean the serial port buffers before switching to 'paste' mode and sanitize the received output of that mode.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9799
diff
changeset
|
424 | out = result |
9799 | 425 | err = b"" |
426 | ||
427 | self.__blockReadyRead = False | |
428 | return out, err | |
429 | ||
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
430 | def executeAsync(self, commandsList): |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
431 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
432 | Public method to execute a series of commands over a period of time |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
433 | without returning any result (asynchronous execution). |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
434 | |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
435 | @param commandsList list of commands to be execute on the device |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
436 | @type list of bytes |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
437 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
438 | if commandsList: |
9764
57496966803c
Fixed a few issues in the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9760
diff
changeset
|
439 | command = commandsList.pop(0) |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
440 | self.__serial.write(command) |
9764
57496966803c
Fixed a few issues in the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9760
diff
changeset
|
441 | QTimer.singleShot(2, lambda: self.executeAsync(commandsList)) |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
442 | else: |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
443 | self.executeAsyncFinished.emit() |
9799 | 444 | |
445 | def executeAsyncPaste(self, commandsList): | |
446 | """ | |
447 | Public method to execute a series of commands over a period of time | |
448 | without returning any result (asynchronous execution). | |
449 | ||
450 | @param commandsList list of commands to be execute on the device | |
451 | @type list of bytes | |
452 | """ | |
453 | if commandsList: | |
454 | self.__blockReadyRead = True | |
455 | command = commandsList.pop(0) | |
456 | if command == "@PasteOn@": | |
457 | self.__pasteOn() | |
458 | else: | |
459 | self.__serial.write(command) | |
460 | self.__serial.readUntil(command) | |
461 | QTimer.singleShot(2, lambda: self.executeAsyncPaste(commandsList)) | |
462 | else: | |
463 | self.__blockReadyRead = False | |
464 | self.__pasteOff() | |
465 | self.executeAsyncFinished.emit() |