src/eric7/MicroPython/MicroPythonWidget.py

branch
eric7
changeset 9765
6378da868bb0
parent 9763
52f982c08301
child 9766
f0e22f3a5878
equal deleted inserted replaced
9764:57496966803c 9765:6378da868bb0
5 5
6 """ 6 """
7 Module implementing the MicroPython REPL widget. 7 Module implementing the MicroPython REPL widget.
8 """ 8 """
9 9
10 import contextlib
10 import functools 11 import functools
11 import os 12 import os
12 import re 13 import re
13 import time 14 import time
14 15
47 HAS_QTCHART = True 48 HAS_QTCHART = True
48 except ImportError: 49 except ImportError:
49 HAS_QTCHART = False 50 HAS_QTCHART = False
50 51
51 try: 52 try:
52 from .MicroPythonCommandsInterface import MicroPythonCommandsInterface 53 from .MicroPythonDeviceInterface import MicroPythonDeviceInterface
53 54
54 HAS_QTSERIALPORT = True 55 HAS_QTSERIALPORT = True
55 except ImportError: 56 except ImportError:
56 HAS_QTSERIALPORT = False 57 HAS_QTSERIALPORT = False
57 58
270 self.__unknownPorts = [] 271 self.__unknownPorts = []
271 self.__lastPort = None 272 self.__lastPort = None
272 self.__lastDeviceType = None 273 self.__lastDeviceType = None
273 274
274 if HAS_QTSERIALPORT: 275 if HAS_QTSERIALPORT:
275 self.__interface = MicroPythonCommandsInterface(self) 276 self.__interface = MicroPythonDeviceInterface(self)
276 else: 277 else:
277 self.__interface = None 278 self.__interface = None
278 self.__device = None 279 self.__device = None
279 self.__connected = False 280 self.__connected = False
280 self.__linkConnected = False 281 self.__linkConnected = False
459 self.replEdit.setLineWrapMode(QTextEdit.LineWrapMode.NoWrap) 460 self.replEdit.setLineWrapMode(QTextEdit.LineWrapMode.NoWrap)
460 461
461 if self.__chartWidget is not None: 462 if self.__chartWidget is not None:
462 self.__chartWidget.preferencesChanged() 463 self.__chartWidget.preferencesChanged()
463 464
464 def commandsInterface(self): 465 def deviceInterface(self):
465 """ 466 """
466 Public method to get a reference to the commands interface object. 467 Public method to get a reference to the device interface object.
467 468
468 @return reference to the commands interface object 469 @return reference to the commands interface object
469 @rtype MicroPythonCommandsInterface 470 @rtype MicroPythonDeviceInterface
470 """ 471 """
471 return self.__interface 472 return self.__interface
472 473
473 def isMicrobit(self): 474 def isMicrobit(self):
474 """ 475 """
496 """ 497 """
497 deviceType = self.deviceTypeComboBox.itemData(index, self.DeviceTypeRole) 498 deviceType = self.deviceTypeComboBox.itemData(index, self.DeviceTypeRole)
498 if deviceType == self.ManualMarker: 499 if deviceType == self.ManualMarker:
499 self.connectButton.setEnabled(bool(self.__unknownPorts)) 500 self.connectButton.setEnabled(bool(self.__unknownPorts))
500 else: 501 else:
501 self.deviceIconLabel.setPixmap( 502 self.deviceIconLabel.setPixmap(Devices.getDeviceIcon(deviceType, False))
502 Devices.getDeviceIcon(deviceType, False)
503 )
504 503
505 boardName = self.deviceTypeComboBox.itemData(index, self.DeviceBoardRole) 504 boardName = self.deviceTypeComboBox.itemData(index, self.DeviceBoardRole)
506 vid = self.deviceTypeComboBox.itemData(index, self.DeviceVidRole) 505 vid = self.deviceTypeComboBox.itemData(index, self.DeviceVidRole)
507 pid = self.deviceTypeComboBox.itemData(index, self.DevicePidRole) 506 pid = self.deviceTypeComboBox.itemData(index, self.DevicePidRole)
508 serNo = self.deviceTypeComboBox.itemData(index, self.DeviceSerNoRole) 507 serNo = self.deviceTypeComboBox.itemData(index, self.DeviceSerNoRole)
688 self.__interface.write(b"\x03") 687 self.__interface.write(b"\x03")
689 688
690 self.__device.setRepl(True) 689 self.__device.setRepl(True)
691 self.replEdit.setFocus(Qt.FocusReason.OtherFocusReason) 690 self.replEdit.setFocus(Qt.FocusReason.OtherFocusReason)
692 else: 691 else:
693 self.__interface.dataReceived.disconnect(self.__processData) 692 with contextlib.suppress(TypeError):
693 self.__interface.dataReceived.disconnect(self.__processData)
694 if not self.chartButton.isChecked() and not self.filesButton.isChecked(): 694 if not self.chartButton.isChecked() and not self.filesButton.isChecked():
695 self.__disconnectFromDevice() 695 self.__disconnectFromDevice()
696 self.__device.setRepl(False) 696 self.__device.setRepl(False)
697 self.replButton.setChecked(checked) 697 self.replButton.setChecked(checked)
698 698
1143 vid, pid, port, deviceType = dlg.getData() 1143 vid, pid, port, deviceType = dlg.getData()
1144 1144
1145 self.deviceIconLabel.setPixmap( 1145 self.deviceIconLabel.setPixmap(
1146 Devices.getDeviceIcon(deviceType, False) 1146 Devices.getDeviceIcon(deviceType, False)
1147 ) 1147 )
1148 self.__device = Devices.getDevice( 1148 self.__device = Devices.getDevice(deviceType, self, vid, pid)
1149 deviceType, self, vid, pid
1150 )
1151 1149
1152 self.__lastPort = port 1150 self.__lastPort = port
1153 self.__lastDeviceType = deviceType 1151 self.__lastDeviceType = deviceType
1154 else: 1152 else:
1155 return 1153 return
1368 with EricOverrideCursor(): 1366 with EricOverrideCursor():
1369 if not self.__interface.isConnected(): 1367 if not self.__interface.isConnected():
1370 self.__connectToDevice() 1368 self.__connectToDevice()
1371 if self.__connected: 1369 if self.__connected:
1372 self.__fileManagerWidget = MicroPythonFileManagerWidget( 1370 self.__fileManagerWidget = MicroPythonFileManagerWidget(
1373 self.__interface, self.__device.supportsLocalFileAccess(), self 1371 self.__device, self
1374 ) 1372 )
1375 1373
1376 self.__ui.addSideWidget( 1374 self.__ui.addSideWidget(
1377 self.__ui.BottomSide, 1375 self.__ui.BottomSide,
1378 self.__fileManagerWidget, 1376 self.__fileManagerWidget,
1385 1383
1386 self.__fileManagerWidget.start() 1384 self.__fileManagerWidget.start()
1387 else: 1385 else:
1388 if self.__fileManagerWidget is not None: 1386 if self.__fileManagerWidget is not None:
1389 self.__fileManagerWidget.stop() 1387 self.__fileManagerWidget.stop()
1388 self.__fileManagerWidget.deleteLater()
1390 1389
1391 if not self.replButton.isChecked() and not self.chartButton.isChecked(): 1390 if not self.replButton.isChecked() and not self.chartButton.isChecked():
1392 self.__disconnectFromDevice() 1391 self.__disconnectFromDevice()
1393 1392
1394 self.__device.setFileManager(False) 1393 self.__device.setFileManager(False)
1395 self.__ui.removeSideWidget(self.__fileManagerWidget) 1394 self.__ui.removeSideWidget(self.__fileManagerWidget)
1396 1395
1397 self.__fileManagerWidget.deleteLater()
1398 self.__fileManagerWidget = None 1396 self.__fileManagerWidget = None
1399 1397
1400 self.filesButton.setChecked(checked) 1398 self.filesButton.setChecked(checked)
1401 1399
1402 ################################################################## 1400 ##################################################################
1561 Private slot to show all available information about a board. 1559 Private slot to show all available information about a board.
1562 """ 1560 """
1563 from .BoardDataDialog import BoardDataDialog 1561 from .BoardDataDialog import BoardDataDialog
1564 1562
1565 try: 1563 try:
1566 boardInfo = self.__interface.getBoardInformation() 1564 boardInfo = self.__device.getBoardInformation()
1567 1565
1568 dlg = BoardDataDialog(boardInfo) 1566 dlg = BoardDataDialog(boardInfo)
1569 dlg.exec() 1567 dlg.exec()
1570 except Exception as exc: 1568 except Exception as exc:
1571 self.__showError("getBoardInformation()", str(exc)) 1569 self.__showError("getBoardInformation()", str(exc))
1583 hasCPy = ( 1581 hasCPy = (
1584 self.__device.checkDeviceData() 1582 self.__device.checkDeviceData()
1585 and self.__device.getDeviceData()["mpy_name"] == "circuitpython" 1583 and self.__device.getDeviceData()["mpy_name"] == "circuitpython"
1586 ) 1584 )
1587 try: 1585 try:
1588 self.__interface.syncTime(self.__device.getDeviceType(), hasCPy=hasCPy) 1586 self.__device.syncTime(self.__device.getDeviceType(), hasCPy=hasCPy)
1589 1587
1590 if not quiet: 1588 if not quiet:
1591 with EricOverridenCursor(): 1589 with EricOverridenCursor():
1592 EricMessageBox.information( 1590 EricMessageBox.information(
1593 self, 1591 self,
1609 @return date and time of the connected device 1607 @return date and time of the connected device
1610 @rtype str 1608 @rtype str
1611 """ 1609 """
1612 if self.__device and self.__device.hasTimeCommands(): 1610 if self.__device and self.__device.hasTimeCommands():
1613 try: 1611 try:
1614 dateTimeString = self.__interface.getTime() 1612 dateTimeString = self.__device.getTime()
1615 try: 1613 try:
1616 date, time = dateTimeString.strip().split(None, 1) 1614 date, time = dateTimeString.strip().split(None, 1)
1617 return self.tr( 1615 return self.tr(
1618 "<h3>Device Date and Time</h3>" 1616 "<h3>Device Date and Time</h3>"
1619 "<table>" 1617 "<table>"
1668 localdatetime = time.localtime() 1666 localdatetime = time.localtime()
1669 localdate = time.strftime("%Y-%m-%d", localdatetime) 1667 localdate = time.strftime("%Y-%m-%d", localdatetime)
1670 localtime = time.strftime("%H:%M:%S", localdatetime) 1668 localtime = time.strftime("%H:%M:%S", localdatetime)
1671 1669
1672 try: 1670 try:
1673 deviceDateTimeString = self.__interface.getTime() 1671 deviceDateTimeString = self.__device.getTime()
1674 try: 1672 try:
1675 devicedate, devicetime = deviceDateTimeString.strip().split(None, 1) 1673 devicedate, devicetime = deviceDateTimeString.strip().split(None, 1)
1676 EricMessageBox.information( 1674 EricMessageBox.information(
1677 self, 1675 self,
1678 self.tr("Date and Time"), 1676 self.tr("Date and Time"),
1960 """ 1958 """
1961 from .ShowModulesDialog import ShowModulesDialog 1959 from .ShowModulesDialog import ShowModulesDialog
1962 1960
1963 if self.__connected: 1961 if self.__connected:
1964 try: 1962 try:
1965 moduleNames = self.__interface.getModules() 1963 moduleNames = self.__device.getModules()
1966 dlg = ShowModulesDialog( 1964 dlg = ShowModulesDialog(
1967 moduleNames, 1965 moduleNames,
1968 info=self.tr("Plus any modules on the filesystem."), 1966 info=self.tr("Plus any modules on the filesystem."),
1969 parent=self, 1967 parent=self,
1970 ) 1968 )

eric ide

mercurial