src/eric7/MicroPython/MicrobitDevices.py

Thu, 09 Feb 2023 09:55:57 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 09 Feb 2023 09:55:57 +0100
branch
eric7
changeset 9750
4958dd72c937
parent 9749
5d409223cf3f
child 9751
606ac0e26533
permissions
-rw-r--r--

Little enhancement to the BBC micro:bit firmware version display function to be a bit more flexible.

# -*- coding: utf-8 -*-

# Copyright (c) 2019 - 2023 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the device interface class for BBC micro:bit and
Calliope mini boards.
"""

import os
import shutil

from PyQt6.QtCore import QStandardPaths, QUrl, pyqtSlot
from PyQt6.QtNetwork import QNetworkRequest
from PyQt6.QtWidgets import QInputDialog, QLineEdit

from eric7 import Globals, Preferences
from eric7.EricWidgets import EricFileDialog, EricMessageBox
from eric7.EricWidgets.EricApplication import ericApp
from eric7.SystemUtilities import FileSystemUtilities

from .MicroPythonDevices import FirmwareGithubUrls, MicroPythonDevice
from .MicroPythonWidget import HAS_QTCHART


class MicrobitDevice(MicroPythonDevice):
    """
    Class implementing the device for BBC micro:bit and Calliope mini boards.
    """

    def __init__(self, microPythonWidget, deviceType, parent=None):
        """
        Constructor

        @param microPythonWidget reference to the main MicroPython widget
        @type MicroPythonWidget
        @param deviceType type of the device
        @type str
        @param parent reference to the parent object
        @type QObject
        """
        super().__init__(microPythonWidget, deviceType, parent)

    def setButtons(self):
        """
        Public method to enable the supported action buttons.
        """
        super().setButtons()
        self.microPython.setActionButtons(
            run=True, repl=True, files=True, chart=HAS_QTCHART
        )

    def forceInterrupt(self):
        """
        Public method to determine the need for an interrupt when opening the
        serial connection.

        @return flag indicating an interrupt is needed
        @rtype bool
        """
        return True

    def deviceName(self):
        """
        Public method to get the name of the device.

        @return name of the device
        @rtype str
        """
        if self.getDeviceType() == "bbc_microbit":
            # BBC micro:bit
            return self.tr("BBC micro:bit")
        else:
            # Calliope mini
            return self.tr("Calliope mini")

    def canStartRepl(self):
        """
        Public method to determine, if a REPL can be started.

        @return tuple containing a flag indicating it is safe to start a REPL
            and a reason why it cannot.
        @rtype tuple of (bool, str)
        """
        return True, ""

    def canStartPlotter(self):
        """
        Public method to determine, if a Plotter can be started.

        @return tuple containing a flag indicating it is safe to start a
            Plotter and a reason why it cannot.
        @rtype tuple of (bool, str)
        """
        return True, ""

    def canRunScript(self):
        """
        Public method to determine, if a script can be executed.

        @return tuple containing a flag indicating it is safe to start a
            Plotter and a reason why it cannot.
        @rtype tuple of (bool, str)
        """
        return True, ""

    def runScript(self, script):
        """
        Public method to run the given Python script.

        @param script script to be executed
        @type str
        """
        pythonScript = script.split("\n")
        self.sendCommands(pythonScript)

    def canStartFileManager(self):
        """
        Public method to determine, if a File Manager can be started.

        @return tuple containing a flag indicating it is safe to start a
            File Manager and a reason why it cannot.
        @rtype tuple of (bool, str)
        """
        return True, ""

    def hasTimeCommands(self):
        """
        Public method to check, if the device supports time commands.

        The default returns True.

        @return flag indicating support for time commands
        @rtype bool
        """
        return False

    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()
        linkConnected = self.microPython.isLinkConnected()

        menu.addAction(
            self.tr("Show MicroPython Versions"), self.__showFirmwareVersions
        ).setEnabled(connected and self.getDeviceType() != "calliope")
        menu.addAction(
            self.tr("Flash MicroPython"), self.__flashMicroPython
        ).setEnabled(not linkConnected)
        menu.addAction(
            self.tr("Flash Firmware"), lambda: self.__flashMicroPython(firmware=True)
        ).setEnabled(not linkConnected)
        menu.addSeparator()
        act = menu.addAction(self.tr("Save Script"), self.__saveScriptToDevice)
        act.setToolTip(self.tr("Save the current script to the selected device"))
        act.setEnabled(connected)
        act = menu.addAction(self.tr("Save Script as 'main.py'"), self.__saveMain)
        act.setToolTip(
            self.tr("Save the current script as 'main.py' on the connected device")
        )
        act.setEnabled(connected)
        menu.addSeparator()
        menu.addAction(
            self.tr("Reset {0}").format(self.deviceName()), self.__resetDevice
        ).setEnabled(connected)

    def hasFlashMenuEntry(self):
        """
        Public method to check, if the device has its own flash menu entry.

        @return flag indicating a specific flash menu entry
        @rtype bool
        """
        return True

    @pyqtSlot()
    def __flashMicroPython(self, firmware=False):
        """
        Private slot to flash MicroPython or the DAPLink firmware to the
        device.

        @param firmware flag indicating to flash the DAPLink firmware
        @type bool
        """
        # Attempts to find the path on the file system that represents the
        # plugged in micro:bit board. To flash the DAPLink firmware, it must be
        # in maintenance mode, for MicroPython in standard mode.
        if self.getDeviceType() == "bbc_microbit":
            # BBC micro:bit
            if firmware:
                deviceDirectories = FileSystemUtilities.findVolume(
                    "MAINTENANCE", findAll=True
                )
            else:
                deviceDirectories = FileSystemUtilities.findVolume(
                    "MICROBIT", findAll=True
                )
        else:
            # Calliope mini
            if firmware:
                deviceDirectories = FileSystemUtilities.findVolume(
                    "MAINTENANCE", findAll=True
                )
            else:
                deviceDirectories = FileSystemUtilities.findVolume("MINI", findAll=True)
        if len(deviceDirectories) == 0:
            if self.getDeviceType() == "bbc_microbit":
                # BBC micro:bit is not ready or not mounted
                if firmware:
                    EricMessageBox.critical(
                        self.microPython,
                        self.tr("Flash MicroPython/Firmware"),
                        self.tr(
                            "<p>The BBC micro:bit is not ready for flashing"
                            " the DAPLink firmware. Follow these"
                            " instructions. </p>"
                            "<ul>"
                            "<li>unplug USB cable and any batteries</li>"
                            "<li>keep RESET button pressed and plug USB cable"
                            " back in</li>"
                            "<li>a drive called MAINTENANCE should be"
                            " available</li>"
                            "</ul>"
                            "<p>See the "
                            '<a href="https://microbit.org/guide/firmware/">'
                            "micro:bit web site</a> for details.</p>"
                        ),
                    )
                else:
                    EricMessageBox.critical(
                        self.microPython,
                        self.tr("Flash MicroPython/Firmware"),
                        self.tr(
                            "<p>The BBC micro:bit is not ready for flashing"
                            " the MicroPython firmware. Please make sure,"
                            " that a drive called MICROBIT is available."
                            "</p>"
                        ),
                    )
            else:
                # Calliope mini is not ready or not mounted
                if firmware:
                    EricMessageBox.critical(
                        self.microPython,
                        self.tr("Flash MicroPython/Firmware"),
                        self.tr(
                            '<p>The "Calliope mini" is not ready for flashing'
                            " the DAPLink firmware. Follow these"
                            " instructions. </p>"
                            "<ul>"
                            "<li>unplug USB cable and any batteries</li>"
                            "<li>keep RESET button pressed an plug USB cable"
                            " back in</li>"
                            "<li>a drive called MAINTENANCE should be"
                            " available</li>"
                            "</ul>"
                        ),
                    )
                else:
                    EricMessageBox.critical(
                        self.microPython,
                        self.tr("Flash MicroPython/Firmware"),
                        self.tr(
                            '<p>The "Calliope mini" is not ready for flashing'
                            " the MicroPython firmware. Please make sure,"
                            " that a drive called MINI is available."
                            "</p>"
                        ),
                    )
        elif len(deviceDirectories) == 1:
            downloadsPath = QStandardPaths.standardLocations(
                QStandardPaths.StandardLocation.DownloadLocation
            )[0]
            firmware = EricFileDialog.getOpenFileName(
                self.microPython,
                self.tr("Flash MicroPython/Firmware"),
                downloadsPath,
                self.tr("MicroPython/Firmware Files (*.hex *.bin);;All Files (*)"),
            )
            if firmware and os.path.exists(firmware):
                shutil.copy2(firmware, deviceDirectories[0])
        else:
            EricMessageBox.warning(
                self,
                self.tr("Flash MicroPython/Firmware"),
                self.tr(
                    "There are multiple devices ready for flashing."
                    " Please make sure, that only one device is prepared."
                ),
            )

    @pyqtSlot()
    def __showFirmwareVersions(self):
        """
        Private slot to show the firmware version of the connected device and the
        available firmware version.
        """
        if self.microPython.isConnected():
            interface = self.microPython.commandsInterface()
            if interface is not None:
                impInfo = interface.getImplementation()
                versionInfo = interface.version()
                if impInfo["name"] != "micropython":
                    EricMessageBox.critical(
                        None,
                        self.tr("Show MicroPython Versions"),
                        self.tr(
                            """The firmware of the connected device cannot be"""
                            """ determined or the board does not run MicroPython."""
                            """ Aborting..."""
                        ),
                    )
                else:
                    impInfo["version"] = versionInfo["release"]
                    if self.getDeviceType() == "bbc_microbit":
                        if "nRF51" in versionInfo["machine"]:
                            url = QUrl(FirmwareGithubUrls["microbit_v1"])
                        elif "nRF52" in versionInfo["machine"]:
                            url = QUrl(FirmwareGithubUrls["microbit_v2"])
                        else:
                            EricMessageBox.critical(
                                None,
                                self.tr("Show MicroPython Versions"),
                                self.tr(
                                    """<p>The BBC micro:bit generation cannot be"""
                                    """ determined. Aborting...</p>"""
                                ),
                            )
                            return
                    else:
                        EricMessageBox.critical(
                            None,
                            self.tr("Show MicroPython Versions"),
                            self.tr(
                                """<p>The firmware URL for the device type <b>{0}</b>"""
                                """ is not known. Aborting...</p>"""
                            ).format(self.getDeviceType()),
                        )
                        return

                    ui = ericApp().getObject("UserInterface")
                    request = QNetworkRequest(url)
                    reply = ui.networkAccessManager().head(request)
                    reply.finished.connect(
                        lambda: self.__firmwareVersionResponse(reply, impInfo)
                    )

    def __firmwareVersionResponse(self, reply, implementation):
        """
        Private method handling the response of the latest version request.

        @param reply reference to the reply object
        @type QNetworkReply
        @param implementation dictionary containing the implementation data of the
            connected device
        @type dict
        """
        latestUrl = reply.url().toString()
        tag = latestUrl.rsplit("/", 1)[-1]
        while tag and not tag[0].isdecimal():
            # get rid of leading non-decimal characters
            tag = tag[1:]
        latestVersion = Globals.versionToTuple(tag)

        if implementation["version"] == "unknown":
            currentVersionStr = self.tr("unknown")
            currentVersion = (0, 0, 0)
        else:
            currentVersionStr = implementation["version"]
            currentVersion = Globals.versionToTuple(currentVersionStr)

        msg = self.tr(
            "<h4>MicroPython Version Information<br/>"
            "(BBC micro:bit v{2})</h4>"
            "<table>"
            "<tr><td>Installed:</td><td>{0}</td></tr>"
            "<tr><td>Available:</td><td>{1}</td></tr>"
            "</table>"
        ).format(currentVersionStr, tag, currentVersionStr[0])
        if currentVersion < latestVersion:
            msg += self.tr("<p><b>Update available!</b></p>")

        EricMessageBox.information(
            None,
            self.tr("MicroPython Version"),
            msg,
        )

    @pyqtSlot()
    def __saveMain(self):
        """
        Private slot to copy the current script as 'main.py' onto the
        connected device.
        """
        self.__saveScriptToDevice("main.py")

    @pyqtSlot()
    def __saveScriptToDevice(self, scriptName=""):
        """
        Private method to save the current script onto the connected
        device.

        @param scriptName name of the file on the device
        @type str
        """
        aw = ericApp().getObject("ViewManager").activeWindow()
        if not aw:
            return

        title = (
            self.tr("Save Script as '{0}'").format(scriptName)
            if scriptName
            else self.tr("Save Script")
        )

        if not (aw.isPyFile() or aw.isMicroPythonFile()):
            yes = EricMessageBox.yesNo(
                self.microPython,
                title,
                self.tr(
                    """The current editor does not contain a Python"""
                    """ script. Write it anyway?"""
                ),
            )
            if not yes:
                return

        script = aw.text().strip()
        if not script:
            EricMessageBox.warning(
                self.microPython, title, self.tr("""The script is empty. Aborting.""")
            )
            return

        if not scriptName:
            scriptName = os.path.basename(aw.getFileName())
            scriptName, ok = QInputDialog.getText(
                self.microPython,
                title,
                self.tr("Enter a file name on the device:"),
                QLineEdit.EchoMode.Normal,
                scriptName,
            )
            if not ok or not bool(scriptName):
                return

            title = self.tr("Save Script as '{0}'").format(scriptName)

        commands = [
            "fd = open('{0}', 'wb')".format(scriptName),
            "f = fd.write",
        ]
        for line in script.splitlines():
            commands.append("f(" + repr(line + "\n") + ")")
        commands.append("fd.close()")
        out, err = self.microPython.commandsInterface().execute(commands)
        if err:
            EricMessageBox.critical(
                self.microPython,
                title,
                self.tr(
                    """<p>The script could not be saved to the"""
                    """ device.</p><p>Reason: {0}</p>"""
                ).format(err.decode("utf-8")),
            )

        # reset the device
        self.__resetDevice()

    @pyqtSlot()
    def __resetDevice(self):
        """
        Private slot to reset the connected device.
        """
        if self.getDeviceType() == "bbc_microbit":
            # BBC micro:bit
            self.microPython.commandsInterface().execute(
                [
                    "import microbit",
                    "microbit.reset()",
                ]
            )
        else:
            # Calliope mini
            self.microPython.commandsInterface().execute(
                [
                    "import calliope_mini",
                    "calliope_mini.reset()",
                ]
            )

    def getDocumentationUrl(self):
        """
        Public method to get the device documentation URL.

        @return documentation URL of the device
        @rtype str
        """
        if self.getDeviceType() == "bbc_microbit":
            # BBC micro:bit
            return Preferences.getMicroPython("MicrobitDocuUrl")
        else:
            # Calliope mini
            return Preferences.getMicroPython("CalliopeDocuUrl")

    def getDownloadMenuEntries(self):
        """
        Public method to retrieve the entries for the downloads menu.

        @return list of tuples with menu text and URL to be opened for each
            entry
        @rtype list of tuple of (str, str)
        """
        if self.getDeviceType() == "bbc_microbit":
            return [
                (
                    self.tr("MicroPython Firmware for BBC micro:bit V1"),
                    Preferences.getMicroPython("MicrobitMicroPythonUrl"),
                ),
                (
                    self.tr("MicroPython Firmware for BBC micro:bit V2"),
                    Preferences.getMicroPython("MicrobitV2MicroPythonUrl"),
                ),
                (
                    self.tr("DAPLink Firmware"),
                    Preferences.getMicroPython("MicrobitFirmwareUrl"),
                ),
            ]
        else:
            return [
                (
                    self.tr("MicroPython Firmware"),
                    Preferences.getMicroPython("CalliopeMicroPythonUrl"),
                ),
                (
                    self.tr("DAPLink Firmware"),
                    Preferences.getMicroPython("CalliopeDAPLinkUrl"),
                ),
            ]


def createDevice(microPythonWidget, deviceType, vid, pid, boardName):
    """
    Function to instantiate a MicroPython device object.

    @param microPythonWidget reference to the main MicroPython widget
    @type MicroPythonWidget
    @param deviceType device type assigned to this device interface
    @type str
    @param vid vendor ID
    @type int
    @param pid product ID
    @type int
    @param boardName name of the board
    @type str
    @return reference to the instantiated device object
    @rtype MicrobitDevice
    """
    return MicrobitDevice(microPythonWidget, deviceType)

eric ide

mercurial