src/eric7/MicroPython/MicroPythonDeviceInterface.py

Sun, 26 Feb 2023 14:25:39 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 26 Feb 2023 14:25:39 +0100
branch
mpy_network
changeset 9805
4a2657e29a32
parent 9799
a79430a8811d
child 9810
39d3b227358c
permissions
-rw-r--r--

MicroPython
- added a 'reset' function to the CircuitPython devices interface

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=== "
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
126 self.__serial.write(b"\x02") # end raw mode if required
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
127 written = self.__serial.waitForBytesWritten(500)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
128 # time out after 500ms if device is not responding
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
129 if not written:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
130 return False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
131 for _i in range(3):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
132 # CTRL-C three times to break out of loops
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
133 self.__serial.write(b"\r\x03")
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
134 written = self.__serial.waitForBytesWritten(500)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
135 # time out after 500ms if device is not responding
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
136 if not written:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
137 return False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
138 QThread.msleep(10)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
139 self.__serial.readAll() # read all data and discard it
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
140 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
141 self.__serial.readUntil(pasteMessage)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
142
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
143 if self.__serial.hasTimedOut():
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
144 # it timed out; try it again and than fail
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
145 self.__serial.write(b"\r\x05") # send CTRL-E again
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
146 self.__serial.readUntil(pasteMessage)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
147 if self.__serial.hasTimedOut():
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
148 return False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
149
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
150 QCoreApplication.processEvents(
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
151 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
152 )
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
153 self.__serial.readAll() # read all data and discard it
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
154 return True
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
155
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
156 def __pasteOff(self):
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 Private method to switch 'paste' mode off.
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 if self.__serial:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
161 self.__serial.write(b"\x04") # send CTRL-D to cancel paste mode
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
162
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
163 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
164 """
8e10acb1cd85 Refactored and improved 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 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
166
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
167 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
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 @return flag indicating success
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
170 @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
171 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 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
173 return False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
174
7108
4f6133a01c6a Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7102
diff changeset
175 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
176
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
177 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
178 written = self.__serial.waitForBytesWritten(500)
979562f350bf MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8058
diff changeset
179 # 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
180 if not written:
979562f350bf MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8058
diff changeset
181 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
182 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
183 # 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
184 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
185 written = self.__serial.waitForBytesWritten(500)
979562f350bf MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8058
diff changeset
186 # 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
187 if not written:
979562f350bf MicroPython: implemented fixes for a few issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8058
diff changeset
188 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
189 QThread.msleep(10)
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
190 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
191 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
192 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
193 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
194 # 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
195 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
196 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
197 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
198 return False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
199
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
200 QCoreApplication.processEvents(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
201 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
202 )
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
203 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
204 return True
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
205
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 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
207 """
8e10acb1cd85 Refactored and improved 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 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
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 if self.__serial:
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
211 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
212 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
213 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
214
9749
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
215 def probeDevice(self):
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
216 """
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
217 Public method to check the device is responding.
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 If the device has not been flashed with a MicroPython formware, the
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
220 probe will fail.
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
221
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
222 @return flag indicating a communicating MicroPython device
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
223 @rtype bool
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
224 """
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
225 if not self.__serial:
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
226 return False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
227
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
228 if not self.__serial.isConnected():
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
229 return False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
230
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
231 # switch on raw mode
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
232 self.__blockReadyRead = True
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
233 ok = self.__rawOn()
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
234 if not ok:
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
235 self.__blockReadyRead = False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
236 return False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
237
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
238 # switch off raw mode
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
239 QThread.msleep(10)
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
240 self.__rawOff()
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
241 self.__blockReadyRead = False
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
242
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
243 return True
5d409223cf3f MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9748
diff changeset
244
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
245 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
246 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 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
248 result.
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
249
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
250 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
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 @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
253 @type str or list of str
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
254 @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
255 'raw')
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
256 @type str
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
257 @keyparam timeout per command timeout in milliseconds (0 for configured default)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
258 (defaults to 0)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
259 @type int (optional)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
260 @return tuple containing stdout and stderr output of the device
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
261 @rtype tuple of (bytes, bytes)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
262 @exception ValueError raised in case of an unsupported submit mode
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
263 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
264 if mode not in ("paste", "raw"):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
265 raise ValueError("Unsupported submit mode given ('{0}').".format(mode))
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
266
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
267 if mode == "raw":
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
268 return self.__execute_raw(commands, timeout=timeout)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
269 elif mode == "paste":
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
270 return self.__execute_paste(commands, timeout=timeout)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
271 else:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
272 # just in case
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
273 return b"", b""
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
274
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
275 def __execute_raw(self, commands, timeout=0):
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 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
278 and return the result.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
279
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
280 If no serial connection is available, empty results will be returned.
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 @param commands list of commands to be executed
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
283 @type str or list of str
9775
c6806d24468b Created new branch <mpy_network>.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9766
diff changeset
284 @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
285 (defaults to 0)
c6806d24468b Created new branch <mpy_network>.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9766
diff changeset
286 @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
287 @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
288 @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
289 """
8e10acb1cd85 Refactored and improved 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 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
291 return b"", b""
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
292
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
293 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
294 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
295
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
296 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
297 err = b""
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
298
9765
6378da868bb0 Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9764
diff changeset
299 if isinstance(commands, str):
6378da868bb0 Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9764
diff changeset
300 commands = [commands]
6378da868bb0 Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9764
diff changeset
301
7112
701cdc76f887 MicroPythonCommandsInterface: made the switch to raw mode more speedy and changed the device code to avoid namespace pollution.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7108
diff changeset
302 # 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
303 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
304 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
305 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
306 self.__blockReadyRead = False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
307 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
308
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
309 # 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
310 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
311 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
312 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
313 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
314 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
315 QCoreApplication.processEvents(
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
316 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
317 )
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
318 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
319 if ok != b"OK":
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
320 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
321 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
322 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
323 "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
324 ok, self.__serial.readAll()
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
325 ).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
326 )
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
327
7095
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
328 # read until prompt
9775
c6806d24468b Created new branch <mpy_network>.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9766
diff changeset
329 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
330 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
331 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
332 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
333 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
334 # 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
335 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
336 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
337 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
338 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
339 if err:
9764
57496966803c Fixed a few issues in the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9760
diff changeset
340 result = b""
57496966803c Fixed a few issues in the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9760
diff changeset
341 break
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
342
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
343 # 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
344 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
345 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
346 self.__blockReadyRead = False
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
347
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
348 return bytes(result), err
9221
bf71ee032bb4 Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9209
diff changeset
349
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
350 def __execute_paste(self, commands, timeout=0):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
351 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
352 Private method to send commands to the connected device using 'paste' mode
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
353 and return the result.
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
354
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
355 If no serial connection is available, empty results will be returned.
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 @param commands list of commands to be executed
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
358 @type str or list of str
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
359 @param timeout per command timeout in milliseconds (0 for configured default)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
360 (defaults to 0)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
361 @type int (optional)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
362 @return tuple containing stdout and stderr output of the device
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
363 @rtype tuple of (bytes, bytes)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
364 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
365 if not self.__serial:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
366 return b"", b""
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
367
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
368 if not self.__serial.isConnected():
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
369 return b"", b"Device not connected or not switched on."
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
370
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
371 if isinstance(commands, list):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
372 commands = "\n".join(commands)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
373
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
374 # switch on raw mode
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
375 self.__blockReadyRead = True
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
376 ok = self.__pasteOn()
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
377 if not ok:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
378 self.__blockReadyRead = False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
379 return (b"", b"Could not switch to raw mode. Is the device switched on?")
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
380
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
381 # send commands
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
382 QThread.msleep(10)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
383 for command in commands.splitlines(keepends=True):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
384 # send the data as single lines
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
385 commandBytes = command.encode("utf-8")
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
386 self.__serial.write(commandBytes)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
387 QCoreApplication.processEvents(
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
388 QEventLoop.ProcessEventsFlag.ExcludeUserInputEvents
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
389 )
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
390 QThread.msleep(10)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
391 ok = self.__serial.readUntil(commandBytes)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
392 if ok != commandBytes:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
393 self.__blockReadyRead = False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
394 return (
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
395 b"",
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
396 "Expected '{0}', got '{1}', followed by '{2}'".format(
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
397 commandBytes, ok, self.__serial.readAll()
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
398 ).encode("utf-8"),
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
399 )
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
400
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
401 # switch off paste mode causing the commands to be executed
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
402 self.__pasteOff()
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
403 QThread.msleep(10)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
404 result = self.__serial.readUntil(b">>> ", timeout=timeout).replace(b">>> ", b"")
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
405 # read until Python prompt
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
406 if self.__serial.hasTimedOut():
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
407 self.__blockReadyRead = False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
408 return b"", b"Timeout while processing commands."
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
409
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
410 if self.TracebackMarker in result:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
411 errorIndex = result.find(self.TracebackMarker)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
412 out, err = result[:errorIndex], result[errorIndex:]
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
413 else:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
414 out = result.strip()
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
415 err = b""
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
416
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
417 self.__blockReadyRead = False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
418 return out, err
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
419
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
420 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
421 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
422 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
423 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
424
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
425 @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
426 @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
427 """
8e10acb1cd85 Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
428 if commandsList:
9764
57496966803c Fixed a few issues in the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9760
diff changeset
429 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
430 self.__serial.write(command)
9764
57496966803c Fixed a few issues in the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9760
diff changeset
431 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
432 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
433 self.executeAsyncFinished.emit()
9799
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
434
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
435 def executeAsyncPaste(self, commandsList):
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
436 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
437 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
438 without returning any result (asynchronous execution).
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
439
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
440 @param commandsList list of commands to be execute on the device
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
441 @type list of bytes
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
442 """
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
443 if commandsList:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
444 self.__blockReadyRead = True
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
445 command = commandsList.pop(0)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
446 if command == "@PasteOn@":
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
447 self.__pasteOn()
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
448 else:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
449 self.__serial.write(command)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
450 self.__serial.readUntil(command)
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
451 QTimer.singleShot(2, lambda: self.executeAsyncPaste(commandsList))
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
452 else:
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
453 self.__blockReadyRead = False
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
454 self.__pasteOff()
a79430a8811d MicroPython
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 9775
diff changeset
455 self.executeAsyncFinished.emit()

eric ide

mercurial