src/eric7/MicroPython/Devices/CircuitPythonDevices.py

branch
mpy_network
changeset 9855
c9244db5566a
parent 9841
3c6118eee33e
child 9857
0122ae72618d
equal deleted inserted replaced
9854:c1e298e5c588 9855:c9244db5566a
80 3: "[wifi.AuthMode.WPA2, wifi.AuthMode.PSK]", 80 3: "[wifi.AuthMode.WPA2, wifi.AuthMode.PSK]",
81 4: "[wifi.AuthMode.WPA, wifi.AuthMode.WPA2, wifi.AuthMode.PSK]", 81 4: "[wifi.AuthMode.WPA, wifi.AuthMode.WPA2, wifi.AuthMode.PSK]",
82 5: "[wifi.AuthMode.WPA2, wifi.AuthMode.ENTERPRISE]", 82 5: "[wifi.AuthMode.WPA2, wifi.AuthMode.ENTERPRISE]",
83 6: "[wifi.AuthMode.WPA3, wifi.AuthMode.PSK]", 83 6: "[wifi.AuthMode.WPA3, wifi.AuthMode.PSK]",
84 7: "[wifi.AuthMode.WPA2, wifi.AuthMode.WPA3, wifi.AuthMode.PSK]", 84 7: "[wifi.AuthMode.WPA2, wifi.AuthMode.WPA3, wifi.AuthMode.PSK]",
85 }
86 self.__bleAddressType = {
87 0: self.tr("Public"),
88 1: self.tr("Random Static"),
89 2: self.tr("Random Private Resolvable"),
90 3: self.tr("Random Private Non-Resolvable"),
85 } 91 }
86 92
87 def setConnected(self, connected): 93 def setConnected(self, connected):
88 """ 94 """
89 Public method to set the connection state. 95 Public method to set the connection state.
1135 return ( 1141 return (
1136 [], 1142 [],
1137 self.tr("CircuitPython does not support reporting of connected clients."), 1143 self.tr("CircuitPython does not support reporting of connected clients."),
1138 ) 1144 )
1139 1145
1146 ##################################################################
1147 ## Methods below implement Bluetooth related methods
1148 ##################################################################
1149
1150 def hasBluetooth(self):
1151 """
1152 Public method to check the availability of Bluetooth.
1153
1154 @return flag indicating the availability of Bluetooth
1155 @rtype bool
1156 """
1157 command = """
1158 def has_bt():
1159 try:
1160 import _bleio
1161 if hasattr(_bleio, 'adapter'):
1162 return True
1163 except ImportError:
1164 pass
1165
1166 return False
1167
1168 print(has_bt())
1169 del has_bt
1170 """
1171 out, err = self._interface.execute(
1172 command, mode=self._submitMode, timeout=10000
1173 )
1174 if err:
1175 raise OSError(self._shortError(err))
1176 return out.strip() == b"True"
1177
1178 def getBluetoothStatus(self):
1179 """
1180 Public method to get Bluetooth status data of the connected board.
1181
1182 @return list of tuples containing the translated status data label and
1183 the associated value
1184 @rtype list of tuples of (str, str)
1185 """
1186 command = """
1187 def ble_status():
1188 import _bleio
1189 import binascii
1190 import json
1191
1192 a = _bleio.adapter
1193
1194 ble_enabled = a.enabled
1195 if not ble_enabled:
1196 a.enabled = True
1197
1198 res = {
1199 'active': ble_enabled,
1200 'mac': binascii.hexlify(bytes(reversed(a.address.address_bytes)), ':').decode(),
1201 'addr_type': a.address.type,
1202 'name': a.name,
1203 'advertising': a.advertising,
1204 'connected': a.connected,
1205 }
1206
1207 if not ble_enabled:
1208 a.enabled = False
1209
1210 print(json.dumps(res))
1211
1212 ble_status()
1213 del ble_status
1214 """
1215 out, err = self._interface.execute(command, mode=self._submitMode)
1216 if err:
1217 raise OSError(self._shortError(err))
1218
1219 status = []
1220 bleStatus = json.loads(out.decode("utf-8"))
1221 status.append((self.tr("Active"), self.bool2str(bleStatus["active"])))
1222 status.append((self.tr("Name"), bleStatus["name"]))
1223 status.append((self.tr("MAC-Address"), bleStatus["mac"]))
1224 status.append(
1225 (self.tr("Address Type"), self.__bleAddressType[bleStatus["addr_type"]])
1226 )
1227 status.append((self.tr("Connected"), self.bool2str(bleStatus["connected"])))
1228 status.append((self.tr("Advertising"), self.bool2str(bleStatus["advertising"])))
1229
1230 return status
1231
1232 def activateBluetoothInterface(self):
1233 """
1234 Public method to activate the Bluetooth interface.
1235
1236 @return flag indicating the new state of the Bluetooth interface
1237 @rtype bool
1238 """
1239 command = """
1240 def activate_ble():
1241 import _bleio
1242
1243 a = _bleio.adapter
1244 if not a.enabled:
1245 a.enabled = True
1246 print(a.enabled)
1247
1248 activate_ble()
1249 del activate_ble
1250 """
1251 out, err = self._interface.execute(command, mode=self._submitMode)
1252 if err:
1253 raise OSError(self._shortError(err))
1254
1255 return out.strip() == b"True"
1256
1257 def deactivateBluetoothInterface(self):
1258 """
1259 Public method to deactivate the Bluetooth interface.
1260
1261 @return flag indicating the new state of the Bluetooth interface
1262 @rtype bool
1263 """
1264 command = """
1265 def deactivate_ble():
1266 import _bleio
1267
1268 a = _bleio.adapter
1269 if a.enabled:
1270 a.enabled = False
1271 print(a.enabled)
1272
1273 deactivate_ble()
1274 del deactivate_ble
1275 """
1276 out, err = self._interface.execute(command, mode=self._submitMode)
1277 if err:
1278 raise OSError(self._shortError(err))
1279
1280 return out.strip() == b"True"
1281
1140 1282
1141 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): 1283 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber):
1142 """ 1284 """
1143 Function to instantiate a MicroPython device object. 1285 Function to instantiate a MicroPython device object.
1144 1286

eric ide

mercurial