Mon, 05 May 2025 17:40:08 +0200
MicroPython
- Added support for IPv6 for WiFi and Ethernet enabled devices (MPy ≥ 1.24.0).
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 | |
11090
f5f5f5803935
Updated copyright for 2025.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11038
diff
changeset
|
3 | # Copyright (c) 2019 - 2025 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 | |
10806
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10683
diff
changeset
|
20 | from eric7 import EricUtilities, 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"), | |
11038 | 62 | 210: self.tr("no access point with compatible security found"), |
63 | 211: self.tr("no access point with suitable authentication mode found"), | |
64 | 212: self.tr("no access point with sufficient RSSI found"), | |
9795 | 65 | 1000: self.tr("idle"), |
66 | 1001: self.tr("connecting"), | |
67 | 1010: self.tr("connected"), | |
68 | } | |
69 | self.__securityTranslations = { | |
70 | 0: self.tr("open", "open WiFi network"), | |
71 | 1: "WEP", | |
72 | 2: "WPA", | |
73 | 3: "WPA2", | |
74 | 4: "WPA/WPA2", | |
75 | 5: "WPA2 (CCMP)", | |
76 | 6: "WPA3", | |
77 | 7: "WPA2/WPA3", | |
78 | } | |
11236 | 79 | self.__securityMapping = { |
80 | "SEC_DPP": "DPP", | |
81 | "SEC_OPEN": self.tr("Open"), | |
82 | "SEC_OWE": "OWE", | |
83 | "SEC_WAPI": "WAPI", | |
84 | "SEC_WEP": "WEP", | |
85 | "SEC_WPA": "WPA", | |
86 | "SEC_WPA_WPA2": "WPA/WPA2", | |
87 | "SEC_WPA2": "WPA2", | |
88 | "SEC_WPA2_ENT": "WPA2 Enterprise", | |
89 | "SEC_WPA2_WPA3": "WPA2/WPA3", | |
90 | "SEC_WPA2_WPA3_ENT": "WPA2/WPA3 Enterprise", | |
91 | "SEC_WPA3": "WPA3", | |
92 | "SEC_WPA3_ENT": "WPA3 Enterprise", | |
93 | "SEC_WPA3_ENT_192": "WPA3 Enterprise (192-bit)", | |
94 | "SEC_WPA3_EXT_PSK": "WPA3 Extended", | |
95 | "SEC_WPA3_EXT_PSK_MIXED_MODE": "WPA3 Extended, Mixed Mode", | |
96 | } | |
9795 | 97 | |
9866 | 98 | def __createCpyDevice(self): |
99 | """ | |
100 | Private method to create a CircuitPython device interface. | |
101 | """ | |
102 | if self.hasCircuitPython() and self.__cpyDevice is None: | |
103 | self.__cpyDevice = CircuitPythonDevice( | |
104 | self.microPython, | |
105 | "esp32_circuitpython", | |
106 | "esp32", | |
107 | hasWorkspace=False, | |
108 | parent=self.parent(), | |
109 | ) | |
10329 | 110 | self.__cpyDevice.setConnected(True) |
9866 | 111 | |
112 | def setConnected(self, connected): | |
113 | """ | |
114 | Public method to set the connection state. | |
115 | ||
116 | Note: This method can be overwritten to perform actions upon connect | |
117 | or disconnect of the device. | |
118 | ||
119 | @param connected connection state | |
120 | @type bool | |
121 | """ | |
122 | super().setConnected(connected) | |
123 | ||
124 | if self.hasCircuitPython(): | |
125 | self._submitMode = "paste" | |
126 | self.__createCpyDevice() | |
127 | ||
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
128 | def setButtons(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
129 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
130 | Public method to enable the supported action buttons. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
131 | """ |
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
|
132 | 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
|
133 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
134 | self.microPython.setActionButtons( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
135 | 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
|
136 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
137 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
138 | def forceInterrupt(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
139 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
140 | 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
|
141 | serial connection. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
142 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
143 | @return flag indicating an interrupt is needed |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
144 | @rtype bool |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
145 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
146 | return True |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
147 | |
7125
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7115
diff
changeset
|
148 | 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
|
149 | """ |
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7115
diff
changeset
|
150 | 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
|
151 | |
7125
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7115
diff
changeset
|
152 | @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
|
153 | @rtype str |
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7115
diff
changeset
|
154 | """ |
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7115
diff
changeset
|
155 | 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
|
156 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
157 | def canStartRepl(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
158 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
159 | 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
|
160 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
161 | @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
|
162 | and a reason why it cannot. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
163 | @rtype tuple of (bool, str) |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
164 | """ |
7095
8e10acb1cd85
Refactored and improved the MicroPython code to be able to show the file manager and the REPL simultaneously.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7091
diff
changeset
|
165 | return True, "" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
166 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
167 | def canStartPlotter(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
168 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
169 | 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
|
170 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
171 | @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
|
172 | Plotter and a reason why it cannot. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
173 | @rtype tuple of (bool, str) |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
174 | """ |
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
|
175 | return True, "" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
176 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
177 | def canRunScript(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
178 | """ |
7091
84d2a73b448a
EspDevices, MicroPythonDevices: fixed a wrong source docu string.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7077
diff
changeset
|
179 | 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
|
180 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
181 | @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
|
182 | Plotter and a reason why it cannot. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
183 | @rtype tuple of (bool, str) |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
184 | """ |
8119
1653972f2de5
EspDevices: fixed a typo and a little issue.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8117
diff
changeset
|
185 | return True, "" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
186 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
187 | def runScript(self, script): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
188 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
189 | 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
|
190 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
191 | @param script script to be executed |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
192 | @type str |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
193 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
194 | pythonScript = script.split("\n") |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
195 | self.sendCommands(pythonScript) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
196 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
197 | def canStartFileManager(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
198 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
199 | 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
|
200 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
201 | @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
|
202 | File Manager and a reason why it cannot. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
203 | @rtype tuple of (bool, str) |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
204 | """ |
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
|
205 | return True, "" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
206 | |
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
207 | def __createEsp32Submenu(self): |
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 | 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
|
210 | """ |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
211 | 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
|
212 | |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
213 | 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
|
214 | 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
|
215 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
216 | self.__espMenu.addSeparator() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
217 | 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
|
218 | 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
|
219 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
220 | 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
|
221 | 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
|
222 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
223 | self.__espMenu.addSeparator() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
224 | 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
|
225 | 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
|
226 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
227 | self.__espMenu.addSeparator() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
228 | 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
|
229 | 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
|
230 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
231 | 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
|
232 | 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
|
233 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
234 | self.__espMenu.addSeparator() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
235 | 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
|
236 | 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
|
237 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
238 | 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
|
239 | 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
|
240 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
241 | 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
|
242 | 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
|
243 | ) |
11038 | 244 | self.__securityInfoAct = self.__espMenu.addAction( |
245 | self.tr("Show Security Information"), self.__showSecurityInfo | |
246 | ) | |
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
247 | self.__espMenu.addSeparator() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
248 | 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
|
249 | 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
|
250 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
251 | self.__espMenu.addSeparator() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
252 | 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
|
253 | |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
254 | 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
|
255 | """ |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
256 | 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
|
257 | |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
258 | @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
|
259 | @type QMenu |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
260 | """ |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
261 | connected = self.microPython.isConnected() |
9749 | 262 | linkConnected = self.microPython.isLinkConnected() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
263 | |
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
264 | 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
|
265 | 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
|
266 | 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
|
267 | 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
|
268 | 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
|
269 | 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
|
270 | 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
|
271 | 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
|
272 | self.__macAddressAct.setEnabled(not linkConnected) |
11038 | 273 | self.__securityInfoAct.setEnabled(not linkConnected) |
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
274 | 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
|
275 | |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
276 | menu.addMenu(self.__espMenu) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
277 | |
8096 | 278 | def hasFlashMenuEntry(self): |
279 | """ | |
280 | 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
|
281 | |
8096 | 282 | @return flag indicating a specific flash menu entry |
283 | @rtype bool | |
284 | """ | |
285 | return True | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
286 | |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
287 | @pyqtSlot() |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
288 | def __eraseFlash(self): |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
289 | """ |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
290 | 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
|
291 | """ |
11006
a671918232f3
Modified modal dialog usage to always include a valid parent (needed for Wayland).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11005
diff
changeset
|
292 | eraseFlash(self.microPython.getCurrentPort(), parent=self.microPython) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
293 | |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
294 | @pyqtSlot() |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
295 | def __flashMicroPython(self): |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
296 | """ |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
297 | 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
|
298 | """ |
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
299 | flashPythonFirmware(self.microPython.getCurrentPort(), parent=self.microPython) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
300 | |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
301 | @pyqtSlot() |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
302 | def __flashAddons(self): |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
303 | """ |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
304 | 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
|
305 | """ |
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
306 | flashAddonFirmware(self.microPython.getCurrentPort(), parent=self.microPython) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
307 | |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
308 | @pyqtSlot() |
7352 | 309 | def __backupFlash(self): |
310 | """ | |
311 | Private slot to backup the currently flashed firmware. | |
312 | """ | |
9756
9854647c8c5c
Reorganized the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9752
diff
changeset
|
313 | from .EspDialogs.EspBackupRestoreFirmwareDialog import ( |
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
314 | EspBackupRestoreFirmwareDialog, |
9756
9854647c8c5c
Reorganized the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9752
diff
changeset
|
315 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
316 | |
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
317 | dlg = EspBackupRestoreFirmwareDialog(backupMode=True, parent=self.microPython) |
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
|
318 | if dlg.exec() == QDialog.DialogCode.Accepted: |
8945 | 319 | chip, flashSize, baudRate, flashMode, firmware = dlg.getData() |
7352 | 320 | flashArgs = [ |
321 | "-u", | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
322 | "-m", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
323 | "esptool", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
324 | "--chip", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
325 | chip, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
326 | "--port", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
327 | self.microPython.getCurrentPort(), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
328 | "--baud", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
329 | baudRate, |
7352 | 330 | "read_flash", |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
331 | "0x0", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
332 | flashSize, |
7352 | 333 | firmware, |
334 | ] | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
335 | dlg = EricProcessDialog( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
336 | 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
|
337 | self.tr("Backup Firmware"), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
338 | showProgress=True, |
10933
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
339 | monospacedFont=Preferences.getEditorOtherFonts("MonospacedFont"), |
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
340 | encoding=Preferences.getSystem("IOEncoding"), |
11006
a671918232f3
Modified modal dialog usage to always include a valid parent (needed for Wayland).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11005
diff
changeset
|
341 | parent=self.microPython, |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
342 | ) |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
343 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs) |
7352 | 344 | if res: |
7759
51aa6c6b66f7
Changed calls to exec_() into exec() (remainder of Python2 elimination).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7595
diff
changeset
|
345 | dlg.exec() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
346 | |
7352 | 347 | @pyqtSlot() |
348 | def __restoreFlash(self): | |
349 | """ | |
350 | Private slot to restore a previously saved firmware. | |
351 | """ | |
9756
9854647c8c5c
Reorganized the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9752
diff
changeset
|
352 | from .EspDialogs.EspBackupRestoreFirmwareDialog import ( |
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
353 | EspBackupRestoreFirmwareDialog, |
9756
9854647c8c5c
Reorganized the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9752
diff
changeset
|
354 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
355 | |
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
356 | dlg = EspBackupRestoreFirmwareDialog(backupMode=False, parent=self.microPython) |
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
|
357 | if dlg.exec() == QDialog.DialogCode.Accepted: |
8945 | 358 | chip, flashSize, baudRate, flashMode, firmware = dlg.getData() |
7352 | 359 | flashArgs = [ |
360 | "-u", | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
361 | "-m", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
362 | "esptool", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
363 | "--chip", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
364 | chip, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
365 | "--port", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
366 | self.microPython.getCurrentPort(), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
367 | "--baud", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
368 | baudRate, |
7352 | 369 | "write_flash", |
370 | ] | |
8945 | 371 | if flashMode: |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
372 | flashArgs.extend( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
373 | [ |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
374 | "--flash_mode", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
375 | flashMode, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
376 | ] |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
377 | ) |
7352 | 378 | if bool(flashSize): |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
379 | flashArgs.extend( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
380 | [ |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
381 | "--flash_size", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
382 | flashSize, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
383 | ] |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
384 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
385 | flashArgs.extend( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
386 | [ |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
387 | "0x0", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
388 | firmware, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
389 | ] |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
390 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
391 | dlg = EricProcessDialog( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
392 | 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
|
393 | self.tr("Restore Firmware"), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
394 | showProgress=True, |
10933
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
395 | monospacedFont=Preferences.getEditorOtherFonts("MonospacedFont"), |
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
396 | encoding=Preferences.getSystem("IOEncoding"), |
11006
a671918232f3
Modified modal dialog usage to always include a valid parent (needed for Wayland).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11005
diff
changeset
|
397 | parent=self.microPython, |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
398 | ) |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
399 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs) |
7352 | 400 | if res: |
7759
51aa6c6b66f7
Changed calls to exec_() into exec() (remainder of Python2 elimination).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7595
diff
changeset
|
401 | dlg.exec() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
402 | |
7352 | 403 | @pyqtSlot() |
9749 | 404 | def __showFirmwareVersions(self): |
405 | """ | |
406 | Private slot to show the firmware version of the connected device and the | |
407 | available firmware version. | |
408 | """ | |
9866 | 409 | if self.hasCircuitPython(): |
9870
0399d3607829
Fixed a few code formatting and style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9868
diff
changeset
|
410 | self.__cpyDevice.showCircuitPythonVersions() |
9866 | 411 | |
10329 | 412 | elif 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
|
413 | 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
|
414 | 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
|
415 | 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
|
416 | 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
|
417 | else: |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
418 | EricMessageBox.critical( |
11034
7b8a21fd2d58
Extended the EricMessageBox module to determine a parent widget if none was given and extended the EricApplication class to store a reference to the main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
419 | self.microPython, |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
420 | 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
|
421 | self.tr( |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
422 | """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
|
423 | """ 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
|
424 | """ or CircuitPython. Aborting...""" |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
425 | ), |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
426 | ) |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
427 | return |
9749 | 428 | |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
429 | 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
|
430 | request = QNetworkRequest(url) |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
431 | 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
|
432 | 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
|
433 | |
9820 | 434 | @pyqtSlot(QNetworkReply) |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
435 | def __firmwareVersionResponse(self, reply): |
9749 | 436 | """ |
9820 | 437 | Private slot handling the response of the latest version request. |
9749 | 438 | |
439 | @param reply reference to the reply object | |
440 | @type QNetworkReply | |
441 | """ | |
442 | latestUrl = reply.url().toString() | |
443 | tag = latestUrl.rsplit("/", 1)[-1] | |
444 | while tag and not tag[0].isdecimal(): | |
445 | # get rid of leading non-decimal characters | |
446 | tag = tag[1:] | |
10806
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10683
diff
changeset
|
447 | latestVersion = EricUtilities.versionToTuple(tag) |
9749 | 448 | |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
449 | if self._deviceData["mpy_version"] == "unknown": |
9749 | 450 | currentVersionStr = self.tr("unknown") |
451 | currentVersion = (0, 0, 0) | |
452 | else: | |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
453 | currentVersionStr = self._deviceData["mpy_version"] |
10806
2f6df822e3b9
Moved some functions to the EricUtilities package for consistency and adapted the code base accordingly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10683
diff
changeset
|
454 | currentVersion = EricUtilities.versionToTuple(currentVersionStr) |
9749 | 455 | |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
456 | 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
|
457 | kind = "CircuitPython" |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
458 | 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
|
459 | kind = "MicroPython" |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
460 | |
9749 | 461 | 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
|
462 | "<h4>{0} Version Information</h4>" |
9749 | 463 | "<table>" |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
464 | "<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
|
465 | "<tr><td>Available:</td><td>{2}</td></tr>" |
9749 | 466 | "</table>" |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
467 | ).format(kind, currentVersionStr, tag) |
9749 | 468 | if currentVersion < latestVersion: |
469 | msg += self.tr("<p><b>Update available!</b></p>") | |
470 | ||
471 | EricMessageBox.information( | |
11034
7b8a21fd2d58
Extended the EricMessageBox module to determine a parent widget if none was given and extended the EricApplication class to store a reference to the main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
472 | self.microPython, |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
473 | self.tr("{0} Version").format(kind), |
9749 | 474 | msg, |
475 | ) | |
476 | ||
477 | @pyqtSlot() | |
7346 | 478 | def __showChipID(self): |
479 | """ | |
480 | Private slot to show the ID of the ESP chip. | |
481 | """ | |
482 | args = [ | |
483 | "-u", | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
484 | "-m", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
485 | "esptool", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
486 | "--port", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
487 | self.microPython.getCurrentPort(), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
488 | "chip_id", |
7346 | 489 | ] |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
490 | dlg = EricProcessDialog( |
10933
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
491 | self.tr("'esptool chip_id' Output"), |
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
492 | self.tr("Show Chip ID"), |
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
493 | monospacedFont=Preferences.getEditorOtherFonts("MonospacedFont"), |
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
494 | encoding=Preferences.getSystem("IOEncoding"), |
11006
a671918232f3
Modified modal dialog usage to always include a valid parent (needed for Wayland).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11005
diff
changeset
|
495 | parent=self.microPython, |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
496 | ) |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
497 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), args) |
7346 | 498 | if res: |
7759
51aa6c6b66f7
Changed calls to exec_() into exec() (remainder of Python2 elimination).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7595
diff
changeset
|
499 | dlg.exec() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
500 | |
7346 | 501 | @pyqtSlot() |
502 | def __showFlashID(self): | |
503 | """ | |
504 | Private slot to show the ID of the ESP flash chip. | |
505 | """ | |
506 | args = [ | |
507 | "-u", | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
508 | "-m", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
509 | "esptool", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
510 | "--port", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
511 | self.microPython.getCurrentPort(), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
512 | "flash_id", |
7346 | 513 | ] |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
514 | dlg = EricProcessDialog( |
10933
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
515 | self.tr("'esptool flash_id' Output"), |
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
516 | self.tr("Show Flash ID"), |
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
517 | monospacedFont=Preferences.getEditorOtherFonts("MonospacedFont"), |
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
518 | encoding=Preferences.getSystem("IOEncoding"), |
11006
a671918232f3
Modified modal dialog usage to always include a valid parent (needed for Wayland).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11005
diff
changeset
|
519 | parent=self.microPython, |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
520 | ) |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
521 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), args) |
7346 | 522 | if res: |
7759
51aa6c6b66f7
Changed calls to exec_() into exec() (remainder of Python2 elimination).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7595
diff
changeset
|
523 | dlg.exec() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
524 | |
7346 | 525 | @pyqtSlot() |
526 | def __showMACAddress(self): | |
527 | """ | |
528 | Private slot to show the MAC address of the ESP chip. | |
529 | """ | |
530 | args = [ | |
531 | "-u", | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
532 | "-m", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
533 | "esptool", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
534 | "--port", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
535 | self.microPython.getCurrentPort(), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
536 | "read_mac", |
7346 | 537 | ] |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
538 | dlg = EricProcessDialog( |
10933
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
539 | self.tr("'esptool read_mac' Output"), |
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
540 | self.tr("Show MAC Address"), |
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
541 | monospacedFont=Preferences.getEditorOtherFonts("MonospacedFont"), |
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
542 | encoding=Preferences.getSystem("IOEncoding"), |
11006
a671918232f3
Modified modal dialog usage to always include a valid parent (needed for Wayland).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11005
diff
changeset
|
543 | parent=self.microPython, |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
544 | ) |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9576
diff
changeset
|
545 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), args) |
7346 | 546 | if res: |
7759
51aa6c6b66f7
Changed calls to exec_() into exec() (remainder of Python2 elimination).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7595
diff
changeset
|
547 | dlg.exec() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
548 | |
7346 | 549 | @pyqtSlot() |
11038 | 550 | def __showSecurityInfo(self): |
551 | """ | |
552 | Private slot to show some security related information of the ESP chip. | |
553 | """ | |
554 | args = [ | |
555 | "-u", | |
556 | "-m", | |
557 | "esptool", | |
558 | "--port", | |
559 | self.microPython.getCurrentPort(), | |
560 | "get_security_info", | |
561 | ] | |
562 | dlg = EricProcessDialog( | |
563 | self.tr("'esptool get_security_info' Output"), | |
564 | self.tr("Show Security Information"), | |
565 | monospacedFont=Preferences.getEditorOtherFonts("MonospacedFont"), | |
566 | encoding=Preferences.getSystem("IOEncoding"), | |
567 | parent=self.microPython, | |
568 | ) | |
569 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), args) | |
570 | if res: | |
571 | dlg.exec() | |
572 | ||
573 | @pyqtSlot() | |
7174
de8175253dfc
MicroPython: did some fine tuning of the MicroPython interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7173
diff
changeset
|
574 | def __resetDevice(self): |
de8175253dfc
MicroPython: did some fine tuning of the MicroPython interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7173
diff
changeset
|
575 | """ |
de8175253dfc
MicroPython: did some fine tuning of the MicroPython interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7173
diff
changeset
|
576 | 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
|
577 | """ |
9866 | 578 | if self.microPython.isConnected() and not self.hasCircuitPython(): |
9989 | 579 | self.executeCommands( |
9834 | 580 | "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
|
581 | ) |
7353
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
582 | else: |
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
583 | # 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
|
584 | # the output |
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
585 | args = [ |
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
586 | "-u", |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
587 | "-m", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
588 | "esptool", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
589 | "--port", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
590 | self.microPython.getCurrentPort(), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
591 | "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
|
592 | ] |
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
593 | 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
|
594 | 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
|
595 | 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
|
596 | if procStarted: |
caa2ccd5677c
EspDevices: fixed an issue resetting the attached device when not connected.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7352
diff
changeset
|
597 | proc.waitForFinished(10000) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
598 | |
7174
de8175253dfc
MicroPython: did some fine tuning of the MicroPython interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7173
diff
changeset
|
599 | @pyqtSlot() |
7108
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
600 | def __installEspTool(self): |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
601 | """ |
4f6133a01c6a
Started rearranging menu structure and testing and fixing on CircuitPython.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
602 | 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
|
603 | """ |
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
|
604 | 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
|
605 | 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
|
606 | ["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
|
607 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
608 | |
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
|
609 | 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
|
610 | """ |
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
|
611 | 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
|
612 | |
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
|
613 | @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
|
614 | @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
|
615 | """ |
9866 | 616 | if self.hasCircuitPython(): |
617 | return self.__cpyDevice.getDocumentationUrl() | |
618 | ||
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
|
619 | return Preferences.getMicroPython("MicroPythonDocuUrl") |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
620 | |
7328 | 621 | def getFirmwareUrl(self): |
622 | """ | |
623 | 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
|
624 | |
7328 | 625 | @return firmware download URL of the device |
626 | @rtype str | |
627 | """ | |
9866 | 628 | if self.hasCircuitPython(): |
629 | return self.__cpyDevice.getFirmwareUrl() | |
630 | ||
7328 | 631 | 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
|
632 | |
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
633 | ################################################################## |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
634 | ## time related methods below |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
635 | ################################################################## |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
636 | |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
637 | def _getSetTimeCode(self): |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
638 | """ |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
639 | 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
|
640 | |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
641 | 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
|
642 | subclasses. |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
643 | |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
644 | @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
|
645 | @rtype str |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
646 | """ |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
647 | # rtc_time[0] - year 4 digit |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
648 | # rtc_time[1] - month 1..12 |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
649 | # rtc_time[2] - day 1..31 |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
650 | # 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
|
651 | # rtc_time[4] - hour 0..23 |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
652 | # rtc_time[5] - minute 0..59 |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
653 | # rtc_time[6] - second 0..59 |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
654 | # rtc_time[7] - yearday 1..366 |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
655 | # 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
|
656 | |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
657 | # 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
|
658 | # take the arguments in the order: |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
659 | # (year, month, day, weekday, hour, minute, second, subseconds) |
11148
15e30f0c76a8
Adjusted the code to the modified issue codes.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11090
diff
changeset
|
660 | # __IGNORE_WARNING_M-891__ |
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
661 | # 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
|
662 | # |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
663 | # LoBo variant of MPy deviates. |
9866 | 664 | if self.hasCircuitPython(): |
665 | return super()._getSetTimeCode() | |
666 | ||
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
667 | return """ |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
668 | def set_time(rtc_time): |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
669 | import machine |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
670 | rtc = machine.RTC() |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
671 | try: |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
672 | rtc.datetime(rtc_time[:7] + (0,)) |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
673 | except Exception: |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
674 | import os |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
675 | if 'LoBo' in os.uname()[0]: |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
676 | 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
|
677 | else: |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
678 | clock_time = rtc_time[:7] + (0,) |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
679 | rtc.init(clock_time) |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
680 | """ |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
681 | |
9795 | 682 | ################################################################## |
683 | ## Methods below implement WiFi related methods | |
684 | ################################################################## | |
685 | ||
10153 | 686 | def addDeviceWifiEntries(self, menu): |
687 | """ | |
688 | Public method to add device specific entries to the given menu. | |
689 | ||
690 | @param menu reference to the context menu | |
691 | @type QMenu | |
692 | """ | |
693 | if not self.hasCircuitPython(): | |
694 | menu.addSeparator() | |
695 | menu.addAction(self.tr("Set Country"), self.__setCountry) | |
696 | menu.addAction(self.tr("Reset Country"), self.__resetCountry) | |
697 | ||
9795 | 698 | def hasWifi(self): |
699 | """ | |
700 | Public method to check the availability of WiFi. | |
701 | ||
702 | @return tuple containing a flag indicating the availability of WiFi | |
703 | and the WiFi type (esp32) | |
704 | @rtype tuple of (bool, str) | |
705 | """ | |
9866 | 706 | if self.hasCircuitPython(): |
707 | self.__createCpyDevice() | |
708 | return self.__cpyDevice.hasWifi() | |
709 | ||
9795 | 710 | return True, "esp32" |
711 | ||
10153 | 712 | def hasWifiCountry(self): |
713 | """ | |
714 | Public method to check, if the device has support to set the WiFi country. | |
715 | ||
716 | @return flag indicating the support of WiFi country | |
717 | @rtype bool | |
718 | """ | |
719 | return True | |
720 | ||
9795 | 721 | def getWifiData(self): |
722 | """ | |
723 | Public method to get data related to the current WiFi status. | |
724 | ||
9798 | 725 | @return tuple of three dictionaries containing the WiFi status data |
726 | for the WiFi client, access point and overall data | |
727 | @rtype tuple of (dict, dict, dict) | |
9795 | 728 | @exception OSError raised to indicate an issue with the device |
729 | """ | |
9866 | 730 | if self.hasCircuitPython(): |
731 | return self.__cpyDevice.getWifiData() | |
732 | ||
9795 | 733 | command = """ |
734 | def wifi_status(): | |
735 | import ubinascii | |
736 | import ujson | |
737 | import network | |
738 | ||
11236 | 739 | def security_str(mode): |
740 | for sm in [e for e in dir(network.WLAN) if e.startswith('SEC_')]: | |
741 | if getattr(network.WLAN, sm, 0) == mode: | |
742 | return sm | |
743 | return "" | |
744 | ||
9795 | 745 | wifi = network.WLAN(network.STA_IF) |
746 | station = { | |
747 | 'active': wifi.active(), | |
748 | 'connected': wifi.isconnected(), | |
749 | 'status': wifi.status(), | |
750 | 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), | |
11263 | 751 | 'channel': wifi.config('channel'), |
752 | 'essid': wifi.config('essid'), | |
9795 | 753 | } |
11263 | 754 | try: |
755 | station['ifconfig'] = ( | |
756 | wifi.ipconfig('addr4') + (wifi.ipconfig('gw4'), network.ipconfig('dns')) | |
757 | ) | |
758 | except AttributeError: | |
759 | station['ifconfig'] = wifi.ifconfig() | |
760 | try: | |
761 | station['ipv6_addr'] = [a[0] for a in wifi.ipconfig('addr6')] | |
762 | except ValueError: | |
763 | station['ipv6_addr'] = [] | |
9795 | 764 | if wifi.active(): |
9797 | 765 | try: |
766 | station['txpower'] = wifi.config('txpower') | |
767 | except ValueError: | |
768 | pass | |
9795 | 769 | print(ujson.dumps(station)) |
770 | ||
771 | wifi = network.WLAN(network.AP_IF) | |
772 | ap = { | |
773 | 'active': wifi.active(), | |
774 | 'connected': wifi.isconnected(), | |
775 | 'status': wifi.status(), | |
776 | 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), | |
777 | 'channel': wifi.config('channel'), | |
778 | 'essid': wifi.config('essid'), | |
11236 | 779 | 'ap_security': security_str(wifi.config('security')), |
9795 | 780 | } |
11263 | 781 | try: |
782 | ap['ifconfig'] = ( | |
783 | wifi.ipconfig('addr4') + (wifi.ipconfig('gw4'), network.ipconfig('dns')) | |
784 | ) | |
785 | except AttributeError: | |
786 | ap['ifconfig'] = wifi.ifconfig() | |
787 | try: | |
788 | ap['ipv6_addr'] = [a[0] for a in wifi.ipconfig('addr6')] | |
789 | except ValueError: | |
790 | ap['ipv6_addr'] = [] | |
9795 | 791 | if wifi.active(): |
9797 | 792 | try: |
793 | ap['txpower'] = wifi.config('txpower') | |
794 | except ValueError: | |
795 | pass | |
9795 | 796 | print(ujson.dumps(ap)) |
797 | ||
9798 | 798 | overall = { |
799 | 'active': station['active'] or ap['active'] | |
800 | } | |
10153 | 801 | try: |
802 | overall['hostname'] = network.hostname() | |
803 | except AttributeError: | |
804 | pass | |
805 | try: | |
806 | overall['country'] = network.country() | |
807 | except AttributeError: | |
808 | pass | |
11263 | 809 | try: |
810 | overall['prefer'] = network.ipconfig('prefer') | |
811 | except ValueError: | |
812 | overall['prefer'] = 4 | |
9798 | 813 | print(ujson.dumps(overall)) |
814 | ||
9795 | 815 | wifi_status() |
816 | del wifi_status | |
817 | """ | |
818 | ||
9989 | 819 | out, err = self.executeCommands(command, mode=self._submitMode) |
9795 | 820 | if err: |
821 | raise OSError(self._shortError(err)) | |
822 | ||
9798 | 823 | stationStr, apStr, overallStr = out.decode("utf-8").splitlines() |
9795 | 824 | station = json.loads(stationStr) |
825 | ap = json.loads(apStr) | |
9798 | 826 | overall = json.loads(overallStr) |
9795 | 827 | try: |
828 | station["status"] = self.__statusTranslations[station["status"]] | |
829 | except KeyError: | |
830 | station["status"] = str(station["status"]) | |
831 | try: | |
832 | ap["status"] = self.__statusTranslations[ap["status"]] | |
833 | except KeyError: | |
834 | ap["status"] = str(ap["status"]) | |
11236 | 835 | if "ap_security" in ap: |
836 | try: | |
837 | ap["ap_security"] = self.__securityMapping[ap["ap_security"]] | |
838 | except KeyError: | |
839 | ap["ap_security"] = self.tr("unknown ({0})").format(ap["ap_security"]) | |
9798 | 840 | return station, ap, overall |
9795 | 841 | |
10153 | 842 | def connectWifi(self, ssid, password, hostname): |
9795 | 843 | """ |
844 | Public method to connect a device to a WiFi network. | |
845 | ||
846 | @param ssid name (SSID) of the WiFi network | |
847 | @type str | |
848 | @param password password needed to connect | |
849 | @type str | |
10153 | 850 | @param hostname host name of the device |
851 | @type str | |
9795 | 852 | @return tuple containing the connection status and an error string |
853 | @rtype tuple of (bool, str) | |
854 | """ | |
9866 | 855 | if self.hasCircuitPython(): |
10153 | 856 | return self.__cpyDevice.connectWifi(ssid, password, hostname) |
9866 | 857 | |
9795 | 858 | command = """ |
10153 | 859 | def connect_wifi(ssid, password, hostname): |
9795 | 860 | import network |
861 | import ujson | |
862 | from time import sleep | |
863 | ||
10153 | 864 | if hostname: |
865 | try: | |
866 | network.hostname(hostname) | |
867 | except AttributeError: | |
868 | pass | |
869 | ||
9795 | 870 | wifi = network.WLAN(network.STA_IF) |
871 | wifi.active(False) | |
872 | wifi.active(True) | |
873 | wifi.connect(ssid, password) | |
874 | max_wait = 140 | |
875 | while max_wait and wifi.status() == network.STAT_CONNECTING: | |
876 | max_wait -= 1 | |
877 | sleep(0.1) | |
878 | status = wifi.status() | |
879 | print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) | |
880 | ||
10153 | 881 | connect_wifi({0}, {1}, {2}) |
9795 | 882 | del connect_wifi |
883 | """.format( | |
9798 | 884 | repr(ssid), |
885 | repr(password if password else ""), | |
10153 | 886 | repr(hostname), |
9798 | 887 | ) |
9795 | 888 | |
889 | with EricOverrideCursor(): | |
9989 | 890 | out, err = self.executeCommands( |
9827 | 891 | command, mode=self._submitMode, timeout=15000 |
9799 | 892 | ) |
9795 | 893 | if err: |
894 | return False, err | |
895 | ||
10235
4a12b160094c
MicroPython interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10233
diff
changeset
|
896 | while not out.startswith(b"{"): |
4a12b160094c
MicroPython interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10233
diff
changeset
|
897 | # discard output until next newline |
4a12b160094c
MicroPython interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10233
diff
changeset
|
898 | _, out = out.split(b"\r\n", 1) |
9795 | 899 | result = json.loads(out.decode("utf-8").strip()) |
900 | if result["connected"]: | |
901 | error = "" | |
902 | else: | |
903 | try: | |
904 | error = self.__statusTranslations[result["status"]] | |
905 | except KeyError: | |
906 | error = str(result["status"]) | |
907 | ||
908 | return result["connected"], error | |
909 | ||
910 | def disconnectWifi(self): | |
911 | """ | |
912 | Public method to disconnect a device from the WiFi network. | |
913 | ||
914 | @return tuple containing a flag indicating success and an error string | |
915 | @rtype tuple of (bool, str) | |
916 | """ | |
9866 | 917 | if self.hasCircuitPython(): |
918 | return self.__cpyDevice.disconnectWifi() | |
919 | ||
9795 | 920 | command = """ |
921 | def disconnect_wifi(): | |
922 | import network | |
923 | from time import sleep | |
924 | ||
925 | wifi = network.WLAN(network.STA_IF) | |
926 | wifi.disconnect() | |
927 | wifi.active(False) | |
928 | sleep(0.1) | |
929 | print(not wifi.isconnected()) | |
930 | ||
931 | disconnect_wifi() | |
932 | del disconnect_wifi | |
933 | """ | |
934 | ||
9989 | 935 | out, err = self.executeCommands(command, mode=self._submitMode) |
9795 | 936 | if err: |
937 | return False, err | |
938 | ||
10144 | 939 | return out.decode("utf-8").strip() == "True", "" |
940 | ||
941 | def isWifiClientConnected(self): | |
942 | """ | |
943 | Public method to check the WiFi connection status as client. | |
944 | ||
945 | @return flag indicating the WiFi connection status | |
946 | @rtype bool | |
947 | """ | |
948 | if self.hasCircuitPython(): | |
949 | return self.__cpyDevice.isWifiClientConnected() | |
950 | ||
951 | command = """ | |
952 | def wifi_connected(): | |
953 | import network | |
954 | ||
955 | wifi = network.WLAN(network.STA_IF) | |
956 | print(wifi.isconnected()) | |
957 | ||
958 | wifi_connected() | |
959 | del wifi_connected | |
960 | """ | |
961 | ||
962 | out, err = self.executeCommands(command, mode=self._submitMode) | |
963 | if err: | |
964 | return False | |
10138 | 965 | |
10144 | 966 | return out.strip() == b"True" |
967 | ||
968 | def isWifiApConnected(self): | |
969 | """ | |
970 | Public method to check the WiFi connection status as access point. | |
971 | ||
972 | @return flag indicating the WiFi connection status | |
973 | @rtype bool | |
974 | """ | |
975 | if self.hasCircuitPython(): | |
976 | return self.__cpyDevice.isWifiApConnected() | |
977 | ||
978 | command = """ | |
979 | def wifi_connected(): | |
980 | import network | |
981 | ||
982 | wifi = network.WLAN(network.AP_IF) | |
983 | print(wifi.isconnected()) | |
984 | ||
985 | wifi_connected() | |
986 | del wifi_connected | |
987 | """ | |
988 | ||
989 | out, err = self.executeCommands(command, mode=self._submitMode) | |
990 | if err: | |
991 | return False | |
992 | ||
993 | return out.strip() == b"True" | |
9795 | 994 | |
10153 | 995 | def writeCredentials(self, ssid, password, hostname, country): |
9795 | 996 | """ |
997 | Public method to write the given credentials to the connected device and modify | |
998 | the start script to connect automatically. | |
999 | ||
1000 | @param ssid SSID of the network to connect to | |
1001 | @type str | |
1002 | @param password password needed to authenticate | |
1003 | @type str | |
10153 | 1004 | @param hostname host name of the device |
1005 | @type str | |
1006 | @param country WiFi country code | |
1007 | @type str | |
9795 | 1008 | @return tuple containing a flag indicating success and an error message |
1009 | @rtype tuple of (bool, str) | |
1010 | """ | |
9866 | 1011 | if self.hasCircuitPython(): |
10153 | 1012 | return self.__cpyDevice.writeCredentials(ssid, password, hostname) |
9866 | 1013 | |
9795 | 1014 | nvsCommand = """ |
10153 | 1015 | def save_wifi_creds(ssid, password, hostname, country): |
9795 | 1016 | import esp32 |
1017 | ||
1018 | nvs = esp32.NVS('wifi_creds') | |
1019 | nvs.set_blob('ssid', ssid) | |
1020 | nvs.set_blob('password', password) | |
10153 | 1021 | nvs.set_blob('hostname', hostname) |
1022 | nvs.set_blob('country', country) | |
9795 | 1023 | nvs.commit() |
1024 | ||
10153 | 1025 | save_wifi_creds({0}, {1}, {2}, {3}) |
9795 | 1026 | del save_wifi_creds |
9798 | 1027 | """.format( |
10153 | 1028 | repr(ssid), |
1029 | repr(password) if password else "''", | |
1030 | repr(hostname) if hostname else "''", | |
1031 | repr(country.upper()) if country else "''", | |
9798 | 1032 | ) |
9795 | 1033 | bootCommand = """ |
1034 | def modify_boot(): | |
1035 | add = True | |
1036 | try: | |
1037 | with open('/boot.py', 'r') as f: | |
1038 | for ln in f.readlines(): | |
1039 | if 'wifi_connect' in ln: | |
1040 | add = False | |
1041 | break | |
1042 | except: | |
1043 | pass | |
1044 | if add: | |
1045 | with open('/boot.py', 'a') as f: | |
1046 | f.write('\\nimport wifi_connect\\n') | |
1047 | print(True) | |
1048 | ||
1049 | modify_boot() | |
1050 | del modify_boot | |
1051 | """ | |
1052 | ||
9989 | 1053 | out, err = self.executeCommands(nvsCommand, mode=self._submitMode) |
9795 | 1054 | if err: |
1055 | return False, self.tr("Error saving credentials: {0}").format(err) | |
1056 | ||
1057 | try: | |
1058 | # copy auto-connect file | |
1059 | self.put( | |
1060 | os.path.join( | |
1061 | os.path.dirname(__file__), "MCUScripts", "esp32WiFiConnect.py" | |
1062 | ), | |
1063 | "/wifi_connect.py", | |
1064 | ) | |
1065 | except OSError as err: | |
1066 | return False, self.tr("Error saving auto-connect script: {0}").format(err) | |
1067 | ||
9989 | 1068 | out, err = self.executeCommands(bootCommand, mode=self._submitMode) |
9795 | 1069 | if err: |
1070 | return False, self.tr("Error modifying 'boot.py': {0}").format(err) | |
1071 | ||
1072 | return True, "" | |
1073 | ||
1074 | def removeCredentials(self): | |
1075 | """ | |
1076 | Public method to remove the saved credentials from the connected device. | |
1077 | ||
1078 | @return tuple containing a flag indicating success and an error message | |
1079 | @rtype tuple of (bool, str) | |
1080 | """ | |
9866 | 1081 | if self.hasCircuitPython(): |
1082 | return self.__cpyDevice.removeCredentials() | |
1083 | ||
9795 | 1084 | nvsCommand = """ |
1085 | def delete_wifi_creds(): | |
1086 | import esp32 | |
1087 | ||
1088 | nvs = esp32.NVS('wifi_creds') | |
1089 | try: | |
1090 | nvs.erase_key('ssid') | |
1091 | nvs.erase_key('password') | |
1092 | nvs.commit() | |
1093 | except OSError: | |
1094 | pass | |
1095 | ||
1096 | delete_wifi_creds() | |
1097 | del delete_wifi_creds | |
1098 | """ | |
1099 | ||
9989 | 1100 | out, err = self.executeCommands(nvsCommand, mode=self._submitMode) |
9795 | 1101 | if err: |
1102 | return False, self.tr("Error deleting credentials: {0}").format(err) | |
1103 | ||
1104 | return True, "" | |
1105 | ||
1106 | def checkInternet(self): | |
1107 | """ | |
1108 | Public method to check, if the internet can be reached. | |
1109 | ||
1110 | @return tuple containing a flag indicating reachability and an error string | |
1111 | @rtype tuple of (bool, str) | |
1112 | """ | |
9866 | 1113 | if self.hasCircuitPython(): |
1114 | return self.__cpyDevice.checkInternet() | |
1115 | ||
9795 | 1116 | command = """ |
1117 | def check_internet(): | |
1118 | import network | |
1119 | import socket | |
1120 | ||
1121 | wifi = network.WLAN(network.STA_IF) | |
1122 | if wifi.isconnected(): | |
1123 | s = socket.socket() | |
1124 | try: | |
10233
51a6649ba79d
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10170
diff
changeset
|
1125 | s.connect(socket.getaddrinfo('quad9.net', 443)[0][-1]) |
9795 | 1126 | s.close() |
1127 | print(True) | |
1128 | except: | |
1129 | print(False) | |
1130 | else: | |
1131 | print(False) | |
1132 | ||
1133 | check_internet() | |
1134 | del check_internet | |
1135 | """ | |
1136 | ||
9989 | 1137 | out, err = self.executeCommands(command, mode=self._submitMode) |
9795 | 1138 | if err: |
1139 | return False, err | |
1140 | ||
1141 | return out.decode("utf-8").strip() == "True", "" | |
1142 | ||
1143 | def scanNetworks(self): | |
1144 | """ | |
1145 | Public method to scan for available WiFi networks. | |
1146 | ||
1147 | @return tuple containing the list of available networks as a tuple of 'Name', | |
1148 | 'MAC-Address', 'channel', 'RSSI' and 'security' and an error string | |
1149 | @rtype tuple of (list of tuple of (str, str, int, int, str), str) | |
1150 | """ | |
9866 | 1151 | if self.hasCircuitPython(): |
1152 | return self.__cpyDevice.scanNetworks() | |
1153 | ||
9795 | 1154 | command = """ |
1155 | def scan_networks(): | |
1156 | import network | |
1157 | ||
1158 | wifi = network.WLAN(network.STA_IF) | |
1159 | active = wifi.active() | |
1160 | if not active: | |
1161 | wifi.active(True) | |
1162 | network_list = wifi.scan() | |
1163 | if not active: | |
1164 | wifi.active(False) | |
1165 | print(network_list) | |
1166 | ||
1167 | scan_networks() | |
1168 | del scan_networks | |
1169 | """ | |
1170 | ||
9989 | 1171 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=15000) |
9795 | 1172 | if err: |
1173 | return [], err | |
1174 | ||
1175 | networksList = ast.literal_eval(out.decode("utf-8")) | |
1176 | networks = [] | |
1177 | for network in networksList: | |
1178 | if network[0]: | |
1179 | ssid = network[0].decode("utf-8") | |
1180 | mac = binascii.hexlify(network[1], ":").decode("utf-8") | |
1181 | channel = network[2] | |
1182 | rssi = network[3] | |
1183 | try: | |
1184 | security = self.__securityTranslations[network[4]] | |
1185 | except KeyError: | |
1186 | security = self.tr("unknown ({0})").format(network[4]) | |
1187 | networks.append((ssid, mac, channel, rssi, security)) | |
1188 | ||
1189 | return networks, "" | |
1190 | ||
1191 | def deactivateInterface(self, interface): | |
1192 | """ | |
1193 | Public method to deactivate a given WiFi interface of the connected device. | |
1194 | ||
1195 | @param interface designation of the interface to be deactivated (one of 'AP' | |
1196 | or 'STA') | |
1197 | @type str | |
1198 | @return tuple containg a flag indicating success and an error message | |
1199 | @rtype tuple of (bool, str) | |
1200 | @exception ValueError raised to indicate a wrong value for the interface type | |
1201 | """ | |
1202 | if interface not in ("STA", "AP"): | |
1203 | raise ValueError( | |
1204 | "interface must be 'AP' or 'STA', got '{0}'".format(interface) | |
1205 | ) | |
1206 | ||
9866 | 1207 | if self.hasCircuitPython(): |
1208 | return self.__cpyDevice.deactivateInterface(interface) | |
1209 | ||
9795 | 1210 | command = """ |
1211 | def deactivate(): | |
1212 | import network | |
1213 | from time import sleep | |
1214 | ||
1215 | wifi = network.WLAN(network.{0}_IF) | |
1216 | wifi.active(False) | |
1217 | sleep(0.1) | |
1218 | print(not wifi.active()) | |
1219 | ||
1220 | deactivate() | |
1221 | del deactivate | |
1222 | """.format( | |
9798 | 1223 | interface |
1224 | ) | |
9795 | 1225 | |
9989 | 1226 | out, err = self.executeCommands(command, mode=self._submitMode) |
9795 | 1227 | if err: |
1228 | return False, err | |
1229 | else: | |
1230 | return out.decode("utf-8").strip() == "True", "" | |
1231 | ||
10153 | 1232 | def startAccessPoint( |
1233 | self, | |
1234 | ssid, | |
1235 | security=None, | |
1236 | password=None, | |
1237 | hostname=None, | |
1238 | ifconfig=None, | |
1239 | ): | |
9795 | 1240 | """ |
1241 | Public method to start the access point interface. | |
1242 | ||
1243 | @param ssid SSID of the access point | |
1244 | @type str | |
11236 | 1245 | @param security security mode (defaults to None) (unused) |
1246 | @type str (optional) | |
9795 | 1247 | @param password password (defaults to None) |
1248 | @type str (optional) | |
10153 | 1249 | @param hostname host name of the device (defaults to None) |
1250 | @type str (optional) | |
9797 | 1251 | @param ifconfig IPv4 configuration for the access point if not default |
1252 | (IPv4 address, netmask, gateway address, DNS server address) | |
1253 | @type tuple of (str, str, str, str) | |
9795 | 1254 | @return tuple containing a flag indicating success and an error message |
1255 | @rtype tuple of (bool, str) | |
1256 | """ | |
9866 | 1257 | if self.hasCircuitPython(): |
1258 | return self.__cpyDevice.startAccessPoint( | |
10153 | 1259 | ssid, |
1260 | security=security, | |
1261 | password=password, | |
1262 | hostname=hostname, | |
1263 | ifconfig=ifconfig, | |
9866 | 1264 | ) |
1265 | ||
9795 | 1266 | if security is None or password is None: |
11236 | 1267 | security = "SEC_OPEN" |
10170
6cf1ee737d8f
Corrected some more code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10153
diff
changeset
|
1268 | password = "" # secok |
9795 | 1269 | |
1270 | command = """ | |
11236 | 1271 | def start_ap(ssid, password, hostname, ifconfig): |
9795 | 1272 | import network |
1273 | ||
10153 | 1274 | if hostname: |
1275 | try: | |
1276 | network.hostname(hostname) | |
1277 | except AttributeError: | |
1278 | pass | |
1279 | ||
9795 | 1280 | ap = network.WLAN(network.AP_IF) |
1281 | ap.active(False) | |
9797 | 1282 | if ifconfig: |
1283 | ap.ifconfig(ifconfig) | |
9795 | 1284 | ap.active(True) |
1285 | try: | |
11236 | 1286 | ap.config(ssid=ssid, authmode=network.WLAN.{4}, password=password) |
9795 | 1287 | except: |
11236 | 1288 | ap.config(essid=ssid, authmode=network.WLAN.{4}, password=password) |
9795 | 1289 | |
11236 | 1290 | start_ap({0}, {1}, {2}, {3}) |
9795 | 1291 | del start_ap |
1292 | """.format( | |
11236 | 1293 | repr(ssid), repr(password), repr(hostname), ifconfig, security |
9798 | 1294 | ) |
9795 | 1295 | |
9989 | 1296 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=15000) |
9795 | 1297 | if err: |
1298 | return False, err | |
1299 | else: | |
1300 | return True, "" | |
1301 | ||
1302 | def stopAccessPoint(self): | |
1303 | """ | |
1304 | Public method to stop the access point interface. | |
1305 | ||
1306 | @return tuple containg a flag indicating success and an error message | |
1307 | @rtype tuple of (bool, str) | |
1308 | """ | |
9866 | 1309 | if self.hasCircuitPython(): |
1310 | return self.__cpyDevice.stopAccessPoint() | |
1311 | ||
9795 | 1312 | return self.deactivateInterface("AP") |
1313 | ||
1314 | def getConnectedClients(self): | |
1315 | """ | |
1316 | Public method to get a list of connected clients. | |
1317 | ||
1318 | @return a tuple containing a list of tuples containing the client MAC-Address | |
1319 | and the RSSI (if supported and available) and an error message | |
1320 | @rtype tuple of ([(bytes, int)], str) | |
1321 | """ | |
9866 | 1322 | if self.hasCircuitPython(): |
1323 | return self.__cpyDevice.getConnectedClients() | |
1324 | ||
9795 | 1325 | command = """ |
1326 | def get_stations(): | |
1327 | import network | |
1328 | ||
1329 | ap = network.WLAN(network.AP_IF) | |
1330 | stations = ap.status('stations') | |
1331 | print(stations) | |
1332 | ||
1333 | get_stations() | |
1334 | del get_stations | |
1335 | """ | |
1336 | ||
9989 | 1337 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=10000) |
9795 | 1338 | if err: |
1339 | return [], err | |
1340 | ||
1341 | clientsList = ast.literal_eval(out.decode("utf-8")) | |
1342 | return clientsList, "" | |
1343 | ||
10022 | 1344 | def enableWebrepl(self, password): |
1345 | """ | |
1346 | Public method to write the given WebREPL password to the connected device and | |
1347 | modify the start script to start the WebREPL server. | |
1348 | ||
1349 | @param password password needed to authenticate | |
1350 | @type str | |
1351 | @return tuple containing a flag indicating success and an error message | |
1352 | @rtype tuple of (bool, str) | |
1353 | """ | |
1354 | command = """ | |
1355 | def modify_boot(): | |
1356 | import os | |
1357 | ||
1358 | try: | |
1359 | with open('/boot.py', 'r') as old_f, open('/boot.py.tmp', 'w') as new_f: | |
1360 | found = False | |
1361 | for l in old_f.read().splitlines(): | |
1362 | if 'webrepl' in l: | |
1363 | found = True | |
1364 | if l.startswith('#'): | |
1365 | l = l[1:] | |
1366 | new_f.write(l + '\\n') | |
1367 | if not found: | |
1368 | new_f.write('\\nimport webrepl\\nwebrepl.start()\\n') | |
1369 | ||
1370 | os.remove('/boot.py') | |
1371 | os.rename('/boot.py.tmp', '/boot.py') | |
1372 | except: | |
1373 | pass | |
1374 | ||
1375 | print(True) | |
1376 | ||
1377 | modify_boot() | |
1378 | del modify_boot | |
1379 | """ | |
1380 | ||
1381 | try: | |
1382 | # write config file | |
1383 | config = "PASS = {0}\n".format(repr(password)) | |
1384 | self.putData("/webrepl_cfg.py", config.encode("utf-8")) | |
1385 | except OSError as err: | |
1386 | return False, str(err) | |
1387 | ||
1388 | # modify boot.py | |
1389 | out, err = self.executeCommands(command, mode=self._submitMode) | |
1390 | if err: | |
1391 | return False, err | |
1392 | ||
1393 | return out.decode("utf-8").strip() == "True", "" | |
1394 | ||
1395 | def disableWebrepl(self): | |
1396 | """ | |
1397 | Public method to write the given WebREPL password to the connected device and | |
1398 | modify the start script to start the WebREPL server. | |
1399 | ||
1400 | @return tuple containing a flag indicating success and an error message | |
1401 | @rtype tuple of (bool, str) | |
1402 | """ | |
1403 | command = """ | |
1404 | def modify_boot(): | |
1405 | import os | |
1406 | ||
1407 | try: | |
1408 | with open('/boot.py', 'r') as old_f, open('/boot.py.tmp', 'w') as new_f: | |
1409 | for l in old_f.read().splitlines(): | |
1410 | if 'webrepl' in l: | |
1411 | if not l.startswith('#'): | |
1412 | l = '#' + l | |
1413 | new_f.write(l + '\\n') | |
1414 | ||
1415 | os.remove('/boot.py') | |
1416 | os.rename('/boot.py.tmp', '/boot.py') | |
1417 | except: | |
1418 | pass | |
1419 | ||
1420 | print(True) | |
1421 | ||
1422 | modify_boot() | |
1423 | del modify_boot | |
1424 | """ | |
1425 | ||
1426 | # modify boot.py | |
1427 | out, err = self.executeCommands(command, mode=self._submitMode) | |
1428 | if err: | |
1429 | return False, err | |
1430 | ||
1431 | return out.decode("utf-8").strip() == "True", "" | |
1432 | ||
10153 | 1433 | @pyqtSlot() |
1434 | def __setCountry(self): | |
1435 | """ | |
1436 | Private slot to configure the country of the connected ESP32 device. | |
1437 | ||
1438 | The country is the two-letter ISO 3166-1 Alpha-2 country code. | |
1439 | """ | |
1440 | from ..WifiDialogs.WifiCountryDialog import WifiCountryDialog | |
1441 | ||
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1442 | dlg = WifiCountryDialog(parent=self.microPython) |
10153 | 1443 | if dlg.exec() == QDialog.DialogCode.Accepted: |
1444 | country, remember = dlg.getCountry() | |
1445 | if remember: | |
1446 | Preferences.setMicroPython("WifiCountry", country) | |
1447 | ||
1448 | command = """ | |
1449 | try: | |
1450 | import network | |
1451 | network.country({0}) | |
1452 | except AttributeError: | |
1453 | pass | |
1454 | """.format( | |
1455 | repr(country) | |
1456 | ) | |
1457 | ||
1458 | out, err = self.executeCommands(command, mode=self._submitMode) | |
1459 | if err: | |
1460 | self.microPython.showError("country()", err) | |
1461 | ||
1462 | @pyqtSlot() | |
1463 | def __resetCountry(self): | |
1464 | """ | |
1465 | Private slot to reset the country of the connected ESP32 device. | |
1466 | ||
1467 | The country is the two-letter ISO 3166-1 Alpha-2 country code. This method | |
1468 | resets it to the default code 'XX' representing the "worldwide" region. | |
1469 | """ | |
1470 | command = """ | |
1471 | try: | |
1472 | import network | |
1473 | network.country('XX') | |
1474 | except AttributeError: | |
1475 | pass | |
1476 | """ | |
1477 | ||
1478 | out, err = self.executeCommands(command, mode=self._submitMode) | |
1479 | if err: | |
1480 | self.microPython.showError("country()", err) | |
1481 | ||
9855 | 1482 | ################################################################## |
1483 | ## Methods below implement Bluetooth related methods | |
1484 | ################################################################## | |
1485 | ||
1486 | def hasBluetooth(self): | |
1487 | """ | |
1488 | Public method to check the availability of Bluetooth. | |
1489 | ||
1490 | @return flag indicating the availability of Bluetooth | |
1491 | @rtype bool | |
9857 | 1492 | @exception OSError raised to indicate an issue with the device |
9855 | 1493 | """ |
9866 | 1494 | if self.hasCircuitPython(): |
1495 | self.__createCpyDevice() | |
1496 | return self.__cpyDevice.hasBluetooth() | |
1497 | ||
9855 | 1498 | command = """ |
1499 | def has_bt(): | |
1500 | try: | |
1501 | import bluetooth | |
1502 | if hasattr(bluetooth, 'BLE'): | |
1503 | return True | |
1504 | except ImportError: | |
1505 | pass | |
1506 | ||
1507 | return False | |
1508 | ||
1509 | print(has_bt()) | |
1510 | del has_bt | |
1511 | """ | |
9989 | 1512 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=10000) |
9855 | 1513 | if err: |
1514 | raise OSError(self._shortError(err)) | |
1515 | return out.strip() == b"True" | |
1516 | ||
1517 | def getBluetoothStatus(self): | |
1518 | """ | |
1519 | Public method to get Bluetooth status data of the connected board. | |
1520 | ||
1521 | @return list of tuples containing the translated status data label and | |
1522 | the associated value | |
1523 | @rtype list of tuples of (str, str) | |
9857 | 1524 | @exception OSError raised to indicate an issue with the device |
9855 | 1525 | """ |
9866 | 1526 | if self.hasCircuitPython(): |
1527 | return self.__cpyDevice.getBluetoothStatus() | |
1528 | ||
9855 | 1529 | command = """ |
1530 | def ble_status(): | |
1531 | import bluetooth | |
1532 | import ubinascii | |
1533 | import ujson | |
1534 | ||
1535 | ble = bluetooth.BLE() | |
1536 | ||
1537 | ble_active = ble.active() | |
1538 | if not ble_active: | |
1539 | ble.active(True) | |
1540 | ||
1541 | res = { | |
1542 | 'active': ble_active, | |
1543 | 'mac': ubinascii.hexlify(ble.config('mac')[1], ':').decode(), | |
1544 | 'addr_type': ble.config('mac')[0], | |
1545 | 'name': ble.config('gap_name'), | |
1546 | 'mtu': ble.config('mtu'), | |
1547 | } | |
1548 | ||
1549 | if not ble_active: | |
1550 | ble.active(False) | |
1551 | ||
1552 | print(ujson.dumps(res)) | |
1553 | ||
1554 | ble_status() | |
1555 | del ble_status | |
1556 | """ | |
9989 | 1557 | out, err = self.executeCommands(command, mode=self._submitMode) |
9855 | 1558 | if err: |
1559 | raise OSError(self._shortError(err)) | |
1560 | ||
1561 | status = [] | |
1562 | bleStatus = json.loads(out.decode("utf-8")) | |
1563 | status.append((self.tr("Active"), self.bool2str(bleStatus["active"]))) | |
1564 | status.append((self.tr("Name"), bleStatus["name"])) | |
1565 | status.append((self.tr("MAC-Address"), bleStatus["mac"])) | |
1566 | status.append( | |
1567 | ( | |
1568 | self.tr("Address Type"), | |
1569 | self.tr("Public") if bleStatus == 0 else self.tr("Random"), | |
1570 | ) | |
1571 | ) | |
9857 | 1572 | status.append((self.tr("MTU"), self.tr("{0} Bytes").format(bleStatus["mtu"]))) |
9855 | 1573 | |
1574 | return status | |
1575 | ||
1576 | def activateBluetoothInterface(self): | |
1577 | """ | |
1578 | Public method to activate the Bluetooth interface. | |
1579 | ||
1580 | @return flag indicating the new state of the Bluetooth interface | |
1581 | @rtype bool | |
9857 | 1582 | @exception OSError raised to indicate an issue with the device |
9855 | 1583 | """ |
9866 | 1584 | if self.hasCircuitPython(): |
1585 | return self.__cpyDevice.activateBluetoothInterface() | |
1586 | ||
9855 | 1587 | command = """ |
1588 | def activate_ble(): | |
1589 | import bluetooth | |
1590 | ||
1591 | ble = bluetooth.BLE() | |
1592 | if not ble.active(): | |
1593 | ble.active(True) | |
1594 | print(ble.active()) | |
1595 | ||
1596 | activate_ble() | |
1597 | del activate_ble | |
1598 | """ | |
9989 | 1599 | out, err = self.executeCommands(command, mode=self._submitMode) |
9855 | 1600 | if err: |
1601 | raise OSError(self._shortError(err)) | |
1602 | ||
1603 | return out.strip() == b"True" | |
1604 | ||
1605 | def deactivateBluetoothInterface(self): | |
1606 | """ | |
1607 | Public method to deactivate the Bluetooth interface. | |
1608 | ||
1609 | @return flag indicating the new state of the Bluetooth interface | |
1610 | @rtype bool | |
9857 | 1611 | @exception OSError raised to indicate an issue with the device |
9855 | 1612 | """ |
9866 | 1613 | if self.hasCircuitPython(): |
1614 | return self.__cpyDevice.deactivateBluetoothInterface() | |
1615 | ||
9855 | 1616 | command = """ |
1617 | def deactivate_ble(): | |
1618 | import bluetooth | |
1619 | ||
1620 | ble = bluetooth.BLE() | |
1621 | if ble.active(): | |
1622 | ble.active(False) | |
1623 | print(ble.active()) | |
1624 | ||
1625 | deactivate_ble() | |
1626 | del deactivate_ble | |
1627 | """ | |
9989 | 1628 | out, err = self.executeCommands(command, mode=self._submitMode) |
9855 | 1629 | if err: |
1630 | raise OSError(self._shortError(err)) | |
1631 | ||
1632 | return out.strip() == b"True" | |
1633 | ||
9859 | 1634 | def getDeviceScan(self, timeout=10): |
1635 | """ | |
1636 | Public method to perform a Bluetooth device scan. | |
1637 | ||
1638 | @param timeout duration of the device scan in seconds (defaults | |
1639 | to 10) | |
1640 | @type int (optional) | |
1641 | @return tuple containing a dictionary with the scan results and | |
1642 | an error string | |
1643 | @rtype tuple of (dict, str) | |
1644 | """ | |
1645 | from ..BluetoothDialogs.BluetoothAdvertisement import BluetoothAdvertisement | |
1646 | ||
9866 | 1647 | if self.hasCircuitPython(): |
1648 | return self.__cpyDevice.getDeviceScan(timeout) | |
1649 | ||
9859 | 1650 | command = """ |
10032
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1651 | _scan_done = False |
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1652 | |
9859 | 1653 | def ble_scan(): |
1654 | import bluetooth | |
1655 | import time | |
1656 | import ubinascii | |
1657 | ||
1658 | IRQ_SCAN_RESULT = 5 | |
1659 | IRQ_SCAN_DONE = 6 | |
1660 | ||
1661 | 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
|
1662 | global _scan_done |
9859 | 1663 | if event == IRQ_SCAN_RESULT: |
1664 | addr_type, addr, adv_type, rssi, adv_data = data | |
1665 | if addr: | |
1666 | print({{ | |
1667 | 'address': ubinascii.hexlify(addr,':').decode('utf-8'), | |
1668 | 'rssi': rssi, | |
1669 | 'adv_type': adv_type, | |
1670 | 'advertisement': bytes(adv_data), | |
1671 | }}) | |
10032
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1672 | 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
|
1673 | _scan_done = True |
9859 | 1674 | |
1675 | ble = bluetooth.BLE() | |
1676 | ||
1677 | ble_active = ble.active() | |
1678 | if not ble_active: | |
1679 | ble.active(True) | |
1680 | ||
1681 | ble.irq(_bleIrq) | |
1682 | 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
|
1683 | while not _scan_done: |
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1684 | time.sleep(0.2) |
9859 | 1685 | |
1686 | if not ble_active: | |
1687 | ble.active(False) | |
1688 | ||
1689 | ble_scan() | |
10032
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1690 | del ble_scan, _scan_done |
9859 | 1691 | """.format( |
1692 | timeout | |
1693 | ) | |
9989 | 1694 | out, err = self.executeCommands( |
9859 | 1695 | command, mode=self._submitMode, timeout=(timeout + 5) * 1000 |
1696 | ) | |
1697 | if err: | |
1698 | return {}, err | |
1699 | ||
1700 | scanResults = {} | |
1701 | for line in out.decode("utf-8").splitlines(): | |
1702 | res = ast.literal_eval(line) | |
1703 | address = res["address"] | |
1704 | if address not in scanResults: | |
1705 | scanResults[address] = BluetoothAdvertisement(address) | |
1706 | scanResults[address].update( | |
1707 | res["adv_type"], res["rssi"], res["advertisement"] | |
1708 | ) | |
1709 | ||
1710 | return scanResults, "" | |
1711 | ||
11167 | 1712 | def supportsDeviceScan(self): |
1713 | """ | |
1714 | Public method to indicate, that the Bluetooth implementation supports | |
1715 | scanning for devices. | |
1716 | ||
1717 | @return flag indicating that the scanning function is supported | |
1718 | @rtype bool | |
1719 | """ | |
1720 | return True | |
1721 | ||
9868 | 1722 | ################################################################## |
1723 | ## Methods below implement NTP related methods | |
1724 | ################################################################## | |
1725 | ||
1726 | def hasNetworkTime(self): | |
1727 | """ | |
1728 | Public method to check the availability of network time functions. | |
1729 | ||
1730 | @return flag indicating the availability of network time functions | |
1731 | @rtype bool | |
9870
0399d3607829
Fixed a few code formatting and style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9868
diff
changeset
|
1732 | @exception OSError raised to indicate an issue with the device |
9868 | 1733 | """ |
1734 | if self.hasCircuitPython(): | |
1735 | self.__createCpyDevice() | |
1736 | return self.__cpyDevice.hasNetworkTime() | |
1737 | ||
1738 | command = """ | |
1739 | def has_ntp(): | |
1740 | try: | |
1741 | import ntptime | |
1742 | return True | |
1743 | except ImportError: | |
1744 | return False | |
1745 | ||
1746 | print(has_ntp()) | |
1747 | del has_ntp | |
1748 | """ | |
9989 | 1749 | out, err = self.executeCommands(command, mode=self._submitMode) |
9868 | 1750 | if err: |
1751 | raise OSError(self._shortError(err)) | |
1752 | return out.strip() == b"True" | |
1753 | ||
1754 | def setNetworkTime(self, server="0.pool.ntp.org", tzOffset=0, timeout=10): | |
1755 | """ | |
1756 | Public method to set the time to the network time retrieved from an | |
1757 | NTP server. | |
1758 | ||
1759 | @param server name of the NTP server to get the network time from | |
1760 | (defaults to "0.pool.ntp.org") | |
1761 | @type str (optional) | |
1762 | @param tzOffset offset with respect to UTC (defaults to 0) | |
1763 | @type int (optional) | |
1764 | @param timeout maximum time to wait for a server response in seconds | |
1765 | (defaults to 10) | |
1766 | @type int | |
1767 | @return tuple containing a flag indicating success and an error string | |
1768 | @rtype tuple of (bool, str) | |
1769 | """ | |
1770 | if self.hasCircuitPython(): | |
1771 | return self.__cpyDevice.setNetworkTime( | |
1772 | server=server, tzOffset=tzOffset, timeout=timeout | |
1773 | ) | |
1774 | ||
1775 | command = """ | |
1776 | def set_ntp_time(server, tz_offset, timeout): | |
1777 | import network | |
1778 | import ntptime | |
1779 | import machine | |
1780 | ||
1781 | if not network.WLAN(network.STA_IF).isconnected(): | |
1782 | return False | |
1783 | ||
1784 | ntptime.host = server | |
1785 | ntptime.timeout = timeout | |
1786 | ntptime.settime() | |
1787 | ||
1788 | rtc = machine.RTC() | |
1789 | t = list(rtc.datetime()) | |
1790 | t[4] += tz_offset | |
1791 | rtc.datetime(t) | |
1792 | ||
1793 | return True | |
1794 | ||
1795 | try: | |
1796 | print({{ | |
1797 | 'result': set_ntp_time({0}, {1}, {2}), | |
1798 | 'error': '', | |
1799 | }}) | |
1800 | except Exception as err: | |
1801 | print({{ | |
1802 | 'result': False, | |
1803 | 'error': str(err), | |
1804 | }}) | |
1805 | del set_ntp_time | |
1806 | """.format( | |
1807 | repr(server), tzOffset, timeout | |
1808 | ) | |
9989 | 1809 | out, err = self.executeCommands( |
9868 | 1810 | command, mode=self._submitMode, timeout=(timeout + 2) * 1000 |
1811 | ) | |
1812 | if err: | |
1813 | return False, err | |
1814 | else: | |
1815 | res = ast.literal_eval(out.decode("utf-8")) | |
1816 | return res["result"], res["error"] | |
1817 | ||
9496
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1818 | |
10683
779cda568acb
Changed the source code and the source code documentation to improve the indication of unused method/function arguments.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
1819 | def createDevice(microPythonWidget, deviceType, _vid, _pid, _boardName, _serialNumber): |
9496
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1820 | """ |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1821 | 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
|
1822 | |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1823 | @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
|
1824 | @type MicroPythonWidget |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1825 | @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
|
1826 | @type str |
10683
779cda568acb
Changed the source code and the source code documentation to improve the indication of unused method/function arguments.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
1827 | @param _vid vendor ID (unused) |
9496
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1828 | @type int |
10683
779cda568acb
Changed the source code and the source code documentation to improve the indication of unused method/function arguments.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
1829 | @param _pid product ID (unused) |
9496
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1830 | @type int |
10683
779cda568acb
Changed the source code and the source code documentation to improve the indication of unused method/function arguments.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
1831 | @param _boardName name of the board (unused) |
9738 | 1832 | @type str |
10683
779cda568acb
Changed the source code and the source code documentation to improve the indication of unused method/function arguments.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10439
diff
changeset
|
1833 | @param _serialNumber serial number of the board (unused) |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
1834 | @type str |
9496
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1835 | @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
|
1836 | @rtype EspDevice |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1837 | """ |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1838 | return EspDevice(microPythonWidget, deviceType) |
9820 | 1839 | |
1840 | ||
1841 | ################################################################################ | |
1842 | ## Functions below implement flashing related functionality needed elsewhere ## | |
1843 | ## as well. ## | |
1844 | ################################################################################ | |
1845 | ||
1846 | ||
1847 | @pyqtSlot() | |
11006
a671918232f3
Modified modal dialog usage to always include a valid parent (needed for Wayland).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11005
diff
changeset
|
1848 | def eraseFlash(port, parent=None): |
9820 | 1849 | """ |
1850 | Slot to erase the device flash memory. | |
1851 | ||
1852 | @param port name of the serial port device to be used | |
1853 | @type str | |
11006
a671918232f3
Modified modal dialog usage to always include a valid parent (needed for Wayland).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11005
diff
changeset
|
1854 | @param parent reference to the parent widget (defaults to None) |
a671918232f3
Modified modal dialog usage to always include a valid parent (needed for Wayland).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11005
diff
changeset
|
1855 | @type QWidget |
9820 | 1856 | """ |
1857 | ok = EricMessageBox.yesNo( | |
11006
a671918232f3
Modified modal dialog usage to always include a valid parent (needed for Wayland).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11005
diff
changeset
|
1858 | parent, |
9820 | 1859 | QCoreApplication.translate("EspDevice", "Erase Flash"), |
1860 | QCoreApplication.translate( | |
1861 | "EspDevice", """Shall the flash of the selected device really be erased?""" | |
1862 | ), | |
1863 | ) | |
1864 | if ok: | |
1865 | flashArgs = [ | |
1866 | "-u", | |
1867 | "-m", | |
1868 | "esptool", | |
1869 | "--port", | |
1870 | port, | |
1871 | "erase_flash", | |
1872 | ] | |
1873 | dlg = EricProcessDialog( | |
1874 | QCoreApplication.translate("EspDevice", "'esptool erase_flash' Output"), | |
1875 | QCoreApplication.translate("EspDevice", "Erase Flash"), | |
1876 | showProgress=True, | |
10933
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
1877 | monospacedFont=Preferences.getEditorOtherFonts("MonospacedFont"), |
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
1878 | encoding=Preferences.getSystem("IOEncoding"), |
11006
a671918232f3
Modified modal dialog usage to always include a valid parent (needed for Wayland).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11005
diff
changeset
|
1879 | parent=parent, |
9820 | 1880 | ) |
1881 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs) | |
1882 | if res: | |
1883 | dlg.exec() | |
1884 | ||
1885 | ||
1886 | @pyqtSlot() | |
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1887 | def flashPythonFirmware(port, parent=None): |
9820 | 1888 | """ |
1889 | Slot to flash a MicroPython firmware to the device. | |
1890 | ||
1891 | @param port name of the serial port device to be used | |
1892 | @type str | |
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1893 | @param parent reference to the parent widget (defaults to None) |
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1894 | @type QWidget |
9820 | 1895 | """ |
1896 | from .EspDialogs.EspFirmwareSelectionDialog import EspFirmwareSelectionDialog | |
1897 | ||
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1898 | dlg = EspFirmwareSelectionDialog(parent=parent) |
9820 | 1899 | if dlg.exec() == QDialog.DialogCode.Accepted: |
1900 | chip, firmware, baudRate, flashMode, flashAddress = dlg.getData() | |
1901 | flashArgs = [ | |
1902 | "-u", | |
1903 | "-m", | |
1904 | "esptool", | |
1905 | "--chip", | |
1906 | chip, | |
1907 | "--port", | |
1908 | port, | |
1909 | ] | |
1910 | if baudRate != "115200": | |
1911 | flashArgs += ["--baud", baudRate] | |
1912 | flashArgs.append("write_flash") | |
1913 | if flashMode: | |
1914 | flashArgs += ["--flash_mode", flashMode] | |
9848
3d750b2e012c
Corrected flashing of ESP 8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9836
diff
changeset
|
1915 | if chip == "esp8266": |
3d750b2e012c
Corrected flashing of ESP 8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9836
diff
changeset
|
1916 | # 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
|
1917 | flashArgs += ["--flash_size", "detect"] |
9820 | 1918 | flashArgs += [ |
1919 | flashAddress, | |
1920 | firmware, | |
1921 | ] | |
1922 | dlg = EricProcessDialog( | |
1923 | QCoreApplication.translate("EspDevice", "'esptool write_flash' Output"), | |
1924 | QCoreApplication.translate("EspDevice", "Flash µPy/CPy Firmware"), | |
1925 | showProgress=True, | |
10933
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
1926 | monospacedFont=Preferences.getEditorOtherFonts("MonospacedFont"), |
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
1927 | encoding=Preferences.getSystem("IOEncoding"), |
11006
a671918232f3
Modified modal dialog usage to always include a valid parent (needed for Wayland).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11005
diff
changeset
|
1928 | parent=parent, |
9820 | 1929 | ) |
1930 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs) | |
1931 | if res: | |
1932 | dlg.exec() | |
1933 | ||
1934 | ||
1935 | @pyqtSlot() | |
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1936 | def flashAddonFirmware(port, parent=None): |
9820 | 1937 | """ |
1938 | Slot to flash some additional firmware images. | |
1939 | ||
1940 | @param port name of the serial port device to be used | |
1941 | @type str | |
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1942 | @param parent reference to the parent widget (defaults to None) |
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1943 | @type QWidget |
9820 | 1944 | """ |
1945 | from .EspDialogs.EspFirmwareSelectionDialog import EspFirmwareSelectionDialog | |
1946 | ||
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1947 | dlg = EspFirmwareSelectionDialog(addon=True, parent=parent) |
9820 | 1948 | if dlg.exec() == QDialog.DialogCode.Accepted: |
1949 | chip, firmware, baudRate, flashMode, flashAddress = dlg.getData() | |
1950 | flashArgs = [ | |
1951 | "-u", | |
1952 | "-m", | |
1953 | "esptool", | |
1954 | "--chip", | |
1955 | chip, | |
1956 | "--port", | |
1957 | port, | |
1958 | ] | |
1959 | if baudRate != "115200": | |
1960 | flashArgs += ["--baud", baudRate] | |
1961 | flashArgs.append("write_flash") | |
1962 | if flashMode: | |
1963 | flashArgs += ["--flash_mode", flashMode] | |
1964 | flashArgs += [ | |
1965 | flashAddress.lower(), | |
1966 | firmware, | |
1967 | ] | |
1968 | dlg = EricProcessDialog( | |
1969 | QCoreApplication.translate("EspDevice", "'esptool write_flash' Output"), | |
1970 | QCoreApplication.translate("EspDevice", "Flash Additional Firmware"), | |
1971 | showProgress=True, | |
10933
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
1972 | monospacedFont=Preferences.getEditorOtherFonts("MonospacedFont"), |
95a15b70f7bb
Refactored some packages, modules and code to allow extracting the 'EricXxx' packages into a library project.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
1973 | encoding=Preferences.getSystem("IOEncoding"), |
11006
a671918232f3
Modified modal dialog usage to always include a valid parent (needed for Wayland).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11005
diff
changeset
|
1974 | parent=parent, |
9820 | 1975 | ) |
1976 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs) | |
1977 | if res: | |
1978 | dlg.exec() |