src/eric7/MicroPython/Devices/EspDevices.py

branch
mpy_network
changeset 9855
c9244db5566a
parent 9848
3d750b2e012c
child 9857
0122ae72618d
equal deleted inserted replaced
9854:c1e298e5c588 9855:c9244db5566a
1036 return [], err 1036 return [], err
1037 1037
1038 clientsList = ast.literal_eval(out.decode("utf-8")) 1038 clientsList = ast.literal_eval(out.decode("utf-8"))
1039 return clientsList, "" 1039 return clientsList, ""
1040 1040
1041 ##################################################################
1042 ## Methods below implement Bluetooth related methods
1043 ##################################################################
1044
1045 def hasBluetooth(self):
1046 """
1047 Public method to check the availability of Bluetooth.
1048
1049 @return flag indicating the availability of Bluetooth
1050 @rtype bool
1051 """
1052 command = """
1053 def has_bt():
1054 try:
1055 import bluetooth
1056 if hasattr(bluetooth, 'BLE'):
1057 return True
1058 except ImportError:
1059 pass
1060
1061 return False
1062
1063 print(has_bt())
1064 del has_bt
1065 """
1066 out, err = self._interface.execute(
1067 command, mode=self._submitMode, timeout=10000
1068 )
1069 if err:
1070 raise OSError(self._shortError(err))
1071 return out.strip() == b"True"
1072
1073 def getBluetoothStatus(self):
1074 """
1075 Public method to get Bluetooth status data of the connected board.
1076
1077 @return list of tuples containing the translated status data label and
1078 the associated value
1079 @rtype list of tuples of (str, str)
1080 """
1081 command = """
1082 def ble_status():
1083 import bluetooth
1084 import ubinascii
1085 import ujson
1086
1087 ble = bluetooth.BLE()
1088
1089 ble_active = ble.active()
1090 if not ble_active:
1091 ble.active(True)
1092
1093 res = {
1094 'active': ble_active,
1095 'mac': ubinascii.hexlify(ble.config('mac')[1], ':').decode(),
1096 'addr_type': ble.config('mac')[0],
1097 'name': ble.config('gap_name'),
1098 'rxbuf': ble.config('rxbuf'),
1099 'mtu': ble.config('mtu'),
1100 }
1101
1102 if not ble_active:
1103 ble.active(False)
1104
1105 print(ujson.dumps(res))
1106
1107 ble_status()
1108 del ble_status
1109 """
1110 out, err = self._interface.execute(command, mode=self._submitMode)
1111 if err:
1112 raise OSError(self._shortError(err))
1113
1114 status = []
1115 bleStatus = json.loads(out.decode("utf-8"))
1116 status.append((self.tr("Active"), self.bool2str(bleStatus["active"])))
1117 status.append((self.tr("Name"), bleStatus["name"]))
1118 status.append((self.tr("MAC-Address"), bleStatus["mac"]))
1119 status.append(
1120 (
1121 self.tr("Address Type"),
1122 self.tr("Public") if bleStatus == 0 else self.tr("Random"),
1123 )
1124 )
1125 status.append(
1126 (self.tr("Rx-Buffer"), self.tr("{0} Bytes").format(bleStatus["rxbuf"]))
1127 )
1128 status.append(
1129 (self.tr("MTU"), self.tr("{0} Bytes").format(bleStatus["mtu"]))
1130 )
1131
1132 return status
1133
1134 def activateBluetoothInterface(self):
1135 """
1136 Public method to activate the Bluetooth interface.
1137
1138 @return flag indicating the new state of the Bluetooth interface
1139 @rtype bool
1140 """
1141 command = """
1142 def activate_ble():
1143 import bluetooth
1144
1145 ble = bluetooth.BLE()
1146 if not ble.active():
1147 ble.active(True)
1148 print(ble.active())
1149
1150 activate_ble()
1151 del activate_ble
1152 """
1153 out, err = self._interface.execute(command, mode=self._submitMode)
1154 if err:
1155 raise OSError(self._shortError(err))
1156
1157 return out.strip() == b"True"
1158
1159 def deactivateBluetoothInterface(self):
1160 """
1161 Public method to deactivate the Bluetooth interface.
1162
1163 @return flag indicating the new state of the Bluetooth interface
1164 @rtype bool
1165 """
1166 command = """
1167 def deactivate_ble():
1168 import bluetooth
1169
1170 ble = bluetooth.BLE()
1171 if ble.active():
1172 ble.active(False)
1173 print(ble.active())
1174
1175 deactivate_ble()
1176 del deactivate_ble
1177 """
1178 out, err = self._interface.execute(command, mode=self._submitMode)
1179 if err:
1180 raise OSError(self._shortError(err))
1181
1182 return out.strip() == b"True"
1183
1041 1184
1042 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): 1185 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber):
1043 """ 1186 """
1044 Function to instantiate a MicroPython device object. 1187 Function to instantiate a MicroPython device object.
1045 1188

eric ide

mercurial