src/eric7/MicroPython/Devices/EspDevices.py

branch
mpy_network
changeset 9820
67597e003373
parent 9808
b68af9fd6a7a
child 9827
21803aa6c3e2
diff -r 640b6c23d97b -r 67597e003373 src/eric7/MicroPython/Devices/EspDevices.py
--- a/src/eric7/MicroPython/Devices/EspDevices.py	Mon Feb 27 16:55:09 2023 +0100
+++ b/src/eric7/MicroPython/Devices/EspDevices.py	Mon Feb 27 17:43:11 2023 +0100
@@ -13,8 +13,8 @@
 import json
 import os
 
-from PyQt6.QtCore import QProcess, QUrl, pyqtSlot
-from PyQt6.QtNetwork import QNetworkRequest
+from PyQt6.QtCore import QCoreApplication, QProcess, QUrl, pyqtSlot
+from PyQt6.QtNetwork import QNetworkReply, QNetworkRequest
 from PyQt6.QtWidgets import QDialog, QMenu
 
 from eric7 import Globals, Preferences
@@ -230,102 +230,21 @@
         """
         Private slot to erase the device flash memory.
         """
-        ok = EricMessageBox.yesNo(
-            self.microPython,
-            self.tr("Erase Flash"),
-            self.tr("""Shall the flash of the selected device really be erased?"""),
-        )
-        if ok:
-            flashArgs = [
-                "-u",
-                "-m",
-                "esptool",
-                "--port",
-                self.microPython.getCurrentPort(),
-                "erase_flash",
-            ]
-            dlg = EricProcessDialog(
-                self.tr("'esptool erase_flash' Output"),
-                self.tr("Erase Flash"),
-                showProgress=True,
-            )
-            res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs)
-            if res:
-                dlg.exec()
+        eraseFlash(self.microPython.getCurrentPort())
 
     @pyqtSlot()
     def __flashMicroPython(self):
         """
         Private slot to flash a MicroPython firmware to the device.
         """
-        from .EspDialogs.EspFirmwareSelectionDialog import EspFirmwareSelectionDialog
-
-        dlg = EspFirmwareSelectionDialog()
-        if dlg.exec() == QDialog.DialogCode.Accepted:
-            chip, firmware, baudRate, flashMode, flashAddress = dlg.getData()
-            flashArgs = [
-                "-u",
-                "-m",
-                "esptool",
-                "--chip",
-                chip,
-                "--port",
-                self.microPython.getCurrentPort(),
-            ]
-            if baudRate != "115200":
-                flashArgs += ["--baud", baudRate]
-            flashArgs.append("write_flash")
-            if flashMode:
-                flashArgs += ["--flash_mode", flashMode]
-            flashArgs += [
-                flashAddress,
-                firmware,
-            ]
-            dlg = EricProcessDialog(
-                self.tr("'esptool write_flash' Output"),
-                self.tr("Flash MicroPython Firmware"),
-                showProgress=True,
-            )
-            res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs)
-            if res:
-                dlg.exec()
+        flashPythonFirmware(self.microPython.getCurrentPort())
 
     @pyqtSlot()
     def __flashAddons(self):
         """
         Private slot to flash some additional firmware images.
         """
-        from .EspDialogs.EspFirmwareSelectionDialog import EspFirmwareSelectionDialog
-
-        dlg = EspFirmwareSelectionDialog(addon=True)
-        if dlg.exec() == QDialog.DialogCode.Accepted:
-            chip, firmware, baudRate, flashMode, flashAddress = dlg.getData()
-            flashArgs = [
-                "-u",
-                "-m",
-                "esptool",
-                "--chip",
-                chip,
-                "--port",
-                self.microPython.getCurrentPort(),
-            ]
-            if baudRate != "115200":
-                flashArgs += ["--baud", baudRate]
-            flashArgs.append("write_flash")
-            if flashMode:
-                flashArgs += ["--flash_mode", flashMode]
-            flashArgs += [
-                flashAddress.lower(),
-                firmware,
-            ]
-            dlg = EricProcessDialog(
-                self.tr("'esptool write_flash' Output"),
-                self.tr("Flash Additional Firmware"),
-                showProgress=True,
-            )
-            res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs)
-            if res:
-                dlg.exec()
+        flashAddonFirmware(self.microPython.getCurrentPort())
 
     @pyqtSlot()
     def __backupFlash(self):
@@ -444,9 +363,10 @@
             reply = ui.networkAccessManager().head(request)
             reply.finished.connect(lambda: self.__firmwareVersionResponse(reply))
 
+    @pyqtSlot(QNetworkReply)
     def __firmwareVersionResponse(self, reply):
         """
-        Private method handling the response of the latest version request.
+        Private slot handling the response of the latest version request.
 
         @param reply reference to the reply object
         @type QNetworkReply
@@ -1133,3 +1053,125 @@
     @rtype EspDevice
     """
     return EspDevice(microPythonWidget, deviceType)
+
+
+################################################################################
+## Functions below implement flashing related functionality needed elsewhere  ##
+## as well.                                                                   ##
+################################################################################
+
+
+@pyqtSlot()
+def eraseFlash(port):
+    """
+    Slot to erase the device flash memory.
+
+    @param port name of the serial port device to be used
+    @type str
+    """
+    ok = EricMessageBox.yesNo(
+        None,
+        QCoreApplication.translate("EspDevice", "Erase Flash"),
+        QCoreApplication.translate(
+            "EspDevice", """Shall the flash of the selected device really be erased?"""
+        ),
+    )
+    if ok:
+        flashArgs = [
+            "-u",
+            "-m",
+            "esptool",
+            "--port",
+            port,
+            "erase_flash",
+        ]
+        dlg = EricProcessDialog(
+            QCoreApplication.translate("EspDevice", "'esptool erase_flash' Output"),
+            QCoreApplication.translate("EspDevice", "Erase Flash"),
+            showProgress=True,
+        )
+        res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs)
+        if res:
+            dlg.exec()
+
+
+@pyqtSlot()
+def flashPythonFirmware(port):
+    """
+    Slot to flash a MicroPython firmware to the device.
+
+    @param port name of the serial port device to be used
+    @type str
+    """
+    from .EspDialogs.EspFirmwareSelectionDialog import EspFirmwareSelectionDialog
+
+    dlg = EspFirmwareSelectionDialog()
+    if dlg.exec() == QDialog.DialogCode.Accepted:
+        chip, firmware, baudRate, flashMode, flashAddress = dlg.getData()
+        flashArgs = [
+            "-u",
+            "-m",
+            "esptool",
+            "--chip",
+            chip,
+            "--port",
+            port,
+        ]
+        if baudRate != "115200":
+            flashArgs += ["--baud", baudRate]
+        flashArgs.append("write_flash")
+        if flashMode:
+            flashArgs += ["--flash_mode", flashMode]
+        flashArgs += [
+            flashAddress,
+            firmware,
+        ]
+        dlg = EricProcessDialog(
+            QCoreApplication.translate("EspDevice", "'esptool write_flash' Output"),
+            QCoreApplication.translate("EspDevice", "Flash µPy/CPy Firmware"),
+            showProgress=True,
+        )
+        res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs)
+        if res:
+            dlg.exec()
+
+
+@pyqtSlot()
+def flashAddonFirmware(port):
+    """
+    Slot to flash some additional firmware images.
+
+    @param port name of the serial port device to be used
+    @type str
+    """
+    from .EspDialogs.EspFirmwareSelectionDialog import EspFirmwareSelectionDialog
+
+    dlg = EspFirmwareSelectionDialog(addon=True)
+    if dlg.exec() == QDialog.DialogCode.Accepted:
+        chip, firmware, baudRate, flashMode, flashAddress = dlg.getData()
+        flashArgs = [
+            "-u",
+            "-m",
+            "esptool",
+            "--chip",
+            chip,
+            "--port",
+            port,
+        ]
+        if baudRate != "115200":
+            flashArgs += ["--baud", baudRate]
+        flashArgs.append("write_flash")
+        if flashMode:
+            flashArgs += ["--flash_mode", flashMode]
+        flashArgs += [
+            flashAddress.lower(),
+            firmware,
+        ]
+        dlg = EricProcessDialog(
+            QCoreApplication.translate("EspDevice", "'esptool write_flash' Output"),
+            QCoreApplication.translate("EspDevice", "Flash Additional Firmware"),
+            showProgress=True,
+        )
+        res = dlg.startProcess(PythonUtilities.getPythonExecutable(), flashArgs)
+        if res:
+            dlg.exec()

eric ide

mercurial