eric6/MicroPython/EspDevices.py

Sun, 21 Jul 2019 19:54:15 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 21 Jul 2019 19:54:15 +0200
branch
micropython
changeset 7077
3b7475b7a1ef
parent 7065
e3d04faced34
child 7091
84d2a73b448a
permissions
-rw-r--r--

MicroPython: started to implement the file manager widget.

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

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

"""
Module implementing some utility functions and the MicroPythonDevice base
class.
"""

from __future__ import unicode_literals

from PyQt5.QtCore import pyqtSlot

from .MicroPythonDevices import MicroPythonDevice
from .MicroPythonReplWidget import HAS_QTCHART


class EspDevice(MicroPythonDevice):
    """
    Class implementing the device for ESP32 and ESP8266 based 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(EspDevice, self).__init__(microPythonWidget, parent)
        
        self.__replActive = False
        self.__fileManagerActive = False
        self.__plotterActive = False
    
    def setButtons(self):
        """
        Public method to enable the supported action buttons.
        """
        super(EspDevice, self).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 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)
        """
        if self.__fileManagerActive:
            return False, self.tr("The REPL and the file manager use the same"
                                  " USB serial connection. Only one can be"
                                  " active at any time. Toggle the file"
                                  " manager off and try again.")
        else:
            return True, ""
    
    def setRepl(self, on):
        """
        Public method to set the REPL status and dependent status.
        
        @param on flag indicating the active status
        @type bool
        """
        self.__replActive = on
        self.microPython.setActionButtons(
            files=not (on or self.__plotterActive))
    
    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)
        """
        if self.__fileManagerActive:
            return False, self.tr("The Plotter and the file manager use the"
                                  " same USB serial connection. Only one can"
                                  " be active at any time. Toggle the file"
                                  " manager off and try again.")
        else:
            return True, ""
    
    def setPlotter(self, on):
        """
        Public method to set the Plotter status and dependent status.
        
        @param on flag indicating the active status
        @type bool
        """
        self.__plotterActive = on
        self.microPython.setActionButtons(
            files=not (on or self.__replActive))
    
    def canRunScript(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 self.canStartRepl()
    
    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)
        """
        if self.__replActive or self.__plotterActive:
            return False, self.tr("The file manager and the REPL/plotter use"
                                  " the same USB serial connection. Only one"
                                  " can be active at any time. Disconnect the"
                                  " REPL/plotter and try again.")
        else:
            return True, ""
    
    def setFileManager(self, on):
        """
        Public method to set the File Manager status and dependent status.
        
        @param on flag indicating the active status
        @type bool
        """
        self.__fileManagerActive = on
        self.microPython.setActionButtons(
            run=not on, repl=not on, chart=HAS_QTCHART and not on)
    
    @pyqtSlot()
    def handleDataFlood(self):
        """
        Public slot handling a data floof from the device.
        """
        self.microPython.setActionButtons(files=True)

eric ide

mercurial