Sat, 29 Jul 2023 19:27:18 +0200
MicroPython
- Optimized the MicroPython super menu handling a little bit.
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
1 | # -*- coding: utf-8 -*- |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
2 | |
9653
e67609152c5e
Updated copyright for 2023.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9624
diff
changeset
|
3 | # Copyright (c) 2019 - 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
4 | # |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
5 | |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
6 | """ |
8119
1653972f2de5
EspDevices: fixed a typo and a little issue.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8117
diff
changeset
|
7 | Module implementing the device interface class for ESP32 and ESP8266 based |
1653972f2de5
EspDevices: fixed a typo and a little issue.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8117
diff
changeset
|
8 | boards. |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
9 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
10 | |
9795 | 11 | import ast |
12 | import binascii | |
13 | import json | |
14 | import os | |
15 | ||
9820 | 16 | from PyQt6.QtCore import QCoreApplication, QProcess, QUrl, pyqtSlot |
17 | from PyQt6.QtNetwork import QNetworkReply, QNetworkRequest | |
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
18 | from PyQt6.QtWidgets import QDialog, QMenu |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
19 | |
9749 | 20 | from eric7 import Globals, Preferences |
9795 | 21 | from eric7.EricGui.EricOverrideCursor import EricOverrideCursor |
9413
80c06d472826
Changed the eric7 import statements to include the package name (i.e. eric7) in order to not fiddle with sys.path.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
22 | from eric7.EricWidgets import EricMessageBox |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
23 | from eric7.EricWidgets.EricApplication import ericApp |
9413
80c06d472826
Changed the eric7 import statements to include the package name (i.e. eric7) in order to not fiddle with sys.path.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
24 | from eric7.EricWidgets.EricProcessDialog import EricProcessDialog |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
25 | from eric7.SystemUtilities import PythonUtilities |
7065
e3d04faced34
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7059
diff
changeset
|
26 | |
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
27 | from ..MicroPythonWidget import HAS_QTCHART |
9756
9854647c8c5c
Reorganized the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9752
diff
changeset
|
28 | from . import FirmwareGithubUrls |
9870
0399d3607829
Fixed a few code formatting and style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9868
diff
changeset
|
29 | from .CircuitPythonDevices import CircuitPythonDevice |
9756
9854647c8c5c
Reorganized the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9752
diff
changeset
|
30 | from .DeviceBase import BaseDevice |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
31 | |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
32 | |
9756
9854647c8c5c
Reorganized the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9752
diff
changeset
|
33 | class EspDevice(BaseDevice): |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
34 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
35 | Class implementing the device for ESP32 and ESP8266 based boards. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
36 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
37 | |
8117
aaa5e0eacd4e
MicroPython: changed the logic to synchronize the time because some devices don't implement long integer and failed defining the 'set_time()' function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8096
diff
changeset
|
38 | def __init__(self, microPythonWidget, deviceType, parent=None): |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
39 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
40 | Constructor |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
41 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
42 | @param microPythonWidget reference to the main MicroPython widget |
7134
21d23ca51680
Renamed "MicroPythonReplWidget" to "MicroPythonWidget".
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7125
diff
changeset
|
43 | @type MicroPythonWidget |
8117
aaa5e0eacd4e
MicroPython: changed the logic to synchronize the time because some devices don't implement long integer and failed defining the 'set_time()' function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8096
diff
changeset
|
44 | @param deviceType device type assigned to this device interface |
aaa5e0eacd4e
MicroPython: changed the logic to synchronize the time because some devices don't implement long integer and failed defining the 'set_time()' function.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8096
diff
changeset
|
45 | @type str |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
46 | @param parent reference to the parent object |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
47 | @type QObject |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
48 | """ |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8205
diff
changeset
|
49 | super().__init__(microPythonWidget, deviceType, parent) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
50 | |
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
51 | self.__createEsp32Submenu() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
52 | |
9866 | 53 | self.__cpyDevice = None |
54 | # needed to delegate some methods to a CircuitPython variant | |
55 | ||
9795 | 56 | self.__statusTranslations = { |
57 | 200: self.tr("beacon timeout"), | |
58 | 201: self.tr("no matching access point found"), | |
59 | 202: self.tr("authentication failed"), | |
60 | 203: self.tr("association failed"), | |
61 | 204: self.tr("handshake timeout"), | |
62 | 1000: self.tr("idle"), | |
63 | 1001: self.tr("connecting"), | |
64 | 1010: self.tr("connected"), | |
65 | } | |
66 | self.__securityTranslations = { | |
67 | 0: self.tr("open", "open WiFi network"), | |
68 | 1: "WEP", | |
69 | 2: "WPA", | |
70 | 3: "WPA2", | |
71 | 4: "WPA/WPA2", | |
72 | 5: "WPA2 (CCMP)", | |
73 | 6: "WPA3", | |
74 | 7: "WPA2/WPA3", | |
75 | } | |
76 | ||
9866 | 77 | def __createCpyDevice(self): |
78 | """ | |
79 | Private method to create a CircuitPython device interface. | |
80 | """ | |
81 | if self.hasCircuitPython() and self.__cpyDevice is None: | |
82 | self.__cpyDevice = CircuitPythonDevice( | |
83 | self.microPython, | |
84 | "esp32_circuitpython", | |
85 | "esp32", | |
86 | hasWorkspace=False, | |
87 | parent=self.parent(), | |
88 | ) | |
89 | ||
90 | def setConnected(self, connected): | |
91 | """ | |
92 | Public method to set the connection state. | |
93 | ||
94 | Note: This method can be overwritten to perform actions upon connect | |
95 | or disconnect of the device. | |
96 | ||
97 | @param connected connection state | |
98 | @type bool | |
99 | """ | |
100 | super().setConnected(connected) | |
101 | ||
102 | if self.hasCircuitPython(): | |
103 | self._submitMode = "paste" | |
104 | self.__createCpyDevice() | |
105 | ||
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
106 | def setButtons(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
107 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
108 | Public method to enable the supported action buttons. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
109 | """ |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8205
diff
changeset
|
110 | super().setButtons() |
9763
52f982c08301
Removed the 'Open' and 'Save' buttons from the MicroPython widget and made the repl and file manager start automatically upon connecting to the selected device.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9756
diff
changeset
|
111 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
112 | self.microPython.setActionButtons( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
113 | run=True, repl=True, files=True, chart=HAS_QTCHART |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
114 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
115 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
116 | def forceInterrupt(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
117 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
118 | Public method to determine the need for an interrupt when opening the |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
119 | serial connection. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
120 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
121 | @return flag indicating an interrupt is needed |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
122 | @rtype bool |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
123 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
124 | return True |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
125 | |
7125
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7115
diff
changeset
|
126 | def deviceName(self): |
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7115
diff
changeset
|
127 | """ |
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7115
diff
changeset
|
128 | Public method to get the name of the device. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
129 | |
7125
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7115
diff
changeset
|
130 | @return name of the device |
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7115
diff
changeset
|
131 | @rtype str |
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7115
diff
changeset
|
132 | """ |
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7115
diff
changeset
|
133 | return self.tr("ESP8266, ESP32") |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
134 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
135 | def canStartRepl(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
136 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
137 | Public method to determine, if a REPL can be started. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
138 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
139 | @return tuple containing a flag indicating it is safe to start a REPL |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
140 | and a reason why it cannot. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
141 | @rtype tuple of (bool, str) |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
142 | """ |
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:
7091
diff
changeset
|
143 | return True, "" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
144 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
145 | def canStartPlotter(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
146 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
147 | Public method to determine, if a Plotter can be started. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
148 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
149 | @return tuple containing a flag indicating it is safe to start a |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
150 | Plotter and a reason why it cannot. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
151 | @rtype tuple of (bool, str) |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
152 | """ |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7091
diff
changeset
|
153 | return True, "" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
154 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
155 | def canRunScript(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
156 | """ |
7091
84d2a73b448a
EspDevices, MicroPythonDevices: fixed a wrong source docu string.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
157 | Public method to determine, if a script can be executed. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
158 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
159 | @return tuple containing a flag indicating it is safe to start a |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
160 | Plotter and a reason why it cannot. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
161 | @rtype tuple of (bool, str) |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
162 | """ |
8119
1653972f2de5
EspDevices: fixed a typo and a little issue.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8117
diff
changeset
|
163 | return True, "" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
164 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
165 | def runScript(self, script): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
166 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
167 | Public method to run the given Python script. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
168 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
169 | @param script script to be executed |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
170 | @type str |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
171 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
172 | pythonScript = script.split("\n") |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
173 | self.sendCommands(pythonScript) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
174 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
175 | def canStartFileManager(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
176 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
177 | Public method to determine, if a File Manager can be started. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
178 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
179 | @return tuple containing a flag indicating it is safe to start a |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
180 | File Manager and a reason why it cannot. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
181 | @rtype tuple of (bool, str) |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
182 | """ |
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:
7091
diff
changeset
|
183 | return True, "" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
184 | |
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
185 | def __createEsp32Submenu(self): |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
186 | """ |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
187 | Private method to create the ESP32 submenu. |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
188 | """ |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
189 | self.__espMenu = QMenu(self.tr("ESP32 Functions")) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
190 | |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
191 | self.__showMpyAct = self.__espMenu.addAction( |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
192 | self.tr("Show MicroPython Versions"), self.__showFirmwareVersions |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
193 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
194 | self.__espMenu.addSeparator() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
195 | self.__eraseFlashAct = self.__espMenu.addAction( |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
196 | self.tr("Erase Flash"), self.__eraseFlash |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
197 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
198 | self.__flashMpyAct = self.__espMenu.addAction( |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
199 | self.tr("Flash MicroPython Firmware"), self.__flashMicroPython |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
200 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
201 | self.__espMenu.addSeparator() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
202 | self.__flashAdditionalAct = self.__espMenu.addAction( |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
203 | self.tr("Flash Additional Firmware"), self.__flashAddons |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
204 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
205 | self.__espMenu.addSeparator() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
206 | self.__backupAct = self.__espMenu.addAction( |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
207 | self.tr("Backup Firmware"), self.__backupFlash |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
208 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
209 | self.__restoreAct = self.__espMenu.addAction( |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
210 | self.tr("Restore Firmware"), self.__restoreFlash |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
211 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
212 | self.__espMenu.addSeparator() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
213 | self.__chipIdAct = self.__espMenu.addAction( |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
214 | self.tr("Show Chip ID"), self.__showChipID |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
215 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
216 | self.__flashIdAct = self.__espMenu.addAction( |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
217 | self.tr("Show Flash ID"), self.__showFlashID |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
218 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
219 | self.__macAddressAct = self.__espMenu.addAction( |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
220 | self.tr("Show MAC Address"), self.__showMACAddress |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
221 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
222 | self.__espMenu.addSeparator() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
223 | self.__resetAct = self.__espMenu.addAction( |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
224 | self.tr("Reset Device"), self.__resetDevice |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
225 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
226 | self.__espMenu.addSeparator() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
227 | self.__espMenu.addAction(self.tr("Install 'esptool.py'"), self.__installEspTool) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
228 | |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
229 | def addDeviceMenuEntries(self, menu): |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
230 | """ |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
231 | Public method to add device specific entries to the given menu. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
232 | |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
233 | @param menu reference to the context menu |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
234 | @type QMenu |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
235 | """ |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
236 | connected = self.microPython.isConnected() |
9749 | 237 | linkConnected = self.microPython.isLinkConnected() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
238 | |
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
239 | self.__showMpyAct.setEnabled(connected) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
240 | self.__eraseFlashAct.setEnabled(not linkConnected) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
241 | self.__flashMpyAct.setEnabled(not linkConnected) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
242 | self.__flashAdditionalAct.setEnabled(not linkConnected) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
243 | self.__backupAct.setEnabled(not linkConnected) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
244 | self.__restoreAct.setEnabled(not linkConnected) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
245 | self.__chipIdAct.setEnabled(not linkConnected) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
246 | self.__flashIdAct.setEnabled(not linkConnected) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
247 | self.__macAddressAct.setEnabled(not linkConnected) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
248 | self.__resetAct.setEnabled(connected or not linkConnected) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
249 | |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
250 | menu.addMenu(self.__espMenu) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
251 | |
8096 | 252 | def hasFlashMenuEntry(self): |
253 | """ | |
254 | Public method to check, if the device has its own flash menu entry. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
255 | |
8096 | 256 | @return flag indicating a specific flash menu entry |
257 | @rtype bool | |
258 | """ | |
259 | return True | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
260 | |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
261 | @pyqtSlot() |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
262 | def __eraseFlash(self): |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
263 | """ |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
264 | Private slot to erase the device flash memory. |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
265 | """ |
9820 | 266 | eraseFlash(self.microPython.getCurrentPort()) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
267 | |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
268 | @pyqtSlot() |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
269 | def __flashMicroPython(self): |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
270 | """ |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
271 | Private slot to flash a MicroPython firmware to the device. |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
272 | """ |
9820 | 273 | flashPythonFirmware(self.microPython.getCurrentPort()) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
274 | |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
275 | @pyqtSlot() |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
276 | def __flashAddons(self): |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
277 | """ |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
278 | Private slot to flash some additional firmware images. |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
279 | """ |
9820 | 280 | flashAddonFirmware(self.microPython.getCurrentPort()) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
281 | |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
282 | @pyqtSlot() |
7352 | 283 | def __backupFlash(self): |
284 | """ | |
285 | Private slot to backup the currently flashed firmware. | |
286 | """ | |
9756
9854647c8c5c
Reorganized the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9752
diff
changeset
|
287 | from .EspDialogs.EspBackupRestoreFirmwareDialog import ( |
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
288 | EspBackupRestoreFirmwareDialog, |
9756
9854647c8c5c
Reorganized the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9752
diff
changeset
|
289 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
290 | |
7352 | 291 | dlg = EspBackupRestoreFirmwareDialog(backupMode=True) |
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:
8119
diff
changeset
|
292 | if dlg.exec() == QDialog.DialogCode.Accepted: |
8945 | 293 | chip, flashSize, baudRate, flashMode, firmware = dlg.getData() |
7352 | 294 | flashArgs = [ |
295 | "-u", | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
296 | "-m", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
297 | "esptool", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
298 | "--chip", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
299 | chip, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
300 | "--port", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
301 | self.microPython.getCurrentPort(), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
302 | "--baud", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
303 | baudRate, |
7352 | 304 | "read_flash", |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
305 | "0x0", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
306 | flashSize, |
7352 | 307 | firmware, |
308 | ] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
309 | dlg = EricProcessDialog( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
310 | self.tr("'esptool read_flash' Output"), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
311 | self.tr("Backup Firmware"), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
312 | showProgress=True, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
313 | ) |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
314 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs) |
7352 | 315 | if res: |
7759
51aa6c6b66f7
Changed calls to exec_() into exec() (remainder of Python2 elimination).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7595
diff
changeset
|
316 | dlg.exec() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
317 | |
7352 | 318 | @pyqtSlot() |
319 | def __restoreFlash(self): | |
320 | """ | |
321 | Private slot to restore a previously saved firmware. | |
322 | """ | |
9756
9854647c8c5c
Reorganized the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9752
diff
changeset
|
323 | from .EspDialogs.EspBackupRestoreFirmwareDialog import ( |
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
324 | EspBackupRestoreFirmwareDialog, |
9756
9854647c8c5c
Reorganized the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9752
diff
changeset
|
325 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
326 | |
7352 | 327 | dlg = EspBackupRestoreFirmwareDialog(backupMode=False) |
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:
8119
diff
changeset
|
328 | if dlg.exec() == QDialog.DialogCode.Accepted: |
8945 | 329 | chip, flashSize, baudRate, flashMode, firmware = dlg.getData() |
7352 | 330 | flashArgs = [ |
331 | "-u", | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
332 | "-m", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
333 | "esptool", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
334 | "--chip", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
335 | chip, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
336 | "--port", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
337 | self.microPython.getCurrentPort(), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
338 | "--baud", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
339 | baudRate, |
7352 | 340 | "write_flash", |
341 | ] | |
8945 | 342 | if flashMode: |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
343 | flashArgs.extend( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
344 | [ |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
345 | "--flash_mode", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
346 | flashMode, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
347 | ] |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
348 | ) |
7352 | 349 | if bool(flashSize): |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
350 | flashArgs.extend( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
351 | [ |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
352 | "--flash_size", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
353 | flashSize, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
354 | ] |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
355 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
356 | flashArgs.extend( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
357 | [ |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
358 | "0x0", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
359 | firmware, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
360 | ] |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
361 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
362 | dlg = EricProcessDialog( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
363 | self.tr("'esptool write_flash' Output"), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
364 | self.tr("Restore Firmware"), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
365 | showProgress=True, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
366 | ) |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
367 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs) |
7352 | 368 | if res: |
7759
51aa6c6b66f7
Changed calls to exec_() into exec() (remainder of Python2 elimination).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7595
diff
changeset
|
369 | dlg.exec() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
370 | |
7352 | 371 | @pyqtSlot() |
9749 | 372 | def __showFirmwareVersions(self): |
373 | """ | |
374 | Private slot to show the firmware version of the connected device and the | |
375 | available firmware version. | |
376 | """ | |
9866 | 377 | if self.hasCircuitPython(): |
9870
0399d3607829
Fixed a few code formatting and style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9868
diff
changeset
|
378 | self.__cpyDevice.showCircuitPythonVersions() |
9866 | 379 | |
9749 | 380 | if self.microPython.isConnected(): |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
381 | if self._deviceData["mpy_name"] == "micropython": |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
382 | url = QUrl(FirmwareGithubUrls["micropython"]) |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
383 | elif self._deviceData["mpy_name"] == "circuitpython": |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
384 | url = QUrl(FirmwareGithubUrls["circuitpython"]) |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
385 | else: |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
386 | EricMessageBox.critical( |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
387 | None, |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
388 | self.tr("Show MicroPython Versions"), |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
389 | self.tr( |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
390 | """The firmware of the connected device cannot be""" |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
391 | """ determined or the board does not run MicroPython""" |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
392 | """ or CircuitPython. Aborting...""" |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
393 | ), |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
394 | ) |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
395 | return |
9749 | 396 | |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
397 | ui = ericApp().getObject("UserInterface") |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
398 | request = QNetworkRequest(url) |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
399 | reply = ui.networkAccessManager().head(request) |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
400 | reply.finished.connect(lambda: self.__firmwareVersionResponse(reply)) |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
401 | |
9820 | 402 | @pyqtSlot(QNetworkReply) |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
403 | def __firmwareVersionResponse(self, reply): |
9749 | 404 | """ |
9820 | 405 | Private slot handling the response of the latest version request. |
9749 | 406 | |
407 | @param reply reference to the reply object | |
408 | @type QNetworkReply | |
409 | """ | |
410 | latestUrl = reply.url().toString() | |
411 | tag = latestUrl.rsplit("/", 1)[-1] | |
412 | while tag and not tag[0].isdecimal(): | |
413 | # get rid of leading non-decimal characters | |
414 | tag = tag[1:] | |
415 | latestVersion = Globals.versionToTuple(tag) | |
416 | ||
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
417 | if self._deviceData["mpy_version"] == "unknown": |
9749 | 418 | currentVersionStr = self.tr("unknown") |
419 | currentVersion = (0, 0, 0) | |
420 | else: | |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
421 | currentVersionStr = self._deviceData["mpy_version"] |
9749 | 422 | currentVersion = Globals.versionToTuple(currentVersionStr) |
423 | ||
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
424 | if self._deviceData["mpy_name"] == "circuitpython": |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
425 | kind = "CircuitPython" |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
426 | elif self._deviceData["mpy_name"] == "micropython": |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
427 | kind = "MicroPython" |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
428 | |
9749 | 429 | msg = self.tr( |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
430 | "<h4>{0} Version Information</h4>" |
9749 | 431 | "<table>" |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
432 | "<tr><td>Installed:</td><td>{1}</td></tr>" |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
433 | "<tr><td>Available:</td><td>{2}</td></tr>" |
9749 | 434 | "</table>" |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
435 | ).format(kind, currentVersionStr, tag) |
9749 | 436 | if currentVersion < latestVersion: |
437 | msg += self.tr("<p><b>Update available!</b></p>") | |
438 | ||
439 | EricMessageBox.information( | |
440 | None, | |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
441 | self.tr("{0} Version").format(kind), |
9749 | 442 | msg, |
443 | ) | |
444 | ||
445 | @pyqtSlot() | |
7346 | 446 | def __showChipID(self): |
447 | """ | |
448 | Private slot to show the ID of the ESP chip. | |
449 | """ | |
450 | args = [ | |
451 | "-u", | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
452 | "-m", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
453 | "esptool", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
454 | "--port", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
455 | self.microPython.getCurrentPort(), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
456 | "chip_id", |
7346 | 457 | ] |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
458 | dlg = EricProcessDialog( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
459 | self.tr("'esptool chip_id' Output"), self.tr("Show Chip ID") |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
460 | ) |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
461 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), args) |
7346 | 462 | if res: |
7759
51aa6c6b66f7
Changed calls to exec_() into exec() (remainder of Python2 elimination).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7595
diff
changeset
|
463 | dlg.exec() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
464 | |
7346 | 465 | @pyqtSlot() |
466 | def __showFlashID(self): | |
467 | """ | |
468 | Private slot to show the ID of the ESP flash chip. | |
469 | """ | |
470 | args = [ | |
471 | "-u", | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
472 | "-m", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
473 | "esptool", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
474 | "--port", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
475 | self.microPython.getCurrentPort(), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
476 | "flash_id", |
7346 | 477 | ] |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
478 | dlg = EricProcessDialog( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
479 | self.tr("'esptool flash_id' Output"), self.tr("Show Flash ID") |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
480 | ) |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
481 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), args) |
7346 | 482 | if res: |
7759
51aa6c6b66f7
Changed calls to exec_() into exec() (remainder of Python2 elimination).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7595
diff
changeset
|
483 | dlg.exec() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
484 | |
7346 | 485 | @pyqtSlot() |
486 | def __showMACAddress(self): | |
487 | """ | |
488 | Private slot to show the MAC address of the ESP chip. | |
489 | """ | |
490 | args = [ | |
491 | "-u", | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
492 | "-m", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
493 | "esptool", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
494 | "--port", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
495 | self.microPython.getCurrentPort(), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
496 | "read_mac", |
7346 | 497 | ] |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
498 | dlg = EricProcessDialog( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
499 | self.tr("'esptool read_mac' Output"), self.tr("Show MAC Address") |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
500 | ) |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
501 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), args) |
7346 | 502 | if res: |
7759
51aa6c6b66f7
Changed calls to exec_() into exec() (remainder of Python2 elimination).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7595
diff
changeset
|
503 | dlg.exec() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
504 | |
7346 | 505 | @pyqtSlot() |
7174
de8175253dfc
MicroPython: did some fine tuning of the MicroPython interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7173
diff
changeset
|
506 | def __resetDevice(self): |
de8175253dfc
MicroPython: did some fine tuning of the MicroPython interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7173
diff
changeset
|
507 | """ |
de8175253dfc
MicroPython: did some fine tuning of the MicroPython interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7173
diff
changeset
|
508 | Private slot to reset the connected device. |
de8175253dfc
MicroPython: did some fine tuning of the MicroPython interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7173
diff
changeset
|
509 | """ |
9866 | 510 | if self.microPython.isConnected() and not self.hasCircuitPython(): |
9989 | 511 | self.executeCommands( |
9834 | 512 | "import machine\nmachine.reset()\n", mode=self._submitMode |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
513 | ) |
7353
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
514 | else: |
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
515 | # perform a reset via esptool using flash_id command ignoring |
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
516 | # the output |
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
517 | args = [ |
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
518 | "-u", |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
519 | "-m", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
520 | "esptool", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
521 | "--port", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
522 | self.microPython.getCurrentPort(), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
523 | "flash_id", |
7353
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
524 | ] |
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
525 | proc = QProcess() |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
526 | proc.start(PythonUtilities.getPythonExecutable(), args) |
7353
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
527 | procStarted = proc.waitForStarted(10000) |
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
528 | if procStarted: |
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
529 | proc.waitForFinished(10000) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
530 | |
7174
de8175253dfc
MicroPython: did some fine tuning of the MicroPython interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7173
diff
changeset
|
531 | @pyqtSlot() |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
532 | def __installEspTool(self): |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
533 | """ |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
534 | Private slot to install the esptool package via pip. |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
535 | """ |
8356
68ec9c3d4de5
Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8318
diff
changeset
|
536 | pip = ericApp().getObject("Pip") |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
537 | pip.installPackages( |
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
538 | ["esptool"], interpreter=PythonUtilities.getPythonExecutable() |
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
539 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
540 | |
7161
728018c32b09
MicroPythonWidget: added actions to show the device documentation and to open the configuration page to the hamburger menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
541 | def getDocumentationUrl(self): |
728018c32b09
MicroPythonWidget: added actions to show the device documentation and to open the configuration page to the hamburger menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
542 | """ |
728018c32b09
MicroPythonWidget: added actions to show the device documentation and to open the configuration page to the hamburger menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
543 | Public method to get the device documentation URL. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
544 | |
7161
728018c32b09
MicroPythonWidget: added actions to show the device documentation and to open the configuration page to the hamburger menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
545 | @return documentation URL of the device |
728018c32b09
MicroPythonWidget: added actions to show the device documentation and to open the configuration page to the hamburger menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
546 | @rtype str |
728018c32b09
MicroPythonWidget: added actions to show the device documentation and to open the configuration page to the hamburger menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
547 | """ |
9866 | 548 | if self.hasCircuitPython(): |
549 | return self.__cpyDevice.getDocumentationUrl() | |
550 | ||
7161
728018c32b09
MicroPythonWidget: added actions to show the device documentation and to open the configuration page to the hamburger menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
551 | return Preferences.getMicroPython("MicroPythonDocuUrl") |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
552 | |
7328 | 553 | def getFirmwareUrl(self): |
554 | """ | |
555 | Public method to get the device firmware download URL. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
556 | |
7328 | 557 | @return firmware download URL of the device |
558 | @rtype str | |
559 | """ | |
9866 | 560 | if self.hasCircuitPython(): |
561 | return self.__cpyDevice.getFirmwareUrl() | |
562 | ||
7328 | 563 | return Preferences.getMicroPython("MicroPythonFirmwareUrl") |
9496
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
564 | |
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
565 | ################################################################## |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
566 | ## time related methods below |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
567 | ################################################################## |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
568 | |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
569 | def _getSetTimeCode(self): |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
570 | """ |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
571 | Protected method to get the device code to set the time. |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
572 | |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
573 | Note: This method must be implemented in the various device specific |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
574 | subclasses. |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
575 | |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
576 | @return code to be executed on the connected device to set the time |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
577 | @rtype str |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
578 | """ |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
579 | # rtc_time[0] - year 4 digit |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
580 | # rtc_time[1] - month 1..12 |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
581 | # rtc_time[2] - day 1..31 |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
582 | # rtc_time[3] - weekday 1..7 1=Monday |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
583 | # rtc_time[4] - hour 0..23 |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
584 | # rtc_time[5] - minute 0..59 |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
585 | # rtc_time[6] - second 0..59 |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
586 | # rtc_time[7] - yearday 1..366 |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
587 | # rtc_time[8] - isdst 0, 1, or -1 |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
588 | |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
589 | # The machine.RTC.init() (ESP32) and machine.rtc.datetime() (ESP8266) functions |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
590 | # take the arguments in the order: |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
591 | # (year, month, day, weekday, hour, minute, second, subseconds) |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
592 | # __IGNORE_WARNING_M891__ |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
593 | # https://docs.micropython.org/en/latest/library/machine.RTC.html#machine-rtc |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
594 | # |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
595 | # LoBo variant of MPy deviates. |
9866 | 596 | if self.hasCircuitPython(): |
597 | return super()._getSetTimeCode() | |
598 | ||
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
599 | return """ |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
600 | def set_time(rtc_time): |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
601 | import machine |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
602 | rtc = machine.RTC() |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
603 | try: |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
604 | rtc.datetime(rtc_time[:7] + (0,)) |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
605 | except Exception: |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
606 | import os |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
607 | if 'LoBo' in os.uname()[0]: |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
608 | clock_time = rtc_time[:3] + rtc_time[4:7] + (rtc_time[3], rtc_time[7]) |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
609 | else: |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
610 | clock_time = rtc_time[:7] + (0,) |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
611 | rtc.init(clock_time) |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
612 | """ |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
613 | |
9795 | 614 | ################################################################## |
615 | ## Methods below implement WiFi related methods | |
616 | ################################################################## | |
617 | ||
618 | def hasWifi(self): | |
619 | """ | |
620 | Public method to check the availability of WiFi. | |
621 | ||
622 | @return tuple containing a flag indicating the availability of WiFi | |
623 | and the WiFi type (esp32) | |
624 | @rtype tuple of (bool, str) | |
625 | """ | |
9866 | 626 | if self.hasCircuitPython(): |
627 | self.__createCpyDevice() | |
628 | return self.__cpyDevice.hasWifi() | |
629 | ||
9795 | 630 | return True, "esp32" |
631 | ||
632 | def getWifiData(self): | |
633 | """ | |
634 | Public method to get data related to the current WiFi status. | |
635 | ||
9798 | 636 | @return tuple of three dictionaries containing the WiFi status data |
637 | for the WiFi client, access point and overall data | |
638 | @rtype tuple of (dict, dict, dict) | |
9795 | 639 | @exception OSError raised to indicate an issue with the device |
640 | """ | |
9866 | 641 | if self.hasCircuitPython(): |
642 | return self.__cpyDevice.getWifiData() | |
643 | ||
9795 | 644 | command = """ |
645 | def wifi_status(): | |
646 | import ubinascii | |
647 | import ujson | |
648 | import network | |
649 | ||
650 | wifi = network.WLAN(network.STA_IF) | |
651 | station = { | |
652 | 'active': wifi.active(), | |
653 | 'connected': wifi.isconnected(), | |
654 | 'status': wifi.status(), | |
655 | 'ifconfig': wifi.ifconfig(), | |
656 | 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), | |
657 | } | |
658 | if wifi.active(): | |
9797 | 659 | try: |
660 | station['txpower'] = wifi.config('txpower') | |
661 | except ValueError: | |
662 | pass | |
9795 | 663 | print(ujson.dumps(station)) |
664 | ||
665 | wifi = network.WLAN(network.AP_IF) | |
666 | ap = { | |
667 | 'active': wifi.active(), | |
668 | 'connected': wifi.isconnected(), | |
669 | 'status': wifi.status(), | |
670 | 'ifconfig': wifi.ifconfig(), | |
671 | 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), | |
672 | 'channel': wifi.config('channel'), | |
673 | 'essid': wifi.config('essid'), | |
674 | } | |
675 | if wifi.active(): | |
9797 | 676 | try: |
677 | ap['txpower'] = wifi.config('txpower') | |
678 | except ValueError: | |
679 | pass | |
9795 | 680 | print(ujson.dumps(ap)) |
681 | ||
9798 | 682 | overall = { |
683 | 'active': station['active'] or ap['active'] | |
684 | } | |
685 | print(ujson.dumps(overall)) | |
686 | ||
9795 | 687 | wifi_status() |
688 | del wifi_status | |
689 | """ | |
690 | ||
9989 | 691 | out, err = self.executeCommands(command, mode=self._submitMode) |
9795 | 692 | if err: |
693 | raise OSError(self._shortError(err)) | |
694 | ||
9798 | 695 | stationStr, apStr, overallStr = out.decode("utf-8").splitlines() |
9795 | 696 | station = json.loads(stationStr) |
697 | ap = json.loads(apStr) | |
9798 | 698 | overall = json.loads(overallStr) |
9795 | 699 | try: |
700 | station["status"] = self.__statusTranslations[station["status"]] | |
701 | except KeyError: | |
702 | station["status"] = str(station["status"]) | |
703 | try: | |
704 | ap["status"] = self.__statusTranslations[ap["status"]] | |
705 | except KeyError: | |
706 | ap["status"] = str(ap["status"]) | |
9798 | 707 | return station, ap, overall |
9795 | 708 | |
709 | def connectWifi(self, ssid, password): | |
710 | """ | |
711 | Public method to connect a device to a WiFi network. | |
712 | ||
713 | @param ssid name (SSID) of the WiFi network | |
714 | @type str | |
715 | @param password password needed to connect | |
716 | @type str | |
717 | @return tuple containing the connection status and an error string | |
718 | @rtype tuple of (bool, str) | |
719 | """ | |
9866 | 720 | if self.hasCircuitPython(): |
721 | return self.__cpyDevice.connectWifi(ssid, password) | |
722 | ||
9795 | 723 | command = """ |
724 | def connect_wifi(ssid, password): | |
725 | import network | |
726 | import ujson | |
727 | from time import sleep | |
728 | ||
729 | wifi = network.WLAN(network.STA_IF) | |
730 | wifi.active(False) | |
731 | wifi.active(True) | |
732 | wifi.connect(ssid, password) | |
733 | max_wait = 140 | |
734 | while max_wait and wifi.status() == network.STAT_CONNECTING: | |
735 | max_wait -= 1 | |
736 | sleep(0.1) | |
737 | status = wifi.status() | |
738 | print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) | |
739 | ||
740 | connect_wifi({0}, {1}) | |
741 | del connect_wifi | |
742 | """.format( | |
9798 | 743 | repr(ssid), |
744 | repr(password if password else ""), | |
745 | ) | |
9795 | 746 | |
747 | with EricOverrideCursor(): | |
9989 | 748 | out, err = self.executeCommands( |
9827 | 749 | command, mode=self._submitMode, timeout=15000 |
9799 | 750 | ) |
9795 | 751 | if err: |
752 | return False, err | |
753 | ||
754 | result = json.loads(out.decode("utf-8").strip()) | |
755 | if result["connected"]: | |
756 | error = "" | |
757 | else: | |
758 | try: | |
759 | error = self.__statusTranslations[result["status"]] | |
760 | except KeyError: | |
761 | error = str(result["status"]) | |
762 | ||
10138 | 763 | self._networkConnected = result["connected"] |
764 | ||
9795 | 765 | return result["connected"], error |
766 | ||
767 | def disconnectWifi(self): | |
768 | """ | |
769 | Public method to disconnect a device from the WiFi network. | |
770 | ||
771 | @return tuple containing a flag indicating success and an error string | |
772 | @rtype tuple of (bool, str) | |
773 | """ | |
9866 | 774 | if self.hasCircuitPython(): |
775 | return self.__cpyDevice.disconnectWifi() | |
776 | ||
9795 | 777 | command = """ |
778 | def disconnect_wifi(): | |
779 | import network | |
780 | from time import sleep | |
781 | ||
782 | wifi = network.WLAN(network.STA_IF) | |
783 | wifi.disconnect() | |
784 | wifi.active(False) | |
785 | sleep(0.1) | |
786 | print(not wifi.isconnected()) | |
787 | ||
788 | disconnect_wifi() | |
789 | del disconnect_wifi | |
790 | """ | |
791 | ||
9989 | 792 | out, err = self.executeCommands(command, mode=self._submitMode) |
9795 | 793 | if err: |
794 | return False, err | |
795 | ||
10138 | 796 | self._networkConnected = False |
797 | ||
9795 | 798 | return out.decode("utf-8").strip() == "True", "" |
799 | ||
800 | def writeCredentials(self, ssid, password): | |
801 | """ | |
802 | Public method to write the given credentials to the connected device and modify | |
803 | the start script to connect automatically. | |
804 | ||
805 | @param ssid SSID of the network to connect to | |
806 | @type str | |
807 | @param password password needed to authenticate | |
808 | @type str | |
809 | @return tuple containing a flag indicating success and an error message | |
810 | @rtype tuple of (bool, str) | |
811 | """ | |
9866 | 812 | if self.hasCircuitPython(): |
813 | return self.__cpyDevice.writeCredentials(ssid, password) | |
814 | ||
9795 | 815 | nvsCommand = """ |
816 | def save_wifi_creds(ssid, password): | |
817 | import esp32 | |
818 | ||
819 | nvs = esp32.NVS('wifi_creds') | |
820 | nvs.set_blob('ssid', ssid) | |
821 | nvs.set_blob('password', password) | |
822 | nvs.commit() | |
823 | ||
824 | save_wifi_creds({0}, {1}) | |
825 | del save_wifi_creds | |
9798 | 826 | """.format( |
827 | repr(ssid), repr(password) if password else "''" | |
828 | ) | |
9795 | 829 | bootCommand = """ |
830 | def modify_boot(): | |
831 | add = True | |
832 | try: | |
833 | with open('/boot.py', 'r') as f: | |
834 | for ln in f.readlines(): | |
835 | if 'wifi_connect' in ln: | |
836 | add = False | |
837 | break | |
838 | except: | |
839 | pass | |
840 | if add: | |
841 | with open('/boot.py', 'a') as f: | |
842 | f.write('\\nimport wifi_connect\\n') | |
843 | print(True) | |
844 | ||
845 | modify_boot() | |
846 | del modify_boot | |
847 | """ | |
848 | ||
9989 | 849 | out, err = self.executeCommands(nvsCommand, mode=self._submitMode) |
9795 | 850 | if err: |
851 | return False, self.tr("Error saving credentials: {0}").format(err) | |
852 | ||
853 | try: | |
854 | # copy auto-connect file | |
855 | self.put( | |
856 | os.path.join( | |
857 | os.path.dirname(__file__), "MCUScripts", "esp32WiFiConnect.py" | |
858 | ), | |
859 | "/wifi_connect.py", | |
860 | ) | |
861 | except OSError as err: | |
862 | return False, self.tr("Error saving auto-connect script: {0}").format(err) | |
863 | ||
9989 | 864 | out, err = self.executeCommands(bootCommand, mode=self._submitMode) |
9795 | 865 | if err: |
866 | return False, self.tr("Error modifying 'boot.py': {0}").format(err) | |
867 | ||
868 | return True, "" | |
869 | ||
870 | def removeCredentials(self): | |
871 | """ | |
872 | Public method to remove the saved credentials from the connected device. | |
873 | ||
874 | @return tuple containing a flag indicating success and an error message | |
875 | @rtype tuple of (bool, str) | |
876 | """ | |
9866 | 877 | if self.hasCircuitPython(): |
878 | return self.__cpyDevice.removeCredentials() | |
879 | ||
9795 | 880 | nvsCommand = """ |
881 | def delete_wifi_creds(): | |
882 | import esp32 | |
883 | ||
884 | nvs = esp32.NVS('wifi_creds') | |
885 | try: | |
886 | nvs.erase_key('ssid') | |
887 | nvs.erase_key('password') | |
888 | nvs.commit() | |
889 | except OSError: | |
890 | pass | |
891 | ||
892 | delete_wifi_creds() | |
893 | del delete_wifi_creds | |
894 | """ | |
895 | ||
9989 | 896 | out, err = self.executeCommands(nvsCommand, mode=self._submitMode) |
9795 | 897 | if err: |
898 | return False, self.tr("Error deleting credentials: {0}").format(err) | |
899 | ||
900 | return True, "" | |
901 | ||
902 | def checkInternet(self): | |
903 | """ | |
904 | Public method to check, if the internet can be reached. | |
905 | ||
906 | @return tuple containing a flag indicating reachability and an error string | |
907 | @rtype tuple of (bool, str) | |
908 | """ | |
9866 | 909 | if self.hasCircuitPython(): |
910 | return self.__cpyDevice.checkInternet() | |
911 | ||
9795 | 912 | command = """ |
913 | def check_internet(): | |
914 | import network | |
915 | import socket | |
916 | ||
917 | wifi = network.WLAN(network.STA_IF) | |
918 | if wifi.isconnected(): | |
919 | s = socket.socket() | |
920 | try: | |
9836 | 921 | s.connect(socket.getaddrinfo('quad9.net', 80)[0][-1]) |
9795 | 922 | s.close() |
923 | print(True) | |
924 | except: | |
925 | print(False) | |
926 | else: | |
927 | print(False) | |
928 | ||
929 | check_internet() | |
930 | del check_internet | |
931 | """ | |
932 | ||
9989 | 933 | out, err = self.executeCommands(command, mode=self._submitMode) |
9795 | 934 | if err: |
935 | return False, err | |
936 | ||
937 | return out.decode("utf-8").strip() == "True", "" | |
938 | ||
939 | def scanNetworks(self): | |
940 | """ | |
941 | Public method to scan for available WiFi networks. | |
942 | ||
943 | @return tuple containing the list of available networks as a tuple of 'Name', | |
944 | 'MAC-Address', 'channel', 'RSSI' and 'security' and an error string | |
945 | @rtype tuple of (list of tuple of (str, str, int, int, str), str) | |
946 | """ | |
9866 | 947 | if self.hasCircuitPython(): |
948 | return self.__cpyDevice.scanNetworks() | |
949 | ||
9795 | 950 | command = """ |
951 | def scan_networks(): | |
952 | import network | |
953 | ||
954 | wifi = network.WLAN(network.STA_IF) | |
955 | active = wifi.active() | |
956 | if not active: | |
957 | wifi.active(True) | |
958 | network_list = wifi.scan() | |
959 | if not active: | |
960 | wifi.active(False) | |
961 | print(network_list) | |
962 | ||
963 | scan_networks() | |
964 | del scan_networks | |
965 | """ | |
966 | ||
9989 | 967 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=15000) |
9795 | 968 | if err: |
969 | return [], err | |
970 | ||
971 | networksList = ast.literal_eval(out.decode("utf-8")) | |
972 | networks = [] | |
973 | for network in networksList: | |
974 | if network[0]: | |
975 | ssid = network[0].decode("utf-8") | |
976 | mac = binascii.hexlify(network[1], ":").decode("utf-8") | |
977 | channel = network[2] | |
978 | rssi = network[3] | |
979 | try: | |
980 | security = self.__securityTranslations[network[4]] | |
981 | except KeyError: | |
982 | security = self.tr("unknown ({0})").format(network[4]) | |
983 | networks.append((ssid, mac, channel, rssi, security)) | |
984 | ||
985 | return networks, "" | |
986 | ||
987 | def deactivateInterface(self, interface): | |
988 | """ | |
989 | Public method to deactivate a given WiFi interface of the connected device. | |
990 | ||
991 | @param interface designation of the interface to be deactivated (one of 'AP' | |
992 | or 'STA') | |
993 | @type str | |
994 | @return tuple containg a flag indicating success and an error message | |
995 | @rtype tuple of (bool, str) | |
996 | @exception ValueError raised to indicate a wrong value for the interface type | |
997 | """ | |
998 | if interface not in ("STA", "AP"): | |
999 | raise ValueError( | |
1000 | "interface must be 'AP' or 'STA', got '{0}'".format(interface) | |
1001 | ) | |
1002 | ||
9866 | 1003 | if self.hasCircuitPython(): |
1004 | return self.__cpyDevice.deactivateInterface(interface) | |
1005 | ||
9795 | 1006 | command = """ |
1007 | def deactivate(): | |
1008 | import network | |
1009 | from time import sleep | |
1010 | ||
1011 | wifi = network.WLAN(network.{0}_IF) | |
1012 | wifi.active(False) | |
1013 | sleep(0.1) | |
1014 | print(not wifi.active()) | |
1015 | ||
1016 | deactivate() | |
1017 | del deactivate | |
1018 | """.format( | |
9798 | 1019 | interface |
1020 | ) | |
9795 | 1021 | |
9989 | 1022 | out, err = self.executeCommands(command, mode=self._submitMode) |
9795 | 1023 | if err: |
1024 | return False, err | |
1025 | else: | |
1026 | return out.decode("utf-8").strip() == "True", "" | |
1027 | ||
9797 | 1028 | def startAccessPoint(self, ssid, security=None, password=None, ifconfig=None): |
9795 | 1029 | """ |
1030 | Public method to start the access point interface. | |
1031 | ||
1032 | @param ssid SSID of the access point | |
1033 | @type str | |
1034 | @param security security method (defaults to None) | |
1035 | @type int (optional) | |
1036 | @param password password (defaults to None) | |
1037 | @type str (optional) | |
9797 | 1038 | @param ifconfig IPv4 configuration for the access point if not default |
1039 | (IPv4 address, netmask, gateway address, DNS server address) | |
1040 | @type tuple of (str, str, str, str) | |
9795 | 1041 | @return tuple containing a flag indicating success and an error message |
1042 | @rtype tuple of (bool, str) | |
1043 | """ | |
9866 | 1044 | if self.hasCircuitPython(): |
1045 | return self.__cpyDevice.startAccessPoint( | |
1046 | ssid, security=security, password=password, ifconfig=ifconfig | |
1047 | ) | |
1048 | ||
9795 | 1049 | if security is None or password is None: |
1050 | security = 0 | |
1051 | password = "" | |
1052 | if security > 4: | |
1053 | security = 4 # security >4 cause an error thrown by the ESP32 | |
1054 | ||
1055 | command = """ | |
9797 | 1056 | def start_ap(ssid, authmode, password, ifconfig): |
9795 | 1057 | import network |
1058 | ||
1059 | ap = network.WLAN(network.AP_IF) | |
1060 | ap.active(False) | |
9797 | 1061 | if ifconfig: |
1062 | ap.ifconfig(ifconfig) | |
9795 | 1063 | ap.active(True) |
1064 | try: | |
9797 | 1065 | ap.config(ssid=ssid, authmode=authmode, password=password) |
9795 | 1066 | except: |
9797 | 1067 | ap.config(essid=ssid, authmode=authmode, password=password) |
9795 | 1068 | |
9797 | 1069 | start_ap({0}, {1}, {2}, {3}) |
9795 | 1070 | del start_ap |
1071 | """.format( | |
9798 | 1072 | repr(ssid), security, repr(password), ifconfig |
1073 | ) | |
9795 | 1074 | |
9989 | 1075 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=15000) |
9795 | 1076 | if err: |
1077 | return False, err | |
1078 | else: | |
1079 | return True, "" | |
1080 | ||
1081 | def stopAccessPoint(self): | |
1082 | """ | |
1083 | Public method to stop the access point interface. | |
1084 | ||
1085 | @return tuple containg a flag indicating success and an error message | |
1086 | @rtype tuple of (bool, str) | |
1087 | """ | |
9866 | 1088 | if self.hasCircuitPython(): |
1089 | return self.__cpyDevice.stopAccessPoint() | |
1090 | ||
9795 | 1091 | return self.deactivateInterface("AP") |
1092 | ||
1093 | def getConnectedClients(self): | |
1094 | """ | |
1095 | Public method to get a list of connected clients. | |
1096 | ||
1097 | @return a tuple containing a list of tuples containing the client MAC-Address | |
1098 | and the RSSI (if supported and available) and an error message | |
1099 | @rtype tuple of ([(bytes, int)], str) | |
1100 | """ | |
9866 | 1101 | if self.hasCircuitPython(): |
1102 | return self.__cpyDevice.getConnectedClients() | |
1103 | ||
9795 | 1104 | command = """ |
1105 | def get_stations(): | |
1106 | import network | |
1107 | ||
1108 | ap = network.WLAN(network.AP_IF) | |
1109 | stations = ap.status('stations') | |
1110 | print(stations) | |
1111 | ||
1112 | get_stations() | |
1113 | del get_stations | |
1114 | """ | |
1115 | ||
9989 | 1116 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=10000) |
9795 | 1117 | if err: |
1118 | return [], err | |
1119 | ||
1120 | clientsList = ast.literal_eval(out.decode("utf-8")) | |
1121 | return clientsList, "" | |
1122 | ||
10022 | 1123 | def enableWebrepl(self, password): |
1124 | """ | |
1125 | Public method to write the given WebREPL password to the connected device and | |
1126 | modify the start script to start the WebREPL server. | |
1127 | ||
1128 | @param password password needed to authenticate | |
1129 | @type str | |
1130 | @return tuple containing a flag indicating success and an error message | |
1131 | @rtype tuple of (bool, str) | |
1132 | """ | |
1133 | command = """ | |
1134 | def modify_boot(): | |
1135 | import os | |
1136 | ||
1137 | try: | |
1138 | with open('/boot.py', 'r') as old_f, open('/boot.py.tmp', 'w') as new_f: | |
1139 | found = False | |
1140 | for l in old_f.read().splitlines(): | |
1141 | if 'webrepl' in l: | |
1142 | found = True | |
1143 | if l.startswith('#'): | |
1144 | l = l[1:] | |
1145 | new_f.write(l + '\\n') | |
1146 | if not found: | |
1147 | new_f.write('\\nimport webrepl\\nwebrepl.start()\\n') | |
1148 | ||
1149 | os.remove('/boot.py') | |
1150 | os.rename('/boot.py.tmp', '/boot.py') | |
1151 | except: | |
1152 | pass | |
1153 | ||
1154 | print(True) | |
1155 | ||
1156 | modify_boot() | |
1157 | del modify_boot | |
1158 | """ | |
1159 | ||
1160 | try: | |
1161 | # write config file | |
1162 | config = "PASS = {0}\n".format(repr(password)) | |
1163 | self.putData("/webrepl_cfg.py", config.encode("utf-8")) | |
1164 | except OSError as err: | |
1165 | return False, str(err) | |
1166 | ||
1167 | # modify boot.py | |
1168 | out, err = self.executeCommands(command, mode=self._submitMode) | |
1169 | if err: | |
1170 | return False, err | |
1171 | ||
1172 | return out.decode("utf-8").strip() == "True", "" | |
1173 | ||
1174 | def disableWebrepl(self): | |
1175 | """ | |
1176 | Public method to write the given WebREPL password to the connected device and | |
1177 | modify the start script to start the WebREPL server. | |
1178 | ||
1179 | @return tuple containing a flag indicating success and an error message | |
1180 | @rtype tuple of (bool, str) | |
1181 | """ | |
1182 | command = """ | |
1183 | def modify_boot(): | |
1184 | import os | |
1185 | ||
1186 | try: | |
1187 | with open('/boot.py', 'r') as old_f, open('/boot.py.tmp', 'w') as new_f: | |
1188 | for l in old_f.read().splitlines(): | |
1189 | if 'webrepl' in l: | |
1190 | if not l.startswith('#'): | |
1191 | l = '#' + l | |
1192 | new_f.write(l + '\\n') | |
1193 | ||
1194 | os.remove('/boot.py') | |
1195 | os.rename('/boot.py.tmp', '/boot.py') | |
1196 | except: | |
1197 | pass | |
1198 | ||
1199 | print(True) | |
1200 | ||
1201 | modify_boot() | |
1202 | del modify_boot | |
1203 | """ | |
1204 | ||
1205 | # modify boot.py | |
1206 | out, err = self.executeCommands(command, mode=self._submitMode) | |
1207 | if err: | |
1208 | return False, err | |
1209 | ||
1210 | return out.decode("utf-8").strip() == "True", "" | |
1211 | ||
9855 | 1212 | ################################################################## |
1213 | ## Methods below implement Bluetooth related methods | |
1214 | ################################################################## | |
1215 | ||
1216 | def hasBluetooth(self): | |
1217 | """ | |
1218 | Public method to check the availability of Bluetooth. | |
1219 | ||
1220 | @return flag indicating the availability of Bluetooth | |
1221 | @rtype bool | |
9857 | 1222 | @exception OSError raised to indicate an issue with the device |
9855 | 1223 | """ |
9866 | 1224 | if self.hasCircuitPython(): |
1225 | self.__createCpyDevice() | |
1226 | return self.__cpyDevice.hasBluetooth() | |
1227 | ||
9855 | 1228 | command = """ |
1229 | def has_bt(): | |
1230 | try: | |
1231 | import bluetooth | |
1232 | if hasattr(bluetooth, 'BLE'): | |
1233 | return True | |
1234 | except ImportError: | |
1235 | pass | |
1236 | ||
1237 | return False | |
1238 | ||
1239 | print(has_bt()) | |
1240 | del has_bt | |
1241 | """ | |
9989 | 1242 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=10000) |
9855 | 1243 | if err: |
1244 | raise OSError(self._shortError(err)) | |
1245 | return out.strip() == b"True" | |
1246 | ||
1247 | def getBluetoothStatus(self): | |
1248 | """ | |
1249 | Public method to get Bluetooth status data of the connected board. | |
1250 | ||
1251 | @return list of tuples containing the translated status data label and | |
1252 | the associated value | |
1253 | @rtype list of tuples of (str, str) | |
9857 | 1254 | @exception OSError raised to indicate an issue with the device |
9855 | 1255 | """ |
9866 | 1256 | if self.hasCircuitPython(): |
1257 | return self.__cpyDevice.getBluetoothStatus() | |
1258 | ||
9855 | 1259 | command = """ |
1260 | def ble_status(): | |
1261 | import bluetooth | |
1262 | import ubinascii | |
1263 | import ujson | |
1264 | ||
1265 | ble = bluetooth.BLE() | |
1266 | ||
1267 | ble_active = ble.active() | |
1268 | if not ble_active: | |
1269 | ble.active(True) | |
1270 | ||
1271 | res = { | |
1272 | 'active': ble_active, | |
1273 | 'mac': ubinascii.hexlify(ble.config('mac')[1], ':').decode(), | |
1274 | 'addr_type': ble.config('mac')[0], | |
1275 | 'name': ble.config('gap_name'), | |
1276 | 'mtu': ble.config('mtu'), | |
1277 | } | |
1278 | ||
1279 | if not ble_active: | |
1280 | ble.active(False) | |
1281 | ||
1282 | print(ujson.dumps(res)) | |
1283 | ||
1284 | ble_status() | |
1285 | del ble_status | |
1286 | """ | |
9989 | 1287 | out, err = self.executeCommands(command, mode=self._submitMode) |
9855 | 1288 | if err: |
1289 | raise OSError(self._shortError(err)) | |
1290 | ||
1291 | status = [] | |
1292 | bleStatus = json.loads(out.decode("utf-8")) | |
1293 | status.append((self.tr("Active"), self.bool2str(bleStatus["active"]))) | |
1294 | status.append((self.tr("Name"), bleStatus["name"])) | |
1295 | status.append((self.tr("MAC-Address"), bleStatus["mac"])) | |
1296 | status.append( | |
1297 | ( | |
1298 | self.tr("Address Type"), | |
1299 | self.tr("Public") if bleStatus == 0 else self.tr("Random"), | |
1300 | ) | |
1301 | ) | |
9857 | 1302 | status.append((self.tr("MTU"), self.tr("{0} Bytes").format(bleStatus["mtu"]))) |
9855 | 1303 | |
1304 | return status | |
1305 | ||
1306 | def activateBluetoothInterface(self): | |
1307 | """ | |
1308 | Public method to activate the Bluetooth interface. | |
1309 | ||
1310 | @return flag indicating the new state of the Bluetooth interface | |
1311 | @rtype bool | |
9857 | 1312 | @exception OSError raised to indicate an issue with the device |
9855 | 1313 | """ |
9866 | 1314 | if self.hasCircuitPython(): |
1315 | return self.__cpyDevice.activateBluetoothInterface() | |
1316 | ||
9855 | 1317 | command = """ |
1318 | def activate_ble(): | |
1319 | import bluetooth | |
1320 | ||
1321 | ble = bluetooth.BLE() | |
1322 | if not ble.active(): | |
1323 | ble.active(True) | |
1324 | print(ble.active()) | |
1325 | ||
1326 | activate_ble() | |
1327 | del activate_ble | |
1328 | """ | |
9989 | 1329 | out, err = self.executeCommands(command, mode=self._submitMode) |
9855 | 1330 | if err: |
1331 | raise OSError(self._shortError(err)) | |
1332 | ||
1333 | return out.strip() == b"True" | |
1334 | ||
1335 | def deactivateBluetoothInterface(self): | |
1336 | """ | |
1337 | Public method to deactivate the Bluetooth interface. | |
1338 | ||
1339 | @return flag indicating the new state of the Bluetooth interface | |
1340 | @rtype bool | |
9857 | 1341 | @exception OSError raised to indicate an issue with the device |
9855 | 1342 | """ |
9866 | 1343 | if self.hasCircuitPython(): |
1344 | return self.__cpyDevice.deactivateBluetoothInterface() | |
1345 | ||
9855 | 1346 | command = """ |
1347 | def deactivate_ble(): | |
1348 | import bluetooth | |
1349 | ||
1350 | ble = bluetooth.BLE() | |
1351 | if ble.active(): | |
1352 | ble.active(False) | |
1353 | print(ble.active()) | |
1354 | ||
1355 | deactivate_ble() | |
1356 | del deactivate_ble | |
1357 | """ | |
9989 | 1358 | out, err = self.executeCommands(command, mode=self._submitMode) |
9855 | 1359 | if err: |
1360 | raise OSError(self._shortError(err)) | |
1361 | ||
1362 | return out.strip() == b"True" | |
1363 | ||
9859 | 1364 | def getDeviceScan(self, timeout=10): |
1365 | """ | |
1366 | Public method to perform a Bluetooth device scan. | |
1367 | ||
1368 | @param timeout duration of the device scan in seconds (defaults | |
1369 | to 10) | |
1370 | @type int (optional) | |
1371 | @return tuple containing a dictionary with the scan results and | |
1372 | an error string | |
1373 | @rtype tuple of (dict, str) | |
1374 | """ | |
1375 | from ..BluetoothDialogs.BluetoothAdvertisement import BluetoothAdvertisement | |
1376 | ||
9866 | 1377 | if self.hasCircuitPython(): |
1378 | return self.__cpyDevice.getDeviceScan(timeout) | |
1379 | ||
9859 | 1380 | command = """ |
10032
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1381 | _scan_done = False |
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1382 | |
9859 | 1383 | def ble_scan(): |
1384 | import bluetooth | |
1385 | import time | |
1386 | import ubinascii | |
1387 | ||
1388 | IRQ_SCAN_RESULT = 5 | |
1389 | IRQ_SCAN_DONE = 6 | |
1390 | ||
1391 | def _bleIrq(event, data): | |
10032
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1392 | global _scan_done |
9859 | 1393 | if event == IRQ_SCAN_RESULT: |
1394 | addr_type, addr, adv_type, rssi, adv_data = data | |
1395 | if addr: | |
1396 | print({{ | |
1397 | 'address': ubinascii.hexlify(addr,':').decode('utf-8'), | |
1398 | 'rssi': rssi, | |
1399 | 'adv_type': adv_type, | |
1400 | 'advertisement': bytes(adv_data), | |
1401 | }}) | |
10032
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1402 | elif event == IRQ_SCAN_DONE: |
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1403 | _scan_done = True |
9859 | 1404 | |
1405 | ble = bluetooth.BLE() | |
1406 | ||
1407 | ble_active = ble.active() | |
1408 | if not ble_active: | |
1409 | ble.active(True) | |
1410 | ||
1411 | ble.irq(_bleIrq) | |
1412 | ble.gap_scan({0} * 1000, 1000000, 50000, True) | |
10032
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1413 | while not _scan_done: |
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1414 | time.sleep(0.2) |
9859 | 1415 | |
1416 | if not ble_active: | |
1417 | ble.active(False) | |
1418 | ||
1419 | ble_scan() | |
10032
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1420 | del ble_scan, _scan_done |
9859 | 1421 | """.format( |
1422 | timeout | |
1423 | ) | |
9989 | 1424 | out, err = self.executeCommands( |
9859 | 1425 | command, mode=self._submitMode, timeout=(timeout + 5) * 1000 |
1426 | ) | |
1427 | if err: | |
1428 | return {}, err | |
1429 | ||
1430 | scanResults = {} | |
1431 | for line in out.decode("utf-8").splitlines(): | |
1432 | res = ast.literal_eval(line) | |
1433 | address = res["address"] | |
1434 | if address not in scanResults: | |
1435 | scanResults[address] = BluetoothAdvertisement(address) | |
1436 | scanResults[address].update( | |
1437 | res["adv_type"], res["rssi"], res["advertisement"] | |
1438 | ) | |
1439 | ||
1440 | return scanResults, "" | |
1441 | ||
9868 | 1442 | ################################################################## |
1443 | ## Methods below implement NTP related methods | |
1444 | ################################################################## | |
1445 | ||
1446 | def hasNetworkTime(self): | |
1447 | """ | |
1448 | Public method to check the availability of network time functions. | |
1449 | ||
1450 | @return flag indicating the availability of network time functions | |
1451 | @rtype bool | |
9870
0399d3607829
Fixed a few code formatting and style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9868
diff
changeset
|
1452 | @exception OSError raised to indicate an issue with the device |
9868 | 1453 | """ |
1454 | if self.hasCircuitPython(): | |
1455 | self.__createCpyDevice() | |
1456 | return self.__cpyDevice.hasNetworkTime() | |
1457 | ||
1458 | command = """ | |
1459 | def has_ntp(): | |
1460 | try: | |
1461 | import ntptime | |
1462 | return True | |
1463 | except ImportError: | |
1464 | return False | |
1465 | ||
1466 | print(has_ntp()) | |
1467 | del has_ntp | |
1468 | """ | |
9989 | 1469 | out, err = self.executeCommands(command, mode=self._submitMode) |
9868 | 1470 | if err: |
1471 | raise OSError(self._shortError(err)) | |
1472 | return out.strip() == b"True" | |
1473 | ||
1474 | def setNetworkTime(self, server="0.pool.ntp.org", tzOffset=0, timeout=10): | |
1475 | """ | |
1476 | Public method to set the time to the network time retrieved from an | |
1477 | NTP server. | |
1478 | ||
1479 | @param server name of the NTP server to get the network time from | |
1480 | (defaults to "0.pool.ntp.org") | |
1481 | @type str (optional) | |
1482 | @param tzOffset offset with respect to UTC (defaults to 0) | |
1483 | @type int (optional) | |
1484 | @param timeout maximum time to wait for a server response in seconds | |
1485 | (defaults to 10) | |
1486 | @type int | |
1487 | @return tuple containing a flag indicating success and an error string | |
1488 | @rtype tuple of (bool, str) | |
1489 | """ | |
1490 | if self.hasCircuitPython(): | |
1491 | return self.__cpyDevice.setNetworkTime( | |
1492 | server=server, tzOffset=tzOffset, timeout=timeout | |
1493 | ) | |
1494 | ||
1495 | command = """ | |
1496 | def set_ntp_time(server, tz_offset, timeout): | |
1497 | import network | |
1498 | import ntptime | |
1499 | import machine | |
1500 | ||
1501 | if not network.WLAN(network.STA_IF).isconnected(): | |
1502 | return False | |
1503 | ||
1504 | ntptime.host = server | |
1505 | ntptime.timeout = timeout | |
1506 | ntptime.settime() | |
1507 | ||
1508 | rtc = machine.RTC() | |
1509 | t = list(rtc.datetime()) | |
1510 | t[4] += tz_offset | |
1511 | rtc.datetime(t) | |
1512 | ||
1513 | return True | |
1514 | ||
1515 | try: | |
1516 | print({{ | |
1517 | 'result': set_ntp_time({0}, {1}, {2}), | |
1518 | 'error': '', | |
1519 | }}) | |
1520 | except Exception as err: | |
1521 | print({{ | |
1522 | 'result': False, | |
1523 | 'error': str(err), | |
1524 | }}) | |
1525 | del set_ntp_time | |
1526 | """.format( | |
1527 | repr(server), tzOffset, timeout | |
1528 | ) | |
9989 | 1529 | out, err = self.executeCommands( |
9868 | 1530 | command, mode=self._submitMode, timeout=(timeout + 2) * 1000 |
1531 | ) | |
1532 | if err: | |
1533 | return False, err | |
1534 | else: | |
1535 | res = ast.literal_eval(out.decode("utf-8")) | |
1536 | return res["result"], res["error"] | |
1537 | ||
9496
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1538 | |
10069
435cc5875135
Corrected and checked some code style issues (unused function arguments).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10032
diff
changeset
|
1539 | def createDevice( |
435cc5875135
Corrected and checked some code style issues (unused function arguments).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10032
diff
changeset
|
1540 | microPythonWidget, deviceType, vid, pid, boardName, serialNumber # noqa: U100 |
435cc5875135
Corrected and checked some code style issues (unused function arguments).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10032
diff
changeset
|
1541 | ): |
9496
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1542 | """ |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1543 | Function to instantiate a MicroPython device object. |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1544 | |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1545 | @param microPythonWidget reference to the main MicroPython widget |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1546 | @type MicroPythonWidget |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1547 | @param deviceType device type assigned to this device interface |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1548 | @type str |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1549 | @param vid vendor ID |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1550 | @type int |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1551 | @param pid product ID |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1552 | @type int |
9738 | 1553 | @param boardName name of the board |
1554 | @type str | |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
1555 | @param serialNumber serial number of the board |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
1556 | @type str |
9496
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1557 | @return reference to the instantiated device object |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1558 | @rtype EspDevice |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1559 | """ |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1560 | return EspDevice(microPythonWidget, deviceType) |
9820 | 1561 | |
1562 | ||
1563 | ################################################################################ | |
1564 | ## Functions below implement flashing related functionality needed elsewhere ## | |
1565 | ## as well. ## | |
1566 | ################################################################################ | |
1567 | ||
1568 | ||
1569 | @pyqtSlot() | |
1570 | def eraseFlash(port): | |
1571 | """ | |
1572 | Slot to erase the device flash memory. | |
1573 | ||
1574 | @param port name of the serial port device to be used | |
1575 | @type str | |
1576 | """ | |
1577 | ok = EricMessageBox.yesNo( | |
1578 | None, | |
1579 | QCoreApplication.translate("EspDevice", "Erase Flash"), | |
1580 | QCoreApplication.translate( | |
1581 | "EspDevice", """Shall the flash of the selected device really be erased?""" | |
1582 | ), | |
1583 | ) | |
1584 | if ok: | |
1585 | flashArgs = [ | |
1586 | "-u", | |
1587 | "-m", | |
1588 | "esptool", | |
1589 | "--port", | |
1590 | port, | |
1591 | "erase_flash", | |
1592 | ] | |
1593 | dlg = EricProcessDialog( | |
1594 | QCoreApplication.translate("EspDevice", "'esptool erase_flash' Output"), | |
1595 | QCoreApplication.translate("EspDevice", "Erase Flash"), | |
1596 | showProgress=True, | |
1597 | ) | |
1598 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs) | |
1599 | if res: | |
1600 | dlg.exec() | |
1601 | ||
1602 | ||
1603 | @pyqtSlot() | |
1604 | def flashPythonFirmware(port): | |
1605 | """ | |
1606 | Slot to flash a MicroPython firmware to the device. | |
1607 | ||
1608 | @param port name of the serial port device to be used | |
1609 | @type str | |
1610 | """ | |
1611 | from .EspDialogs.EspFirmwareSelectionDialog import EspFirmwareSelectionDialog | |
1612 | ||
1613 | dlg = EspFirmwareSelectionDialog() | |
1614 | if dlg.exec() == QDialog.DialogCode.Accepted: | |
1615 | chip, firmware, baudRate, flashMode, flashAddress = dlg.getData() | |
1616 | flashArgs = [ | |
1617 | "-u", | |
1618 | "-m", | |
1619 | "esptool", | |
1620 | "--chip", | |
1621 | chip, | |
1622 | "--port", | |
1623 | port, | |
1624 | ] | |
1625 | if baudRate != "115200": | |
1626 | flashArgs += ["--baud", baudRate] | |
1627 | flashArgs.append("write_flash") | |
1628 | if flashMode: | |
1629 | flashArgs += ["--flash_mode", flashMode] | |
9848
3d750b2e012c
Corrected flashing of ESP 8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9836
diff
changeset
|
1630 | if chip == "esp8266": |
3d750b2e012c
Corrected flashing of ESP 8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9836
diff
changeset
|
1631 | # ESP 8266 seems to need flash size detection |
3d750b2e012c
Corrected flashing of ESP 8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9836
diff
changeset
|
1632 | flashArgs += ["--flash_size", "detect"] |
9820 | 1633 | flashArgs += [ |
1634 | flashAddress, | |
1635 | firmware, | |
1636 | ] | |
1637 | dlg = EricProcessDialog( | |
1638 | QCoreApplication.translate("EspDevice", "'esptool write_flash' Output"), | |
1639 | QCoreApplication.translate("EspDevice", "Flash µPy/CPy Firmware"), | |
1640 | showProgress=True, | |
1641 | ) | |
1642 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs) | |
1643 | if res: | |
1644 | dlg.exec() | |
1645 | ||
1646 | ||
1647 | @pyqtSlot() | |
1648 | def flashAddonFirmware(port): | |
1649 | """ | |
1650 | Slot to flash some additional firmware images. | |
1651 | ||
1652 | @param port name of the serial port device to be used | |
1653 | @type str | |
1654 | """ | |
1655 | from .EspDialogs.EspFirmwareSelectionDialog import EspFirmwareSelectionDialog | |
1656 | ||
1657 | dlg = EspFirmwareSelectionDialog(addon=True) | |
1658 | if dlg.exec() == QDialog.DialogCode.Accepted: | |
1659 | chip, firmware, baudRate, flashMode, flashAddress = dlg.getData() | |
1660 | flashArgs = [ | |
1661 | "-u", | |
1662 | "-m", | |
1663 | "esptool", | |
1664 | "--chip", | |
1665 | chip, | |
1666 | "--port", | |
1667 | port, | |
1668 | ] | |
1669 | if baudRate != "115200": | |
1670 | flashArgs += ["--baud", baudRate] | |
1671 | flashArgs.append("write_flash") | |
1672 | if flashMode: | |
1673 | flashArgs += ["--flash_mode", flashMode] | |
1674 | flashArgs += [ | |
1675 | flashAddress.lower(), | |
1676 | firmware, | |
1677 | ] | |
1678 | dlg = EricProcessDialog( | |
1679 | QCoreApplication.translate("EspDevice", "'esptool write_flash' Output"), | |
1680 | QCoreApplication.translate("EspDevice", "Flash Additional Firmware"), | |
1681 | showProgress=True, | |
1682 | ) | |
1683 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs) | |
1684 | if res: | |
1685 | dlg.exec() |