src/eric7/MicroPython/Devices/CircuitPythonDevices.py

branch
mpy_network
changeset 9857
0122ae72618d
parent 9855
c9244db5566a
child 9858
6518c336fcd3
equal deleted inserted replaced
9856:df2ff78bbc01 9857:0122ae72618d
1151 """ 1151 """
1152 Public method to check the availability of Bluetooth. 1152 Public method to check the availability of Bluetooth.
1153 1153
1154 @return flag indicating the availability of Bluetooth 1154 @return flag indicating the availability of Bluetooth
1155 @rtype bool 1155 @rtype bool
1156 @exception OSError raised to indicate an issue with the device
1156 """ 1157 """
1157 command = """ 1158 command = """
1158 def has_bt(): 1159 def has_bt():
1159 try: 1160 try:
1160 import _bleio 1161 import _bleio
1180 Public method to get Bluetooth status data of the connected board. 1181 Public method to get Bluetooth status data of the connected board.
1181 1182
1182 @return list of tuples containing the translated status data label and 1183 @return list of tuples containing the translated status data label and
1183 the associated value 1184 the associated value
1184 @rtype list of tuples of (str, str) 1185 @rtype list of tuples of (str, str)
1186 @exception OSError raised to indicate an issue with the device
1185 """ 1187 """
1186 command = """ 1188 command = """
1187 def ble_status(): 1189 def ble_status():
1188 import _bleio 1190 import _bleio
1189 import binascii 1191 import binascii
1233 """ 1235 """
1234 Public method to activate the Bluetooth interface. 1236 Public method to activate the Bluetooth interface.
1235 1237
1236 @return flag indicating the new state of the Bluetooth interface 1238 @return flag indicating the new state of the Bluetooth interface
1237 @rtype bool 1239 @rtype bool
1240 @exception OSError raised to indicate an issue with the device
1238 """ 1241 """
1239 command = """ 1242 command = """
1240 def activate_ble(): 1243 def activate_ble():
1241 import _bleio 1244 import _bleio
1242 1245
1258 """ 1261 """
1259 Public method to deactivate the Bluetooth interface. 1262 Public method to deactivate the Bluetooth interface.
1260 1263
1261 @return flag indicating the new state of the Bluetooth interface 1264 @return flag indicating the new state of the Bluetooth interface
1262 @rtype bool 1265 @rtype bool
1266 @exception OSError raised to indicate an issue with the device
1263 """ 1267 """
1264 command = """ 1268 command = """
1265 def deactivate_ble(): 1269 def deactivate_ble():
1266 import _bleio 1270 import _bleio
1267 1271
1276 out, err = self._interface.execute(command, mode=self._submitMode) 1280 out, err = self._interface.execute(command, mode=self._submitMode)
1277 if err: 1281 if err:
1278 raise OSError(self._shortError(err)) 1282 raise OSError(self._shortError(err))
1279 1283
1280 return out.strip() == b"True" 1284 return out.strip() == b"True"
1285
1286 def getDeviceScan(self, timeout=10):
1287 """
1288 Public method to perform a Bluetooth device scan.
1289
1290 @param timeout duration of the device scan in seconds (defaults
1291 to 10)
1292 @type int (optional)
1293 @return tuple containing a dictionary with the scan results and
1294 an error string
1295 @rtype tuple of (dict, str)
1296 """
1297 from ..BluetoothDialogs.BluetoothAdvertisement import (
1298 ADV_IND,
1299 ADV_SCAN_IND,
1300 SCAN_RSP,
1301 BluetoothAdvertisement,
1302 )
1303
1304 command = """
1305 def ble_scan():
1306 import _bleio
1307 import binascii
1308 import time
1309
1310 a = _bleio.adapter
1311
1312 ble_enabled = a.enabled
1313 if not ble_enabled:
1314 a.enabled = True
1315
1316 scanResults = a.start_scan(
1317 buffer_size=1024, extended=True, timeout={0}, minimum_rssi=-100, active=True
1318 )
1319 time.sleep(10)
1320 a.stop_scan()
1321
1322 for res in scanResults:
1323 print({{
1324 'address': binascii.hexlify(
1325 bytes(reversed(res.address.address_bytes)), ':'
1326 ).decode(),
1327 'advertisement': res.advertisement_bytes,
1328 'connectable': res.connectable,
1329 'rssi': res.rssi,
1330 'scan_response': res.scan_response,
1331 }})
1332
1333 if not ble_enabled:
1334 a.enabled = False
1335
1336 ble_scan()
1337 del ble_scan
1338 """.format(
1339 timeout
1340 )
1341 out, err = self._interface.execute(
1342 command, mode=self._submitMode, timeout=(timeout + 5) * 1000
1343 )
1344 if err:
1345 return {}, err
1346
1347 scanResults = {}
1348 for line in out.decode("utf-8").splitlines():
1349 res = ast.literal_eval(line)
1350 address = res["address"]
1351 if address not in scanResults:
1352 scanResults[address] = BluetoothAdvertisement(address)
1353 if res["scan_response"]:
1354 advType = SCAN_RSP
1355 elif res["connectable"]:
1356 advType = ADV_IND
1357 else:
1358 advType = ADV_SCAN_IND
1359 scanResults[address].update(advType, res["rssi"], res["advertisement"])
1360
1361 return scanResults, ""
1281 1362
1282 1363
1283 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): 1364 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber):
1284 """ 1365 """
1285 Function to instantiate a MicroPython device object. 1366 Function to instantiate a MicroPython device object.

eric ide

mercurial