src/eric7/MicroPython/Devices/MicrobitDevices.py

branch
eric7
changeset 9765
6378da868bb0
parent 9763
52f982c08301
child 9766
f0e22f3a5878
equal deleted inserted replaced
9764:57496966803c 9765:6378da868bb0
6 """ 6 """
7 Module implementing the device interface class for BBC micro:bit and 7 Module implementing the device interface class for BBC micro:bit and
8 Calliope mini boards. 8 Calliope mini boards.
9 """ 9 """
10 10
11 import ast
11 import contextlib 12 import contextlib
12 import os 13 import os
13 import shutil 14 import shutil
14 15
15 from PyQt6.QtCore import QStandardPaths, QUrl, pyqtSlot 16 from PyQt6.QtCore import QStandardPaths, QUrl, pyqtSlot
19 from eric7 import Globals, Preferences 20 from eric7 import Globals, Preferences
20 from eric7.EricWidgets import EricFileDialog, EricMessageBox 21 from eric7.EricWidgets import EricFileDialog, EricMessageBox
21 from eric7.EricWidgets.EricApplication import ericApp 22 from eric7.EricWidgets.EricApplication import ericApp
22 from eric7.SystemUtilities import FileSystemUtilities 23 from eric7.SystemUtilities import FileSystemUtilities
23 24
25 from ..MicroPythonWidget import HAS_QTCHART
24 from . import FirmwareGithubUrls 26 from . import FirmwareGithubUrls
25 from .DeviceBase import BaseDevice 27 from .DeviceBase import BaseDevice
26 from ..MicroPythonWidget import HAS_QTCHART
27 28
28 29
29 class MicrobitDevice(BaseDevice): 30 class MicrobitDevice(BaseDevice):
30 """ 31 """
31 Class implementing the device for BBC micro:bit and Calliope mini boards. 32 Class implementing the device for BBC micro:bit and Calliope mini boards.
528 "f = fd.write", 529 "f = fd.write",
529 ] 530 ]
530 for line in script.splitlines(): 531 for line in script.splitlines():
531 commands.append("f(" + repr(line + "\n") + ")") 532 commands.append("f(" + repr(line + "\n") + ")")
532 commands.append("fd.close()") 533 commands.append("fd.close()")
533 out, err = self.microPython.commandsInterface().execute(commands) 534 out, err = self.microPython.deviceInterface().execute(commands)
534 if err: 535 if err:
535 EricMessageBox.critical( 536 EricMessageBox.critical(
536 self.microPython, 537 self.microPython,
537 title, 538 title,
538 self.tr( 539 self.tr(
549 """ 550 """
550 Private slot to reset the connected device. 551 Private slot to reset the connected device.
551 """ 552 """
552 if self.getDeviceType() == "bbc_microbit": 553 if self.getDeviceType() == "bbc_microbit":
553 # BBC micro:bit 554 # BBC micro:bit
554 self.microPython.commandsInterface().execute( 555 self.microPython.deviceInterface().execute(
555 [ 556 [
556 "import microbit", 557 "import microbit",
557 "microbit.reset()", 558 "microbit.reset()",
558 ] 559 ]
559 ) 560 )
560 else: 561 else:
561 # Calliope mini 562 # Calliope mini
562 self.microPython.commandsInterface().execute( 563 self.microPython.deviceInterface().execute(
563 [ 564 [
564 "import calliope_mini", 565 "import calliope_mini",
565 "calliope_mini.reset()", 566 "calliope_mini.reset()",
566 ] 567 ]
567 ) 568 )
630 self.tr("DAPLink Firmware"), 631 self.tr("DAPLink Firmware"),
631 Preferences.getMicroPython("CalliopeDAPLinkUrl"), 632 Preferences.getMicroPython("CalliopeDAPLinkUrl"),
632 ), 633 ),
633 ] 634 ]
634 635
636 ##################################################################
637 ## Methods below implement the file system commands
638 ##################################################################
639
640 def ls(self, dirname=""):
641 """
642 Public method to get a directory listing of the connected device.
643
644 @param dirname name of the directory to be listed
645 @type str
646 @return tuple containg the directory listing
647 @rtype tuple of str
648 @exception OSError raised to indicate an issue with the device
649 """
650 # BBC micro:bit does not support directories
651 commands = [
652 "import os as __os_",
653 "print(__os_.listdir())",
654 "del __os_",
655 ]
656 out, err = self._interface.execute(commands)
657 if err:
658 raise OSError(self._shortError(err))
659 return ast.literal_eval(out.decode("utf-8"))
660
661 def lls(self, dirname="", fullstat=False, showHidden=False):
662 """
663 Public method to get a long directory listing of the connected device
664 including meta data.
665
666 @param dirname name of the directory to be listed
667 @type str
668 @param fullstat flag indicating to return the full stat() tuple
669 @type bool
670 @param showHidden flag indicating to show hidden files as well
671 @type bool
672 @return list containing the directory listing with tuple entries of
673 the name and and a tuple of mode, size and time (if fullstat is
674 false) or the complete stat() tuple. 'None' is returned in case the
675 directory doesn't exist.
676 @rtype tuple of (str, tuple)
677 @exception OSError raised to indicate an issue with the device
678 """
679 # BBC micro:bit does not support directories
680 commands = [
681 "import os as __os_",
682 "\n".join(
683 [
684 "def is_visible(filename, showHidden):",
685 " return showHidden or "
686 "(filename[0] != '.' and filename[-1] != '~')",
687 ]
688 ),
689 "\n".join(
690 [
691 "def stat(filename):",
692 " size = __os_.size(filename)",
693 " return (0, 0, 0, 0, 0, 0, size, 0, 0, 0)",
694 ]
695 ),
696 "\n".join(
697 [
698 "def listdir_stat(showHidden):",
699 " files = __os_.listdir()",
700 " return list((f, stat(f)) for f in files if"
701 " is_visible(f,showHidden))",
702 ]
703 ),
704 "print(listdir_stat({0}))".format(showHidden),
705 "del __os_, stat, listdir_stat, is_visible",
706 ]
707 out, err = self._interface.execute(commands)
708 if err:
709 raise OSError(self._shortError(err))
710 fileslist = ast.literal_eval(out.decode("utf-8"))
711 if fileslist is None:
712 return None
713 else:
714 if fullstat:
715 return fileslist
716 else:
717 return [(f, (s[0], s[6], s[8])) for f, s in fileslist]
718
719 def pwd(self):
720 """
721 Public method to get the current directory of the connected device.
722
723 @return current directory
724 @rtype str
725 """
726 # BBC micro:bit does not support directories
727 return ""
728
729 ##################################################################
730 ## time related methods below
731 ##################################################################
732
733 def _getSetTimeCode(self):
734 """
735 Protected method to get the device code to set the time.
736
737 Note: This method must be implemented in the various device specific
738 subclasses.
739
740 @return code to be executed on the connected device to set the time
741 @rtype str
742 """
743 # rtc_time[0] - year 4 digit
744 # rtc_time[1] - month 1..12
745 # rtc_time[2] - day 1..31
746 # rtc_time[3] - weekday 1..7 1=Monday
747 # rtc_time[4] - hour 0..23
748 # rtc_time[5] - minute 0..59
749 # rtc_time[6] - second 0..59
750 # rtc_time[7] - yearday 1..366
751 # rtc_time[8] - isdst 0, 1, or -1
752 return ""
753
635 754
636 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): 755 def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber):
637 """ 756 """
638 Function to instantiate a MicroPython device object. 757 Function to instantiate a MicroPython device object.
639 758

eric ide

mercurial