src/eric7/MicroPython/Devices/DeviceBase.py

branch
eric7
changeset 10173
9c64ac720853
parent 10153
ffe7432f716b
child 10230
1311cd5d117e
equal deleted inserted replaced
10172:7367a211d865 10173:9c64ac720853
140 self._deviceData = {} 140 self._deviceData = {}
141 141
142 if connected: 142 if connected:
143 self._interface = self.microPython.deviceInterface() 143 self._interface = self.microPython.deviceInterface()
144 with contextlib.suppress(OSError): 144 with contextlib.suppress(OSError):
145 self._deviceData = self.__getDeviceData() 145 data = self.__getDeviceData()
146 self._deviceData["local_mip"] = ( 146 if "mpy_name" in data:
147 not self._deviceData["mip"] 147 self._deviceData = data
148 and not self._deviceData["upip"] 148 self._deviceData["local_mip"] = (
149 and not self.hasCircuitPython() 149 not self._deviceData["mip"]
150 ) 150 and not self._deviceData["upip"]
151 self._deviceData["wifi"], self._deviceData["wifi_type"] = self.hasWifi() 151 and not self.hasCircuitPython()
152 self._deviceData["bluetooth"] = self.hasBluetooth() 152 )
153 ( 153 (
154 self._deviceData["ethernet"], 154 self._deviceData["wifi"],
155 self._deviceData["ethernet_type"], 155 self._deviceData["wifi_type"],
156 ) = self.hasEthernet() 156 ) = self.hasWifi()
157 self._deviceData["ntp"] = self.hasNetworkTime() 157 self._deviceData["bluetooth"] = self.hasBluetooth()
158 (
159 self._deviceData["ethernet"],
160 self._deviceData["ethernet_type"],
161 ) = self.hasEthernet()
162 self._deviceData["ntp"] = self.hasNetworkTime()
163 else:
164 self._deviceData = {}
158 else: 165 else:
159 self._interface = None 166 self._interface = None
160 167
161 def getDeviceType(self): 168 def getDeviceType(self):
162 """ 169 """
223 Public method to check, if the connected device is flashed with CircuitPython. 230 Public method to check, if the connected device is flashed with CircuitPython.
224 231
225 @return flag indicating CircuitPython 232 @return flag indicating CircuitPython
226 @rtype bool 233 @rtype bool
227 """ 234 """
228 return ( 235 try:
229 self.checkDeviceData() 236 return (
230 and self._deviceData["mpy_name"].lower() == "circuitpython" 237 self.checkDeviceData()
231 ) 238 and self._deviceData["mpy_name"].lower() == "circuitpython"
239 )
240 except KeyError:
241 return False
232 242
233 def submitMode(self): 243 def submitMode(self):
234 """ 244 """
235 Public method to get the submit mode of the device. 245 Public method to get the submit mode of the device.
236 246
1124 1134
1125 @return dictionary containing the determined data 1135 @return dictionary containing the determined data
1126 @rtype dict 1136 @rtype dict
1127 @exception OSError raised to indicate an issue with the device 1137 @exception OSError raised to indicate an issue with the device
1128 """ 1138 """
1129 command = """ 1139 commands = [ # needs to be splitted for boards with low memory
1130 def get_board_info(): 1140 """def get_board_info():
1131 res = {} 1141 res = {}
1132 1142
1133 import gc 1143 import gc
1134 gc.enable() 1144 gc.enable()
1135 gc.collect() 1145 gc.collect()
1140 res['mem_used_kb'] = mem_alloc / 1024.0 1150 res['mem_used_kb'] = mem_alloc / 1024.0
1141 res['mem_used_pc'] = mem_alloc / mem_total * 100.0 1151 res['mem_used_pc'] = mem_alloc / mem_total * 100.0
1142 res['mem_free_kb'] = mem_free / 1024.0 1152 res['mem_free_kb'] = mem_free / 1024.0
1143 res['mem_free_pc'] = mem_free / mem_total * 100.0 1153 res['mem_free_pc'] = mem_free / mem_total * 100.0
1144 del gc, mem_alloc, mem_free, mem_total 1154 del gc, mem_alloc, mem_free, mem_total
1155
1156 return res
1157
1158 print(get_board_info())
1159 del get_board_info
1160 """,
1161 """def get_board_info():
1162 res = {}
1145 1163
1146 try: 1164 try:
1147 import os 1165 import os
1148 uname = os.uname() 1166 uname = os.uname()
1149 res['sysname'] = uname.sysname 1167 res['sysname'] = uname.sysname
1156 res['sysname'] = sys.platform 1174 res['sysname'] = sys.platform
1157 res['nodename'] = sys.platform 1175 res['nodename'] = sys.platform
1158 res['release'] = '.'.join(str(v) for v in sys.implementation.version) 1176 res['release'] = '.'.join(str(v) for v in sys.implementation.version)
1159 res['version'] = sys.version.split(';', 1)[-1].strip() 1177 res['version'] = sys.version.split(';', 1)[-1].strip()
1160 res['machine'] = sys.implementation._machine 1178 res['machine'] = sys.implementation._machine
1179
1180 return res
1181
1182 print(get_board_info())
1183 del get_board_info
1184 """,
1185 """def get_board_info():
1186 res = {}
1161 1187
1162 import sys 1188 import sys
1163 res['py_platform'] = sys.platform 1189 res['py_platform'] = sys.platform
1164 res['py_version'] = sys.version 1190 res['py_version'] = sys.version
1165 1191
1184 except ImportError: 1210 except ImportError:
1185 res['mpy_variant'] = '' 1211 res['mpy_variant'] = ''
1186 res['mpy_variant_info'] = '' 1212 res['mpy_variant_info'] = ''
1187 res['mpy_variant_version'] = '' 1213 res['mpy_variant_version'] = ''
1188 1214
1215 return res
1216
1217 print(get_board_info())
1218 del get_board_info
1219 """,
1220 """def get_board_info():
1221 res = {}
1222
1189 try: 1223 try:
1224 import os
1190 stat_ = os.statvfs('/flash') 1225 stat_ = os.statvfs('/flash')
1191 res['flash_info_available'] = True 1226 res['flash_info_available'] = True
1192 res['flash_total_kb'] = stat_[2] * stat_[0] / 1024.0 1227 res['flash_total_kb'] = stat_[2] * stat_[0] / 1024.0
1193 res['flash_free_kb'] = stat_[3] * stat_[0] / 1024.0 1228 res['flash_free_kb'] = stat_[3] * stat_[0] / 1024.0
1194 res['flash_used_kb'] = res['flash_total_kb'] - res['flash_free_kb'] 1229 res['flash_used_kb'] = res['flash_total_kb'] - res['flash_free_kb']
1195 res['flash_free_pc'] = res['flash_free_kb'] / res['flash_total_kb'] * 100.0 1230 res['flash_free_pc'] = res['flash_free_kb'] / res['flash_total_kb'] * 100.0
1196 res['flash_used_pc'] = res['flash_used_kb'] / res['flash_total_kb'] * 100.0 1231 res['flash_used_pc'] = res['flash_used_kb'] / res['flash_total_kb'] * 100.0
1197 except (AttributeError, OSError): 1232 except (AttributeError, OSError):
1198 res['flash_info_available'] = False 1233 res['flash_info_available'] = False
1234
1235 return res
1236
1237 print(get_board_info())
1238 del get_board_info
1239 """,
1240 """def get_board_info():
1241 res = {}
1199 1242
1200 try: 1243 try:
1201 import machine as mc 1244 import machine as mc
1202 if isinstance(mc.freq(), tuple): 1245 if isinstance(mc.freq(), tuple):
1203 res['mc_frequency_mhz'] = mc.freq()[0] / 1000000.0 1246 res['mc_frequency_mhz'] = mc.freq()[0] / 1000000.0
1214 res['mc_frequency'] = None 1257 res['mc_frequency'] = None
1215 res['mc_temp'] = None 1258 res['mc_temp'] = None
1216 if 'mc_id' in res: 1259 if 'mc_id' in res:
1217 res['mc_id'] = ':'.join('{0:02X}'.format(x) for x in res['mc_id']) 1260 res['mc_id'] = ':'.join('{0:02X}'.format(x) for x in res['mc_id'])
1218 1261
1262 return res
1263
1264 print(get_board_info())
1265 del get_board_info
1266 """,
1267 """def get_board_info():
1268 res = {}
1269
1219 try: 1270 try:
1220 import ulab 1271 import ulab
1221 res['ulab'] = ulab.__version__ 1272 res['ulab'] = ulab.__version__
1222 except ImportError: 1273 except ImportError:
1223 res['ulab'] = None 1274 res['ulab'] = None
1224 1275
1225 return res 1276 return res
1226 1277
1227 print(get_board_info()) 1278 print(get_board_info())
1228 del get_board_info 1279 del get_board_info
1229 """ 1280 """,
1230 out, err = self.executeCommands(command, mode=self._submitMode) 1281 ]
1231 if err: 1282 res = {}
1232 raise OSError(self._shortError(err)) 1283 for command in commands:
1233 return ast.literal_eval(out.decode("utf-8")) 1284 out, err = self.executeCommands(command, mode=self._submitMode)
1285 if err:
1286 raise OSError(self._shortError(err))
1287 res.update(ast.literal_eval(out.decode("utf-8")))
1288 return res
1234 1289
1235 def getModules(self): 1290 def getModules(self):
1236 """ 1291 """
1237 Public method to show a list of modules built into the firmware. 1292 Public method to show a list of modules built into the firmware.
1238 1293

eric ide

mercurial