Sat, 26 Apr 2025 12:34:32 +0200
MicroPython
- Added a configuration option to disable the support for the no longer produced Pimoroni Pico Wireless Pack.
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 | |
11090
f5f5f5803935
Updated copyright for 2025.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
3 | # Copyright (c) 2023 - 2025 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 base class to talk to a connected MicroPython 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
|
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 | |
9990 | 10 | from PyQt6.QtCore import QObject, pyqtSignal, 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
|
11 | |
8e10acb1cd85
Refactored and improved 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 | |
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9764
diff
changeset
|
13 | 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
|
14 | """ |
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
|
15 | 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
|
16 | |
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 | @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
|
18 | asynchronously executed list of commands (e.g. a script) |
9990 | 19 | @signal dataReceived(data) emitted to send data received via the connection |
20 | for further processing | |
10012 | 21 | @signal osdInfo(str) emitted when some OSD data was received 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
|
22 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
23 | |
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
|
24 | 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
|
25 | dataReceived = pyqtSignal(bytes) |
10012 | 26 | osdInfo = pyqtSignal(str) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
27 | |
10008 | 28 | PasteModePrompt = b"=== " |
29 | TracebackMarker = b"Traceback (most recent call last):" | |
30 | ||
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
|
31 | 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
|
32 | """ |
8e10acb1cd85
Refactored and improved 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 | Constructor |
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 | @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
|
36 | @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
|
37 | """ |
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
|
38 | super().__init__(parent) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
39 | |
9990 | 40 | 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
|
41 | """ |
10229
e50bbf250343
Extended the MicroPython code to give an indication, why the connection to a device failed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
42 | Public method 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
|
43 | |
9990 | 44 | @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
|
45 | @type str |
10229
e50bbf250343
Extended the MicroPython code to give an indication, why the connection to a device failed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
46 | @return flag indicating success and an error message |
e50bbf250343
Extended the MicroPython code to give an indication, why the connection to a device failed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
47 | @rtype tuple of (bool, str) |
9990 | 48 | @exception NotImplementedError raised to indicate that this method needs to |
49 | be implemented in a derived class | |
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
|
50 | """ |
9990 | 51 | raise NotImplementedError( |
52 | "This method needs to be implemented in a derived class." | |
53 | ) | |
54 | ||
10229
e50bbf250343
Extended the MicroPython code to give an indication, why the connection to a device failed.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10012
diff
changeset
|
55 | return False, "" |
9221
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 | @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
|
58 | 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
|
59 | """ |
9990 | 60 | Public slot to disconnect from the device. |
61 | ||
62 | @exception NotImplementedError raised to indicate that this method needs to | |
63 | be implemented in a derived class | |
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
|
64 | """ |
9990 | 65 | raise NotImplementedError( |
66 | "This method needs to be implemented in a derived class." | |
67 | ) | |
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 | 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
|
70 | """ |
8e10acb1cd85
Refactored and improved 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 | 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
|
72 | |
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
|
73 | @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
|
74 | @rtype bool |
9990 | 75 | @exception NotImplementedError raised to indicate that this method needs to |
76 | be implemented in a derived class | |
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
|
77 | """ |
9990 | 78 | raise NotImplementedError( |
79 | "This method needs to be implemented in a derived class." | |
80 | ) | |
81 | ||
82 | return False | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
83 | |
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
|
84 | @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
|
85 | 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
|
86 | """ |
8e10acb1cd85
Refactored and improved 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 | 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
|
88 | """ |
9990 | 89 | pass |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
90 | |
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
|
91 | 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
|
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 | 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
|
94 | |
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
|
95 | @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
|
96 | @type bytes or bytearray |
9990 | 97 | @exception NotImplementedError raised to indicate that this method needs to |
98 | be implemented in a derived class | |
9799 | 99 | """ |
9990 | 100 | raise NotImplementedError( |
101 | "This method needs to be implemented in a derived class." | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
102 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
103 | |
9749 | 104 | def probeDevice(self): |
105 | """ | |
106 | Public method to check the device is responding. | |
107 | ||
9990 | 108 | If the device has not been flashed with a MicroPython firmware, the |
9749 | 109 | probe will fail. |
110 | ||
111 | @return flag indicating a communicating MicroPython device | |
112 | @rtype bool | |
9990 | 113 | @exception NotImplementedError raised to indicate that this method needs to |
114 | be implemented in a derived class | |
9749 | 115 | """ |
9990 | 116 | raise NotImplementedError( |
117 | "This method needs to be implemented in a derived class." | |
118 | ) | |
9749 | 119 | |
9990 | 120 | return False |
9749 | 121 | |
9799 | 122 | 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
|
123 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
124 | 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
|
125 | result. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
126 | |
9990 | 127 | If no 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
|
128 | |
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
|
129 | @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
|
130 | @type str or list of str |
9799 | 131 | @keyparam mode submit mode to be used (one of 'raw' or 'paste') (defaults to |
132 | 'raw') | |
133 | @type str | |
134 | @keyparam timeout per command timeout in milliseconds (0 for configured default) | |
135 | (defaults to 0) | |
136 | @type int (optional) | |
137 | @return tuple containing stdout and stderr output of the device | |
138 | @rtype tuple of (bytes, bytes) | |
9990 | 139 | @exception NotImplementedError raised to indicate that this method needs to |
140 | be implemented in a derived class | |
9799 | 141 | @exception ValueError raised in case of an unsupported submit mode |
142 | """ | |
9990 | 143 | raise NotImplementedError( |
144 | "This method needs to be implemented in a derived class." | |
145 | ) | |
146 | ||
9799 | 147 | if mode not in ("paste", "raw"): |
148 | raise ValueError("Unsupported submit mode given ('{0}').".format(mode)) | |
149 | ||
9990 | 150 | return b"", b"" |
9799 | 151 | |
9989 | 152 | 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
|
153 | """ |
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
154 | 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
|
155 | 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
|
156 | |
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
|
157 | @param commandsList list of commands to be execute on the device |
9989 | 158 | @type list of str |
9990 | 159 | @param submitMode mode to be used to submit the commands (one of 'raw' |
160 | or 'paste') | |
161 | @type str | |
162 | @exception NotImplementedError raised to indicate that this method needs to | |
163 | be implemented in a derived class | |
9989 | 164 | @exception ValueError raised to indicate an unknown submit mode |
165 | """ | |
9990 | 166 | raise NotImplementedError( |
167 | "This method needs to be implemented in a derived class." | |
168 | ) | |
9989 | 169 | |
9990 | 170 | if submitMode not in ("raw", "paste"): |
171 | raise ValueError( | |
172 | "Unsupported submit mode given ('{0}').".format(submitMode) | |
173 | ) |