src/eric7/MicroPython/MicroPythonDeviceInterface.py

Mon, 27 Feb 2023 17:43:51 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 27 Feb 2023 17:43:51 +0100
branch
mpy_network
changeset 9821
6b1b06d74532
parent 9810
39d3b227358c
child 9826
9340ce7fb12f
permissions
-rw-r--r--

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
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
38 PasteModePrompt = b"=== "
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
39 TracebackMarker = b"Traceback (most recent call last):"
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
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 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
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
113 def __pasteOn(self):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
114 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
115 Private method to switch the connected device to 'paste' mode.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
116
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
117 Note: switching to paste mode is done with synchronous writes.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
118
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
119 @return flag indicating success
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
120 @rtype bool
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
121 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
122 if not self.__serial:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
123 return False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
124
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
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
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
128 self.__serial.write(b"\x02") # end raw mode if required
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
129 written = self.__serial.waitForBytesWritten(500)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
130 # time out after 500ms if device is not responding
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
131 if not written:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
132 return False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
133 for _i in range(3):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
134 # CTRL-C three times to break out of loops
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
135 self.__serial.write(b"\r\x03")
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
136 written = self.__serial.waitForBytesWritten(500)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
137 # time out after 500ms if device is not responding
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
138 if not written:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
139 return False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
140 QThread.msleep(10)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
141 self.__serial.readAll() # read all data and discard it
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
142 self.__serial.write(b"\r\x05") # send CTRL-E to enter paste mode
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
143 self.__serial.readUntil(pasteMessage)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
144
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
145 if self.__serial.hasTimedOut():
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
146 # it timed out; try it again and than fail
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
147 self.__serial.write(b"\r\x05") # send CTRL-E again
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
148 self.__serial.readUntil(pasteMessage)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
149 if self.__serial.hasTimedOut():
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
150 return False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
151
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
152 QCoreApplication.processEvents(
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
153 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
154 )
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
155 self.__serial.readAll() # read all data and discard it
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
156 return True
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
157
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
158 def __pasteOff(self):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
159 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
160 Private method to switch 'paste' mode off.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
161 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
162 if self.__serial:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
163 self.__serial.write(b"\x04") # send CTRL-D to cancel paste mode
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
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
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
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
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
217 def probeDevice(self):
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
218 """
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
219 Public method to check the device is responding.
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
220
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
221 If the device has not been flashed with a MicroPython formware, the
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
222 probe will fail.
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
223
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
224 @return flag indicating a communicating MicroPython device
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
225 @rtype bool
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
226 """
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
227 if not self.__serial:
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
228 return False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
229
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
230 if not self.__serial.isConnected():
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
231 return False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
232
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
233 # switch on raw mode
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
234 self.__blockReadyRead = True
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
235 ok = self.__rawOn()
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
236 if not ok:
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
237 self.__blockReadyRead = False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
238 return False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
239
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
240 # switch off raw mode
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
241 QThread.msleep(10)
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
242 self.__rawOff()
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
243 self.__blockReadyRead = False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
244
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
245 return True
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
246
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
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
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
256 @keyparam mode submit mode to be used (one of 'raw' or 'paste') (defaults to
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
257 'raw')
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
258 @type str
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
259 @keyparam timeout per command timeout in milliseconds (0 for configured default)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
260 (defaults to 0)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
261 @type int (optional)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
262 @return tuple containing stdout and stderr output of the device
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
263 @rtype tuple of (bytes, bytes)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
264 @exception ValueError raised in case of an unsupported submit mode
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
265 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
266 if mode not in ("paste", "raw"):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
267 raise ValueError("Unsupported submit mode given ('{0}').".format(mode))
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
268
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
269 if mode == "raw":
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
270 return self.__execute_raw(commands, timeout=timeout)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
271 elif mode == "paste":
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
272 return self.__execute_paste(commands, timeout=timeout)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
273 else:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
274 # just in case
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
275 return b"", b""
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
276
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
277 def __execute_raw(self, commands, timeout=0):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
278 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
279 Private method to send commands to the connected device using 'raw REPL' mode
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
280 and return the result.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
281
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
282 If no serial connection is available, empty results will be returned.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
283
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
284 @param commands list of commands to be executed
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
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
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
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
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
352 def __execute_paste(self, commands, timeout=0):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
353 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
354 Private method to send commands to the connected device using 'paste' mode
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
355 and return the result.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
356
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
357 If no serial connection is available, empty results will be returned.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
358
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
359 @param commands list of commands to be executed
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
360 @type str or list of str
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
361 @param timeout per command timeout in milliseconds (0 for configured default)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
362 (defaults to 0)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
363 @type int (optional)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
364 @return tuple containing stdout and stderr output of the device
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
365 @rtype tuple of (bytes, bytes)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
366 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
367 if not self.__serial:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
368 return b"", b""
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
369
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
370 if not self.__serial.isConnected():
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
371 return b"", b"Device not connected or not switched on."
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
372
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
373 if isinstance(commands, list):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
374 commands = "\n".join(commands)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
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
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
377 self.__blockReadyRead = True
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
378 ok = self.__pasteOn()
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
379 if not ok:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
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
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
382
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
383 # send commands
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
384 QThread.msleep(10)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
385 for command in commands.splitlines(keepends=True):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
386 # send the data as single lines
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
387 commandBytes = command.encode("utf-8")
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
388 self.__serial.write(commandBytes)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
389 QCoreApplication.processEvents(
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
390 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
391 )
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
392 QThread.msleep(10)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
393 ok = self.__serial.readUntil(commandBytes)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
394 if ok != commandBytes:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
395 self.__blockReadyRead = False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
396 return (
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
397 b"",
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
398 "Expected '{0}', got '{1}', followed by '{2}'".format(
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
399 commandBytes, ok, self.__serial.readAll()
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
400 ).encode("utf-8"),
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
401 )
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
402
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
403 # switch off paste mode causing the commands to be executed
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
404 self.__pasteOff()
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
405 QThread.msleep(10)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
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
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
412 if self.__serial.hasTimedOut():
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
413 self.__blockReadyRead = False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
414 return b"", b"Timeout while processing commands."
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
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
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
420 if self.TracebackMarker in result:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
421 errorIndex = result.find(self.TracebackMarker)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
422 out, err = result[:errorIndex], result[errorIndex:]
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
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
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
425 err = b""
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
426
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
427 self.__blockReadyRead = False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
428 return out, err
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
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
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
444
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
445 def executeAsyncPaste(self, commandsList):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
446 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
447 Public method to execute a series of commands over a period of time
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
448 without returning any result (asynchronous execution).
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
449
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
450 @param commandsList list of commands to be execute on the device
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
451 @type list of bytes
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
452 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
453 if commandsList:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
454 self.__blockReadyRead = True
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
455 command = commandsList.pop(0)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
456 if command == "@PasteOn@":
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
457 self.__pasteOn()
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
458 else:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
459 self.__serial.write(command)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
460 self.__serial.readUntil(command)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
461 QTimer.singleShot(2, lambda: self.executeAsyncPaste(commandsList))
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
462 else:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
463 self.__blockReadyRead = False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
464 self.__pasteOff()
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
465 self.executeAsyncFinished.emit()

eric ide

mercurial