Tue, 26 Nov 2024 19:46:36 +0100
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
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 | |
10439
21c28b0f9e41
Updated copyright for 2024.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10330
diff
changeset
|
3 | # Copyright (c) 2019 - 2024 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 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
7 | Module implementing the device interface class for CircuitPython boards. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
8 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
9 | |
9820 | 10 | import ast |
9828 | 11 | import binascii |
9820 | 12 | import json |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
13 | import os |
7116
233b6e62ca2b
CircuitPython: added code to flash a new CircuitPython firmware.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
14 | import shutil |
233b6e62ca2b
CircuitPython: added code to flash a new CircuitPython firmware.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
15 | |
9820 | 16 | from PyQt6.QtCore import QUrl, pyqtSlot |
17 | from PyQt6.QtNetwork import QNetworkReply, QNetworkRequest | |
9944 | 18 | from PyQt6.QtWidgets import QInputDialog, QMenu |
7116
233b6e62ca2b
CircuitPython: added code to flash a new CircuitPython firmware.
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 |
9885 | 21 | from eric7.EricGui.EricOverrideCursor import EricOverrideCursor, EricOverridenCursor |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
22 | from eric7.EricWidgets import EricFileDialog, EricMessageBox |
9742
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
23 | from eric7.EricWidgets.EricApplication import ericApp |
9944 | 24 | from eric7.SystemUtilities import FileSystemUtilities, OSUtilities |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
25 | |
9885 | 26 | from ..EthernetDialogs import WiznetUtilities |
9765
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
27 | from ..MicroPythonWidget import HAS_QTCHART |
6378da868bb0
Reorganized the MicroPython code even more.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9763
diff
changeset
|
28 | from . import FirmwareGithubUrls |
9740 | 29 | from .CircuitPythonUpdater.CircuitPythonUpdaterInterface import ( |
30 | CircuitPythonUpdaterInterface, | |
31 | isCircupAvailable, | |
32 | ) | |
9756
9854647c8c5c
Reorganized the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9755
diff
changeset
|
33 | from .DeviceBase import 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 | |
9756
9854647c8c5c
Reorganized the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9755
diff
changeset
|
36 | class CircuitPythonDevice(BaseDevice): |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
37 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
38 | Class implementing the device for CircuitPython boards. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
39 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
40 | |
7132
c4682cfcd160
CircuitPythonDevice: only report local access if the well known volume is mounted.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7129
diff
changeset
|
41 | DeviceVolumeName = "CIRCUITPY" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
42 | |
9866 | 43 | def __init__( |
9885 | 44 | self, |
45 | microPythonWidget, | |
46 | deviceType, | |
47 | boardName, | |
48 | vid=0, | |
49 | pid=0, | |
50 | hasWorkspace=True, | |
51 | parent=None, | |
9866 | 52 | ): |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
53 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
54 | Constructor |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
55 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
56 | @param microPythonWidget reference to the main MicroPython widget |
7134
21d23ca51680
Renamed "MicroPythonReplWidget" to "MicroPythonWidget".
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7132
diff
changeset
|
57 | @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
|
58 | @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
|
59 | @type str |
9738 | 60 | @param boardName name of the board |
61 | @type str | |
9885 | 62 | @param vid vendor ID (defaults to 0) |
63 | @type int (optional) | |
64 | @param pid product ID (defaults to 0) | |
65 | @type int (optional) | |
9866 | 66 | @param hasWorkspace flag indicating that the devices supports access via |
67 | a mounted volume (defaults to True) | |
68 | @type bool (optional) | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
69 | @param parent reference to the parent object |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
70 | @type QObject |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
71 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
72 | super().__init__(microPythonWidget, deviceType, parent) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
73 | |
9828 | 74 | self._submitMode = "paste" # use 'paste' mode to avoid loosing state |
9799 | 75 | |
9738 | 76 | self.__boardName = boardName |
9885 | 77 | self.__vidpid = (vid, pid) |
9866 | 78 | |
9944 | 79 | self.__workspaceSelected = False |
9870
0399d3607829
Fixed a few code formatting and style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9868
diff
changeset
|
80 | self.__workspace = self.__findWorkspace() if hasWorkspace else None |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
81 | |
9740 | 82 | self.__updater = CircuitPythonUpdaterInterface(self) |
83 | ||
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
84 | self.__createCPyMenu() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
85 | |
9885 | 86 | self.__wiznetVidPid = ( |
87 | (0x2E8A, 0x1027), # WIZnet W5100S-EVB-Pico | |
88 | (0x2E8A, 0x1029), # WIZnet W5500-EVB-Pico | |
89 | ) | |
90 | ||
9828 | 91 | self.__securityTranslations = { |
92 | "OPEN": self.tr("open", "open WiFi network"), | |
93 | "WEP": "WEP", | |
94 | "WPA_PSK": "WPA", | |
95 | "WPA2_PSK": "WPA2", | |
96 | "WPA_WPA2_PSK": "WPA/WPA2", | |
97 | "WPA2_ENTERPRISE": "WPA2 (CCMP)", | |
98 | "WPA3_PSK": "WPA3", | |
99 | "WPA2_WPA3_PSK": "WPA2/WPA3", | |
100 | } | |
101 | self.__securityCode2AuthModeString = { | |
102 | 0: "[wifi.AuthMode.OPEN]", | |
103 | 1: "[wifi.AuthMode.WEP]", | |
104 | 2: "[wifi.AuthMode.WPA, wifi.AuthMode.PSK]", | |
105 | 3: "[wifi.AuthMode.WPA2, wifi.AuthMode.PSK]", | |
106 | 4: "[wifi.AuthMode.WPA, wifi.AuthMode.WPA2, wifi.AuthMode.PSK]", | |
107 | 5: "[wifi.AuthMode.WPA2, wifi.AuthMode.ENTERPRISE]", | |
108 | 6: "[wifi.AuthMode.WPA3, wifi.AuthMode.PSK]", | |
109 | 7: "[wifi.AuthMode.WPA2, wifi.AuthMode.WPA3, wifi.AuthMode.PSK]", | |
110 | } | |
9855 | 111 | self.__bleAddressType = { |
112 | 0: self.tr("Public"), | |
113 | 1: self.tr("Random Static"), | |
114 | 2: self.tr("Random Private Resolvable"), | |
115 | 3: self.tr("Random Private Non-Resolvable"), | |
116 | } | |
9828 | 117 | |
9783
5f84d5eeee9e
Corrected an issue in the CircuitPython device causing a torn off menu to remain visible after a device disconnect.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9767
diff
changeset
|
118 | def setConnected(self, connected): |
5f84d5eeee9e
Corrected an issue in the CircuitPython device causing a torn off menu to remain visible after a device disconnect.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9767
diff
changeset
|
119 | """ |
5f84d5eeee9e
Corrected an issue in the CircuitPython device causing a torn off menu to remain visible after a device disconnect.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9767
diff
changeset
|
120 | Public method to set the connection state. |
5f84d5eeee9e
Corrected an issue in the CircuitPython device causing a torn off menu to remain visible after a device disconnect.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9767
diff
changeset
|
121 | |
5f84d5eeee9e
Corrected an issue in the CircuitPython device causing a torn off menu to remain visible after a device disconnect.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9767
diff
changeset
|
122 | @param connected connection state |
5f84d5eeee9e
Corrected an issue in the CircuitPython device causing a torn off menu to remain visible after a device disconnect.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9767
diff
changeset
|
123 | @type bool |
5f84d5eeee9e
Corrected an issue in the CircuitPython device causing a torn off menu to remain visible after a device disconnect.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9767
diff
changeset
|
124 | """ |
5f84d5eeee9e
Corrected an issue in the CircuitPython device causing a torn off menu to remain visible after a device disconnect.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9767
diff
changeset
|
125 | if not connected and self.__libraryMenu.isTearOffMenuVisible(): |
5f84d5eeee9e
Corrected an issue in the CircuitPython device causing a torn off menu to remain visible after a device disconnect.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9767
diff
changeset
|
126 | self.__libraryMenu.hideTearOffMenu() |
5f84d5eeee9e
Corrected an issue in the CircuitPython device causing a torn off menu to remain visible after a device disconnect.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9767
diff
changeset
|
127 | |
9820 | 128 | if self.__flashMenu.isTearOffMenuVisible(): |
129 | self.__flashMenu.hideTearOffMenu() | |
130 | ||
9783
5f84d5eeee9e
Corrected an issue in the CircuitPython device causing a torn off menu to remain visible after a device disconnect.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9767
diff
changeset
|
131 | super().setConnected(connected) |
9979 | 132 | self._deviceData["local_mip"] = False |
9783
5f84d5eeee9e
Corrected an issue in the CircuitPython device causing a torn off menu to remain visible after a device disconnect.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9767
diff
changeset
|
133 | |
9885 | 134 | if ( |
135 | connected | |
136 | and not self._deviceData["ethernet"] | |
137 | and self.__vidpid in self.__wiznetVidPid | |
138 | ): | |
139 | with EricOverridenCursor(): | |
140 | EricMessageBox.warning( | |
141 | None, | |
142 | self.tr("WIZnet 5x00 Ethernet"), | |
143 | self.tr( | |
144 | "<p>Support for <b>WIZnet 5x00</b> Ethernet boards could not be" | |
145 | " detected. Is the module <b>adafruit_wiznet5k</b> installed?" | |
146 | "</p>" | |
147 | ), | |
148 | ) | |
149 | ||
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
150 | def setButtons(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
151 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
152 | Public method to enable the supported action buttons. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
153 | """ |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8117
diff
changeset
|
154 | 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
|
155 | |
7100
c4d9c28ebcd8
Devices.py files: removed some methods no longer needed after the recent improvements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7092
diff
changeset
|
156 | self.microPython.setActionButtons( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
157 | 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
|
158 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
159 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
160 | def forceInterrupt(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
161 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
162 | 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
|
163 | serial connection. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
164 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
165 | @return flag indicating an interrupt is needed |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
166 | @rtype bool |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
167 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
168 | return False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
169 | |
7125
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7122
diff
changeset
|
170 | def deviceName(self): |
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7122
diff
changeset
|
171 | """ |
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7122
diff
changeset
|
172 | 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
|
173 | |
7125
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7122
diff
changeset
|
174 | @return name of the device |
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7122
diff
changeset
|
175 | @rtype str |
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7122
diff
changeset
|
176 | """ |
2028553ee58c
CircuitPythonDevices, EspDevices, MicroPythonDevices: added a method to get the device name.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7122
diff
changeset
|
177 | return self.tr("CircuitPython") |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
178 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
179 | def canStartRepl(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
180 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
181 | 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
|
182 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
183 | @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
|
184 | and a reason why it cannot. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
185 | @rtype tuple of (bool, str) |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
186 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
187 | return True, "" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
188 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
189 | def canStartPlotter(self): |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
190 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
191 | 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
|
192 | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
193 | @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
|
194 | Plotter and a reason why it cannot. |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
195 | @rtype tuple of (bool, str) |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
196 | """ |
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
197 | return True, "" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
198 | |
7092
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
199 | def canRunScript(self): |
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
200 | """ |
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
201 | 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
|
202 | |
7092
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
203 | @return tuple containing a flag indicating it is safe to start a |
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
204 | Plotter and a reason why it cannot. |
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
205 | @rtype tuple of (bool, str) |
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
206 | """ |
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
207 | return True, "" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
208 | |
7092
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
209 | def runScript(self, script): |
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
210 | """ |
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
211 | 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
|
212 | |
7092
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
213 | @param script script to be executed |
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
214 | @type str |
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
215 | """ |
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
216 | pythonScript = script.split("\n") |
7414b3b012b1
CircuitPythonDevices: started the adaption for 'CircuitPython' based devices.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7084
diff
changeset
|
217 | self.sendCommands(pythonScript) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
218 | |
7100
c4d9c28ebcd8
Devices.py files: removed some methods no longer needed after the recent improvements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7092
diff
changeset
|
219 | def canStartFileManager(self): |
c4d9c28ebcd8
Devices.py files: removed some methods no longer needed after the recent improvements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7092
diff
changeset
|
220 | """ |
c4d9c28ebcd8
Devices.py files: removed some methods no longer needed after the recent improvements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7092
diff
changeset
|
221 | 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
|
222 | |
7100
c4d9c28ebcd8
Devices.py files: removed some methods no longer needed after the recent improvements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7092
diff
changeset
|
223 | @return tuple containing a flag indicating it is safe to start a |
c4d9c28ebcd8
Devices.py files: removed some methods no longer needed after the recent improvements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7092
diff
changeset
|
224 | File Manager and a reason why it cannot. |
c4d9c28ebcd8
Devices.py files: removed some methods no longer needed after the recent improvements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7092
diff
changeset
|
225 | @rtype tuple of (bool, str) |
c4d9c28ebcd8
Devices.py files: removed some methods no longer needed after the recent improvements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7092
diff
changeset
|
226 | """ |
c4d9c28ebcd8
Devices.py files: removed some methods no longer needed after the recent improvements.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7092
diff
changeset
|
227 | return True, "" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
228 | |
7129
3cc19aec959a
MicroPythonDevicces: added a method to indicate that a device allows access to its file system via a local directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7125
diff
changeset
|
229 | def supportsLocalFileAccess(self): |
3cc19aec959a
MicroPythonDevicces: added a method to indicate that a device allows access to its file system via a local directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7125
diff
changeset
|
230 | """ |
3cc19aec959a
MicroPythonDevicces: added a method to indicate that a device allows access to its file system via a local directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7125
diff
changeset
|
231 | Public method to indicate file access via a local directory. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
232 | |
7129
3cc19aec959a
MicroPythonDevicces: added a method to indicate that a device allows access to its file system via a local directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7125
diff
changeset
|
233 | @return flag indicating file access via local directory |
7145
ceb3e8b242c1
Regenerated source docu after merge with 'micropython' branch.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7138
diff
changeset
|
234 | @rtype bool |
7129
3cc19aec959a
MicroPythonDevicces: added a method to indicate that a device allows access to its file system via a local directory.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7125
diff
changeset
|
235 | """ |
7132
c4682cfcd160
CircuitPythonDevice: only report local access if the well known volume is mounted.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7129
diff
changeset
|
236 | return self.__deviceVolumeMounted() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
237 | |
7132
c4682cfcd160
CircuitPythonDevice: only report local access if the well known volume is mounted.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7129
diff
changeset
|
238 | def __deviceVolumeMounted(self): |
c4682cfcd160
CircuitPythonDevice: only report local access if the well known volume is mounted.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7129
diff
changeset
|
239 | """ |
c4682cfcd160
CircuitPythonDevice: only report local access if the well known volume is mounted.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7129
diff
changeset
|
240 | Private method to check, if the device volume is mounted. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
241 | |
7132
c4682cfcd160
CircuitPythonDevice: only report local access if the well known volume is mounted.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7129
diff
changeset
|
242 | @return flag indicated a mounted device |
c4682cfcd160
CircuitPythonDevice: only report local access if the well known volume is mounted.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7129
diff
changeset
|
243 | @rtype bool |
c4682cfcd160
CircuitPythonDevice: only report local access if the well known volume is mounted.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7129
diff
changeset
|
244 | """ |
8072
58491f4c99d6
MicroPython: added code to give a hint for CircuitPython devices, that do not support the UF2 bootloader for flashing.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8067
diff
changeset
|
245 | if self.__workspace and not os.path.exists(self.__workspace): |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
246 | self.__workspace = "" # reset |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
247 | |
9944 | 248 | if ( |
249 | self.__workspace | |
250 | and self.DeviceVolumeName not in self.__workspace | |
251 | and not self.__workspaceSelected | |
252 | ): | |
9786
f94b530722af
Corrected some code style and formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9783
diff
changeset
|
253 | self.__workspace = "" # reset |
9767
2eed840795c0
Fixed a few issues in the MicroPython file manager widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9765
diff
changeset
|
254 | |
9972
68ac01294544
Corrected some code style and formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9944
diff
changeset
|
255 | return self.__workspaceSelected or self.DeviceVolumeName in self.getWorkspace( |
68ac01294544
Corrected some code style and formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9944
diff
changeset
|
256 | silent=True |
9944 | 257 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
258 | |
9738 | 259 | def __findDeviceDirectories(self, directories): |
260 | """ | |
261 | Private method to find the device directories associated with the | |
262 | current board name. | |
263 | ||
264 | @param directories list of directories to be checked | |
265 | @type list of str | |
266 | @return list of associated directories | |
267 | @rtype list of str | |
268 | """ | |
269 | boardDirectories = [] | |
270 | for directory in directories: | |
271 | bootFile = os.path.join(directory, "boot_out.txt") | |
272 | if os.path.exists(bootFile): | |
273 | with open(bootFile, "r") as f: | |
274 | line = f.readline() | |
275 | if self.__boardName in line: | |
276 | boardDirectories.append(directory) | |
277 | ||
278 | return boardDirectories | |
279 | ||
8062
8dc5acb30a8b
MicroPython: implemented code to deal with multiple devices attached to the computer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8053
diff
changeset
|
280 | def __findWorkspace(self, silent=False): |
8dc5acb30a8b
MicroPython: implemented code to deal with multiple devices attached to the computer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8053
diff
changeset
|
281 | """ |
8067
a467ab075be0
MicroPython: added buttons to go to the 'home' directory (local and on device) to the MicroPython file manager and improved the workspace handling.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8062
diff
changeset
|
282 | Private method to find the workspace directory. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
283 | |
9828 | 284 | @param silent flag indicating silent operations (defaults to False) |
285 | @type bool (optional) | |
8062
8dc5acb30a8b
MicroPython: implemented code to deal with multiple devices attached to the computer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8053
diff
changeset
|
286 | @return workspace directory used for saving files |
8dc5acb30a8b
MicroPython: implemented code to deal with multiple devices attached to the computer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8053
diff
changeset
|
287 | @rtype str |
8dc5acb30a8b
MicroPython: implemented code to deal with multiple devices attached to the computer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8053
diff
changeset
|
288 | """ |
8dc5acb30a8b
MicroPython: implemented code to deal with multiple devices attached to the computer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8053
diff
changeset
|
289 | # Attempts to find the paths on the filesystem that represents the |
8dc5acb30a8b
MicroPython: implemented code to deal with multiple devices attached to the computer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8053
diff
changeset
|
290 | # plugged in CIRCUITPY boards. |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9496
diff
changeset
|
291 | deviceDirectories = FileSystemUtilities.findVolume( |
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9496
diff
changeset
|
292 | self.DeviceVolumeName, findAll=True |
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9496
diff
changeset
|
293 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
294 | |
8062
8dc5acb30a8b
MicroPython: implemented code to deal with multiple devices attached to the computer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8053
diff
changeset
|
295 | if deviceDirectories: |
8dc5acb30a8b
MicroPython: implemented code to deal with multiple devices attached to the computer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8053
diff
changeset
|
296 | if len(deviceDirectories) == 1: |
8dc5acb30a8b
MicroPython: implemented code to deal with multiple devices attached to the computer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8053
diff
changeset
|
297 | return deviceDirectories[0] |
8dc5acb30a8b
MicroPython: implemented code to deal with multiple devices attached to the computer.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8053
diff
changeset
|
298 | else: |
9738 | 299 | boardDirectories = self.__findDeviceDirectories(deviceDirectories) |
300 | if len(boardDirectories) == 1: | |
301 | return boardDirectories[0] | |
302 | elif len(boardDirectories) > 1: | |
303 | return self.selectDeviceDirectory(boardDirectories) | |
304 | else: | |
305 | return self.selectDeviceDirectory(deviceDirectories) | |
7059
a8fad276cbd5
Continued implementing the MicroPython support.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
306 | else: |
7174
de8175253dfc
MicroPython: did some fine tuning of the MicroPython interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7161
diff
changeset
|
307 | # return the default workspace and give the user a warning (unless |
de8175253dfc
MicroPython: did some fine tuning of the MicroPython interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7161
diff
changeset
|
308 | # silent mode is selected) |
de8175253dfc
MicroPython: did some fine tuning of the MicroPython interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7161
diff
changeset
|
309 | if not silent: |
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
|
310 | EricMessageBox.warning( |
7174
de8175253dfc
MicroPython: did some fine tuning of the MicroPython interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7161
diff
changeset
|
311 | self.microPython, |
de8175253dfc
MicroPython: did some fine tuning of the MicroPython interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7161
diff
changeset
|
312 | self.tr("Workspace Directory"), |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
313 | self.tr( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
314 | "Python files for CircuitPython can be edited in" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
315 | " place, if the device volume is locally" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
316 | " available. Such a volume was not found. In" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
317 | " place editing will not be available." |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
318 | ), |
8067
a467ab075be0
MicroPython: added buttons to go to the 'home' directory (local and on device) to the MicroPython file manager and improved the workspace handling.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8062
diff
changeset
|
319 | ) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
320 | |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8117
diff
changeset
|
321 | return super().getWorkspace() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
322 | |
9740 | 323 | def getWorkspace(self, silent=False): |
324 | """ | |
325 | Public method to get the workspace directory. | |
326 | ||
327 | @param silent flag indicating silent operations | |
328 | @type bool | |
329 | @return workspace directory used for saving files | |
330 | @rtype str | |
331 | """ | |
332 | if self.__workspace: | |
333 | # return cached entry | |
334 | return self.__workspace | |
335 | else: | |
336 | self.__workspace = self.__findWorkspace(silent=silent) | |
337 | return self.__workspace | |
338 | ||
9944 | 339 | def setWorkspace(self, workspacePath): |
340 | """ | |
341 | Public method to set the device workspace directory. | |
342 | ||
343 | @param workspacePath directory to be used for saving files | |
344 | @type str | |
345 | """ | |
346 | self.__workspace = workspacePath | |
347 | self.__workspaceSelected = True | |
348 | ||
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
349 | def __createCPyMenu(self): |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
350 | """ |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
351 | Private method to create the CircuitPython submenu. |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
352 | """ |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
353 | self.__libraryMenu = QMenu(self.tr("Library Management")) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
354 | self.__libraryMenu.aboutToShow.connect(self.__aboutToShowLibraryMenu) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
355 | self.__libraryMenu.setTearOffEnabled(True) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
356 | |
9820 | 357 | self.__flashMenu = self.__createFlashMenus() |
358 | ||
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
359 | self.__cpyMenu = QMenu(self.tr("CircuitPython Functions")) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
360 | self.__cpyMenu.addAction( |
9866 | 361 | self.tr("Show CircuitPython Versions"), self.showCircuitPythonVersions |
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
362 | ) |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
363 | self.__cpyMenu.addSeparator() |
9944 | 364 | self.__cpyMenu.addAction( |
365 | self.tr("Select Device Volume"), self.__selectDeviceVolume | |
366 | ) | |
367 | self.__cpyMenu.addSeparator() | |
9898 | 368 | self.__bootloaderAct = self.__cpyMenu.addAction( |
369 | self.tr("Activate Bootloader"), self.__activateBootloader | |
370 | ) | |
9902 | 371 | self.__uf2Act = self.__cpyMenu.addAction( |
372 | self.tr("Activate UF2 Mode"), self.__activateUF2Boot | |
373 | ) | |
9820 | 374 | self.__flashCpyAct = self.__cpyMenu.addMenu(self.__flashMenu) |
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
375 | self.__cpyMenu.addSeparator() |
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
376 | self.__cpyMenu.addMenu(self.__libraryMenu) |
9805 | 377 | self.__cpyMenu.addSeparator() |
378 | self.__resetAct = self.__cpyMenu.addAction( | |
379 | self.tr("Reset Device"), self.__resetDevice | |
380 | ) | |
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
381 | |
9820 | 382 | def __createFlashMenus(self): |
383 | """ | |
384 | Private method to create the various menus to flash a CircuitPython firmware. | |
385 | ||
386 | @return reference to the created top level flash menu | |
387 | @rtype QMenu | |
388 | """ | |
389 | menu = QMenu(self.tr("Flash CircuitPython Firmware")) | |
390 | menu.setTearOffEnabled(True) | |
391 | ||
392 | # UF2 devices | |
393 | menu.addAction(self.tr("UF2 Device"), self.__flashCircuitPython) | |
394 | menu.addSeparator() | |
395 | ||
396 | # ESP32 specific submenu | |
397 | self.__esp32FlashMenu = QMenu(self.tr("ESP32 Device")) | |
398 | self.__esp32FlashMenu.addAction(self.tr("Erase Flash"), self.__esp32EraseFlash) | |
399 | self.__esp32FlashMenu.addAction( | |
400 | self.tr("Flash MicroPython Firmware"), self.__esp32FlashPython | |
401 | ) | |
402 | self.__esp32FlashMenu.addSeparator() | |
403 | self.__esp32FlashMenu.addAction( | |
404 | self.tr("Flash Additional Firmware"), self.__esp32FlashAddons | |
405 | ) | |
406 | menu.addMenu(self.__esp32FlashMenu) | |
407 | ||
408 | # Teensy 4.0 and 4.1 specific submenu | |
409 | self.__teensyFlashMenu = QMenu(self.tr("Teensy Device")) | |
410 | self.__teensyFlashMenu.addAction( | |
411 | self.tr("CircuitPython Flash Instructions"), | |
412 | self.__showTeensyFlashInstructions, | |
413 | ) | |
414 | act = self.__teensyFlashMenu.addAction( | |
415 | self.tr("Start 'Teensy Loader'"), self.__startTeensyLoader | |
416 | ) | |
417 | act.setToolTip( | |
418 | self.tr("Start the 'Teensy Loader' application to flash the Teensy device.") | |
419 | ) | |
420 | menu.addMenu(self.__teensyFlashMenu) | |
421 | ||
422 | return menu | |
423 | ||
7116
233b6e62ca2b
CircuitPython: added code to flash a new CircuitPython firmware.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
424 | def addDeviceMenuEntries(self, menu): |
233b6e62ca2b
CircuitPython: added code to flash a new CircuitPython firmware.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
425 | """ |
233b6e62ca2b
CircuitPython: added code to flash a new CircuitPython firmware.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
426 | 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
|
427 | |
7116
233b6e62ca2b
CircuitPython: added code to flash a new CircuitPython firmware.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
428 | @param menu reference to the context menu |
233b6e62ca2b
CircuitPython: added code to flash a new CircuitPython firmware.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
429 | @type QMenu |
233b6e62ca2b
CircuitPython: added code to flash a new CircuitPython firmware.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
430 | """ |
9805 | 431 | connected = self.microPython.isConnected() |
9749 | 432 | linkConnected = self.microPython.isLinkConnected() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
433 | |
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
434 | self.__flashCpyAct.setEnabled(not linkConnected) |
9805 | 435 | self.__resetAct.setEnabled(connected) |
9898 | 436 | self.__bootloaderAct.setEnabled(connected) |
9902 | 437 | self.__uf2Act.setEnabled(connected) |
9738 | 438 | |
9752
2b9546c0cbd9
Moved the MicroPython board specific functions to a separate submenu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9751
diff
changeset
|
439 | menu.addMenu(self.__cpyMenu) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
440 | |
9740 | 441 | @pyqtSlot() |
442 | def __aboutToShowLibraryMenu(self): | |
443 | """ | |
444 | Private slot to populate the 'Library Management' menu. | |
445 | """ | |
446 | self.__libraryMenu.clear() | |
447 | ||
448 | if isCircupAvailable(): | |
449 | self.__updater.populateMenu(self.__libraryMenu) | |
450 | else: | |
451 | act = self.__libraryMenu.addAction( | |
452 | self.tr("Install Library Files"), self.__installLibraryFiles | |
453 | ) | |
454 | act.setEnabled(self.__deviceVolumeMounted()) | |
455 | act = self.__libraryMenu.addAction( | |
456 | self.tr("Install Library Package"), | |
457 | lambda: self.__installLibraryFiles(packageMode=True), | |
458 | ) | |
459 | act.setEnabled(self.__deviceVolumeMounted()) | |
460 | self.__libraryMenu.addSeparator() | |
461 | self.__libraryMenu.addAction( | |
462 | self.tr("Install 'circup' Package"), | |
463 | self.__updater.installCircup, | |
464 | ) | |
465 | ||
9805 | 466 | @pyqtSlot() |
467 | def __resetDevice(self): | |
468 | """ | |
469 | Private slot to reset the connected device. | |
470 | """ | |
471 | if self.microPython.isConnected(): | |
9989 | 472 | self.executeCommands( |
9929 | 473 | "import microcontroller as mc\n" |
474 | "mc.on_next_reset(mc.RunMode.NORMAL)" | |
475 | "mc.reset()\n", | |
9834 | 476 | mode=self._submitMode, |
9805 | 477 | ) |
478 | ||
8096 | 479 | def hasFlashMenuEntry(self): |
480 | """ | |
481 | 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
|
482 | |
8096 | 483 | @return flag indicating a specific flash menu entry |
484 | @rtype bool | |
485 | """ | |
486 | return True | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
487 | |
7116
233b6e62ca2b
CircuitPython: added code to flash a new CircuitPython firmware.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
488 | @pyqtSlot() |
233b6e62ca2b
CircuitPython: added code to flash a new CircuitPython firmware.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
489 | def __flashCircuitPython(self): |
233b6e62ca2b
CircuitPython: added code to flash a new CircuitPython firmware.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
490 | """ |
9755 | 491 | Private slot to flash a CircuitPython firmware to a device supporting UF2. |
7116
233b6e62ca2b
CircuitPython: added code to flash a new CircuitPython firmware.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7100
diff
changeset
|
492 | """ |
9756
9854647c8c5c
Reorganized the MicroPython package.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9755
diff
changeset
|
493 | from ..UF2FlashDialog import UF2FlashDialog |
9482
a2bc06a54d9d
Corrected/acknowledged some bad import style and removed some obsolete code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
494 | |
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
495 | dlg = UF2FlashDialog(boardType="circuitpython", parent=self.microPython) |
9755 | 496 | dlg.exec() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
497 | |
9820 | 498 | @pyqtSlot() |
9898 | 499 | def __activateBootloader(self): |
500 | """ | |
501 | Private slot to switch the board into 'bootloader' mode. | |
502 | """ | |
503 | if self.microPython.isConnected(): | |
9989 | 504 | self.executeCommands( |
9898 | 505 | "import microcontroller as mc\n" |
506 | "mc.on_next_reset(mc.RunMode.BOOTLOADER)\n" | |
507 | "mc.reset()\n", | |
508 | mode=self._submitMode, | |
509 | ) | |
510 | ||
511 | @pyqtSlot() | |
9902 | 512 | def __activateUF2Boot(self): |
513 | """ | |
514 | Private slot to switch the board into 'UF2 Boot' mode. | |
515 | """ | |
516 | if self.microPython.isConnected(): | |
9989 | 517 | self.executeCommands( |
9902 | 518 | "import microcontroller as mc\n" |
519 | "mc.on_next_reset(mc.RunMode.UF2)\n" | |
520 | "mc.reset()\n", | |
521 | mode=self._submitMode, | |
522 | ) | |
523 | ||
524 | @pyqtSlot() | |
9755 | 525 | def __showTeensyFlashInstructions(self): |
8072
58491f4c99d6
MicroPython: added code to give a hint for CircuitPython devices, that do not support the UF2 bootloader for flashing.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8067
diff
changeset
|
526 | """ |
9820 | 527 | Private slot to show a message box because Teensy does not support |
8072
58491f4c99d6
MicroPython: added code to give a hint for CircuitPython devices, that do not support the UF2 bootloader for flashing.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8067
diff
changeset
|
528 | the UF2 bootloader yet. |
58491f4c99d6
MicroPython: added code to give a hint for CircuitPython devices, that do not support the UF2 bootloader for flashing.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8067
diff
changeset
|
529 | """ |
9820 | 530 | from .TeensyDevices import showTeensyFlashInstructions |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
531 | |
9820 | 532 | showTeensyFlashInstructions() |
533 | ||
534 | @pyqtSlot() | |
9755 | 535 | def __startTeensyLoader(self): |
536 | """ | |
9820 | 537 | Private slot to start the 'Teensy Loader' application. |
9755 | 538 | |
539 | Note: The application must be accessible via the application search path. | |
540 | """ | |
9820 | 541 | from .TeensyDevices import startTeensyLoader |
542 | ||
543 | startTeensyLoader() | |
544 | ||
545 | @pyqtSlot() | |
546 | def __esp32EraseFlash(self): | |
547 | """ | |
548 | Private slot to erase the flash of an ESP32 device. | |
549 | """ | |
550 | from .EspDevices import eraseFlash | |
551 | ||
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
|
552 | eraseFlash(self.microPython.getCurrentPort(), parent=self.microPython) |
9820 | 553 | |
554 | @pyqtSlot() | |
555 | def __esp32FlashPython(self): | |
556 | """ | |
557 | Private slot to flash a MicroPython or CircuitPython firmware to an ESP32 | |
558 | device. | |
559 | """ | |
560 | from .EspDevices import flashPythonFirmware | |
561 | ||
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
562 | flashPythonFirmware(self.microPython.getCurrentPort(), parent=self.microPython) |
9820 | 563 | |
564 | @pyqtSlot() | |
565 | def __esp32FlashAddons(self): | |
566 | """ | |
567 | Private slot to flash additional firmware to an ESP32 device. | |
568 | """ | |
569 | from .EspDevices import flashAddonFirmware | |
570 | ||
11005
b918c6c2736b
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10806
diff
changeset
|
571 | flashAddonFirmware(self.microPython.getCurrentPort(), parent=self.microPython) |
9755 | 572 | |
7138
a2a53535d855
CircuitPythonDevices: added capability to install library files onto the device to the device menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
573 | @pyqtSlot() |
9866 | 574 | def showCircuitPythonVersions(self): |
9742
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
575 | """ |
9866 | 576 | Public slot to show the CircuitPython version of a connected device and |
9742
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
577 | the latest available one (from Github). |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
578 | """ |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
579 | ui = ericApp().getObject("UserInterface") |
9747 | 580 | request = QNetworkRequest(QUrl(FirmwareGithubUrls["circuitpython"])) |
9742
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
581 | reply = ui.networkAccessManager().head(request) |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
582 | reply.finished.connect(lambda: self.__cpyVersionResponse(reply)) |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
583 | |
9820 | 584 | @pyqtSlot(QNetworkReply) |
9742
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
585 | def __cpyVersionResponse(self, reply): |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
586 | """ |
9820 | 587 | Private slot handling the response of the latest version request. |
9742
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
588 | |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
589 | @param reply reference to the reply object |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
590 | @type QNetworkReply |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
591 | """ |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
592 | latestUrl = reply.url().toString() |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
593 | tag = latestUrl.rsplit("/", 1)[-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
|
594 | latestVersion = EricUtilities.versionToTuple(tag) |
9742
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
595 | |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
596 | cpyVersionStr = self.tr("unknown") |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
597 | cpyVersion = (0, 0, 0) |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
598 | if self.supportsLocalFileAccess(): |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
599 | bootFile = os.path.join(self.getWorkspace(), "boot_out.txt") |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
600 | if os.path.exists(bootFile): |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
601 | with open(bootFile, "r") as f: |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
602 | line = f.readline() |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
603 | cpyVersionStr = line.split(";")[0].split()[2] |
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
|
604 | cpyVersion = EricUtilities.versionToTuple(cpyVersionStr) |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
605 | if ( |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
606 | cpyVersion == (0, 0, 0) |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
607 | and self._deviceData |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
608 | and self._deviceData["mpy_version"] != "unknown" |
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
609 | ): |
9745
2c706ccc2b42
Added code to the CircuitPython interface to show the installed firmware version even if the drive is not mounted but the device is connected via the serial console.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9742
diff
changeset
|
610 | # drive is not mounted or 'boot_out.txt' is missing but the device |
2c706ccc2b42
Added code to the CircuitPython interface to show the installed firmware version even if the drive is not mounted but the device is connected via the serial console.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9742
diff
changeset
|
611 | # is connected via the serial console |
9751
606ac0e26533
Various enhancements and improvements to the MicroPython/CircuitPython related modules.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9749
diff
changeset
|
612 | cpyVersionStr = 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
|
613 | cpyVersion = EricUtilities.versionToTuple(cpyVersionStr) |
9742
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
614 | |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
615 | msg = self.tr( |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
616 | "<h4>CircuitPython Version Information</h4>" |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
617 | "<table>" |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
618 | "<tr><td>Installed:</td><td>{0}</td></tr>" |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
619 | "<tr><td>Available:</td><td>{1}</td></tr>" |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
620 | "</table>" |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
621 | ).format(cpyVersionStr, tag) |
9746
37d460e32843
Corrected the CircuitPython interface code to show the update availability notice correctly.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9745
diff
changeset
|
622 | if cpyVersion < latestVersion and cpyVersion != (0, 0, 0): |
9742
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
623 | msg += self.tr("<p><b>Update available!</b></p>") |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
624 | |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
625 | EricMessageBox.information( |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
626 | None, |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
627 | self.tr("CircuitPython Version"), |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
628 | msg, |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
629 | ) |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
630 | |
48dbfea4ac06
Added a CircuitPython menu entry to show the installed and available firmware version.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9740
diff
changeset
|
631 | @pyqtSlot() |
9738 | 632 | def __installLibraryFiles(self, packageMode=False): |
7138
a2a53535d855
CircuitPythonDevices: added capability to install library files onto the device to the device menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
633 | """ |
a2a53535d855
CircuitPythonDevices: added capability to install library files onto the device to the device menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
634 | Private slot to install Python files into the onboard library. |
9738 | 635 | |
636 | @param packageMode flag indicating to install a library package | |
637 | (defaults to False) | |
638 | @type bool (optional) | |
7138
a2a53535d855
CircuitPythonDevices: added capability to install library files onto the device to the device menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
639 | """ |
9738 | 640 | title = ( |
641 | self.tr("Install Library Package") | |
642 | if packageMode | |
643 | else self.tr("Install Library Files") | |
644 | ) | |
7138
a2a53535d855
CircuitPythonDevices: added capability to install library files onto the device to the device menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
645 | if not self.__deviceVolumeMounted(): |
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
|
646 | EricMessageBox.critical( |
7138
a2a53535d855
CircuitPythonDevices: added capability to install library files onto the device to the device menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
647 | self.microPython, |
9738 | 648 | title, |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
649 | self.tr( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
650 | """The device volume "<b>{0}</b>" is not available.""" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
651 | """ Ensure it is mounted properly and try again.""" |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
652 | ), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
653 | ) |
7138
a2a53535d855
CircuitPythonDevices: added capability to install library files onto the device to the device menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
654 | return |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
655 | |
7138
a2a53535d855
CircuitPythonDevices: added capability to install library files onto the device to the device menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
656 | target = os.path.join(self.getWorkspace(), "lib") |
a2a53535d855
CircuitPythonDevices: added capability to install library files onto the device to the device menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
657 | # ensure that the library directory exists on the device |
a2a53535d855
CircuitPythonDevices: added capability to install library files onto the device to the device menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
658 | if not os.path.isdir(target): |
a2a53535d855
CircuitPythonDevices: added capability to install library files onto the device to the device menu.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7134
diff
changeset
|
659 | os.makedirs(target) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
660 | |
9738 | 661 | if packageMode: |
662 | libraryPackage = EricFileDialog.getExistingDirectory( | |
663 | self.microPython, | |
664 | title, | |
665 | os.path.expanduser("~"), | |
666 | EricFileDialog.Option(0), | |
667 | ) | |
668 | if libraryPackage: | |
669 | target = os.path.join(target, os.path.basename(libraryPackage)) | |
670 | shutil.rmtree(target, ignore_errors=True) | |
671 | shutil.copytree(libraryPackage, target) | |
672 | else: | |
673 | libraryFiles = EricFileDialog.getOpenFileNames( | |
674 | self.microPython, | |
675 | title, | |
676 | os.path.expanduser("~"), | |
677 | self.tr( | |
678 | "Compiled Python Files (*.mpy);;" | |
679 | "Python Files (*.py);;" | |
680 | "All Files (*)" | |
681 | ), | |
682 | ) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
683 | |
9738 | 684 | for libraryFile in libraryFiles: |
685 | if os.path.exists(libraryFile): | |
686 | shutil.copy2(libraryFile, target) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
687 | |
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:
7145
diff
changeset
|
688 | 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:
7145
diff
changeset
|
689 | """ |
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:
7145
diff
changeset
|
690 | 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
|
691 | |
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:
7145
diff
changeset
|
692 | @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:
7145
diff
changeset
|
693 | @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:
7145
diff
changeset
|
694 | """ |
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:
7145
diff
changeset
|
695 | return Preferences.getMicroPython("CircuitPythonDocuUrl") |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
696 | |
8053
5316e335722f
CircuitPython: added downloads menu for Firmware and Libraries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
697 | def getDownloadMenuEntries(self): |
7328 | 698 | """ |
8053
5316e335722f
CircuitPython: added downloads menu for Firmware and Libraries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
699 | Public method to retrieve the entries for the downloads menu. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
700 | |
8053
5316e335722f
CircuitPython: added downloads menu for Firmware and Libraries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
701 | @return list of tuples with menu text and URL to be opened for each |
5316e335722f
CircuitPython: added downloads menu for Firmware and Libraries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
702 | entry |
5316e335722f
CircuitPython: added downloads menu for Firmware and Libraries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
703 | @rtype list of tuple of (str, str) |
7328 | 704 | """ |
8053
5316e335722f
CircuitPython: added downloads menu for Firmware and Libraries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
705 | return [ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
706 | ( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
707 | self.tr("CircuitPython Firmware"), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
708 | Preferences.getMicroPython("CircuitPythonFirmwareUrl"), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
709 | ), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
710 | ( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
711 | self.tr("CircuitPython Libraries"), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
712 | Preferences.getMicroPython("CircuitPythonLibrariesUrl"), |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
713 | ), |
8053
5316e335722f
CircuitPython: added downloads menu for Firmware and Libraries.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
714 | ] |
9496
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
715 | |
9944 | 716 | @pyqtSlot() |
717 | def __selectDeviceVolume(self): | |
718 | """ | |
719 | Private slot to select the mounted device volume, if it could not be found | |
720 | automatically. | |
721 | """ | |
722 | userMounts = FileSystemUtilities.getUserMounts() | |
723 | msg = ( | |
724 | self.tr("Select the drive letter of the device:") | |
725 | if OSUtilities.isWindowsPlatform() | |
726 | else self.tr("Select the path of the mounted device:") | |
727 | ) | |
728 | selectedMount, ok = QInputDialog.getItem( | |
729 | None, | |
730 | self.tr("Select Device Volume"), | |
731 | msg, | |
732 | userMounts, | |
9972
68ac01294544
Corrected some code style and formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9944
diff
changeset
|
733 | 0, |
68ac01294544
Corrected some code style and formatting issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9944
diff
changeset
|
734 | False, |
9944 | 735 | ) |
736 | if ok and selectedMount: | |
737 | self.__workspace = selectedMount | |
738 | self.__workspaceSelected = self.DeviceVolumeName not in self.__workspace | |
739 | ||
9820 | 740 | ################################################################## |
741 | ## Methods below implement WiFi related methods | |
742 | ################################################################## | |
743 | ||
744 | def hasWifi(self): | |
745 | """ | |
746 | Public method to check the availability of WiFi. | |
747 | ||
748 | @return tuple containing a flag indicating the availability of WiFi | |
10329 | 749 | and the WiFi type (circuitpython) |
9820 | 750 | @rtype tuple of (bool, str) |
751 | @exception OSError raised to indicate an issue with the device | |
752 | """ | |
753 | command = """ | |
754 | def has_wifi(): | |
755 | try: | |
756 | import wifi | |
757 | if hasattr(wifi, 'radio'): | |
758 | return True, 'circuitpython' | |
11055
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
759 | except (ImportError, MemoryError): |
9820 | 760 | pass |
761 | ||
762 | return False, '' | |
763 | ||
764 | print(has_wifi()) | |
765 | del has_wifi | |
766 | """ | |
11055
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
767 | try: |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
768 | return self._deviceData["wifi"], self._deviceData["wifi_type"] |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
769 | except KeyError: |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
770 | out, err = self.executeCommands(command, mode=self._submitMode) |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
771 | if err: |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
772 | raise OSError(self._shortError(err)) |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
773 | return ast.literal_eval(out.decode("utf-8")) |
9820 | 774 | |
775 | def getWifiData(self): | |
776 | """ | |
777 | Public method to get data related to the current WiFi status. | |
778 | ||
779 | @return tuple of three dictionaries containing the WiFi status data | |
780 | for the WiFi client, access point and overall data | |
781 | @rtype tuple of (dict, dict, dict) | |
782 | @exception OSError raised to indicate an issue with the device | |
783 | """ | |
784 | command = """ | |
785 | def wifi_status(): | |
786 | import binascii | |
787 | import json | |
788 | import wifi | |
789 | ||
790 | r = wifi.radio | |
791 | ||
792 | station = { | |
9828 | 793 | 'active': r.enabled and r.ipv4_address is not None, |
9820 | 794 | 'connected': r.ipv4_address is not None, |
795 | 'ifconfig': ( | |
796 | str(r.ipv4_address) if r.ipv4_address else'0.0.0.0', | |
797 | str(r.ipv4_subnet) if r.ipv4_subnet else'0.0.0.0', | |
798 | str(r.ipv4_gateway) if r.ipv4_gateway else'0.0.0.0', | |
799 | str(r.ipv4_dns) if r.ipv4_dns else'0.0.0.0', | |
800 | ), | |
801 | 'mac': binascii.hexlify(r.mac_address, ':').decode(), | |
802 | } | |
9828 | 803 | try: |
804 | station['txpower'] = r.tx_power | |
805 | except AttributeError: | |
806 | pass | |
807 | try: | |
808 | if r.ap_info is not None: | |
809 | station.update({ | |
810 | 'ap_ssid': r.ap_info.ssid, | |
811 | 'ap_bssid': binascii.hexlify(r.ap_info.bssid, ':'), | |
812 | 'ap_rssi': r.ap_info.rssi, | |
813 | 'ap_channel': r.ap_info.channel, | |
814 | 'ap_country': r.ap_info.country, | |
815 | }) | |
816 | authmode = r.ap_info.authmode | |
817 | station['ap_security'] = ( | |
818 | '_'.join(str(x).split('.')[-1] for x in authmode) | |
819 | if isinstance(authmode, list) | |
820 | else authmode | |
821 | ) | |
822 | except (NotImplementedError, AttributeError): | |
823 | pass | |
9820 | 824 | print(json.dumps(station)) |
825 | ||
826 | ap = { | |
827 | 'active': r.enabled and r.ipv4_address_ap is not None, | |
828 | 'connected': r.ipv4_address_ap is not None, | |
829 | 'ifconfig': ( | |
830 | str(r.ipv4_address_ap) if r.ipv4_address_ap else'0.0.0.0', | |
831 | str(r.ipv4_subnet_ap) if r.ipv4_subnet_ap else'0.0.0.0', | |
832 | str(r.ipv4_gateway_ap) if r.ipv4_gateway_ap else'0.0.0.0', | |
833 | str(r.ipv4_dns) if r.ipv4_dns else'0.0.0.0', | |
834 | ), | |
835 | 'mac': binascii.hexlify(r.mac_address_ap, ':').decode(), | |
836 | } | |
9828 | 837 | try: |
838 | ap['txpower'] = r.tx_power | |
839 | except AttributeError: | |
840 | pass | |
9820 | 841 | print(json.dumps(ap)) |
842 | ||
843 | overall = { | |
10153 | 844 | 'active': r.enabled, |
845 | 'hostname': r.hostname, | |
9820 | 846 | } |
847 | print(json.dumps(overall)) | |
848 | ||
849 | wifi_status() | |
850 | del wifi_status | |
851 | """ | |
852 | ||
9989 | 853 | out, err = self.executeCommands(command, mode=self._submitMode) |
9820 | 854 | if err: |
855 | raise OSError(self._shortError(err)) | |
856 | ||
857 | stationStr, apStr, overallStr = out.decode("utf-8").splitlines() | |
858 | station = json.loads(stationStr) | |
859 | ap = json.loads(apStr) | |
860 | overall = json.loads(overallStr) | |
9828 | 861 | if "ap_security" in station: |
862 | try: | |
863 | station["ap_security"] = self.__securityTranslations[ | |
864 | station["ap_security"] | |
865 | ] | |
866 | except KeyError: | |
867 | station["ap_security"] = self.tr("unknown ({0})").format( | |
868 | station["ap_security"] | |
869 | ) | |
870 | ||
9820 | 871 | return station, ap, overall |
872 | ||
10153 | 873 | def connectWifi(self, ssid, password, hostname): |
9828 | 874 | """ |
875 | Public method to connect a device to a WiFi network. | |
876 | ||
877 | @param ssid name (SSID) of the WiFi network | |
878 | @type str | |
879 | @param password password needed to connect | |
880 | @type str | |
10153 | 881 | @param hostname host name of the device |
882 | @type str | |
9828 | 883 | @return tuple containing the connection status and an error string |
884 | @rtype tuple of (bool, str) | |
885 | """ | |
886 | command = """ | |
10153 | 887 | def connect_wifi(ssid, password, hostname): |
9828 | 888 | import json |
889 | import wifi | |
890 | ||
891 | r = wifi.radio | |
892 | try: | |
10153 | 893 | if hostname: |
894 | r.hostname = hostname | |
9828 | 895 | r.start_station() |
896 | r.connect(ssid, password) | |
897 | status = 'connected' | |
898 | except Exception as exc: | |
899 | status = str(exc) | |
900 | ||
901 | print(json.dumps({{'connected': r.ipv4_address is not None, 'status': status}})) | |
902 | ||
10153 | 903 | connect_wifi({0}, {1}, {2}) |
9828 | 904 | del connect_wifi |
905 | """.format( | |
906 | repr(ssid), | |
907 | repr(password if password else ""), | |
10153 | 908 | repr(hostname), |
9828 | 909 | ) |
910 | ||
911 | with EricOverrideCursor(): | |
9989 | 912 | out, err = self.executeCommands( |
9828 | 913 | command, mode=self._submitMode, timeout=15000 |
914 | ) | |
915 | if err: | |
916 | return False, err | |
917 | ||
918 | result = json.loads(out.decode("utf-8").strip()) | |
919 | error = "" if result["connected"] else result["status"] | |
920 | ||
921 | return result["connected"], error | |
922 | ||
923 | def disconnectWifi(self): | |
924 | """ | |
925 | Public method to disconnect a device from the WiFi network. | |
926 | ||
927 | @return tuple containing a flag indicating success and an error string | |
928 | @rtype tuple of (bool, str) | |
929 | """ | |
930 | command = """ | |
931 | def disconnect_wifi(): | |
932 | import json | |
933 | import wifi | |
934 | ||
935 | r = wifi.radio | |
936 | try: | |
937 | r.stop_station() | |
938 | status = '' | |
939 | except Exception as exc: | |
940 | status = str(exc) | |
941 | ||
942 | print(json.dumps({'success': status == '', 'status': status})) | |
943 | ||
944 | disconnect_wifi() | |
945 | del disconnect_wifi | |
946 | """ | |
947 | ||
9989 | 948 | out, err = self.executeCommands(command, mode=self._submitMode) |
9828 | 949 | if err: |
950 | return False, err | |
951 | ||
952 | result = json.loads(out.decode("utf-8").strip()) | |
953 | return result["success"], result["status"] | |
954 | ||
10144 | 955 | def isWifiClientConnected(self): |
956 | """ | |
957 | Public method to check the WiFi connection status as client. | |
958 | ||
959 | @return flag indicating the WiFi connection status | |
960 | @rtype bool | |
961 | """ | |
962 | command = """ | |
963 | def wifi_connected(): | |
964 | import wifi | |
965 | ||
966 | r = wifi.radio | |
967 | print(r.ipv4_address is not None) | |
968 | ||
969 | wifi_connected() | |
970 | del wifi_connected | |
971 | """ | |
972 | ||
973 | out, err = self.executeCommands(command, mode=self._submitMode) | |
974 | if err: | |
975 | return False | |
976 | ||
977 | return out.strip() == b"True" | |
978 | ||
979 | def isWifiApConnected(self): | |
980 | """ | |
981 | Public method to check the WiFi connection status as access point. | |
982 | ||
983 | @return flag indicating the WiFi connection status | |
984 | @rtype bool | |
985 | """ | |
986 | command = """ | |
987 | def wifi_connected(): | |
988 | import wifi | |
989 | ||
990 | r = wifi.radio | |
991 | print(r.ipv4_address_ap is not None) | |
992 | """ | |
993 | ||
994 | out, err = self.executeCommands(command, mode=self._submitMode) | |
995 | if err: | |
996 | return False | |
997 | ||
998 | return out.strip() == b"True" | |
999 | ||
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
|
1000 | def writeCredentials(self, ssid, password, hostname, _country): |
9828 | 1001 | """ |
1002 | Public method to write the given credentials to the connected device and modify | |
1003 | the start script to connect automatically. | |
1004 | ||
1005 | @param ssid SSID of the network to connect to | |
1006 | @type str | |
1007 | @param password password needed to authenticate | |
1008 | @type str | |
10153 | 1009 | @param hostname host name of the device |
1010 | @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
|
1011 | @param _country WiFi country code (unused) |
10153 | 1012 | @type str |
9828 | 1013 | @return tuple containing a flag indicating success and an error message |
1014 | @rtype tuple of (bool, str) | |
1015 | """ | |
1016 | if not self.__deviceVolumeMounted(): | |
1017 | return False, self.tr("The device volume is not available.") | |
1018 | ||
1019 | workspace = self.getWorkspace() | |
1020 | ||
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
|
1021 | if EricUtilities.versionToTuple(self._deviceData["release"]) >= (8, 0, 0): |
9828 | 1022 | # CircuitPython >= 8.0.0: generate 'settings.toml' file |
1023 | contents = ( | |
10153 | 1024 | 'CIRCUITPY_WIFI_SSID = "{0}"\nCIRCUITPY_WIFI_PASSWORD = "{1}"\n' |
1025 | 'CIRCUITPY_WIFI_HOSTNAME = "{2}"\n'.format(ssid, password, hostname) | |
9828 | 1026 | ) |
1027 | filename = os.path.join(workspace, "settings.toml") | |
1028 | if os.path.exists(filename): | |
1029 | ok = EricMessageBox.yesNo( | |
1030 | None, | |
1031 | self.tr("Write WiFi Credentials"), | |
1032 | self.tr( | |
1033 | """<p>The file <b>{0}</b> exists already. Shall it be""" | |
1034 | """ replaced?</p>""" | |
1035 | ).format(filename), | |
1036 | icon=EricMessageBox.Warning, | |
1037 | ) | |
1038 | if not ok: | |
1039 | return False, self.tr("Aborted") | |
1040 | try: | |
1041 | with open(filename, "w") as f: | |
1042 | f.write(contents) | |
1043 | return True, "" | |
1044 | except OSError as err: | |
1045 | return False, str(err) | |
1046 | ||
1047 | else: | |
1048 | # CircuitPython < 8.0.0: generate a secrets.py script | |
1049 | # step 1: generate the secrets.py file | |
1050 | contents = ( | |
10153 | 1051 | 'secrets = {{\n "ssid": "{0}",\n "password": "{1}",\n' |
1052 | ' "hostname": "{2}",\n}}\n'.format(ssid, password, hostname) | |
9828 | 1053 | ) |
1054 | filename = os.path.join(workspace, "secrets.py") | |
1055 | if os.path.exists(filename): | |
1056 | ok = EricMessageBox.yesNo( | |
1057 | None, | |
1058 | self.tr("Write WiFi Credentials"), | |
1059 | self.tr( | |
1060 | """<p>The file <b>{0}</b> exists already. Shall it be""" | |
1061 | """ replaced?</p>""" | |
1062 | ).format(filename), | |
1063 | icon=EricMessageBox.Warning, | |
1064 | ) | |
1065 | if not ok: | |
1066 | return False, self.tr("Aborted") | |
1067 | try: | |
1068 | with open(filename, "w") as f: | |
1069 | f.write(contents) | |
1070 | except OSError as err: | |
1071 | return False, str(err) | |
9885 | 1072 | |
1073 | # step 2: create the auto-connect script (wifi_connect.py) | |
9828 | 1074 | scriptFile = os.path.join( |
1075 | os.path.dirname(__file__), "MCUScripts", "circuitPy7WiFiConnect.py" | |
1076 | ) | |
1077 | targetFile = os.path.join(workspace, "wifi_connect.py") | |
1078 | try: | |
1079 | shutil.copy2(scriptFile, targetFile) | |
1080 | except OSError as err: | |
1081 | return False, str(err) | |
1082 | # Note: code.py will not be modified because the connection will be | |
1083 | # reset anyway | |
1084 | return True, "" | |
1085 | ||
1086 | def removeCredentials(self): | |
1087 | """ | |
1088 | Public method to remove the saved credentials from the connected device. | |
1089 | ||
1090 | @return tuple containing a flag indicating success and an error message | |
1091 | @rtype tuple of (bool, str) | |
1092 | """ | |
1093 | if not self.__deviceVolumeMounted(): | |
1094 | return False, self.tr("The device volume is not available.") | |
1095 | ||
1096 | workspace = self.getWorkspace() | |
1097 | for name in ("settings.toml", "secrets.py"): | |
1098 | filename = os.path.join(workspace, name) | |
1099 | if os.path.exists(filename): | |
1100 | os.remove(filename) | |
1101 | ||
1102 | return True, "" | |
1103 | ||
1104 | def checkInternet(self): | |
1105 | """ | |
1106 | Public method to check, if the internet can be reached. | |
1107 | ||
1108 | @return tuple containing a flag indicating reachability and an error string | |
1109 | @rtype tuple of (bool, str) | |
1110 | """ | |
1111 | command = """ | |
1112 | def check_internet(): | |
1113 | import ipaddress | |
1114 | import wifi | |
1115 | ||
1116 | r = wifi.radio | |
1117 | if r.ipv4_address is not None: | |
9836 | 1118 | ping = r.ping(ipaddress.IPv4Address("9.9.9.9")) |
9828 | 1119 | print(ping is not None) |
1120 | else: | |
1121 | print(False) | |
1122 | ||
1123 | check_internet() | |
1124 | del check_internet | |
1125 | """ | |
1126 | ||
9989 | 1127 | out, err = self.executeCommands(command, mode=self._submitMode) |
9828 | 1128 | if err: |
1129 | return False, err | |
1130 | ||
1131 | return out.decode("utf-8").strip() == "True", "" | |
1132 | ||
1133 | def scanNetworks(self): | |
1134 | """ | |
1135 | Public method to scan for available WiFi networks. | |
1136 | ||
1137 | @return tuple containing the list of available networks as a tuple of 'Name', | |
1138 | 'MAC-Address', 'channel', 'RSSI' and 'security' and an error string | |
1139 | @rtype tuple of (list of tuple of (str, str, int, int, str), str) | |
1140 | """ | |
1141 | command = """ | |
1142 | def scan_networks(): | |
1143 | import wifi | |
1144 | ||
1145 | r = wifi.radio | |
1146 | network_list = [] | |
1147 | enabled = r.enabled | |
1148 | if not enabled: | |
1149 | r.enabled = True | |
1150 | for net in r.start_scanning_networks(): | |
1151 | network_list.append( | |
1152 | (net.ssid, net.bssid, net.channel, net.rssi, | |
1153 | '_'.join(str(x).split('.')[-1] for x in net.authmode)) | |
1154 | ) | |
1155 | r.stop_scanning_networks() | |
1156 | if not enabled: | |
1157 | r.enabled = False | |
1158 | print(network_list) | |
1159 | ||
1160 | scan_networks() | |
1161 | del scan_networks | |
1162 | """ | |
1163 | ||
9989 | 1164 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=15000) |
9828 | 1165 | if err: |
1166 | return [], err | |
1167 | ||
1168 | networksList = ast.literal_eval(out.decode("utf-8")) | |
1169 | networks = [] | |
1170 | seenNetworks = [] | |
1171 | for network in networksList: | |
1172 | if network[0]: | |
1173 | ssid = network[0] | |
1174 | mac = binascii.hexlify(network[1], ":").decode("utf-8") | |
1175 | channel = network[2] | |
1176 | rssi = network[3] | |
1177 | try: | |
1178 | security = self.__securityTranslations[network[4]] | |
1179 | except KeyError: | |
1180 | security = self.tr("unknown ({0})").format(network[4]) | |
1181 | if (ssid, mac, channel) not in seenNetworks: | |
1182 | seenNetworks.append((ssid, mac, channel)) | |
1183 | networks.append((ssid, mac, channel, rssi, security)) | |
1184 | ||
1185 | return networks, "" | |
1186 | ||
1187 | def deactivateInterface(self, interface): | |
1188 | """ | |
1189 | Public method to deactivate a given WiFi interface of the connected device. | |
1190 | ||
1191 | Note: With CircuitPython it is not possible to deactivate the station and | |
1192 | access point interfaces separately. | |
1193 | ||
1194 | @param interface designation of the interface to be deactivated (one of 'AP' | |
1195 | or 'STA') | |
1196 | @type str | |
1197 | @return tuple containg a flag indicating success and an error message | |
1198 | @rtype tuple of (bool, str) | |
1199 | @exception ValueError raised to indicate a wrong value for the interface type | |
1200 | """ | |
1201 | if interface not in ("STA", "AP"): | |
1202 | raise ValueError( | |
1203 | "interface must be 'AP' or 'STA', got '{0}'".format(interface) | |
1204 | ) | |
1205 | ||
1206 | command = """ | |
1207 | def deactivate(): | |
1208 | import wifi | |
1209 | ||
1210 | wifi.radio.enabled = False | |
1211 | print(not wifi.radio.enabled) | |
1212 | ||
1213 | deactivate() | |
1214 | del deactivate | |
1215 | """ | |
1216 | ||
9989 | 1217 | out, err = self.executeCommands(command, mode=self._submitMode) |
9828 | 1218 | if err: |
1219 | return False, err | |
1220 | else: | |
1221 | return out.decode("utf-8").strip() == "True", "" | |
1222 | ||
10153 | 1223 | def startAccessPoint( |
1224 | self, | |
1225 | ssid, | |
1226 | security=None, | |
1227 | password=None, | |
1228 | hostname=None, | |
1229 | ifconfig=None, | |
1230 | ): | |
9828 | 1231 | """ |
1232 | Public method to start the access point interface. | |
1233 | ||
1234 | @param ssid SSID of the access point | |
1235 | @type str | |
1236 | @param security security method (defaults to None) | |
1237 | @type int (optional) | |
1238 | @param password password (defaults to None) | |
1239 | @type str (optional) | |
10153 | 1240 | @param hostname host name of the device (defaults to None) |
1241 | @type str (optional) | |
9828 | 1242 | @param ifconfig IPv4 configuration for the access point if not default |
1243 | (IPv4 address, netmask, gateway address, DNS server address) | |
1244 | @type tuple of (str, str, str, str) | |
1245 | @return tuple containing a flag indicating success and an error message | |
1246 | @rtype tuple of (bool, str) | |
1247 | """ | |
1248 | if security is None or password is None: | |
1249 | security = 0 | |
10170
6cf1ee737d8f
Corrected some more code style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10153
diff
changeset
|
1250 | password = "" # secok |
9828 | 1251 | authmode = self.__securityCode2AuthModeString[security] |
1252 | ||
1253 | if ifconfig: | |
1254 | return ( | |
1255 | False, | |
1256 | self.tr( | |
1257 | "CircuitPython does not support setting the IPv4 parameters of the" | |
1258 | " WiFi access point." | |
1259 | ), | |
1260 | ) | |
1261 | ||
1262 | command = """ | |
10153 | 1263 | def start_ap(ssid, password, hostname): |
9828 | 1264 | import wifi |
1265 | ||
1266 | r = wifi.radio | |
10153 | 1267 | r.enabled = True |
1268 | if hostname: | |
1269 | r.hostname = hostname | |
9828 | 1270 | try: |
10153 | 1271 | r.start_ap(ssid, password, authmode={3}) |
1272 | except (NotImplementedError, ValueError) as exc: | |
9828 | 1273 | print('Error:', str(exc)) |
1274 | ||
10153 | 1275 | start_ap({0}, {1}, {2}) |
9828 | 1276 | del start_ap |
1277 | """.format( | |
10153 | 1278 | repr(ssid), repr(password), repr(hostname), authmode |
9828 | 1279 | ) |
1280 | ||
9989 | 1281 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=15000) |
9828 | 1282 | if err: |
1283 | return False, err | |
1284 | elif out and out.startswith(b"Error:"): | |
1285 | return False, out.decode("utf-8").split(None, 1)[-1] | |
1286 | else: | |
1287 | return True, "" | |
1288 | ||
1289 | def stopAccessPoint(self): | |
1290 | """ | |
1291 | Public method to stop the access point interface. | |
1292 | ||
1293 | @return tuple containg a flag indicating success and an error message | |
1294 | @rtype tuple of (bool, str) | |
1295 | """ | |
1296 | command = """ | |
1297 | def stop_ap(): | |
1298 | import wifi | |
1299 | ||
1300 | r = wifi.radio | |
1301 | try: | |
1302 | r.stop_ap() | |
1303 | except NotImplementedError as exc: | |
1304 | print('Error:', str(exc)) | |
1305 | ||
1306 | stop_ap() | |
1307 | del stop_ap | |
1308 | """ | |
1309 | ||
9989 | 1310 | out, err = self.executeCommands(command, mode=self._submitMode) |
9828 | 1311 | if err: |
1312 | return False, err | |
1313 | elif out and out.startswith(b"Error:"): | |
1314 | return False, out.decode("utf-8").split(None, 1)[-1] | |
1315 | else: | |
1316 | return True, "" | |
1317 | ||
1318 | def getConnectedClients(self): | |
1319 | """ | |
1320 | Public method to get a list of connected clients. | |
1321 | ||
1322 | @return a tuple containing a list of tuples containing the client MAC-Address | |
1323 | and the RSSI (if supported and available) and an error message | |
1324 | @rtype tuple of ([(bytes, int)], str) | |
1325 | """ | |
1326 | return ( | |
1327 | [], | |
9841
3c6118eee33e
Updated translations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9836
diff
changeset
|
1328 | self.tr("CircuitPython does not support reporting of connected clients."), |
9828 | 1329 | ) |
1330 | ||
9855 | 1331 | ################################################################## |
9885 | 1332 | ## Methods below implement Ethernet related methods |
1333 | ################################################################## | |
1334 | ||
1335 | def hasEthernet(self): | |
1336 | """ | |
1337 | Public method to check the availability of Ethernet. | |
1338 | ||
1339 | @return tuple containing a flag indicating the availability of Ethernet | |
1340 | and the Ethernet type | |
1341 | @rtype tuple of (bool, str) | |
1342 | @exception OSError raised to indicate an issue with the device | |
1343 | """ | |
1344 | command = """ | |
1345 | def has_eth(): | |
1346 | try: | |
1347 | from adafruit_wiznet5k import adafruit_wiznet5k | |
1348 | if hasattr(adafruit_wiznet5k, 'WIZNET5K'): | |
1349 | return True, 'cpypicowiz' | |
1350 | except ImportError: | |
1351 | pass | |
1352 | ||
1353 | return False, '' | |
1354 | ||
1355 | print(has_eth()) | |
1356 | del has_eth | |
1357 | """ | |
11055
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1358 | try: |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1359 | return self._deviceData["ethernet"], self._deviceData["ethernet_type"] |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1360 | except KeyError: |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1361 | out, err = self.executeCommands( |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1362 | command, mode=self._submitMode, timeout=10000 |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1363 | ) |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1364 | if err: |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1365 | raise OSError(self._shortError(err)) |
9885 | 1366 | |
11055
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1367 | return ast.literal_eval(out.decode("utf-8")) |
9885 | 1368 | |
1369 | def getEthernetStatus(self): | |
1370 | """ | |
1371 | Public method to get Ethernet status data of the connected board. | |
1372 | ||
1373 | @return list of tuples containing the translated status data label and | |
1374 | the associated value | |
1375 | @rtype list of tuples of (str, str) | |
1376 | @exception OSError raised to indicate an issue with the device | |
1377 | """ | |
1378 | command = """{0} | |
1379 | def ethernet_status(): | |
1380 | import binascii | |
1381 | import json | |
1382 | ||
1383 | w5x00_init() | |
1384 | ||
1385 | res = {{ | |
1386 | 'active': nic.link_status != 0, | |
1387 | 'connected': nic.link_status == 1 and nic.ifconfig[0] != b'\x00\x00\x00\x00', | |
1388 | 'ifconfig': ( | |
1389 | nic.pretty_ip(nic.ifconfig[0]), | |
1390 | nic.pretty_ip(nic.ifconfig[1]), | |
1391 | nic.pretty_ip(nic.ifconfig[2]), | |
1392 | nic.pretty_ip(nic.ifconfig[3]), | |
1393 | ), | |
1394 | 'mac': binascii.hexlify(nic.mac_address, ':').decode(), | |
1395 | 'chip': nic.chip, | |
1396 | 'max_sockets': nic.max_sockets, | |
1397 | }} | |
1398 | print(json.dumps(res)) | |
1399 | ||
1400 | ethernet_status() | |
1401 | del ethernet_status, w5x00_init | |
1402 | """.format( | |
1403 | WiznetUtilities.cpyWiznetInit() | |
1404 | ) | |
1405 | ||
9989 | 1406 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=10000) |
9885 | 1407 | if err: |
1408 | raise OSError(self._shortError(err)) | |
1409 | ||
1410 | status = [] | |
1411 | ethStatus = json.loads(out.decode("utf-8")) | |
1412 | status.append((self.tr("Active"), self.bool2str(ethStatus["active"]))) | |
1413 | status.append((self.tr("Connected"), self.bool2str(ethStatus["connected"]))) | |
1414 | status.append((self.tr("IPv4 Address"), ethStatus["ifconfig"][0])) | |
1415 | status.append((self.tr("Netmask"), ethStatus["ifconfig"][1])) | |
1416 | status.append((self.tr("Gateway"), ethStatus["ifconfig"][2])) | |
1417 | status.append((self.tr("DNS"), ethStatus["ifconfig"][3])) | |
1418 | status.append((self.tr("MAC-Address"), ethStatus["mac"])) | |
1419 | status.append((self.tr("Chip Type"), ethStatus["chip"])) | |
1420 | status.append((self.tr("max. Sockets"), ethStatus["max_sockets"])) | |
1421 | ||
1422 | return status | |
1423 | ||
10153 | 1424 | def connectToLan(self, config, hostname): |
9885 | 1425 | """ |
1426 | Public method to connect the connected device to the LAN. | |
1427 | ||
1428 | Note: The MAC address of the interface is configured with the WIZ | |
1429 | ||
1430 | @param config configuration for the connection (either the string 'dhcp' | |
1431 | for a dynamic address or a tuple of four strings with the IPv4 parameters. | |
1432 | @type str or tuple of (str, str, str, str) | |
10153 | 1433 | @param hostname host name of the device |
1434 | @type str | |
9885 | 1435 | @return tuple containing a flag indicating success and an error message |
1436 | @rtype tuple of (bool, str) | |
1437 | """ | |
1438 | command = """{0} | |
10153 | 1439 | def connect_lan(config, hostname): |
9885 | 1440 | from adafruit_wiznet5k import adafruit_wiznet5k |
1441 | ||
1442 | w5x00_init() | |
1443 | ||
1444 | nic.mac_address = adafruit_wiznet5k._DEFAULT_MAC | |
1445 | if config == 'dhcp': | |
10153 | 1446 | nic.set_dhcp(hostname=hostname) |
9885 | 1447 | else: |
1448 | nic.ifconfig = ( | |
1449 | nic.unpretty_ip(config[0]), | |
1450 | nic.unpretty_ip(config[1]), | |
1451 | nic.unpretty_ip(config[2]), | |
1452 | tuple(int(a) for a in config[3].split('.')), | |
1453 | ) | |
1454 | print(nic.ifconfig[0] != b'\x00\x00\x00\x00') | |
1455 | ||
10153 | 1456 | connect_lan({1}, {2}) |
9885 | 1457 | del connect_lan, w5x00_init |
1458 | """.format( | |
10153 | 1459 | WiznetUtilities.cpyWiznetInit(), |
1460 | "'dhcp'" if config == "dhcp" else config, | |
1461 | repr(hostname) if hostname else "''", | |
9885 | 1462 | ) |
1463 | ||
1464 | with EricOverrideCursor(): | |
9989 | 1465 | out, err = self.executeCommands( |
9885 | 1466 | command, mode=self._submitMode, timeout=15000 |
1467 | ) | |
1468 | if err: | |
1469 | return False, err | |
1470 | ||
1471 | return out.strip() == b"True", "" | |
1472 | ||
1473 | def disconnectFromLan(self): | |
1474 | """ | |
1475 | Public method to disconnect from the LAN. | |
1476 | ||
1477 | @return tuple containing a flag indicating success and an error message | |
1478 | @rtype tuple of (bool, str) | |
1479 | """ | |
1480 | command = """{0} | |
1481 | def disconnect_lan(): | |
1482 | import time | |
1483 | ||
1484 | w5x00_init() | |
1485 | ||
1486 | nic.sw_reset() | |
1487 | time.sleep(1) | |
1488 | print(nic.ifconfig[0] == b'\x00\x00\x00\x00') | |
1489 | ||
1490 | disconnect_lan() | |
1491 | del disconnect_lan, w5x00_init | |
1492 | """.format( | |
1493 | WiznetUtilities.cpyWiznetInit(), | |
1494 | ) | |
1495 | ||
1496 | with EricOverrideCursor(): | |
9989 | 1497 | out, err = self.executeCommands( |
9885 | 1498 | command, mode=self._submitMode, timeout=15000 |
1499 | ) | |
1500 | if err: | |
1501 | return False, err | |
1502 | ||
10144 | 1503 | return out.strip() == b"True", "" |
1504 | ||
1505 | def isLanConnected(self): | |
1506 | """ | |
1507 | Public method to check the LAN connection status. | |
1508 | ||
1509 | @return flag indicating that the device is connected to the LAN | |
1510 | @rtype bool | |
1511 | """ | |
1512 | command = """{0} | |
1513 | def is_connected(): | |
1514 | w5x00_init() | |
10138 | 1515 | |
10144 | 1516 | print(nic.link_status == 1 and nic.ifconfig[0] != b'\x00\x00\x00\x00') |
1517 | ||
1518 | is_connected() | |
1519 | del is_connected, w5x00_init | |
1520 | """.format( | |
1521 | WiznetUtilities.cpyWiznetInit(), | |
1522 | ) | |
1523 | ||
1524 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=10000) | |
1525 | if err: | |
1526 | return False | |
1527 | ||
1528 | return out.strip() == b"True" | |
9885 | 1529 | |
1530 | def checkInternetViaLan(self): | |
1531 | """ | |
1532 | Public method to check, if the internet can be reached (LAN variant). | |
1533 | ||
1534 | @return tuple containing a flag indicating reachability and an error string | |
1535 | @rtype tuple of (bool, str) | |
1536 | """ | |
1537 | command = """{0} | |
1538 | def check_internet(): | |
1539 | w5x00_init() | |
1540 | ||
1541 | if nic.ifconfig[0] != b'\x00\x00\x00\x00': | |
1542 | sock = nic.get_socket() | |
1543 | try: | |
10233
51a6649ba79d
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10170
diff
changeset
|
1544 | nic.socket_connect(sock, nic.get_host_by_name('quad9.net'), 443) |
9885 | 1545 | nic.socket_disconnect(sock) |
1546 | print(True) | |
1547 | except: | |
1548 | print(False) | |
1549 | nic.socket_close(sock) | |
1550 | else: | |
1551 | print(False) | |
1552 | ||
1553 | check_internet() | |
1554 | del check_internet, w5x00_init | |
1555 | """.format( | |
1556 | WiznetUtilities.cpyWiznetInit(), | |
1557 | ) | |
1558 | ||
9989 | 1559 | out, err = self.executeCommands(command, mode=self._submitMode, timeout=15000) |
9885 | 1560 | if err: |
1561 | return False, err | |
1562 | ||
1563 | return out.strip() == b"True", "" | |
1564 | ||
1565 | def deactivateEthernet(self): | |
1566 | """ | |
1567 | Public method to deactivate the Ethernet interface of the connected device. | |
1568 | ||
1569 | @return tuple containg a flag indicating success and an error message | |
1570 | @rtype tuple of (bool, str) | |
1571 | """ | |
1572 | # The WIZnet 5x00 interface cannot be switched off explicitly. That means, | |
1573 | # disconnect from the LAN is all we can do. | |
1574 | ||
1575 | return self.disconnectFromLan() | |
1576 | ||
10153 | 1577 | def writeLanAutoConnect(self, config, hostname): |
9885 | 1578 | """ |
1579 | Public method to generate a script and associated configuration to connect the | |
1580 | device to the LAN during boot time. | |
1581 | ||
1582 | @param config configuration for the connection (either the string 'dhcp' | |
1583 | for a dynamic address or a tuple of four strings with the IPv4 parameters. | |
1584 | @type str or tuple of (str, str, str, str) | |
10153 | 1585 | @param hostname host name of the device |
1586 | @type str | |
9885 | 1587 | @return tuple containing a flag indicating success and an error message |
1588 | @rtype tuple of (bool, str) | |
1589 | """ | |
1590 | if not self.__deviceVolumeMounted(): | |
1591 | return False, self.tr("The device volume is not available.") | |
1592 | ||
1593 | workspace = self.getWorkspace() | |
1594 | ||
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
|
1595 | if EricUtilities.versionToTuple(self._deviceData["release"]) >= (8, 0, 0): |
9885 | 1596 | # CircuitPython >= 8.0.0: generate 'settings.toml' file |
1597 | newConfig = ( | |
1598 | { | |
1599 | "WIZNET_IFCONFIG_0": '"dhcp"', | |
1600 | "WIZNET_IFCONFIG_1": "", | |
1601 | "WIZNET_IFCONFIG_2": "", | |
1602 | "WIZNET_IFCONFIG_3": "", | |
10153 | 1603 | "WIZNET_HOSTNAME": '"{0}"'.format(hostname) if hostname else '""', |
9885 | 1604 | } |
1605 | if config == "dhcp" | |
1606 | else { | |
1607 | "WIZNET_IFCONFIG_0": '"{0}"'.format(config[0]), | |
1608 | "WIZNET_IFCONFIG_1": '"{0}"'.format(config[1]), | |
1609 | "WIZNET_IFCONFIG_2": '"{0}"'.format(config[2]), | |
1610 | "WIZNET_IFCONFIG_3": '"{0}"'.format(config[3]), | |
1611 | } | |
1612 | ) | |
1613 | ok, err = self.__modifySettings(newConfig) | |
1614 | if not ok: | |
1615 | return False, err | |
1616 | ||
1617 | scriptFile = os.path.join( | |
1618 | os.path.dirname(__file__), "MCUScripts", "picoWiznetConnectCpy8.py" | |
1619 | ) | |
1620 | ||
1621 | else: | |
1622 | # step 1: generate the wiznet_config.py file | |
10153 | 1623 | ifconfig = "ifconfig = {0}\nhostname={1}\n".format( |
1624 | "'dhcp'" if config == "dhcp" else config, | |
1625 | repr(hostname) if hostname else "''", | |
9885 | 1626 | ) |
1627 | filename = os.path.join(workspace, "wiznet_config.py") | |
1628 | if os.path.exists(filename): | |
1629 | ok = EricMessageBox.yesNo( | |
1630 | None, | |
1631 | self.tr("Write Connect Script"), | |
1632 | self.tr( | |
1633 | """<p>The file <b>{0}</b> exists already. Shall it be""" | |
1634 | """ replaced?</p>""" | |
1635 | ).format(filename), | |
1636 | icon=EricMessageBox.Warning, | |
1637 | ) | |
1638 | if not ok: | |
1639 | return False, self.tr("Aborted") | |
1640 | try: | |
1641 | with open(filename, "w") as f: | |
1642 | f.write(ifconfig) | |
1643 | except OSError as err: | |
1644 | return False, str(err) | |
1645 | ||
1646 | scriptFile = os.path.join( | |
1647 | os.path.dirname(__file__), "MCUScripts", "picoWiznetConnectCpy7.py" | |
1648 | ) | |
1649 | ||
1650 | # step 2: create the auto-connect script (wiznet_connect.py) | |
1651 | targetFile = os.path.join(workspace, "wiznet_connect.py") | |
1652 | try: | |
1653 | shutil.copy2(scriptFile, targetFile) | |
1654 | except OSError as err: | |
1655 | return False, str(err) | |
1656 | # Note: code.py will not be modified because the connection will be | |
1657 | # reset anyway | |
1658 | return True, "" | |
1659 | ||
1660 | def removeLanAutoConnect(self): | |
1661 | """ | |
1662 | Public method to remove the saved IPv4 parameters from the connected device. | |
1663 | ||
1664 | Note: This disables the LAN auto-connect feature. | |
1665 | ||
1666 | @return tuple containing a flag indicating success and an error message | |
1667 | @rtype tuple of (bool, str) | |
1668 | """ | |
1669 | if not self.__deviceVolumeMounted(): | |
1670 | return False, self.tr("The device volume is not available.") | |
1671 | ||
1672 | workspace = self.getWorkspace() | |
1673 | ||
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
|
1674 | if EricUtilities.versionToTuple(self._deviceData["release"]) >= (8, 0, 0): |
9885 | 1675 | # CircuitPython >= 8.0.0: generate 'settings.toml' file |
1676 | newConfig = { | |
1677 | "WIZNET_IFCONFIG_0": "", | |
1678 | "WIZNET_IFCONFIG_1": "", | |
1679 | "WIZNET_IFCONFIG_2": "", | |
1680 | "WIZNET_IFCONFIG_3": "", | |
1681 | } | |
1682 | self.__modifySettings(newConfig) | |
1683 | ||
1684 | for name in ("wiznet_config.py", "wiznet_connect.py"): | |
1685 | filename = os.path.join(workspace, name) | |
1686 | if os.path.exists(filename): | |
1687 | os.remove(filename) | |
1688 | ||
1689 | return True, "" | |
1690 | ||
1691 | ################################################################## | |
9855 | 1692 | ## Methods below implement Bluetooth related methods |
1693 | ################################################################## | |
1694 | ||
1695 | def hasBluetooth(self): | |
1696 | """ | |
1697 | Public method to check the availability of Bluetooth. | |
1698 | ||
1699 | @return flag indicating the availability of Bluetooth | |
1700 | @rtype bool | |
9857 | 1701 | @exception OSError raised to indicate an issue with the device |
9855 | 1702 | """ |
1703 | command = """ | |
1704 | def has_bt(): | |
1705 | try: | |
1706 | import _bleio | |
9881
5ce653f9dac8
Fixed an issue in the CircuitPython Bluetooth detection code.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9870
diff
changeset
|
1707 | if hasattr(_bleio, 'adapter') and _bleio.adapter is not None: |
9855 | 1708 | return True |
1709 | except ImportError: | |
1710 | pass | |
1711 | ||
1712 | return False | |
1713 | ||
1714 | print(has_bt()) | |
1715 | del has_bt | |
1716 | """ | |
11055
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1717 | try: |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1718 | return self._deviceData["bluetooth"] |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1719 | except KeyError: |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1720 | out, err = self.executeCommands( |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1721 | command, mode=self._submitMode, timeout=10000 |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1722 | ) |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1723 | if err: |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1724 | raise OSError(self._shortError(err)) |
e68bcb2b5ec1
Improved the CircuitPython device class and fixed som issues in the MPy REPL and main widget.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
11006
diff
changeset
|
1725 | return out.strip() == b"True" |
9855 | 1726 | |
1727 | def getBluetoothStatus(self): | |
1728 | """ | |
1729 | Public method to get Bluetooth status data of the connected board. | |
1730 | ||
1731 | @return list of tuples containing the translated status data label and | |
1732 | the associated value | |
1733 | @rtype list of tuples of (str, str) | |
9857 | 1734 | @exception OSError raised to indicate an issue with the device |
9855 | 1735 | """ |
1736 | command = """ | |
1737 | def ble_status(): | |
1738 | import _bleio | |
1739 | import binascii | |
1740 | import json | |
1741 | ||
1742 | a = _bleio.adapter | |
1743 | ||
1744 | ble_enabled = a.enabled | |
1745 | if not ble_enabled: | |
1746 | a.enabled = True | |
1747 | ||
1748 | res = { | |
1749 | 'active': ble_enabled, | |
1750 | 'mac': binascii.hexlify(bytes(reversed(a.address.address_bytes)), ':').decode(), | |
1751 | 'addr_type': a.address.type, | |
1752 | 'name': a.name, | |
1753 | 'advertising': a.advertising, | |
1754 | 'connected': a.connected, | |
1755 | } | |
1756 | ||
1757 | if not ble_enabled: | |
1758 | a.enabled = False | |
1759 | ||
1760 | print(json.dumps(res)) | |
1761 | ||
1762 | ble_status() | |
1763 | del ble_status | |
1764 | """ | |
9989 | 1765 | out, err = self.executeCommands(command, mode=self._submitMode) |
9855 | 1766 | if err: |
1767 | raise OSError(self._shortError(err)) | |
1768 | ||
1769 | status = [] | |
1770 | bleStatus = json.loads(out.decode("utf-8")) | |
1771 | status.append((self.tr("Active"), self.bool2str(bleStatus["active"]))) | |
1772 | status.append((self.tr("Name"), bleStatus["name"])) | |
1773 | status.append((self.tr("MAC-Address"), bleStatus["mac"])) | |
1774 | status.append( | |
1775 | (self.tr("Address Type"), self.__bleAddressType[bleStatus["addr_type"]]) | |
1776 | ) | |
1777 | status.append((self.tr("Connected"), self.bool2str(bleStatus["connected"]))) | |
1778 | status.append((self.tr("Advertising"), self.bool2str(bleStatus["advertising"]))) | |
1779 | ||
1780 | return status | |
1781 | ||
1782 | def activateBluetoothInterface(self): | |
1783 | """ | |
1784 | Public method to activate the Bluetooth interface. | |
1785 | ||
1786 | @return flag indicating the new state of the Bluetooth interface | |
1787 | @rtype bool | |
9857 | 1788 | @exception OSError raised to indicate an issue with the device |
9855 | 1789 | """ |
1790 | command = """ | |
1791 | def activate_ble(): | |
1792 | import _bleio | |
1793 | ||
1794 | a = _bleio.adapter | |
1795 | if not a.enabled: | |
1796 | a.enabled = True | |
1797 | print(a.enabled) | |
1798 | ||
1799 | activate_ble() | |
1800 | del activate_ble | |
1801 | """ | |
9989 | 1802 | out, err = self.executeCommands(command, mode=self._submitMode) |
9855 | 1803 | if err: |
1804 | raise OSError(self._shortError(err)) | |
1805 | ||
1806 | return out.strip() == b"True" | |
1807 | ||
1808 | def deactivateBluetoothInterface(self): | |
1809 | """ | |
1810 | Public method to deactivate the Bluetooth interface. | |
1811 | ||
1812 | @return flag indicating the new state of the Bluetooth interface | |
1813 | @rtype bool | |
9857 | 1814 | @exception OSError raised to indicate an issue with the device |
9855 | 1815 | """ |
1816 | command = """ | |
1817 | def deactivate_ble(): | |
1818 | import _bleio | |
1819 | ||
1820 | a = _bleio.adapter | |
1821 | if a.enabled: | |
1822 | a.enabled = False | |
1823 | print(a.enabled) | |
1824 | ||
1825 | deactivate_ble() | |
1826 | del deactivate_ble | |
1827 | """ | |
9989 | 1828 | out, err = self.executeCommands(command, mode=self._submitMode) |
9855 | 1829 | if err: |
1830 | raise OSError(self._shortError(err)) | |
1831 | ||
1832 | return out.strip() == b"True" | |
1833 | ||
9857 | 1834 | def getDeviceScan(self, timeout=10): |
1835 | """ | |
1836 | Public method to perform a Bluetooth device scan. | |
1837 | ||
1838 | @param timeout duration of the device scan in seconds (defaults | |
1839 | to 10) | |
1840 | @type int (optional) | |
1841 | @return tuple containing a dictionary with the scan results and | |
1842 | an error string | |
1843 | @rtype tuple of (dict, str) | |
1844 | """ | |
1845 | from ..BluetoothDialogs.BluetoothAdvertisement import ( | |
1846 | ADV_IND, | |
1847 | ADV_SCAN_IND, | |
1848 | SCAN_RSP, | |
1849 | BluetoothAdvertisement, | |
1850 | ) | |
1851 | ||
1852 | command = """ | |
1853 | def ble_scan(): | |
1854 | import _bleio | |
1855 | import binascii | |
1856 | import time | |
1857 | ||
1858 | a = _bleio.adapter | |
1859 | ||
1860 | ble_enabled = a.enabled | |
1861 | if not ble_enabled: | |
1862 | a.enabled = True | |
1863 | ||
1864 | scanResults = a.start_scan( | |
9859 | 1865 | buffer_size=1024, extended=True, timeout={0}, minimum_rssi=-120, active=True |
9857 | 1866 | ) |
10032
102b79b2a8cd
Finetuned the bluetooth scan function for various MicroPython boards.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9989
diff
changeset
|
1867 | time.sleep({0} + 0.2) |
9857 | 1868 | a.stop_scan() |
1869 | ||
1870 | for res in scanResults: | |
1871 | print({{ | |
1872 | 'address': binascii.hexlify( | |
1873 | bytes(reversed(res.address.address_bytes)), ':' | |
1874 | ).decode(), | |
1875 | 'advertisement': res.advertisement_bytes, | |
1876 | 'connectable': res.connectable, | |
1877 | 'rssi': res.rssi, | |
1878 | 'scan_response': res.scan_response, | |
1879 | }}) | |
1880 | ||
1881 | if not ble_enabled: | |
1882 | a.enabled = False | |
1883 | ||
1884 | ble_scan() | |
1885 | del ble_scan | |
1886 | """.format( | |
1887 | timeout | |
1888 | ) | |
9989 | 1889 | out, err = self.executeCommands( |
9857 | 1890 | command, mode=self._submitMode, timeout=(timeout + 5) * 1000 |
1891 | ) | |
1892 | if err: | |
1893 | return {}, err | |
1894 | ||
1895 | scanResults = {} | |
1896 | for line in out.decode("utf-8").splitlines(): | |
1897 | res = ast.literal_eval(line) | |
1898 | address = res["address"] | |
1899 | if address not in scanResults: | |
1900 | scanResults[address] = BluetoothAdvertisement(address) | |
1901 | if res["scan_response"]: | |
1902 | advType = SCAN_RSP | |
1903 | elif res["connectable"]: | |
1904 | advType = ADV_IND | |
1905 | else: | |
1906 | advType = ADV_SCAN_IND | |
1907 | scanResults[address].update(advType, res["rssi"], res["advertisement"]) | |
1908 | ||
1909 | return scanResults, "" | |
1910 | ||
9868 | 1911 | ################################################################## |
1912 | ## Methods below implement NTP related methods | |
1913 | ################################################################## | |
1914 | ||
1915 | def hasNetworkTime(self): | |
1916 | """ | |
1917 | Public method to check the availability of network time functions. | |
1918 | ||
1919 | @return flag indicating the availability of network time functions | |
1920 | @rtype bool | |
9870
0399d3607829
Fixed a few code formatting and style issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9868
diff
changeset
|
1921 | @exception OSError raised to indicate an issue with the device |
9868 | 1922 | """ |
1923 | command = """ | |
1924 | def has_ntp(): | |
1925 | try: | |
1926 | import adafruit_ntp | |
1927 | if hasattr(adafruit_ntp, 'NTP'): | |
1928 | return True | |
1929 | except ImportError: | |
1930 | pass | |
1931 | ||
9885 | 1932 | try: |
1933 | from adafruit_wiznet5k import adafruit_wiznet5k_ntp | |
1934 | if hasattr(adafruit_wiznet5k_ntp, 'NTP'): | |
1935 | return True | |
1936 | except ImportError: | |
1937 | pass | |
1938 | ||
9868 | 1939 | return False |
1940 | ||
1941 | print(has_ntp()) | |
1942 | del has_ntp | |
1943 | """ | |
9989 | 1944 | out, err = self.executeCommands(command, mode=self._submitMode) |
9868 | 1945 | if err: |
1946 | raise OSError(self._shortError(err)) | |
1947 | return out.strip() == b"True" | |
1948 | ||
1949 | def setNetworkTime(self, server="0.pool.ntp.org", tzOffset=0, timeout=10): | |
1950 | """ | |
1951 | Public method to set the time to the network time retrieved from an | |
1952 | NTP server. | |
1953 | ||
1954 | @param server name of the NTP server to get the network time from | |
1955 | (defaults to "0.pool.ntp.org") | |
1956 | @type str (optional) | |
1957 | @param tzOffset offset with respect to UTC (defaults to 0) | |
1958 | @type int (optional) | |
1959 | @param timeout maximum time to wait for a server response in seconds | |
1960 | (defaults to 10) | |
1961 | @type int | |
1962 | @return tuple containing a flag indicating success and an error string | |
1963 | @rtype tuple of (bool, str) | |
1964 | """ | |
9885 | 1965 | if self.getDeviceData("ethernet"): |
1966 | # WIZnet 5x00 Ethernet interface | |
1967 | # Note: The Adafruit NTP implementation does not close the socket after | |
1968 | # calling get_time(). That causes follow-on calls to fail. We | |
1969 | # close the socket in our code as a workaround. | |
1970 | command = """{0} | |
10330
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1971 | def set_ntp_time(server, tz_offset, timeout): |
9885 | 1972 | import rtc |
1973 | ||
10330
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1974 | try: |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1975 | import adafruit_ntp |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1976 | from adafruit_wiznet5k import adafruit_wiznet5k_socket as socket |
9885 | 1977 | |
10330
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1978 | w5x00_init() |
9885 | 1979 | |
10330
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1980 | socket.set_interface(nic) |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1981 | ntp = adafruit_ntp.NTP( |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1982 | socket, server=server, tz_offset=tz_offset, socket_timeout=timeout |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1983 | ) |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1984 | rtc.RTC().datetime = ntp.datetime |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1985 | return True |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1986 | except ImportError: |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1987 | from adafruit_wiznet5k import adafruit_wiznet5k_ntp |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1988 | |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1989 | w5x00_init() |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1990 | |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1991 | server_ip = nic.pretty_ip(nic.get_host_by_name(server)) |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1992 | ntp = adafruit_wiznet5k_ntp.NTP(iface=nic, ntp_address=server_ip, utc=tz_offset) |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1993 | rtc.RTC().datetime = ntp.get_time() |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1994 | ntp._sock.close() |
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1995 | return True |
9885 | 1996 | |
1997 | try: | |
1998 | print({{ | |
10330
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
1999 | 'result': set_ntp_time({1}, {2}, {3}), |
9885 | 2000 | 'error': '', |
2001 | }}) | |
2002 | except Exception as err: | |
2003 | print({{ | |
2004 | 'result': False, | |
2005 | 'error': str(err), | |
2006 | }}) | |
2007 | del set_ntp_time, w5x00_init | |
2008 | """.format( | |
10330
5ea038882dd6
MicroPython Interface
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
10329
diff
changeset
|
2009 | WiznetUtilities.cpyWiznetInit(), repr(server), tzOffset, timeout |
9885 | 2010 | ) |
2011 | ||
2012 | elif self.getDeviceData("wifi"): | |
2013 | # WiFi enabled board | |
2014 | command = """ | |
9868 | 2015 | def set_ntp_time(server, tz_offset, timeout): |
2016 | import rtc | |
2017 | import socketpool | |
2018 | import wifi | |
2019 | ||
2020 | import adafruit_ntp | |
2021 | ||
2022 | ||
2023 | r = wifi.radio | |
2024 | if r.ipv4_address is None: | |
2025 | return False | |
2026 | ||
2027 | pool = socketpool.SocketPool(r) | |
2028 | ntp = adafruit_ntp.NTP( | |
2029 | pool, server=server, tz_offset=tz_offset, socket_timeout=timeout | |
2030 | ) | |
2031 | rtc.RTC().datetime = ntp.datetime | |
2032 | return True | |
2033 | ||
2034 | try: | |
2035 | print({{ | |
2036 | 'result': set_ntp_time({0}, {1}, {2}), | |
2037 | 'error': '', | |
2038 | }}) | |
2039 | except Exception as err: | |
2040 | print({{ | |
2041 | 'result': False, | |
2042 | 'error': str(err), | |
2043 | }}) | |
2044 | del set_ntp_time | |
2045 | """.format( | |
9885 | 2046 | repr(server), tzOffset, timeout |
2047 | ) | |
2048 | ||
9989 | 2049 | out, err = self.executeCommands( |
9868 | 2050 | command, mode=self._submitMode, timeout=(timeout + 2) * 1000 |
2051 | ) | |
2052 | if err: | |
2053 | return False, err | |
2054 | else: | |
2055 | res = ast.literal_eval(out.decode("utf-8")) | |
2056 | return res["result"], res["error"] | |
2057 | ||
9885 | 2058 | ################################################################## |
2059 | ## Methods below implement some utility methods | |
2060 | ################################################################## | |
2061 | ||
2062 | def __modifySettings(self, changedEntries): | |
2063 | """ | |
2064 | Private method to modify the 'settings.toml' file as of CircuitPython 8.0.0. | |
2065 | ||
2066 | @param changedEntries dictionary containing the TOML entries to be changed | |
2067 | @type dict of {str: str} | |
2068 | @return tuple containing a success flag and an error message | |
2069 | @rtype tuple of (bool, str) | |
2070 | """ | |
2071 | workspace = self.getWorkspace() | |
2072 | filename = os.path.join(workspace, "settings.toml") | |
2073 | if os.path.exists(filename): | |
2074 | try: | |
2075 | with open(filename, "r") as f: | |
2076 | lines = f.read().splitlines() | |
2077 | except OSError as err: | |
2078 | return False, str(err) | |
2079 | else: | |
2080 | lines = [] | |
2081 | ||
2082 | for key, value in changedEntries.items(): | |
2083 | newLine = "{0} = {1}".format(key, value) | |
2084 | for row in range(len(lines)): | |
2085 | if lines[row].split("=")[0].strip() == key: | |
2086 | if value == "": | |
2087 | del lines[row] | |
2088 | else: | |
2089 | lines[row] = newLine | |
2090 | break | |
2091 | else: | |
2092 | if value != "": | |
2093 | lines.append(newLine) | |
2094 | ||
2095 | try: | |
2096 | with open(filename, "w") as f: | |
2097 | f.write("\n".join(lines)) | |
2098 | except OSError as err: | |
2099 | return False, str(err) | |
2100 | ||
2101 | return True, "" | |
2102 | ||
9496
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
2103 | |
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
|
2104 | 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:
9482
diff
changeset
|
2105 | """ |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
2106 | Function to instantiate a MicroPython device object. |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
2107 | |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
2108 | @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:
9482
diff
changeset
|
2109 | @type MicroPythonWidget |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
2110 | @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:
9482
diff
changeset
|
2111 | @type str |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
2112 | @param vid vendor ID |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
2113 | @type int |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
2114 | @param pid product ID |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
2115 | @type int |
9738 | 2116 | @param boardName name of the board |
2117 | @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
|
2118 | @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
|
2119 | @type str |
9496
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
2120 | @return reference to the instantiated device object |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
2121 | @rtype CircuitPythonDevice |
05017f795c24
Changed MicroPython device imports to use importlib.import_module().
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9482
diff
changeset
|
2122 | """ |
9885 | 2123 | return CircuitPythonDevice( |
2124 | microPythonWidget, deviceType, boardName, vid=vid, pid=pid | |
2125 | ) |