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