eric6/MicroPython/CircuitPythonDevices.py

Tue, 06 Aug 2019 16:42:39 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 06 Aug 2019 16:42:39 +0200
branch
micropython
changeset 7122
8e67ef7975de
parent 7116
233b6e62ca2b
child 7125
2028553ee58c
permissions
-rw-r--r--

CircuitPythonDevices: added some TODO markers.

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

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

"""
Module implementing the device interface class for CircuitPython boards.
"""

from __future__ import unicode_literals

import shutil

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QDialog

from E5Gui import E5MessageBox

from .MicroPythonDevices import MicroPythonDevice
from .MicroPythonReplWidget import HAS_QTCHART

import Utilities


class CircuitPythonDevice(MicroPythonDevice):
    """
    Class implementing the device for CircuitPython boards.
    """
    def __init__(self, microPythonWidget, parent=None):
        """
        Constructor
        
        @param microPythonWidget reference to the main MicroPython widget
        @type MicroPythonReplWidget
        @param parent reference to the parent object
        @type QObject
        """
        super(CircuitPythonDevice, self).__init__(microPythonWidget, parent)
    
    def setButtons(self):
        """
        Public method to enable the supported action buttons.
        """
        super(CircuitPythonDevice, self).setButtons()
        self.microPython.setActionButtons(
            run=True, repl=True, files=True, chart=HAS_QTCHART)
        
        workspace = self.getWorkspace()
        if workspace.endswith("CIRCUITPY"):
            self.microPython.setActionButtons(open=True, save=True)
    
    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 False
    
    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 getWorkspace(self):
        """
        Public method to get the workspace directory.
        
        @return workspace directory used for saving files
        @rtype str
        """
        # Attempts to find the path on the filesystem that represents the
        # plugged in CIRCUITPY board.
        deviceDirectory = Utilities.findVolume("CIRCUITPY")
        
        if deviceDirectory:
            return deviceDirectory
        else:
            # return the default workspace and give the user a warning
            E5MessageBox.warning(
                self.microPython,
                self.tr("Workspace Directory"),
                self.tr("Python files for CircuitPython devices are stored on"
                        " the device. Therefore, to edit these files you need"
                        " to have the device plugged in. Until you plug in a"
                        " device, the standard directory will be used."))
            
            return super(CircuitPythonDevice, self).getWorkspace()
    
    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("Flash CircuitPython Firmware"),
                             self.__flashCircuitPython)
        act.setEnabled(not connected)
        # TODO: add menu entry to copy a .mpy or .py file to the
        #       device library (creating the /lib folder if it doesn't
        #       exist already)
        # TODO: add menu entry to cross-compile a .py file (using mpy-cross)
    
    @pyqtSlot()
    def __flashCircuitPython(self):
        """
        Private slot to flash a CircuitPython firmware to the device.
        """
        ok = E5MessageBox.information(
            self.microPython,
            self.tr("Flash CircuitPython Firmware"),
            self.tr("Please reset the device to bootloader mode and confirm"
                    " when ready."),
            E5MessageBox.StandardButtons(
                E5MessageBox.Abort |
                E5MessageBox.Ok))
        if ok:
            from .CircuitPythonFirmwareSelectionDialog import (
                CircuitPythonFirmwareSelectionDialog)
            dlg = CircuitPythonFirmwareSelectionDialog()
            if dlg.exec_() == QDialog.Accepted:
                cpyPath, devicePath = dlg.getData()
                shutil.copy2(cpyPath, devicePath)

eric ide

mercurial