src/eric7/MicroPython/Devices/EspDevices.py

branch
eric7
changeset 9866
0cf5dda5512f
parent 9859
829c1edbf253
child 9868
467288cffee2
--- a/src/eric7/MicroPython/Devices/EspDevices.py	Thu Mar 09 14:55:17 2023 +0100
+++ b/src/eric7/MicroPython/Devices/EspDevices.py	Thu Mar 09 16:56:24 2023 +0100
@@ -27,6 +27,7 @@
 from ..MicroPythonWidget import HAS_QTCHART
 from . import FirmwareGithubUrls
 from .DeviceBase import BaseDevice
+from .CircuitPythonDevices import CircuitPythonDevice
 
 
 class EspDevice(BaseDevice):
@@ -49,6 +50,9 @@
 
         self.__createEsp32Submenu()
 
+        self.__cpyDevice = None
+        # needed to delegate some methods to a CircuitPython variant
+
         self.__statusTranslations = {
             200: self.tr("beacon timeout"),
             201: self.tr("no matching access point found"),
@@ -70,6 +74,35 @@
             7: "WPA2/WPA3",
         }
 
+    def __createCpyDevice(self):
+        """
+        Private method to create a CircuitPython device interface.
+        """
+        if self.hasCircuitPython() and self.__cpyDevice is None:
+            self.__cpyDevice = CircuitPythonDevice(
+                self.microPython,
+                "esp32_circuitpython",
+                "esp32",
+                hasWorkspace=False,
+                parent=self.parent(),
+            )
+
+    def setConnected(self, connected):
+        """
+        Public method to set the connection state.
+
+        Note: This method can be overwritten to perform actions upon connect
+        or disconnect of the device.
+
+        @param connected connection state
+        @type bool
+        """
+        super().setConnected(connected)
+
+        if self.hasCircuitPython():
+            self._submitMode = "paste"
+            self.__createCpyDevice()
+
     def setButtons(self):
         """
         Public method to enable the supported action buttons.
@@ -341,6 +374,9 @@
         Private slot to show the firmware version of the connected device and the
         available firmware version.
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.showCircuitPythonVersions()
+
         if self.microPython.isConnected():
             if self._deviceData["mpy_name"] == "micropython":
                 url = QUrl(FirmwareGithubUrls["micropython"])
@@ -471,7 +507,7 @@
         """
         Private slot to reset the connected device.
         """
-        if self.microPython.isConnected():
+        if self.microPython.isConnected() and not self.hasCircuitPython():
             self.microPython.deviceInterface().execute(
                 "import machine\nmachine.reset()\n", mode=self._submitMode
             )
@@ -509,6 +545,9 @@
         @return documentation URL of the device
         @rtype str
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.getDocumentationUrl()
+
         return Preferences.getMicroPython("MicroPythonDocuUrl")
 
     def getFirmwareUrl(self):
@@ -518,6 +557,9 @@
         @return firmware download URL of the device
         @rtype str
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.getFirmwareUrl()
+
         return Preferences.getMicroPython("MicroPythonFirmwareUrl")
 
     ##################################################################
@@ -551,6 +593,9 @@
         # https://docs.micropython.org/en/latest/library/machine.RTC.html#machine-rtc
         #
         # LoBo variant of MPy deviates.
+        if self.hasCircuitPython():
+            return super()._getSetTimeCode()
+
         return """
 def set_time(rtc_time):
     import machine
@@ -578,6 +623,10 @@
             and the WiFi type (esp32)
         @rtype tuple of (bool, str)
         """
+        if self.hasCircuitPython():
+            self.__createCpyDevice()
+            return self.__cpyDevice.hasWifi()
+
         return True, "esp32"
 
     def getWifiData(self):
@@ -589,6 +638,9 @@
         @rtype tuple of (dict, dict, dict)
         @exception OSError raised to indicate an issue with the device
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.getWifiData()
+
         command = """
 def wifi_status():
     import ubinascii
@@ -665,6 +717,9 @@
         @return tuple containing the connection status and an error string
         @rtype tuple of (bool, str)
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.connectWifi(ssid, password)
+
         command = """
 def connect_wifi(ssid, password):
     import network
@@ -714,6 +769,9 @@
         @return tuple containing a flag indicating success and an error string
         @rtype tuple of (bool, str)
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.disconnectWifi()
+
         command = """
 def disconnect_wifi():
     import network
@@ -747,6 +805,9 @@
         @return tuple containing a flag indicating success and an error message
         @rtype tuple of (bool, str)
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.writeCredentials(ssid, password)
+
         nvsCommand = """
 def save_wifi_creds(ssid, password):
     import esp32
@@ -809,6 +870,9 @@
         @return tuple containing a flag indicating success and an error message
         @rtype tuple of (bool, str)
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.removeCredentials()
+
         nvsCommand = """
 def delete_wifi_creds():
     import esp32
@@ -838,6 +902,9 @@
         @return tuple containing a flag indicating reachability and an error string
         @rtype tuple of (bool, str)
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.checkInternet()
+
         command = """
 def check_internet():
     import network
@@ -873,6 +940,9 @@
             'MAC-Address', 'channel', 'RSSI' and 'security' and an error string
         @rtype tuple of (list of tuple of (str, str, int, int, str), str)
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.scanNetworks()
+
         command = """
 def scan_networks():
     import network
@@ -928,6 +998,9 @@
                 "interface must be 'AP' or 'STA', got '{0}'".format(interface)
             )
 
+        if self.hasCircuitPython():
+            return self.__cpyDevice.deactivateInterface(interface)
+
         command = """
 def deactivate():
     import network
@@ -966,6 +1039,11 @@
         @return tuple containing a flag indicating success and an error message
         @rtype tuple of (bool, str)
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.startAccessPoint(
+                ssid, security=security, password=password, ifconfig=ifconfig
+            )
+
         if security is None or password is None:
             security = 0
             password = ""
@@ -1007,6 +1085,9 @@
         @return tuple containg a flag indicating success and an error message
         @rtype tuple of (bool, str)
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.stopAccessPoint()
+
         return self.deactivateInterface("AP")
 
     def getConnectedClients(self):
@@ -1017,6 +1098,9 @@
             and the RSSI (if supported and available) and an error message
         @rtype tuple of ([(bytes, int)], str)
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.getConnectedClients()
+
         command = """
 def get_stations():
     import network
@@ -1050,6 +1134,10 @@
         @rtype bool
         @exception OSError raised to indicate an issue with the device
         """
+        if self.hasCircuitPython():
+            self.__createCpyDevice()
+            return self.__cpyDevice.hasBluetooth()
+
         command = """
 def has_bt():
     try:
@@ -1080,6 +1168,9 @@
         @rtype list of tuples of (str, str)
         @exception OSError raised to indicate an issue with the device
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.getBluetoothStatus()
+
         command = """
 def ble_status():
     import bluetooth
@@ -1139,6 +1230,9 @@
         @rtype bool
         @exception OSError raised to indicate an issue with the device
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.activateBluetoothInterface()
+
         command = """
 def activate_ble():
     import bluetooth
@@ -1165,6 +1259,9 @@
         @rtype bool
         @exception OSError raised to indicate an issue with the device
         """
+        if self.hasCircuitPython():
+            return self.__cpyDevice.deactivateBluetoothInterface()
+
         command = """
 def deactivate_ble():
     import bluetooth
@@ -1196,6 +1293,9 @@
         """
         from ..BluetoothDialogs.BluetoothAdvertisement import BluetoothAdvertisement
 
+        if self.hasCircuitPython():
+            return self.__cpyDevice.getDeviceScan(timeout)
+
         command = """
 def ble_scan():
     import bluetooth

eric ide

mercurial