Tue, 16 May 2023 14:36:37 +0200
Optimized the MicroPython execute() functions.
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 | """ |
9990 | 7 | Module implementing an interface to talk to a connected MicroPython device via |
8 | a serial link. | |
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
|
9 | """ |
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
|
10 | |
9990 | 11 | from PyQt6.QtCore import QCoreApplication, QEventLoop, QThread, QTimer, pyqtSlot |
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
|
12 | |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
13 | 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
|
14 | |
9990 | 15 | from .MicroPythonDeviceInterface import MicroPythonDeviceInterface |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
16 | 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
|
17 | |
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
|
18 | |
9990 | 19 | class MicroPythonSerialDeviceInterface(MicroPythonDeviceInterface): |
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
|
20 | """ |
9990 | 21 | Class implementing an interface to talk to a connected MicroPython device via |
22 | a serial link. | |
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 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
24 | |
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
|
25 | 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
|
26 | """ |
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
|
27 | Constructor |
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 | @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
|
30 | @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
|
31 | """ |
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
|
32 | super().__init__(parent) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
33 | |
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
|
34 | self.__blockReadyRead = False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
35 | |
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
|
36 | self.__serial = MicroPythonSerialPort( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
37 | 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
|
38 | ) |
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
|
39 | 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
|
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 | @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
|
42 | 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
|
43 | """ |
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
|
44 | 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
|
45 | "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
|
46 | """ |
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 | 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
|
48 | 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
|
49 | self.dataReceived.emit(data) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
50 | |
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
|
51 | @pyqtSlot() |
9990 | 52 | def connectToDevice(self, connection): |
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
|
53 | """ |
9990 | 54 | Public slot to connect to the device. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
55 | |
9990 | 56 | @param connection name of the connection to be used |
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 | @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
|
58 | @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
|
59 | @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
|
60 | """ |
9990 | 61 | return self.__serial.openSerialLink(connection) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
62 | |
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
|
63 | @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
|
64 | 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
|
65 | """ |
9990 | 66 | Public slot to disconnect from the device. |
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
|
67 | """ |
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
|
68 | self.__serial.closeSerialLink() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
69 | |
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
|
70 | 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
|
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 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
|
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 | @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
|
75 | @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
|
76 | """ |
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 | return self.__serial.isConnected() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
78 | |
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
|
79 | @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
|
80 | 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
|
81 | """ |
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 | 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
|
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 | 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
|
85 | |
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
|
86 | 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
|
87 | """ |
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 | 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
|
89 | |
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
|
90 | @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
|
91 | @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
|
92 | """ |
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 | 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
|
94 | |
9799 | 95 | def __pasteOn(self): |
96 | """ | |
97 | Private method to switch the connected device to 'paste' mode. | |
98 | ||
99 | Note: switching to paste mode is done with synchronous writes. | |
100 | ||
101 | @return flag indicating success | |
102 | @rtype bool | |
103 | """ | |
104 | if not self.__serial: | |
105 | return False | |
106 | ||
107 | 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
|
108 | |
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
|
109 | self.__serial.clear() # clear any buffered output before entering paste mode |
9799 | 110 | self.__serial.write(b"\x02") # end raw mode if required |
111 | written = self.__serial.waitForBytesWritten(500) | |
112 | # time out after 500ms if device is not responding | |
113 | if not written: | |
114 | return False | |
115 | for _i in range(3): | |
116 | # CTRL-C three times to break out of loops | |
117 | self.__serial.write(b"\r\x03") | |
118 | written = self.__serial.waitForBytesWritten(500) | |
119 | # time out after 500ms if device is not responding | |
120 | if not written: | |
121 | return False | |
122 | QThread.msleep(10) | |
123 | self.__serial.readAll() # read all data and discard it | |
124 | self.__serial.write(b"\r\x05") # send CTRL-E to enter paste mode | |
125 | self.__serial.readUntil(pasteMessage) | |
126 | ||
127 | if self.__serial.hasTimedOut(): | |
128 | # it timed out; try it again and than fail | |
129 | self.__serial.write(b"\r\x05") # send CTRL-E again | |
130 | self.__serial.readUntil(pasteMessage) | |
131 | if self.__serial.hasTimedOut(): | |
132 | return False | |
133 | ||
134 | QCoreApplication.processEvents( | |
135 | QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents | |
136 | ) | |
137 | self.__serial.readAll() # read all data and discard it | |
138 | return True | |
139 | ||
140 | def __pasteOff(self): | |
141 | """ | |
142 | Private method to switch 'paste' mode off. | |
143 | """ | |
144 | if self.__serial: | |
145 | self.__serial.write(b"\x04") # send CTRL-D to cancel paste mode | |
146 | ||
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
|
147 | 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
|
148 | """ |
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
|
149 | 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
|
150 | |
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
|
151 | 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
|
152 | |
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
|
153 | @return flag indicating success |
9799 | 154 | @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
|
155 | """ |
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
|
156 | 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
|
157 | return False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
158 | |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7102
diff
changeset
|
159 | 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
|
160 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
161 | 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
|
162 | written = self.__serial.waitForBytesWritten(500) |
979562f350bf
MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8058
diff
changeset
|
163 | # 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
|
164 | if not written: |
979562f350bf
MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8058
diff
changeset
|
165 | 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
|
166 | 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
|
167 | # 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
|
168 | 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
|
169 | written = self.__serial.waitForBytesWritten(500) |
979562f350bf
MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8058
diff
changeset
|
170 | # 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
|
171 | if not written: |
979562f350bf
MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8058
diff
changeset
|
172 | 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
|
173 | QThread.msleep(10) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
174 | 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
|
175 | 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
|
176 | 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
|
177 | 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
|
178 | # 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
|
179 | 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
|
180 | 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
|
181 | 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
|
182 | return False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
183 | |
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
|
184 | QCoreApplication.processEvents( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
185 | QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
186 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
187 | 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
|
188 | return True |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
189 | |
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
|
190 | 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
|
191 | """ |
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
|
192 | 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
|
193 | """ |
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 | if self.__serial: |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
195 | 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
|
196 | 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
|
197 | 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
|
198 | |
9749 | 199 | def probeDevice(self): |
200 | """ | |
201 | Public method to check the device is responding. | |
202 | ||
9990 | 203 | If the device has not been flashed with a MicroPython firmware, the |
9749 | 204 | probe will fail. |
205 | ||
206 | @return flag indicating a communicating MicroPython device | |
207 | @rtype bool | |
208 | """ | |
209 | if not self.__serial: | |
210 | return False | |
211 | ||
212 | if not self.__serial.isConnected(): | |
213 | return False | |
214 | ||
10008 | 215 | # switch on paste mode |
9749 | 216 | self.__blockReadyRead = True |
9826 | 217 | ok = self.__pasteOn() |
9749 | 218 | if not ok: |
219 | self.__blockReadyRead = False | |
220 | return False | |
221 | ||
10008 | 222 | # switch off paste mode |
9749 | 223 | QThread.msleep(10) |
9826 | 224 | self.__pasteOff() |
9749 | 225 | self.__blockReadyRead = False |
226 | ||
227 | return True | |
228 | ||
9799 | 229 | 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
|
230 | """ |
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
|
231 | 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
|
232 | result. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
233 | |
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
|
234 | 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
|
235 | |
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
|
236 | @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
|
237 | @type str or list of str |
9799 | 238 | @keyparam mode submit mode to be used (one of 'raw' or 'paste') (defaults to |
239 | 'raw') | |
240 | @type str | |
241 | @keyparam timeout per command timeout in milliseconds (0 for configured default) | |
242 | (defaults to 0) | |
243 | @type int (optional) | |
244 | @return tuple containing stdout and stderr output of the device | |
245 | @rtype tuple of (bytes, bytes) | |
246 | @exception ValueError raised in case of an unsupported submit mode | |
247 | """ | |
248 | if mode not in ("paste", "raw"): | |
249 | raise ValueError("Unsupported submit mode given ('{0}').".format(mode)) | |
250 | ||
251 | if mode == "raw": | |
252 | return self.__execute_raw(commands, timeout=timeout) | |
253 | elif mode == "paste": | |
254 | return self.__execute_paste(commands, timeout=timeout) | |
255 | else: | |
256 | # just in case | |
257 | return b"", b"" | |
258 | ||
259 | def __execute_raw(self, commands, timeout=0): | |
260 | """ | |
261 | Private method to send commands to the connected device using 'raw REPL' mode | |
262 | and return the result. | |
263 | ||
264 | If no serial connection is available, empty results will be returned. | |
265 | ||
266 | @param commands list of commands to be executed | |
267 | @type str or list of str | |
9775
c6806d24468b
Created new branch <mpy_network>.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9766
diff
changeset
|
268 | @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
|
269 | (defaults to 0) |
c6806d24468b
Created new branch <mpy_network>.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9766
diff
changeset
|
270 | @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
|
271 | @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
|
272 | @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
|
273 | """ |
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
|
274 | 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
|
275 | return b"", b"" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
276 | |
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
|
277 | 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
|
278 | 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
|
279 | |
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
|
280 | 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
|
281 | err = b"" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
282 | |
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9764
diff
changeset
|
283 | if isinstance(commands, str): |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9764
diff
changeset
|
284 | commands = [commands] |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9764
diff
changeset
|
285 | |
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
|
286 | # 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
|
287 | 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
|
288 | 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
|
289 | 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
|
290 | self.__blockReadyRead = False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
291 | 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
|
292 | |
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
|
293 | # 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
|
294 | 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
|
295 | 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
|
296 | 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
|
297 | 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
|
298 | 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
|
299 | QCoreApplication.processEvents( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
300 | QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
301 | ) |
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
|
302 | 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
|
303 | if ok != b"OK": |
9799 | 304 | 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
|
305 | 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
|
306 | 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
|
307 | "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
|
308 | ok, self.__serial.readAll() |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
309 | ).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
|
310 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
311 | |
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 | # read until prompt |
9775
c6806d24468b
Created new branch <mpy_network>.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9766
diff
changeset
|
313 | 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
|
314 | if self.__serial.hasTimedOut(): |
10033
91b0939626ff
Optimized the MicroPython execute() functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
315 | out, err = b"", b"Timeout while processing commands." |
91b0939626ff
Optimized the MicroPython execute() functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
316 | break |
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 | 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
|
318 | # 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
|
319 | 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
|
320 | 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
|
321 | 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
|
322 | 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
|
323 | if err: |
9764
57496966803c
Fixed a few issues in the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9760
diff
changeset
|
324 | result = b"" |
57496966803c
Fixed a few issues in the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9760
diff
changeset
|
325 | break |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
326 | |
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
|
327 | # 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
|
328 | 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
|
329 | 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
|
330 | self.__blockReadyRead = False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
331 | |
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 | return bytes(result), err |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
333 | |
9799 | 334 | def __execute_paste(self, commands, timeout=0): |
335 | """ | |
336 | Private method to send commands to the connected device using 'paste' mode | |
337 | and return the result. | |
338 | ||
339 | If no serial connection is available, empty results will be returned. | |
340 | ||
341 | @param commands list of commands to be executed | |
342 | @type str or list of str | |
343 | @param timeout per command timeout in milliseconds (0 for configured default) | |
344 | (defaults to 0) | |
345 | @type int (optional) | |
346 | @return tuple containing stdout and stderr output of the device | |
347 | @rtype tuple of (bytes, bytes) | |
348 | """ | |
349 | if not self.__serial: | |
350 | return b"", b"" | |
351 | ||
352 | if not self.__serial.isConnected(): | |
10008 | 353 | return b"", b"Device is not connected or not switched on." |
9799 | 354 | |
355 | if isinstance(commands, list): | |
356 | commands = "\n".join(commands) | |
357 | ||
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
|
358 | # switch on paste mode |
9799 | 359 | self.__blockReadyRead = True |
360 | ok = self.__pasteOn() | |
361 | if not ok: | |
362 | 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
|
363 | return (b"", b"Could not switch to paste mode. Is the device switched on?") |
9799 | 364 | |
365 | # send commands | |
366 | QThread.msleep(10) | |
367 | for command in commands.splitlines(keepends=True): | |
368 | # send the data as single lines | |
369 | commandBytes = command.encode("utf-8") | |
370 | self.__serial.write(commandBytes) | |
371 | QCoreApplication.processEvents( | |
372 | QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents | |
373 | ) | |
374 | QThread.msleep(10) | |
375 | ok = self.__serial.readUntil(commandBytes) | |
376 | if ok != commandBytes: | |
377 | self.__blockReadyRead = False | |
378 | return ( | |
379 | b"", | |
380 | "Expected '{0}', got '{1}', followed by '{2}'".format( | |
381 | commandBytes, ok, self.__serial.readAll() | |
382 | ).encode("utf-8"), | |
383 | ) | |
384 | ||
385 | # switch off paste mode causing the commands to be executed | |
386 | self.__pasteOff() | |
387 | QThread.msleep(10) | |
388 | # read until Python prompt | |
9821
6b1b06d74532
Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9810
diff
changeset
|
389 | result = ( |
6b1b06d74532
Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9810
diff
changeset
|
390 | self.__serial.readUntil(b">>> ", timeout=timeout) |
6b1b06d74532
Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9810
diff
changeset
|
391 | .replace(b">>> ", b"") |
6b1b06d74532
Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9810
diff
changeset
|
392 | .strip() |
6b1b06d74532
Fixed some code formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9810
diff
changeset
|
393 | ) |
9799 | 394 | if self.__serial.hasTimedOut(): |
10033
91b0939626ff
Optimized the MicroPython execute() functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
395 | out, err = b"", b"Timeout while processing commands." |
91b0939626ff
Optimized the MicroPython execute() functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
396 | else: |
91b0939626ff
Optimized the MicroPython execute() functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
397 | # get rid of any OSD string and send it |
91b0939626ff
Optimized the MicroPython execute() functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
398 | if result.startswith(b"\x1b]0;"): |
91b0939626ff
Optimized the MicroPython execute() functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
399 | osd, result = result.split(b"\x1b\\", 1) |
91b0939626ff
Optimized the MicroPython execute() functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
400 | self.osdInfo.emit(osd[4:].decode("utf-8")) |
9799 | 401 | |
10033
91b0939626ff
Optimized the MicroPython execute() functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
402 | if self.TracebackMarker in result: |
91b0939626ff
Optimized the MicroPython execute() functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
403 | errorIndex = result.find(self.TracebackMarker) |
91b0939626ff
Optimized the MicroPython execute() functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
404 | out, err = result[:errorIndex], result[errorIndex:] |
91b0939626ff
Optimized the MicroPython execute() functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
405 | else: |
91b0939626ff
Optimized the MicroPython execute() functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
406 | out = result |
91b0939626ff
Optimized the MicroPython execute() functions.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
407 | err = b"" |
9799 | 408 | |
409 | self.__blockReadyRead = False | |
410 | return out, err | |
411 | ||
9989 | 412 | def executeAsync(self, commandsList, submitMode): |
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
|
413 | """ |
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
|
414 | 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
|
415 | 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
|
416 | |
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
|
417 | @param commandsList list of commands to be execute on the device |
9989 | 418 | @type list of str |
419 | @param submitMode mode to be used to submit the commands | |
420 | @type str (one of 'raw' or 'paste') | |
421 | @exception ValueError raised to indicate an unknown submit mode | |
422 | """ | |
423 | if submitMode not in ("raw", "paste"): | |
424 | raise ValueError("Illegal submit mode given ({0})".format(submitMode)) | |
425 | ||
426 | if submitMode == "raw": | |
427 | startSequence = [ # sequence of commands to enter raw mode | |
428 | b"\x02", # Ctrl-B: exit raw repl (just in case) | |
429 | b"\r\x03\x03\x03", # Ctrl-C three times: interrupt any running program | |
430 | b"\r\x01", # Ctrl-A: enter raw REPL | |
431 | b'print("\\n")\r', | |
432 | ] | |
433 | endSequence = [ | |
434 | b"\r", | |
435 | b"\x04", | |
436 | ] | |
437 | self.__executeAsyncRaw( | |
438 | startSequence | |
439 | + [c.encode("utf-8") + b"\r" for c in commandsList] | |
440 | + endSequence | |
441 | ) | |
442 | elif submitMode == "paste": | |
443 | self.__executeAsyncPaste(commandsList) | |
444 | ||
445 | def __executeAsyncRaw(self, commandsList): | |
446 | """ | |
447 | Private 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 | |
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
|
451 | @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
|
452 | """ |
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
|
453 | if commandsList: |
9764
57496966803c
Fixed a few issues in the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9760
diff
changeset
|
454 | 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
|
455 | self.__serial.write(command) |
9989 | 456 | QTimer.singleShot(2, lambda: self.__executeAsyncRaw(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
|
457 | else: |
9989 | 458 | self.__rawOff() |
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
|
459 | self.executeAsyncFinished.emit() |
9799 | 460 | |
9989 | 461 | def __executeAsyncPaste(self, commandsList): |
9799 | 462 | """ |
9989 | 463 | Private method to execute a series of commands over a period of time |
9799 | 464 | without returning any result (asynchronous execution). |
465 | ||
466 | @param commandsList list of commands to be execute on the device | |
9989 | 467 | @type list of str |
9799 | 468 | """ |
9989 | 469 | self.__blockReadyRead = True |
470 | self.__pasteOn() | |
471 | command = b"\n".join(c.encode("utf-8)") for c in commandsList) | |
472 | self.__serial.write(command) | |
473 | self.__serial.readUntil(command) | |
474 | self.__blockReadyRead = False | |
475 | self.__pasteOff() | |
10008 | 476 | self.executeAsyncFinished.emit() |