Fri, 25 Apr 2025 16:23:02 +0200
MicroPython
- Extended the handling of Access Point security/authmode settings to be more dynamic.
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 | 'ifconfig': wifi.ifconfig(), | |
751 | 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), | |
752 | } | |
753 | if wifi.active(): | |
9797 | 754 | try: |
755 | station['txpower'] = wifi.config('txpower') | |
756 | except ValueError: | |
757 | pass | |
9795 | 758 | print(ujson.dumps(station)) |
759 | ||
760 | wifi = network.WLAN(network.AP_IF) | |
761 | ap = { | |
762 | 'active': wifi.active(), | |
763 | 'connected': wifi.isconnected(), | |
764 | 'status': wifi.status(), | |
765 | 'ifconfig': wifi.ifconfig(), | |
766 | 'mac': ubinascii.hexlify(wifi.config('mac'), ':').decode(), | |
767 | 'channel': wifi.config('channel'), | |
768 | 'essid': wifi.config('essid'), | |
11236 | 769 | 'ap_security': security_str(wifi.config('security')), |
9795 | 770 | } |
771 | if wifi.active(): | |
9797 | 772 | try: |
773 | ap['txpower'] = wifi.config('txpower') | |
774 | except ValueError: | |
775 | pass | |
9795 | 776 | print(ujson.dumps(ap)) |
777 | ||
9798 | 778 | overall = { |
779 | 'active': station['active'] or ap['active'] | |
780 | } | |
10153 | 781 | try: |
782 | overall['hostname'] = network.hostname() | |
783 | except AttributeError: | |
784 | pass | |
785 | try: | |
786 | overall['country'] = network.country() | |
787 | except AttributeError: | |
788 | pass | |
9798 | 789 | print(ujson.dumps(overall)) |
790 | ||
9795 | 791 | wifi_status() |
792 | del wifi_status | |
793 | """ | |
794 | ||
9989 | 795 | out, err = self.executeCommands(command, mode=self._submitMode) |
9795 | 796 | if err: |
797 | raise OSError(self._shortError(err)) | |
798 | ||
9798 | 799 | stationStr, apStr, overallStr = out.decode("utf-8").splitlines() |
9795 | 800 | station = json.loads(stationStr) |
801 | ap = json.loads(apStr) | |
9798 | 802 | overall = json.loads(overallStr) |
9795 | 803 | try: |
804 | station["status"] = self.__statusTranslations[station["status"]] | |
805 | except KeyError: | |
806 | station["status"] = str(station["status"]) | |
807 | try: | |
808 | ap["status"] = self.__statusTranslations[ap["status"]] | |
809 | except KeyError: | |
810 | ap["status"] = str(ap["status"]) | |
11236 | 811 | if "ap_security" in ap: |
812 | try: | |
813 | ap["ap_security"] = self.__securityMapping[ap["ap_security"]] | |
814 | except KeyError: | |
815 | ap["ap_security"] = self.tr("unknown ({0})").format(ap["ap_security"]) | |
9798 | 816 | return station, ap, overall |
9795 | 817 | |
10153 | 818 | def connectWifi(self, ssid, password, hostname): |
9795 | 819 | """ |
820 | Public method to connect a device to a WiFi network. | |
821 | ||
822 | @param ssid name (SSID) of the WiFi network | |
823 | @type str | |
824 | @param password password needed to connect | |
825 | @type str | |
10153 | 826 | @param hostname host name of the device |
827 | @type str | |
9795 | 828 | @return tuple containing the connection status and an error string |
829 | @rtype tuple of (bool, str) | |
830 | """ | |
9866 | 831 | if self.hasCircuitPython(): |
10153 | 832 | return self.__cpyDevice.connectWifi(ssid, password, hostname) |
9866 | 833 | |
9795 | 834 | command = """ |
10153 | 835 | def connect_wifi(ssid, password, hostname): |
9795 | 836 | import network |
837 | import ujson | |
838 | from time import sleep | |
839 | ||
10153 | 840 | if hostname: |
841 | try: | |
842 | network.hostname(hostname) | |
843 | except AttributeError: | |
844 | pass | |
845 | ||
9795 | 846 | wifi = network.WLAN(network.STA_IF) |
847 | wifi.active(False) | |
848 | wifi.active(True) | |
849 | wifi.connect(ssid, password) | |
850 | max_wait = 140 | |
851 | while max_wait and wifi.status() == network.STAT_CONNECTING: | |
852 | max_wait -= 1 | |
853 | sleep(0.1) | |
854 | status = wifi.status() | |
855 | print(ujson.dumps({{'connected': wifi.isconnected(), 'status': status}})) | |
856 | ||
10153 | 857 | connect_wifi({0}, {1}, {2}) |
9795 | 858 | del connect_wifi |
859 | """.format( | |
9798 | 860 | repr(ssid), |
861 | repr(password if password else ""), | |
10153 | 862 | repr(hostname), |
9798 | 863 | ) |
9795 | 864 | |
865 | with EricOverrideCursor(): | |
9989 | 866 | out, err = self.executeCommands( |
9827 | 867 | command, mode=self._submitMode, timeout=15000 |
9799 | 868 | ) |
9795 | 869 | if err: |
870 | return False, err | |
871 | ||
10235
4a12b160094c
MicroPython interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10233
diff
changeset
|
872 | while not out.startswith(b"{"): |
4a12b160094c
MicroPython interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10233
diff
changeset
|
873 | # discard output until next newline |
4a12b160094c
MicroPython interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10233
diff
changeset
|
874 | _, out = out.split(b"\r\n", 1) |
9795 | 875 | result = json.loads(out.decode("utf-8").strip()) |
876 | if result["connected"]: | |
877 | error = "" | |
878 | else: | |
879 | try: | |
880 | error = self.__statusTranslations[result["status"]] | |
881 | except KeyError: | |
882 | error = str(result["status"]) | |
883 | ||
884 | return result["connected"], error | |
885 | ||
886 | def disconnectWifi(self): | |
887 | """ | |
888 | Public method to disconnect a device from the WiFi network. | |
889 | ||
890 | @return tuple containing a flag indicating success and an error string | |
891 | @rtype tuple of (bool, str) | |
892 | """ | |
9866 | 893 | if self.hasCircuitPython(): |
894 | return self.__cpyDevice.disconnectWifi() | |
895 | ||
9795 | 896 | command = """ |
897 | def disconnect_wifi(): | |
898 | import network | |
899 | from time import sleep | |
900 | ||
901 | wifi = network.WLAN(network.STA_IF) | |
902 | wifi.disconnect() | |
903 | wifi.active(False) | |
904 | sleep(0.1) | |
905 | print(not wifi.isconnected()) | |
906 | ||
907 | disconnect_wifi() | |
908 | del disconnect_wifi | |
909 | """ | |
910 | ||
9989 | 911 | out, err = self.executeCommands(command, mode=self._submitMode) |
9795 | 912 | if err: |
913 | return False, err | |
914 | ||
10144 | 915 | return out.decode("utf-8").strip() == "True", "" |
916 | ||
917 | def isWifiClientConnected(self): | |
918 | """ | |
919 | Public method to check the WiFi connection status as client. | |
920 | ||
921 | @return flag indicating the WiFi connection status | |
922 | @rtype bool | |
923 | """ | |
924 | if self.hasCircuitPython(): | |
925 | return self.__cpyDevice.isWifiClientConnected() | |
926 | ||
927 | command = """ | |
928 | def wifi_connected(): | |
929 | import network | |
930 | ||
931 | wifi = network.WLAN(network.STA_IF) | |
932 | print(wifi.isconnected()) | |
933 | ||
934 | wifi_connected() | |
935 | del wifi_connected | |
936 | """ | |
937 | ||
938 | out, err = self.executeCommands(command, mode=self._submitMode) | |
939 | if err: | |
940 | return False | |
10138 | 941 | |
10144 | 942 | return out.strip() == b"True" |
943 | ||
944 | def isWifiApConnected(self): | |
945 | """ | |
946 | Public method to check the WiFi connection status as access point. | |
947 | ||
948 | @return flag indicating the WiFi connection status | |
949 | @rtype bool | |
950 | """ | |
951 | if self.hasCircuitPython(): | |
952 | return self.__cpyDevice.isWifiApConnected() | |
953 | ||
954 | command = """ | |
955 | def wifi_connected(): | |
956 | import network | |
957 | ||
958 | wifi = network.WLAN(network.AP_IF) | |
959 | print(wifi.isconnected()) | |
960 | ||
961 | wifi_connected() | |
962 | del wifi_connected | |
963 | """ | |
964 | ||
965 | out, err = self.executeCommands(command, mode=self._submitMode) | |
966 | if err: | |
967 | return False | |
968 | ||
969 | return out.strip() == b"True" | |
9795 | 970 | |
10153 | 971 | def writeCredentials(self, ssid, password, hostname, country): |
9795 | 972 | """ |
973 | Public method to write the given credentials to the connected device and modify | |
974 | the start script to connect automatically. | |
975 | ||
976 | @param ssid SSID of the network to connect to | |
977 | @type str | |
978 | @param password password needed to authenticate | |
979 | @type str | |
10153 | 980 | @param hostname host name of the device |
981 | @type str | |
982 | @param country WiFi country code | |
983 | @type str | |
9795 | 984 | @return tuple containing a flag indicating success and an error message |
985 | @rtype tuple of (bool, str) | |
986 | """ | |
9866 | 987 | if self.hasCircuitPython(): |
10153 | 988 | return self.__cpyDevice.writeCredentials(ssid, password, hostname) |
9866 | 989 | |
9795 | 990 | nvsCommand = """ |
10153 | 991 | def save_wifi_creds(ssid, password, hostname, country): |
9795 | 992 | import esp32 |
993 | ||
994 | nvs = esp32.NVS('wifi_creds') | |
995 | nvs.set_blob('ssid', ssid) | |
996 | nvs.set_blob('password', password) | |
10153 | 997 | nvs.set_blob('hostname', hostname) |
998 | nvs.set_blob('country', country) | |
9795 | 999 | nvs.commit() |
1000 | ||
10153 | 1001 | save_wifi_creds({0}, {1}, {2}, {3}) |
9795 | 1002 | del save_wifi_creds |
9798 | 1003 | """.format( |
10153 | 1004 | repr(ssid), |
1005 | repr(password) if password else "''", | |
1006 | repr(hostname) if hostname else "''", | |
1007 | repr(country.upper()) if country else "''", | |
9798 | 1008 | ) |
9795 | 1009 | bootCommand = """ |
1010 | def modify_boot(): | |
1011 | add = True | |
1012 | try: | |
1013 | with open('/boot.py', 'r') as f: | |
1014 | for ln in f.readlines(): | |
1015 | if 'wifi_connect' in ln: | |
1016 | add = False | |
1017 | break | |
1018 | except: | |
1019 | pass | |
1020 | if add: | |
1021 | with open('/boot.py', 'a') as f: | |
1022 | f.write('\\nimport wifi_connect\\n') | |
1023 | print(True) | |
1024 | ||
1025 | modify_boot() | |
1026 | del modify_boot | |
1027 | """ | |
1028 | ||
9989 | 1029 | out, err = self.executeCommands(nvsCommand, mode=self._submitMode) |
9795 | 1030 | if err: |
1031 | return False, self.tr("Error saving credentials: {0}").format(err) | |
1032 | ||
1033 | try: | |
1034 | # copy auto-connect file | |
1035 | self.put( | |
1036 | os.path.join( | |
1037 | os.path.dirname(__file__), "MCUScripts", "esp32WiFiConnect.py" | |
1038 | ), | |
1039 | "/wifi_connect.py", | |
1040 | ) | |
1041 | except OSError as err: | |
1042 | return False, self.tr("Error saving auto-connect script: {0}").format(err) | |
1043 | ||
9989 | 1044 | out, err = self.executeCommands(bootCommand, mode=self._submitMode) |
9795 | 1045 | if err: |
1046 | return False, self.tr("Error modifying 'boot.py': {0}").format(err) | |
1047 | ||
1048 | return True, "" | |
1049 | ||
1050 | def removeCredentials(self): | |
1051 | """ | |
1052 | Public method to remove the saved credentials from the connected device. | |
1053 | ||
1054 | @return tuple containing a flag indicating success and an error message | |
1055 | @rtype tuple of (bool, str) | |
1056 | """ | |
9866 | 1057 | if self.hasCircuitPython(): |
1058 | return self.__cpyDevice.removeCredentials() | |
1059 | ||
9795 | 1060 | nvsCommand = """ |
1061 | def delete_wifi_creds(): | |
1062 | import esp32 | |
1063 | ||
1064 | nvs = esp32.NVS('wifi_creds') | |
1065 | try: | |
1066 | nvs.erase_key('ssid') | |
1067 | nvs.erase_key('password') | |
1068 | nvs.commit() | |
1069 | except OSError: | |
1070 | pass | |
1071 | ||
1072 | delete_wifi_creds() | |
1073 | del delete_wifi_creds | |
1074 | """ | |
1075 | ||
9989 | 1076 | out, err = self.executeCommands(nvsCommand, mode=self._submitMode) |
9795 | 1077 | if err: |
1078 | return False, self.tr("Error deleting credentials: {0}").format(err) | |
1079 | ||
1080 | return True, "" | |
1081 | ||
1082 | def checkInternet(self): | |
1083 | """ | |
1084 | Public method to check, if the internet can be reached. | |
1085 | ||
1086 | @return tuple containing a flag indicating reachability and an error string | |
1087 | @rtype tuple of (bool, str) | |
1088 | """ | |
9866 | 1089 | if self.hasCircuitPython(): |
1090 | return self.__cpyDevice.checkInternet() | |
1091 | ||
9795 | 1092 | command = """ |
1093 | def check_internet(): | |
1094 | import network | |
1095 | import socket | |
1096 | ||
1097 | wifi = network.WLAN(network.STA_IF) | |
1098 | if wifi.isconnected(): | |
1099 | s = socket.socket() | |
1100 | try: | |
10233
51a6649ba79d
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10170
diff
changeset
|
1101 | s.connect(socket.getaddrinfo('quad9.net', 443)[0][-1]) |
9795 | 1102 | s.close() |
1103 | print(True) | |
1104 | except: | |
1105 | print(False) | |
1106 | else: | |
1107 | print(False) | |
1108 | ||
1109 | check_internet() | |
1110 | del check_internet | |
1111 | """ | |
1112 | ||
9989 | 1113 | out, err = self.executeCommands(command, mode=self._submitMode) |
9795 | 1114 | if err: |
1115 | return False, err | |
1116 | ||
1117 | return out.decode("utf-8").strip() == "True", "" | |
1118 | ||
1119 | def scanNetworks(self): | |
1120 | """ | |
1121 | Public method to scan for available WiFi networks. | |
1122 | ||
1123 | @return tuple containing the list of available networks as a tuple of 'Name', | |
1124 | 'MAC-Address', 'channel', 'RSSI' and 'security' and an error string | |
1125 | @rtype tuple of (list of tuple of (str, str, int, int, str), str) | |
1126 | """ | |
9866 | 1127 | if self.hasCircuitPython(): |
1128 | return self.__cpyDevice.scanNetworks() | |
1129 | ||
9795 | 1130 | command = """ |
1131 | def scan_networks(): | |
1132 | import network | |
1133 | ||
1134 | wifi = network.WLAN(network.STA_IF) | |
1135 | active = wifi.active() | |
1136 | if not active: | |
1137 | wifi.active(True) | |
1138 | network_list = wifi.scan() | |
1139 | if not active: | |
1140 | wifi.active(False) | |
1141 | print(network_list) | |
1142 | ||
1143 | scan_networks() | |
1144 | del scan_networks | |
1145 | """ | |
1146 | ||
9989 | 1147 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=15000) |
9795 | 1148 | if err: |
1149 | return [], err | |
1150 | ||
1151 | networksList = ast.literal_eval(out.decode("utf-8")) | |
1152 | networks = [] | |
1153 | for network in networksList: | |
1154 | if network[0]: | |
1155 | ssid = network[0].decode("utf-8") | |
1156 | mac = binascii.hexlify(network[1], ":").decode("utf-8") | |
1157 | channel = network[2] | |
1158 | rssi = network[3] | |
1159 | try: | |
1160 | security = self.__securityTranslations[network[4]] | |
1161 | except KeyError: | |
1162 | security = self.tr("unknown ({0})").format(network[4]) | |
1163 | networks.append((ssid, mac, channel, rssi, security)) | |
1164 | ||
1165 | return networks, "" | |
1166 | ||
1167 | def deactivateInterface(self, interface): | |
1168 | """ | |
1169 | Public method to deactivate a given WiFi interface of the connected device. | |
1170 | ||
1171 | @param interface designation of the interface to be deactivated (one of 'AP' | |
1172 | or 'STA') | |
1173 | @type str | |
1174 | @return tuple containg a flag indicating success and an error message | |
1175 | @rtype tuple of (bool, str) | |
1176 | @exception ValueError raised to indicate a wrong value for the interface type | |
1177 | """ | |
1178 | if interface not in ("STA", "AP"): | |
1179 | raise ValueError( | |
1180 | "interface must be 'AP' or 'STA', got '{0}'".format(interface) | |
1181 | ) | |
1182 | ||
9866 | 1183 | if self.hasCircuitPython(): |
1184 | return self.__cpyDevice.deactivateInterface(interface) | |
1185 | ||
9795 | 1186 | command = """ |
1187 | def deactivate(): | |
1188 | import network | |
1189 | from time import sleep | |
1190 | ||
1191 | wifi = network.WLAN(network.{0}_IF) | |
1192 | wifi.active(False) | |
1193 | sleep(0.1) | |
1194 | print(not wifi.active()) | |
1195 | ||
1196 | deactivate() | |
1197 | del deactivate | |
1198 | """.format( | |
9798 | 1199 | interface |
1200 | ) | |
9795 | 1201 | |
9989 | 1202 | out, err = self.executeCommands(command, mode=self._submitMode) |
9795 | 1203 | if err: |
1204 | return False, err | |
1205 | else: | |
1206 | return out.decode("utf-8").strip() == "True", "" | |
1207 | ||
10153 | 1208 | def startAccessPoint( |
1209 | self, | |
1210 | ssid, | |
1211 | security=None, | |
1212 | password=None, | |
1213 | hostname=None, | |
1214 | ifconfig=None, | |
1215 | ): | |
9795 | 1216 | """ |
1217 | Public method to start the access point interface. | |
1218 | ||
1219 | @param ssid SSID of the access point | |
1220 | @type str | |
11236 | 1221 | @param security security mode (defaults to None) (unused) |
1222 | @type str (optional) | |
9795 | 1223 | @param password password (defaults to None) |
1224 | @type str (optional) | |
10153 | 1225 | @param hostname host name of the device (defaults to None) |
1226 | @type str (optional) | |
9797 | 1227 | @param ifconfig IPv4 configuration for the access point if not default |
1228 | (IPv4 address, netmask, gateway address, DNS server address) | |
1229 | @type tuple of (str, str, str, str) | |
9795 | 1230 | @return tuple containing a flag indicating success and an error message |
1231 | @rtype tuple of (bool, str) | |
1232 | """ | |
9866 | 1233 | if self.hasCircuitPython(): |
1234 | return self.__cpyDevice.startAccessPoint( | |
10153 | 1235 | ssid, |
1236 | security=security, | |
1237 | password=password, | |
1238 | hostname=hostname, | |
1239 | ifconfig=ifconfig, | |
9866 | 1240 | ) |
1241 | ||
9795 | 1242 | if security is None or password is None: |
11236 | 1243 | security = "SEC_OPEN" |
10170
6cf1ee737d8f
Corrected some more code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10153
diff
changeset
|
1244 | password = "" # secok |
9795 | 1245 | |
1246 | command = """ | |
11236 | 1247 | def start_ap(ssid, password, hostname, ifconfig): |
9795 | 1248 | import network |
1249 | ||
10153 | 1250 | if hostname: |
1251 | try: | |
1252 | network.hostname(hostname) | |
1253 | except AttributeError: | |
1254 | pass | |
1255 | ||
9795 | 1256 | ap = network.WLAN(network.AP_IF) |
1257 | ap.active(False) | |
9797 | 1258 | if ifconfig: |
1259 | ap.ifconfig(ifconfig) | |
9795 | 1260 | ap.active(True) |
1261 | try: | |
11236 | 1262 | ap.config(ssid=ssid, authmode=network.WLAN.{4}, password=password) |
9795 | 1263 | except: |
11236 | 1264 | ap.config(essid=ssid, authmode=network.WLAN.{4}, password=password) |
9795 | 1265 | |
11236 | 1266 | start_ap({0}, {1}, {2}, {3}) |
9795 | 1267 | del start_ap |
1268 | """.format( | |
11236 | 1269 | repr(ssid), repr(password), repr(hostname), ifconfig, security |
9798 | 1270 | ) |
9795 | 1271 | |
9989 | 1272 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=15000) |
9795 | 1273 | if err: |
1274 | return False, err | |
1275 | else: | |
1276 | return True, "" | |
1277 | ||
1278 | def stopAccessPoint(self): | |
1279 | """ | |
1280 | Public method to stop the access point interface. | |
1281 | ||
1282 | @return tuple containg a flag indicating success and an error message | |
1283 | @rtype tuple of (bool, str) | |
1284 | """ | |
9866 | 1285 | if self.hasCircuitPython(): |
1286 | return self.__cpyDevice.stopAccessPoint() | |
1287 | ||
9795 | 1288 | return self.deactivateInterface("AP") |
1289 | ||
1290 | def getConnectedClients(self): | |
1291 | """ | |
1292 | Public method to get a list of connected clients. | |
1293 | ||
1294 | @return a tuple containing a list of tuples containing the client MAC-Address | |
1295 | and the RSSI (if supported and available) and an error message | |
1296 | @rtype tuple of ([(bytes, int)], str) | |
1297 | """ | |
9866 | 1298 | if self.hasCircuitPython(): |
1299 | return self.__cpyDevice.getConnectedClients() | |
1300 | ||
9795 | 1301 | command = """ |
1302 | def get_stations(): | |
1303 | import network | |
1304 | ||
1305 | ap = network.WLAN(network.AP_IF) | |
1306 | stations = ap.status('stations') | |
1307 | print(stations) | |
1308 | ||
1309 | get_stations() | |
1310 | del get_stations | |
1311 | """ | |
1312 | ||
9989 | 1313 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=10000) |
9795 | 1314 | if err: |
1315 | return [], err | |
1316 | ||
1317 | clientsList = ast.literal_eval(out.decode("utf-8")) | |
1318 | return clientsList, "" | |
1319 | ||
10022 | 1320 | def enableWebrepl(self, password): |
1321 | """ | |
1322 | Public method to write the given WebREPL password to the connected device and | |
1323 | modify the start script to start the WebREPL server. | |
1324 | ||
1325 | @param password password needed to authenticate | |
1326 | @type str | |
1327 | @return tuple containing a flag indicating success and an error message | |
1328 | @rtype tuple of (bool, str) | |
1329 | """ | |
1330 | command = """ | |
1331 | def modify_boot(): | |
1332 | import os | |
1333 | ||
1334 | try: | |
1335 | with open('/boot.py', 'r') as old_f, open('/boot.py.tmp', 'w') as new_f: | |
1336 | found = False | |
1337 | for l in old_f.read().splitlines(): | |
1338 | if 'webrepl' in l: | |
1339 | found = True | |
1340 | if l.startswith('#'): | |
1341 | l = l[1:] | |
1342 | new_f.write(l + '\\n') | |
1343 | if not found: | |
1344 | new_f.write('\\nimport webrepl\\nwebrepl.start()\\n') | |
1345 | ||
1346 | os.remove('/boot.py') | |
1347 | os.rename('/boot.py.tmp', '/boot.py') | |
1348 | except: | |
1349 | pass | |
1350 | ||
1351 | print(True) | |
1352 | ||
1353 | modify_boot() | |
1354 | del modify_boot | |
1355 | """ | |
1356 | ||
1357 | try: | |
1358 | # write config file | |
1359 | config = "PASS = {0}\n".format(repr(password)) | |
1360 | self.putData("/webrepl_cfg.py", config.encode("utf-8")) | |
1361 | except OSError as err: | |
1362 | return False, str(err) | |
1363 | ||
1364 | # modify boot.py | |
1365 | out, err = self.executeCommands(command, mode=self._submitMode) | |
1366 | if err: | |
1367 | return False, err | |
1368 | ||
1369 | return out.decode("utf-8").strip() == "True", "" | |
1370 | ||
1371 | def disableWebrepl(self): | |
1372 | """ | |
1373 | Public method to write the given WebREPL password to the connected device and | |
1374 | modify the start script to start the WebREPL server. | |
1375 | ||
1376 | @return tuple containing a flag indicating success and an error message | |
1377 | @rtype tuple of (bool, str) | |
1378 | """ | |
1379 | command = """ | |
1380 | def modify_boot(): | |
1381 | import os | |
1382 | ||
1383 | try: | |
1384 | with open('/boot.py', 'r') as old_f, open('/boot.py.tmp', 'w') as new_f: | |
1385 | for l in old_f.read().splitlines(): | |
1386 | if 'webrepl' in l: | |
1387 | if not l.startswith('#'): | |
1388 | l = '#' + l | |
1389 | new_f.write(l + '\\n') | |
1390 | ||
1391 | os.remove('/boot.py') | |
1392 | os.rename('/boot.py.tmp', '/boot.py') | |
1393 | except: | |
1394 | pass | |
1395 | ||
1396 | print(True) | |
1397 | ||
1398 | modify_boot() | |
1399 | del modify_boot | |
1400 | """ | |
1401 | ||
1402 | # modify boot.py | |
1403 | out, err = self.executeCommands(command, mode=self._submitMode) | |
1404 | if err: | |
1405 | return False, err | |
1406 | ||
1407 | return out.decode("utf-8").strip() == "True", "" | |
1408 | ||
10153 | 1409 | @pyqtSlot() |
1410 | def __setCountry(self): | |
1411 | """ | |
1412 | Private slot to configure the country of the connected ESP32 device. | |
1413 | ||
1414 | The country is the two-letter ISO 3166-1 Alpha-2 country code. | |
1415 | """ | |
1416 | from ..WifiDialogs.WifiCountryDialog import WifiCountryDialog | |
1417 | ||
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1418 | dlg = WifiCountryDialog(parent=self.microPython) |
10153 | 1419 | if dlg.exec() == QDialog.DialogCode.Accepted: |
1420 | country, remember = dlg.getCountry() | |
1421 | if remember: | |
1422 | Preferences.setMicroPython("WifiCountry", country) | |
1423 | ||
1424 | command = """ | |
1425 | try: | |
1426 | import network | |
1427 | network.country({0}) | |
1428 | except AttributeError: | |
1429 | pass | |
1430 | """.format( | |
1431 | repr(country) | |
1432 | ) | |
1433 | ||
1434 | out, err = self.executeCommands(command, mode=self._submitMode) | |
1435 | if err: | |
1436 | self.microPython.showError("country()", err) | |
1437 | ||
1438 | @pyqtSlot() | |
1439 | def __resetCountry(self): | |
1440 | """ | |
1441 | Private slot to reset the country of the connected ESP32 device. | |
1442 | ||
1443 | The country is the two-letter ISO 3166-1 Alpha-2 country code. This method | |
1444 | resets it to the default code 'XX' representing the "worldwide" region. | |
1445 | """ | |
1446 | command = """ | |
1447 | try: | |
1448 | import network | |
1449 | network.country('XX') | |
1450 | except AttributeError: | |
1451 | pass | |
1452 | """ | |
1453 | ||
1454 | out, err = self.executeCommands(command, mode=self._submitMode) | |
1455 | if err: | |
1456 | self.microPython.showError("country()", err) | |
1457 | ||
9855 | 1458 | ################################################################## |
1459 | ## Methods below implement Bluetooth related methods | |
1460 | ################################################################## | |
1461 | ||
1462 | def hasBluetooth(self): | |
1463 | """ | |
1464 | Public method to check the availability of Bluetooth. | |
1465 | ||
1466 | @return flag indicating the availability of Bluetooth | |
1467 | @rtype bool | |
9857 | 1468 | @exception OSError raised to indicate an issue with the device |
9855 | 1469 | """ |
9866 | 1470 | if self.hasCircuitPython(): |
1471 | self.__createCpyDevice() | |
1472 | return self.__cpyDevice.hasBluetooth() | |
1473 | ||
9855 | 1474 | command = """ |
1475 | def has_bt(): | |
1476 | try: | |
1477 | import bluetooth | |
1478 | if hasattr(bluetooth, 'BLE'): | |
1479 | return True | |
1480 | except ImportError: | |
1481 | pass | |
1482 | ||
1483 | return False | |
1484 | ||
1485 | print(has_bt()) | |
1486 | del has_bt | |
1487 | """ | |
9989 | 1488 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=10000) |
9855 | 1489 | if err: |
1490 | raise OSError(self._shortError(err)) | |
1491 | return out.strip() == b"True" | |
1492 | ||
1493 | def getBluetoothStatus(self): | |
1494 | """ | |
1495 | Public method to get Bluetooth status data of the connected board. | |
1496 | ||
1497 | @return list of tuples containing the translated status data label and | |
1498 | the associated value | |
1499 | @rtype list of tuples of (str, str) | |
9857 | 1500 | @exception OSError raised to indicate an issue with the device |
9855 | 1501 | """ |
9866 | 1502 | if self.hasCircuitPython(): |
1503 | return self.__cpyDevice.getBluetoothStatus() | |
1504 | ||
9855 | 1505 | command = """ |
1506 | def ble_status(): | |
1507 | import bluetooth | |
1508 | import ubinascii | |
1509 | import ujson | |
1510 | ||
1511 | ble = bluetooth.BLE() | |
1512 | ||
1513 | ble_active = ble.active() | |
1514 | if not ble_active: | |
1515 | ble.active(True) | |
1516 | ||
1517 | res = { | |
1518 | 'active': ble_active, | |
1519 | 'mac': ubinascii.hexlify(ble.config('mac')[1], ':').decode(), | |
1520 | 'addr_type': ble.config('mac')[0], | |
1521 | 'name': ble.config('gap_name'), | |
1522 | 'mtu': ble.config('mtu'), | |
1523 | } | |
1524 | ||
1525 | if not ble_active: | |
1526 | ble.active(False) | |
1527 | ||
1528 | print(ujson.dumps(res)) | |
1529 | ||
1530 | ble_status() | |
1531 | del ble_status | |
1532 | """ | |
9989 | 1533 | out, err = self.executeCommands(command, mode=self._submitMode) |
9855 | 1534 | if err: |
1535 | raise OSError(self._shortError(err)) | |
1536 | ||
1537 | status = [] | |
1538 | bleStatus = json.loads(out.decode("utf-8")) | |
1539 | status.append((self.tr("Active"), self.bool2str(bleStatus["active"]))) | |
1540 | status.append((self.tr("Name"), bleStatus["name"])) | |
1541 | status.append((self.tr("MAC-Address"), bleStatus["mac"])) | |
1542 | status.append( | |
1543 | ( | |
1544 | self.tr("Address Type"), | |
1545 | self.tr("Public") if bleStatus == 0 else self.tr("Random"), | |
1546 | ) | |
1547 | ) | |
9857 | 1548 | status.append((self.tr("MTU"), self.tr("{0} Bytes").format(bleStatus["mtu"]))) |
9855 | 1549 | |
1550 | return status | |
1551 | ||
1552 | def activateBluetoothInterface(self): | |
1553 | """ | |
1554 | Public method to activate the Bluetooth interface. | |
1555 | ||
1556 | @return flag indicating the new state of the Bluetooth interface | |
1557 | @rtype bool | |
9857 | 1558 | @exception OSError raised to indicate an issue with the device |
9855 | 1559 | """ |
9866 | 1560 | if self.hasCircuitPython(): |
1561 | return self.__cpyDevice.activateBluetoothInterface() | |
1562 | ||
9855 | 1563 | command = """ |
1564 | def activate_ble(): | |
1565 | import bluetooth | |
1566 | ||
1567 | ble = bluetooth.BLE() | |
1568 | if not ble.active(): | |
1569 | ble.active(True) | |
1570 | print(ble.active()) | |
1571 | ||
1572 | activate_ble() | |
1573 | del activate_ble | |
1574 | """ | |
9989 | 1575 | out, err = self.executeCommands(command, mode=self._submitMode) |
9855 | 1576 | if err: |
1577 | raise OSError(self._shortError(err)) | |
1578 | ||
1579 | return out.strip() == b"True" | |
1580 | ||
1581 | def deactivateBluetoothInterface(self): | |
1582 | """ | |
1583 | Public method to deactivate the Bluetooth interface. | |
1584 | ||
1585 | @return flag indicating the new state of the Bluetooth interface | |
1586 | @rtype bool | |
9857 | 1587 | @exception OSError raised to indicate an issue with the device |
9855 | 1588 | """ |
9866 | 1589 | if self.hasCircuitPython(): |
1590 | return self.__cpyDevice.deactivateBluetoothInterface() | |
1591 | ||
9855 | 1592 | command = """ |
1593 | def deactivate_ble(): | |
1594 | import bluetooth | |
1595 | ||
1596 | ble = bluetooth.BLE() | |
1597 | if ble.active(): | |
1598 | ble.active(False) | |
1599 | print(ble.active()) | |
1600 | ||
1601 | deactivate_ble() | |
1602 | del deactivate_ble | |
1603 | """ | |
9989 | 1604 | out, err = self.executeCommands(command, mode=self._submitMode) |
9855 | 1605 | if err: |
1606 | raise OSError(self._shortError(err)) | |
1607 | ||
1608 | return out.strip() == b"True" | |
1609 | ||
9859 | 1610 | def getDeviceScan(self, timeout=10): |
1611 | """ | |
1612 | Public method to perform a Bluetooth device scan. | |
1613 | ||
1614 | @param timeout duration of the device scan in seconds (defaults | |
1615 | to 10) | |
1616 | @type int (optional) | |
1617 | @return tuple containing a dictionary with the scan results and | |
1618 | an error string | |
1619 | @rtype tuple of (dict, str) | |
1620 | """ | |
1621 | from ..BluetoothDialogs.BluetoothAdvertisement import BluetoothAdvertisement | |
1622 | ||
9866 | 1623 | if self.hasCircuitPython(): |
1624 | return self.__cpyDevice.getDeviceScan(timeout) | |
1625 | ||
9859 | 1626 | command = """ |
10032
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1627 | _scan_done = False |
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1628 | |
9859 | 1629 | def ble_scan(): |
1630 | import bluetooth | |
1631 | import time | |
1632 | import ubinascii | |
1633 | ||
1634 | IRQ_SCAN_RESULT = 5 | |
1635 | IRQ_SCAN_DONE = 6 | |
1636 | ||
1637 | 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
|
1638 | global _scan_done |
9859 | 1639 | if event == IRQ_SCAN_RESULT: |
1640 | addr_type, addr, adv_type, rssi, adv_data = data | |
1641 | if addr: | |
1642 | print({{ | |
1643 | 'address': ubinascii.hexlify(addr,':').decode('utf-8'), | |
1644 | 'rssi': rssi, | |
1645 | 'adv_type': adv_type, | |
1646 | 'advertisement': bytes(adv_data), | |
1647 | }}) | |
10032
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1648 | 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
|
1649 | _scan_done = True |
9859 | 1650 | |
1651 | ble = bluetooth.BLE() | |
1652 | ||
1653 | ble_active = ble.active() | |
1654 | if not ble_active: | |
1655 | ble.active(True) | |
1656 | ||
1657 | ble.irq(_bleIrq) | |
1658 | 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
|
1659 | while not _scan_done: |
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1660 | time.sleep(0.2) |
9859 | 1661 | |
1662 | if not ble_active: | |
1663 | ble.active(False) | |
1664 | ||
1665 | ble_scan() | |
10032
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10022
diff
changeset
|
1666 | del ble_scan, _scan_done |
9859 | 1667 | """.format( |
1668 | timeout | |
1669 | ) | |
9989 | 1670 | out, err = self.executeCommands( |
9859 | 1671 | command, mode=self._submitMode, timeout=(timeout + 5) * 1000 |
1672 | ) | |
1673 | if err: | |
1674 | return {}, err | |
1675 | ||
1676 | scanResults = {} | |
1677 | for line in out.decode("utf-8").splitlines(): | |
1678 | res = ast.literal_eval(line) | |
1679 | address = res["address"] | |
1680 | if address not in scanResults: | |
1681 | scanResults[address] = BluetoothAdvertisement(address) | |
1682 | scanResults[address].update( | |
1683 | res["adv_type"], res["rssi"], res["advertisement"] | |
1684 | ) | |
1685 | ||
1686 | return scanResults, "" | |
1687 | ||
11167 | 1688 | def supportsDeviceScan(self): |
1689 | """ | |
1690 | Public method to indicate, that the Bluetooth implementation supports | |
1691 | scanning for devices. | |
1692 | ||
1693 | @return flag indicating that the scanning function is supported | |
1694 | @rtype bool | |
1695 | """ | |
1696 | return True | |
1697 | ||
9868 | 1698 | ################################################################## |
1699 | ## Methods below implement NTP related methods | |
1700 | ################################################################## | |
1701 | ||
1702 | def hasNetworkTime(self): | |
1703 | """ | |
1704 | Public method to check the availability of network time functions. | |
1705 | ||
1706 | @return flag indicating the availability of network time functions | |
1707 | @rtype bool | |
9870
0399d3607829
Fixed a few code formatting and style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9868
diff
changeset
|
1708 | @exception OSError raised to indicate an issue with the device |
9868 | 1709 | """ |
1710 | if self.hasCircuitPython(): | |
1711 | self.__createCpyDevice() | |
1712 | return self.__cpyDevice.hasNetworkTime() | |
1713 | ||
1714 | command = """ | |
1715 | def has_ntp(): | |
1716 | try: | |
1717 | import ntptime | |
1718 | return True | |
1719 | except ImportError: | |
1720 | return False | |
1721 | ||
1722 | print(has_ntp()) | |
1723 | del has_ntp | |
1724 | """ | |
9989 | 1725 | out, err = self.executeCommands(command, mode=self._submitMode) |
9868 | 1726 | if err: |
1727 | raise OSError(self._shortError(err)) | |
1728 | return out.strip() == b"True" | |
1729 | ||
1730 | def setNetworkTime(self, server="0.pool.ntp.org", tzOffset=0, timeout=10): | |
1731 | """ | |
1732 | Public method to set the time to the network time retrieved from an | |
1733 | NTP server. | |
1734 | ||
1735 | @param server name of the NTP server to get the network time from | |
1736 | (defaults to "0.pool.ntp.org") | |
1737 | @type str (optional) | |
1738 | @param tzOffset offset with respect to UTC (defaults to 0) | |
1739 | @type int (optional) | |
1740 | @param timeout maximum time to wait for a server response in seconds | |
1741 | (defaults to 10) | |
1742 | @type int | |
1743 | @return tuple containing a flag indicating success and an error string | |
1744 | @rtype tuple of (bool, str) | |
1745 | """ | |
1746 | if self.hasCircuitPython(): | |
1747 | return self.__cpyDevice.setNetworkTime( | |
1748 | server=server, tzOffset=tzOffset, timeout=timeout | |
1749 | ) | |
1750 | ||
1751 | command = """ | |
1752 | def set_ntp_time(server, tz_offset, timeout): | |
1753 | import network | |
1754 | import ntptime | |
1755 | import machine | |
1756 | ||
1757 | if not network.WLAN(network.STA_IF).isconnected(): | |
1758 | return False | |
1759 | ||
1760 | ntptime.host = server | |
1761 | ntptime.timeout = timeout | |
1762 | ntptime.settime() | |
1763 | ||
1764 | rtc = machine.RTC() | |
1765 | t = list(rtc.datetime()) | |
1766 | t[4] += tz_offset | |
1767 | rtc.datetime(t) | |
1768 | ||
1769 | return True | |
1770 | ||
1771 | try: | |
1772 | print({{ | |
1773 | 'result': set_ntp_time({0}, {1}, {2}), | |
1774 | 'error': '', | |
1775 | }}) | |
1776 | except Exception as err: | |
1777 | print({{ | |
1778 | 'result': False, | |
1779 | 'error': str(err), | |
1780 | }}) | |
1781 | del set_ntp_time | |
1782 | """.format( | |
1783 | repr(server), tzOffset, timeout | |
1784 | ) | |
9989 | 1785 | out, err = self.executeCommands( |
9868 | 1786 | command, mode=self._submitMode, timeout=(timeout + 2) * 1000 |
1787 | ) | |
1788 | if err: | |
1789 | return False, err | |
1790 | else: | |
1791 | res = ast.literal_eval(out.decode("utf-8")) | |
1792 | return res["result"], res["error"] | |
1793 | ||
9496
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1794 | |
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
|
1795 | 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
|
1796 | """ |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1797 | 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
|
1798 | |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1799 | @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
|
1800 | @type MicroPythonWidget |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1801 | @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
|
1802 | @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
|
1803 | @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
|
1804 | @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
|
1805 | @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
|
1806 | @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
|
1807 | @param _boardName name of the board (unused) |
9738 | 1808 | @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
|
1809 | @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
|
1810 | @type str |
9496
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1811 | @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
|
1812 | @rtype EspDevice |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1813 | """ |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
1814 | return EspDevice(microPythonWidget, deviceType) |
9820 | 1815 | |
1816 | ||
1817 | ################################################################################ | |
1818 | ## Functions below implement flashing related functionality needed elsewhere ## | |
1819 | ## as well. ## | |
1820 | ################################################################################ | |
1821 | ||
1822 | ||
1823 | @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
|
1824 | def eraseFlash(port, parent=None): |
9820 | 1825 | """ |
1826 | Slot to erase the device flash memory. | |
1827 | ||
1828 | @param port name of the serial port device to be used | |
1829 | @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
|
1830 | @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
|
1831 | @type QWidget |
9820 | 1832 | """ |
1833 | 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
|
1834 | parent, |
9820 | 1835 | QCoreApplication.translate("EspDevice", "Erase Flash"), |
1836 | QCoreApplication.translate( | |
1837 | "EspDevice", """Shall the flash of the selected device really be erased?""" | |
1838 | ), | |
1839 | ) | |
1840 | if ok: | |
1841 | flashArgs = [ | |
1842 | "-u", | |
1843 | "-m", | |
1844 | "esptool", | |
1845 | "--port", | |
1846 | port, | |
1847 | "erase_flash", | |
1848 | ] | |
1849 | dlg = EricProcessDialog( | |
1850 | QCoreApplication.translate("EspDevice", "'esptool erase_flash' Output"), | |
1851 | QCoreApplication.translate("EspDevice", "Erase Flash"), | |
1852 | 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
|
1853 | 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
|
1854 | 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
|
1855 | parent=parent, |
9820 | 1856 | ) |
1857 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs) | |
1858 | if res: | |
1859 | dlg.exec() | |
1860 | ||
1861 | ||
1862 | @pyqtSlot() | |
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1863 | def flashPythonFirmware(port, parent=None): |
9820 | 1864 | """ |
1865 | Slot to flash a MicroPython firmware to the device. | |
1866 | ||
1867 | @param port name of the serial port device to be used | |
1868 | @type str | |
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1869 | @param parent reference to the parent widget (defaults to None) |
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1870 | @type QWidget |
9820 | 1871 | """ |
1872 | from .EspDialogs.EspFirmwareSelectionDialog import EspFirmwareSelectionDialog | |
1873 | ||
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1874 | dlg = EspFirmwareSelectionDialog(parent=parent) |
9820 | 1875 | if dlg.exec() == QDialog.DialogCode.Accepted: |
1876 | chip, firmware, baudRate, flashMode, flashAddress = dlg.getData() | |
1877 | flashArgs = [ | |
1878 | "-u", | |
1879 | "-m", | |
1880 | "esptool", | |
1881 | "--chip", | |
1882 | chip, | |
1883 | "--port", | |
1884 | port, | |
1885 | ] | |
1886 | if baudRate != "115200": | |
1887 | flashArgs += ["--baud", baudRate] | |
1888 | flashArgs.append("write_flash") | |
1889 | if flashMode: | |
1890 | flashArgs += ["--flash_mode", flashMode] | |
9848
3d750b2e012c
Corrected flashing of ESP 8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9836
diff
changeset
|
1891 | if chip == "esp8266": |
3d750b2e012c
Corrected flashing of ESP 8266 devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9836
diff
changeset
|
1892 | # 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
|
1893 | flashArgs += ["--flash_size", "detect"] |
9820 | 1894 | flashArgs += [ |
1895 | flashAddress, | |
1896 | firmware, | |
1897 | ] | |
1898 | dlg = EricProcessDialog( | |
1899 | QCoreApplication.translate("EspDevice", "'esptool write_flash' Output"), | |
1900 | QCoreApplication.translate("EspDevice", "Flash µPy/CPy Firmware"), | |
1901 | 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
|
1902 | 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
|
1903 | 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
|
1904 | parent=parent, |
9820 | 1905 | ) |
1906 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs) | |
1907 | if res: | |
1908 | dlg.exec() | |
1909 | ||
1910 | ||
1911 | @pyqtSlot() | |
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1912 | def flashAddonFirmware(port, parent=None): |
9820 | 1913 | """ |
1914 | Slot to flash some additional firmware images. | |
1915 | ||
1916 | @param port name of the serial port device to be used | |
1917 | @type str | |
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1918 | @param parent reference to the parent widget (defaults to None) |
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1919 | @type QWidget |
9820 | 1920 | """ |
1921 | from .EspDialogs.EspFirmwareSelectionDialog import EspFirmwareSelectionDialog | |
1922 | ||
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10933
diff
changeset
|
1923 | dlg = EspFirmwareSelectionDialog(addon=True, parent=parent) |
9820 | 1924 | if dlg.exec() == QDialog.DialogCode.Accepted: |
1925 | chip, firmware, baudRate, flashMode, flashAddress = dlg.getData() | |
1926 | flashArgs = [ | |
1927 | "-u", | |
1928 | "-m", | |
1929 | "esptool", | |
1930 | "--chip", | |
1931 | chip, | |
1932 | "--port", | |
1933 | port, | |
1934 | ] | |
1935 | if baudRate != "115200": | |
1936 | flashArgs += ["--baud", baudRate] | |
1937 | flashArgs.append("write_flash") | |
1938 | if flashMode: | |
1939 | flashArgs += ["--flash_mode", flashMode] | |
1940 | flashArgs += [ | |
1941 | flashAddress.lower(), | |
1942 | firmware, | |
1943 | ] | |
1944 | dlg = EricProcessDialog( | |
1945 | QCoreApplication.translate("EspDevice", "'esptool write_flash' Output"), | |
1946 | QCoreApplication.translate("EspDevice", "Flash Additional Firmware"), | |
1947 | 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
|
1948 | 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
|
1949 | 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
|
1950 | parent=parent, |
9820 | 1951 | ) |
1952 | res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs) | |
1953 | if res: | |
1954 | dlg.exec() |