src/eric7/MicroPython/MicroPythonWidget.py

branch
mpy_network
changeset 9990
54c614d91eff
parent 9979
dbafba79461d
child 10008
c5bcafe3485c
equal deleted inserted replaced
9989:286c2a21f36f 9990:54c614d91eff
4 # 4 #
5 5
6 """ 6 """
7 Module implementing the MicroPython REPL widget. 7 Module implementing the MicroPython REPL widget.
8 """ 8 """
9
10 # TODO: refactor the code such to have the MicroPythonWidget as the top level
11 # container and a MicroPythonReplWidget containing the REPL related stuff
12 # (incl. device status line and zoom widget ?)
9 13
10 import contextlib 14 import contextlib
11 import functools 15 import functools
12 import os 16 import os
13 import re 17 import re
55 HAS_QTCHART = True 59 HAS_QTCHART = True
56 except ImportError: 60 except ImportError:
57 HAS_QTCHART = False 61 HAS_QTCHART = False
58 62
59 try: 63 try:
60 from .MicroPythonDeviceInterface import MicroPythonDeviceInterface 64 from .MicroPythonSerialDeviceInterface import MicroPythonSerialDeviceInterface
61 65
62 HAS_QTSERIALPORT = True 66 HAS_QTSERIALPORT = True
63 except ImportError: 67 except ImportError:
64 HAS_QTSERIALPORT = False 68 HAS_QTSERIALPORT = False
65 69
209 DeviceBoardRole = Qt.ItemDataRole.UserRole + 1 213 DeviceBoardRole = Qt.ItemDataRole.UserRole + 1
210 DevicePortRole = Qt.ItemDataRole.UserRole + 2 214 DevicePortRole = Qt.ItemDataRole.UserRole + 2
211 DeviceVidRole = Qt.ItemDataRole.UserRole + 3 215 DeviceVidRole = Qt.ItemDataRole.UserRole + 3
212 DevicePidRole = Qt.ItemDataRole.UserRole + 4 216 DevicePidRole = Qt.ItemDataRole.UserRole + 4
213 DeviceSerNoRole = Qt.ItemDataRole.UserRole + 5 217 DeviceSerNoRole = Qt.ItemDataRole.UserRole + 5
218 DeviceInterfaceTypeRole = Qt.ItemDataRole.UserRole + 6
214 219
215 dataReceived = pyqtSignal(bytes) 220 dataReceived = pyqtSignal(bytes)
216 221
217 ManualMarker = "<manual>" 222 ManualMarker = "<manual>"
218 223
288 293
289 self.__unknownPorts = [] 294 self.__unknownPorts = []
290 self.__lastPort = None 295 self.__lastPort = None
291 self.__lastDeviceType = None 296 self.__lastDeviceType = None
292 297
293 if HAS_QTSERIALPORT: 298 self.__interface = None
294 self.__interface = MicroPythonDeviceInterface(self)
295 else:
296 self.__interface = None
297 self.__device = None 299 self.__device = None
298 self.__connected = False 300 self.__connected = False
299 self.__linkConnected = False 301 self.__linkConnected = False
300 self.__setConnected(False) 302 self.__setConnected(False)
301 303
322 self.__origReplEditMouseReleaseEvent = self.replEdit.mouseReleaseEvent 324 self.__origReplEditMouseReleaseEvent = self.replEdit.mouseReleaseEvent
323 self.replEdit.mouseReleaseEvent = self.__replEditMouseReleaseEvent 325 self.replEdit.mouseReleaseEvent = self.__replEditMouseReleaseEvent
324 326
325 self.replEdit.customContextMenuRequested.connect(self.__showContextMenu) 327 self.replEdit.customContextMenuRequested.connect(self.__showContextMenu)
326 self.__ui.preferencesChanged.connect(self.__handlePreferencesChanged) 328 self.__ui.preferencesChanged.connect(self.__handlePreferencesChanged)
327 self.__ui.preferencesChanged.connect(self.__interface.handlePreferencesChanged)
328 329
329 self.__handlePreferencesChanged() 330 self.__handlePreferencesChanged()
330 331
331 charFormat = self.replEdit.currentCharFormat() 332 charFormat = self.replEdit.currentCharFormat()
332 self.DefaultForeground = charFormat.foreground() 333 self.DefaultForeground = charFormat.foreground()
373 ) 374 )
374 self.deviceTypeComboBox.setItemData(index, vid, self.DeviceVidRole) 375 self.deviceTypeComboBox.setItemData(index, vid, self.DeviceVidRole)
375 self.deviceTypeComboBox.setItemData(index, pid, self.DevicePidRole) 376 self.deviceTypeComboBox.setItemData(index, pid, self.DevicePidRole)
376 self.deviceTypeComboBox.setItemData( 377 self.deviceTypeComboBox.setItemData(
377 index, serialNumber, self.DeviceSerNoRole 378 index, serialNumber, self.DeviceSerNoRole
379 )
380 self.deviceTypeComboBox.setItemData(
381 index, "serial", self.DeviceInterfaceTypeRole
378 ) 382 )
379 383
380 else: 384 else:
381 supportedMessage = self.tr("No supported devices detected.") 385 supportedMessage = self.tr("No supported devices detected.")
382 386
476 if Preferences.getMicroPython("ReplLineWrap"): 480 if Preferences.getMicroPython("ReplLineWrap"):
477 self.replEdit.setLineWrapMode(QTextEdit.LineWrapMode.WidgetWidth) 481 self.replEdit.setLineWrapMode(QTextEdit.LineWrapMode.WidgetWidth)
478 else: 482 else:
479 self.replEdit.setLineWrapMode(QTextEdit.LineWrapMode.NoWrap) 483 self.replEdit.setLineWrapMode(QTextEdit.LineWrapMode.NoWrap)
480 484
485 if self.__interface is not None:
486 self.__interface.handlePreferencesChanged
487
481 if self.__chartWidget is not None: 488 if self.__chartWidget is not None:
482 self.__chartWidget.preferencesChanged() 489 self.__chartWidget.preferencesChanged()
483 490
484 def deviceInterface(self): 491 def deviceInterface(self):
485 """ 492 """
620 627
621 @param connected connection state 628 @param connected connection state
622 @type bool 629 @type bool
623 """ 630 """
624 self.__connected = connected 631 self.__connected = connected
625 self.__linkConnected = self.__interface.isConnected() 632 self.__linkConnected = bool(self.__interface) and self.__interface.isConnected()
626 633
627 self.deviceConnectedLed.setOn(self.__linkConnected) 634 self.deviceConnectedLed.setOn(self.__linkConnected)
628 if self.__fileManagerWidget: 635 if self.__fileManagerWidget:
629 self.__fileManagerWidget.deviceConnectedLed.setOn(connected) 636 self.__fileManagerWidget.deviceConnectedLed.setOn(connected)
630 637
721 728
722 self.__device.setRepl(True) 729 self.__device.setRepl(True)
723 self.replEdit.setFocus(Qt.FocusReason.OtherFocusReason) 730 self.replEdit.setFocus(Qt.FocusReason.OtherFocusReason)
724 else: 731 else:
725 with contextlib.suppress(TypeError): 732 with contextlib.suppress(TypeError):
726 self.__interface.dataReceived.disconnect(self.__processData) 733 if self.__interface is not None:
734 self.__interface.dataReceived.disconnect(self.__processData)
727 if not self.chartButton.isChecked() and not self.filesButton.isChecked(): 735 if not self.chartButton.isChecked() and not self.filesButton.isChecked():
728 self.__disconnectFromDevice() 736 self.__disconnectFromDevice()
729 self.__device.setRepl(False) 737 self.__device.setRepl(False)
730 self.replButton.setChecked(checked) 738 self.replButton.setChecked(checked)
731 739
1132 # return with device path prepended 1140 # return with device path prepended
1133 return "/dev/{0}".format(portName) 1141 return "/dev/{0}".format(portName)
1134 else: 1142 else:
1135 return "" 1143 return ""
1136 1144
1137 def getCurrentBoard(self):
1138 """
1139 Public method to get the board name of the selected device.
1140
1141 @return board name of the selected device
1142 @rtype str
1143 """
1144 boardName = self.deviceTypeComboBox.currentData(self.DeviceBoardRole)
1145 return boardName
1146
1147 def getDevice(self): 1145 def getDevice(self):
1148 """ 1146 """
1149 Public method to get a reference to the current device. 1147 Public method to get a reference to the current device.
1150 1148
1151 @return reference to the current device 1149 @return reference to the current device
1180 Private method to connect to the selected device. 1178 Private method to connect to the selected device.
1181 1179
1182 @param withAutostart flag indicating to start the repl and file manager 1180 @param withAutostart flag indicating to start the repl and file manager
1183 automatically 1181 automatically
1184 @type bool 1182 @type bool
1183 @exception ValueError raised to indicate an unsupported interface type
1185 """ 1184 """
1186 from .ConnectionSelectionDialog import ConnectionSelectionDialog 1185 from .ConnectionSelectionDialog import ConnectionSelectionDialog
1187 1186
1188 port = self.getCurrentPort() 1187 interfaceType = (
1189 if not port: 1188 self.deviceTypeComboBox.currentData(self.DeviceInterfaceTypeRole)
1190 with EricOverridenCursor(): 1189 or "serial"
1191 dlg = ConnectionSelectionDialog( 1190 ) # 'serial' is the default
1192 self.__unknownPorts, self.__lastPort, self.__lastDeviceType 1191
1193 ) 1192 if interfaceType not in ("serial", "webrepl"):
1194 if dlg.exec() == QDialog.DialogCode.Accepted: 1193 raise ValueError(
1195 vid, pid, port, deviceType = dlg.getData() 1194 "Unsupported interface type detected ('{0}')".format(interfaceType)
1196 1195 )
1197 self.deviceIconLabel.setPixmap( 1196
1198 Devices.getDeviceIcon(deviceType, False) 1197 if interfaceType == "serial":
1198 port = self.getCurrentPort()
1199 if not port:
1200 with EricOverridenCursor():
1201 dlg = ConnectionSelectionDialog(
1202 self.__unknownPorts, self.__lastPort, self.__lastDeviceType
1199 ) 1203 )
1200 self.__device = Devices.getDevice(deviceType, self, vid, pid) 1204 if dlg.exec() == QDialog.DialogCode.Accepted:
1201 1205 vid, pid, port, deviceType = dlg.getData()
1202 self.__lastPort = port 1206
1203 self.__lastDeviceType = deviceType 1207 self.deviceIconLabel.setPixmap(
1204 else: 1208 Devices.getDeviceIcon(deviceType, False)
1205 return 1209 )
1210 self.__device = Devices.getDevice(deviceType, self, vid, pid)
1211
1212 self.__lastPort = port
1213 self.__lastDeviceType = deviceType
1214 else:
1215 return
1216
1217 self.__interface = MicroPythonSerialDeviceInterface(self)
1218 elif interfaceType == "webrepl":
1219 # TODO: not yet implemented
1220 return
1206 1221
1207 if self.__interface.connectToDevice(port): 1222 if self.__interface.connectToDevice(port):
1208 deviceResponding = self.__interface.probeDevice() 1223 deviceResponding = self.__interface.probeDevice()
1209 self.__setConnected(deviceResponding) 1224 self.__setConnected(deviceResponding)
1210 self.__device.setConnected(deviceResponding) 1225 self.__device.setConnected(deviceResponding)
1248 def __disconnectFromDevice(self): 1263 def __disconnectFromDevice(self):
1249 """ 1264 """
1250 Private method to disconnect from the device. 1265 Private method to disconnect from the device.
1251 """ 1266 """
1252 self.__device and self.__device.setConnected(False) 1267 self.__device and self.__device.setConnected(False)
1253 self.__interface.disconnectFromDevice()
1254 self.__setConnected(False) 1268 self.__setConnected(False)
1269
1270 if self.__interface is not None:
1271 self.__interface.disconnectFromDevice()
1272 self.__interface.deleteLater()
1273 self.__interface = None
1255 1274
1256 @pyqtSlot() 1275 @pyqtSlot()
1257 def on_runButton_clicked(self): 1276 def on_runButton_clicked(self):
1258 """ 1277 """
1259 Private slot to execute the script of the active editor on the 1278 Private slot to execute the script of the active editor on the

eric ide

mercurial