--- a/eric6/MicroPython/EspDevices.py Tue Jul 30 19:44:27 2019 +0200 +++ b/eric6/MicroPython/EspDevices.py Wed Jul 31 20:41:39 2019 +0200 @@ -10,7 +10,14 @@ from __future__ import unicode_literals +import sys + from PyQt5.QtCore import pyqtSlot +from PyQt5.QtWidgets import QDialog + +from E5Gui import E5MessageBox +from E5Gui.E5ProcessDialog import E5ProcessDialog +from E5Gui.E5Application import e5App from .MicroPythonDevices import MicroPythonDevice from .MicroPythonReplWidget import HAS_QTCHART @@ -105,3 +112,111 @@ Public slot handling a data flood from the device. """ self.microPython.setActionButtons(files=True) + + def addDeviceMenuEntries(self, menu): + """ + Public method to add device specific entries to the given menu. + + @param menu reference to the context menu + @type QMenu + """ + connected = self.microPython.isConnected() + + act = menu.addAction(self.tr("Erase Flash"), + self.__eraseFlash) + act.setEnabled(not connected) + act = menu.addAction(self.tr("Flash MicroPython Firmware"), + self.__flashMicroPython) + act.setEnabled(not connected) + menu.addSeparator() + act = menu.addAction(self.tr("Flash Additional Firmware"), + self.__flashAddons) + act.setEnabled(not connected) + menu.addSeparator() + menu.addAction(self.tr("Install 'esptool.py'"), self.__installEspTool) + + @pyqtSlot() + def __eraseFlash(self): + """ + Private slot to erase the device flash memory. + """ + ok = E5MessageBox.yesNo( + self, + 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 = E5ProcessDialog(self.tr("'esptool erase_flash' Output"), + self.tr("Erase Flash")) + res = dlg.startProcess(sys.executable, flashArgs) + if res: + dlg.exec_() + + @pyqtSlot() + def __flashMicroPython(self): + """ + Private slot to flash a MicroPython firmware to the device. + """ + from .EspFirmwareSelectionDialog import EspFirmwareSelectionDialog + dlg = EspFirmwareSelectionDialog() + if dlg.exec_() == QDialog.Accepted: + chip, firmware, _ = dlg.getData() + if chip == "esp8266": + flashAddress = "0x0000" + elif chip == "esp32": + flashAddress = "0x1000" + else: + raise ValueError(self.tr("Unsupported chip type '{0}'.") + .format(chip)) + flashArgs = [ + "-u", + "-m", "esptool", + "--chip", chip, + "--port", self.microPython.getCurrentPort(), + "write_flash", + flashAddress, + firmware, + ] + dlg = E5ProcessDialog(self.tr("'esptool write_flash' Output"), + self.tr("Flash MicroPython Firmware")) + res = dlg.startProcess(sys.executable, flashArgs) + if res: + dlg.exec_() + + @pyqtSlot() + def __flashAddons(self): + """ + Private slot to flash some additional firmware images. + """ + from .EspFirmwareSelectionDialog import EspFirmwareSelectionDialog + dlg = EspFirmwareSelectionDialog(addon=True) + if dlg.exec_() == QDialog.Accepted: + chip, firmware, flashAddress = dlg.getData() + flashArgs = [ + "-u", + "-m", "esptool", + "--chip", chip, + "--port", self.microPython.getCurrentPort(), + "write_flash", + flashAddress.lower(), + firmware, + ] + dlg = E5ProcessDialog(self.tr("'esptool write_flash' Output"), + self.tr("Flash Additional Firmware")) + res = dlg.startProcess(sys.executable, flashArgs) + if res: + dlg.exec_() + + @pyqtSlot() + def __installEspTool(self): + """ + Private slot to install the esptool package via pip. + """ + pip = e5App().getObject("Pip") + pip.installPackages(["esptool"], interpreter=sys.executable)