src/eric7/MicroPython/MicroPythonCommandsInterface.py

branch
eric7
changeset 9749
5d409223cf3f
parent 9748
df9520c864f2
child 9751
606ac0e26533
--- a/src/eric7/MicroPython/MicroPythonCommandsInterface.py	Wed Feb 08 11:54:36 2023 +0100
+++ b/src/eric7/MicroPython/MicroPythonCommandsInterface.py	Wed Feb 08 18:09:19 2023 +0100
@@ -186,6 +186,36 @@
             self.__serial.readUntil(b">>> ")  # read until Python prompt
             self.__serial.readAll()  # read all data and discard it
 
+    def probeDevice(self):
+        """
+        Public method to check the device is responding.
+
+        If the device has not been flashed with a MicroPython formware, the
+        probe will fail.
+
+        @return flag indicating a communicating MicroPython device
+        @rtype bool
+        """
+        if not self.__serial:
+            return False
+
+        if not self.__serial.isConnected():
+            return False
+
+        # switch on raw mode
+        self.__blockReadyRead = True
+        ok = self.__rawOn()
+        if not ok:
+            self.__blockReadyRead = False
+            return False
+
+        # switch off raw mode
+        QThread.msleep(10)
+        self.__rawOff()
+        self.__blockReadyRead = False
+
+        return True
+
     def execute(self, commands):
         """
         Public method to send commands to the connected device and return the
@@ -849,19 +879,32 @@
                     "    res['mpy_variant'] = ''",
                 ]
             ),
-            "stat_ = __os_.statvfs('/flash')",
-            "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",
+            "\n".join(
+                [
+                    "try:",
+                    "    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",
+                    "    del stat_",
+                    "except AttributeError:",
+                    "    res['flash_info_available'] = False",
+                ]
+            ),
             "\n".join(
                 [
                     "try:",
                     "    import machine as __mc_",
-                    "    res['mc_frequency_mhz'] = __mc_.freq() / 1000000.0",
+                    "    if isinstance(__mc_.freq(), tuple):",
+                    "        res['mc_frequency_mhz'] = __mc_.freq()[0] / 1000000.0",
+                    "    else:",
+                    "       res['mc_frequency_mhz'] = __mc_.freq() / 1000000.0",
                     "    res['mc_id'] = ':'.join(['{0:X}'.format(x)"
                     " for x in __mc_.unique_id()])",
                     "    del __mc_",
@@ -894,7 +937,7 @@
                 ]
             ),
             "print(res)",
-            "del res, stat_, __os_, __sys_",
+            "del res, __os_, __sys_",
         ]
         out, err = self.execute(commands)
         if err:

eric ide

mercurial