7 Module implementing some utility functions and the MicroPythonDevice base |
7 Module implementing some utility functions and the MicroPythonDevice base |
8 class. |
8 class. |
9 """ |
9 """ |
10 |
10 |
11 import contextlib |
11 import contextlib |
|
12 import copy |
12 import importlib |
13 import importlib |
13 import logging |
14 import logging |
14 import os |
15 import os |
15 |
16 |
16 from PyQt6.QtCore import QCoreApplication, QObject, pyqtSlot |
17 from PyQt6.QtCore import QCoreApplication, QObject, pyqtSlot |
17 from PyQt6.QtSerialPort import QSerialPortInfo |
18 from PyQt6.QtSerialPort import QSerialPortInfo |
18 from PyQt6.QtWidgets import QInputDialog |
19 from PyQt6.QtWidgets import QInputDialog |
19 |
20 |
20 from eric7 import Preferences |
21 from eric7 import Preferences |
21 from eric7.EricGui import EricPixmapCache |
22 from eric7.EricGui import EricPixmapCache |
|
23 from eric7.EricWidgets import EricMessageBox |
22 from eric7.EricWidgets.EricApplication import ericApp |
24 from eric7.EricWidgets.EricApplication import ericApp |
23 |
25 |
24 SupportedBoards = { |
26 SupportedBoards = { |
25 "esp": { |
27 "esp": { |
26 "ids": [ |
28 "ids": [ |
431 return EricPixmapCache.getIcon(iconName) |
435 return EricPixmapCache.getIcon(iconName) |
432 else: |
436 else: |
433 return EricPixmapCache.getPixmap(iconName) |
437 return EricPixmapCache.getPixmap(iconName) |
434 |
438 |
435 |
439 |
436 def getDevice(deviceType, microPythonWidget, vid, pid, boardName=""): |
440 def getDevice(deviceType, microPythonWidget, vid, pid, boardName="", serialNumber=""): |
437 """ |
441 """ |
438 Public method to instantiate a specific MicroPython device interface. |
442 Public method to instantiate a specific MicroPython device interface. |
439 |
443 |
440 @param deviceType type of the device interface |
444 @param deviceType type of the device interface |
441 @type str |
445 @type str |
445 @type int |
449 @type int |
446 @param pid product ID (only used for deviceType 'generic') |
450 @param pid product ID (only used for deviceType 'generic') |
447 @type int |
451 @type int |
448 @param boardName name of the board (defaults to "") |
452 @param boardName name of the board (defaults to "") |
449 @type str (optional) |
453 @type str (optional) |
|
454 @param serialNumber serial number of the board (defaults to "") |
|
455 @type str (optional) |
450 @return instantiated device interface |
456 @return instantiated device interface |
451 @rtype MicroPythonDevice |
457 @rtype MicroPythonDevice |
452 """ |
458 """ |
453 deviceMapping = { |
459 deviceMapping = { |
454 "bbc_microbit": ".MicrobitDevices", |
460 "bbc_microbit": ".MicrobitDevices", |
461 } |
467 } |
462 |
468 |
463 with contextlib.suppress(KeyError): |
469 with contextlib.suppress(KeyError): |
464 mod = importlib.import_module(deviceMapping[deviceType], __package__) |
470 mod = importlib.import_module(deviceMapping[deviceType], __package__) |
465 if mod: |
471 if mod: |
466 return mod.createDevice(microPythonWidget, deviceType, vid, pid, boardName) |
472 return mod.createDevice( |
|
473 microPythonWidget, deviceType, vid, pid, boardName, serialNumber |
|
474 ) |
467 |
475 |
468 # nothing specific requested or specific one failed or is not supported yet |
476 # nothing specific requested or specific one failed or is not supported yet |
469 return MicroPythonDevice(microPythonWidget, deviceType) |
477 return MicroPythonDevice(microPythonWidget, deviceType) |
470 |
478 |
471 |
479 |
487 """ |
495 """ |
488 super().__init__(parent) |
496 super().__init__(parent) |
489 |
497 |
490 self._deviceType = deviceType |
498 self._deviceType = deviceType |
491 self.microPython = microPythonWidget |
499 self.microPython = microPythonWidget |
|
500 self._deviceData = {} # dictionary with essential device data |
|
501 |
|
502 def setConnected(self, connected): |
|
503 """ |
|
504 Public method to set the connection state. |
|
505 |
|
506 Note: This method can be overwritten to perform actions upon connect |
|
507 or disconnect of the device. |
|
508 |
|
509 @param connected connection state |
|
510 @type bool |
|
511 """ |
|
512 self._deviceData = {} |
|
513 |
|
514 if connected: |
|
515 with contextlib.suppress(OSError): |
|
516 self._deviceData = self.microPython.commandsInterface().getDeviceData() |
492 |
517 |
493 def getDeviceType(self): |
518 def getDeviceType(self): |
494 """ |
519 """ |
495 Public method to get the device type. |
520 Public method to get the device type. |
496 |
521 |
497 @return type of the device |
522 @return type of the device |
498 @rtype str |
523 @rtype str |
499 """ |
524 """ |
500 return self._deviceType |
525 return self._deviceType |
|
526 |
|
527 def getDeviceData(self): |
|
528 """ |
|
529 Public method to get a copy of the determined device data. |
|
530 |
|
531 @return dictionary containing the essential device data |
|
532 @rtype dict |
|
533 """ |
|
534 return copy.deepcopy(self._deviceData) |
|
535 |
|
536 def checkDeviceData(self): |
|
537 """ |
|
538 Public method to check the validity of the device data determined during |
|
539 connecting the device. |
|
540 |
|
541 @return flag indicating valid device data |
|
542 @rtype bool |
|
543 """ |
|
544 if bool(self._deviceData): |
|
545 return True |
|
546 else: |
|
547 EricMessageBox.critical( |
|
548 None, |
|
549 self.tr("Show MicroPython Versions"), |
|
550 self.tr( |
|
551 """<p>The device data is not available. Try to connect to the""" |
|
552 """ device again. Aborting...</p>""" |
|
553 ).format(self.getDeviceType()), |
|
554 ) |
|
555 return False |
501 |
556 |
502 def setButtons(self): |
557 def setButtons(self): |
503 """ |
558 """ |
504 Public method to enable the supported action buttons. |
559 Public method to enable the supported action buttons. |
505 """ |
560 """ |