src/eric7/MicroPython/Devices/DeviceBase.py

branch
eric7
changeset 11177
f511038a0061
parent 11167
a3f5af773bc7
child 11183
553b50b72f5b
--- a/src/eric7/MicroPython/Devices/DeviceBase.py	Tue Mar 18 18:32:29 2025 +0100
+++ b/src/eric7/MicroPython/Devices/DeviceBase.py	Thu Mar 20 11:54:57 2025 +0100
@@ -1065,8 +1065,8 @@
         @rtype dict
         @exception OSError raised to indicate an issue with the device
         """
-        command = """
-def get_device_data():
+        commands = [  # needs to be splitted for boards with low memory
+            """def get_device_data():
     res = {}
 
     try:
@@ -1084,6 +1084,13 @@
         res['release'] = '.'.join(str(v) for v in sys.implementation.version)
         res['version'] = sys.version.split(';', 1)[-1].strip()
         res['machine'] = sys.implementation._machine
+    return res
+
+print(get_device_data())
+del get_device_data
+""",
+            """def get_device_data():
+    res = {}
 
     import sys
     res['py_platform'] = sys.platform
@@ -1105,6 +1112,13 @@
         res['mpy_file_version'] = sys.implementation.mpy & 0xff
     else:
         res['mpy_file_version'] = 0
+    return res
+
+print(get_device_data())
+del get_device_data
+""",
+            """def get_device_data():
+    res = {}
 
     try:
         import pimoroni
@@ -1120,6 +1134,13 @@
         res['mpy_variant'] = ''
         res['mpy_variant_info'] = ''
         res['mpy_variant_version'] = ''
+    return res
+
+print(get_device_data())
+del get_device_data
+""",
+            """def get_device_data():
+    res = {}
 
     res['mip'] = False
     res['upip'] = False
@@ -1144,10 +1165,14 @@
 print(get_device_data())
 del get_device_data
 """
-        out, err = self.executeCommands(command, mode=self._submitMode)
-        if err:
-            raise OSError(self._shortError(err))
-        return ast.literal_eval(out.decode("utf-8"))
+        ]
+        res = {}
+        for command in commands:
+            out, err = self.executeCommands(command, mode=self._submitMode)
+            if err:
+                raise OSError(self._shortError(err))
+            res.update(ast.literal_eval(out.decode("utf-8")))
+        return res
 
     def getBoardInformation(self):
         """
@@ -1161,27 +1186,6 @@
             """def get_board_info():
     res = {}
 
-    import gc
-    gc.enable()
-    gc.collect()
-    mem_alloc = gc.mem_alloc()
-    mem_free = gc.mem_free()
-    mem_total = mem_alloc + mem_free
-    res['mem_total_kb'] = mem_total / 1024.0
-    res['mem_used_kb'] = mem_alloc / 1024.0
-    res['mem_used_pc'] = mem_alloc / mem_total * 100.0
-    res['mem_free_kb'] = mem_free / 1024.0
-    res['mem_free_pc'] = mem_free / mem_total * 100.0
-    del gc, mem_alloc, mem_free, mem_total
-
-    return res
-
-print(get_board_info())
-del get_board_info
-""",
-            """def get_board_info():
-    res = {}
-
     try:
         import os
         uname = os.uname()
@@ -1242,60 +1246,6 @@
     res = {}
 
     try:
-        import os
-        stat_ = os.statvfs('/flash')
-        res['flash_info_available'] = True
-        res['flash_total_kb'] = stat_[2] * stat_[0] / 1024.0
-        res['flash_free_kb'] = stat_[3] * stat_[0] / 1024.0
-        res['flash_used_kb'] = res['flash_total_kb'] - res['flash_free_kb']
-        res['flash_free_pc'] = res['flash_free_kb'] / res['flash_total_kb'] * 100.0
-        res['flash_used_pc'] = res['flash_used_kb'] / res['flash_total_kb'] * 100.0
-    except (AttributeError, OSError):
-        res['flash_info_available'] = False
-
-    return res
-
-print(get_board_info())
-del get_board_info
-""",
-            """def get_board_info():
-    res = {}
-
-    try:
-        import machine as mc
-        try:
-            if isinstance(mc.freq(), tuple):
-                res['mc_frequency_mhz'] = mc.freq()[0] / 1000000.0
-            else:
-               res['mc_frequency_mhz'] = mc.freq() / 1000000.0
-        except NotImplementedError:
-            res['mc_frequency_mhz'] = None
-        try:
-            res['mc_temp_c'] = mc.Temp().read()
-        except AttributeError:
-            pass
-        res['mc_id'] = mc.unique_id()
-    except ImportError:
-        try:
-            import microcontroller as mc
-            res['mc_frequency_mhz'] = mc.cpu.frequency / 1000000.0
-            res['mc_temp_c'] = mc.cpu.temperature
-            res['mc_id'] = mc.cpu.uid
-        except ImportError:
-            res['mc_frequency_mhz'] = None
-            res['mc_temp_c'] = None
-    if 'mc_id' in res:
-        res['mc_id'] = ':'.join('{0:02X}'.format(x) for x in res['mc_id'])
-
-    return res
-
-print(get_board_info())
-del get_board_info
-""",
-            """def get_board_info():
-    res = {}
-
-    try:
         import ulab
         res['ulab'] = ulab.__version__
     except ImportError:
@@ -1307,12 +1257,124 @@
 del get_board_info
 """,
         ]
+
+        gc_command = """def get_board_info():
+    res = {}
+
+    import gc
+    gc.enable()
+    gc.collect()
+    mem_alloc = gc.mem_alloc()
+    mem_free = gc.mem_free()
+    mem_total = mem_alloc + mem_free
+    res['mem_total'] = mem_total
+    res['mem_used'] = mem_alloc
+    res['mem_free'] = mem_free
+    del gc, mem_alloc, mem_free, mem_total
+
+    return res
+
+print(get_board_info())
+del get_board_info
+"""
+
+        flash_command = """def get_board_info():
+    res = {}
+
+    try:
+        import os
+        stat_ = os.statvfs('/flash')
+        res['flash_info_available'] = True
+        res['flash_total'] = stat_[2] * stat_[0]
+        res['flash_free'] = stat_[3] * stat_[0]
+    except (AttributeError, OSError):
+        res['flash_info_available'] = False
+
+    return res
+
+print(get_board_info())
+del get_board_info
+"""
+
+        machine_command = """def get_board_info():
+    res = {}
+
+    try:
+        import machine as mc
+        try:
+            if isinstance(mc.freq(), tuple):
+                res['mc_frequency'] = mc.freq()[0]
+            else:
+               res['mc_frequency'] = mc.freq()
+        except NotImplementedError:
+            res['mc_frequency'] = None
+        try:
+            res['mc_temp_c'] = mc.Temp().read()
+        except AttributeError:
+            res['mc_temp_c'] = None
+        res['mc_id'] = mc.unique_id()
+    except ImportError:
+        try:
+            import microcontroller as mc
+            res['mc_frequency'] = mc.cpu.frequency
+            res['mc_temp_c'] = mc.cpu.temperature
+            res['mc_id'] = mc.cpu.uid
+        except ImportError:
+            res['mc_frequency'] = None
+            res['mc_temp_c'] = None
+    if 'mc_id' in res:
+        res['mc_id'] = ':'.join('{0:02X}'.format(x) for x in res['mc_id'])
+    else:
+        res['mc_id'] = None
+
+    return res
+
+print(get_board_info())
+del get_board_info
+"""
         res = {}
         for command in commands:
             out, err = self.executeCommands(command, mode=self._submitMode)
             if err:
                 raise OSError(self._shortError(err))
             res.update(ast.literal_eval(out.decode("utf-8")))
+
+        # Execute the commands needing some special postprocessing due to some
+        # boards supporting integer only.
+        out, err = self.executeCommands(gc_command, mode=self._submitMode)
+        if err:
+            raise OSError(self._shortError(err))
+        gc_res = ast.literal_eval(out.decode("utf-8"))
+        res['mem_total_kb'] = gc_res["mem_total"] / 1024.0
+        res['mem_used_kb'] = gc_res["mem_used"] / 1024.0
+        res['mem_used_pc'] = gc_res["mem_used"] / gc_res["mem_total"] * 100.0
+        res['mem_free_kb'] = gc_res["mem_free"] / 1024.0
+        res['mem_free_pc'] = gc_res["mem_free"] / gc_res["mem_total"] * 100.0
+
+        out, err = self.executeCommands(flash_command, mode=self._submitMode)
+        if err:
+            raise OSError(self._shortError(err))
+        flash_res = ast.literal_eval(out.decode("utf-8"))
+        res['flash_info_available'] = flash_res["flash_info_available"]
+        if flash_res["flash_info_available"]:
+            res['flash_total_kb'] = flash_res["flash_total"] / 1024.0
+            res['flash_free_kb'] = flash_res["flash_free"] / 1024.0
+            res['flash_used_kb'] = res['flash_total_kb'] - res['flash_free_kb']
+            res['flash_free_pc'] = res['flash_free_kb'] / res['flash_total_kb'] * 100.0
+            res['flash_used_pc'] = res['flash_used_kb'] / res['flash_total_kb'] * 100.0
+
+        out, err = self.executeCommands(machine_command, mode=self._submitMode)
+        if err:
+            raise OSError(self._shortError(err))
+        machine_res = ast.literal_eval(out.decode("utf-8"))
+        res['mc_frequency_mhz'] = (
+            machine_res["mc_frequency"] / 1000000.0
+            if machine_res["mc_frequency"] is not None
+            else None
+        )
+        res["mc_temp_c"] = machine_res["mc_temp_c"]
+        res["mc_id"] = machine_res["mc_id"]
+
         return res
 
     def getModules(self):

eric ide

mercurial