744 |
744 |
745 ################################################################## |
745 ################################################################## |
746 ## non-filesystem related methods below |
746 ## non-filesystem related methods below |
747 ################################################################## |
747 ################################################################## |
748 |
748 |
749 def version(self): |
749 def getDeviceData(self): |
750 """ |
750 """ |
751 Public method to get the MicroPython version information of the |
751 Public method to get some essential data for the connected board. |
752 connected device. |
752 |
753 |
753 @return dictionary containing the determined data |
754 @return dictionary containing the version information |
|
755 @rtype dict |
754 @rtype dict |
756 @exception OSError raised to indicate an issue with the device |
755 @exception OSError raised to indicate an issue with the device |
757 """ |
756 """ |
758 commands = [ |
757 commands = [ |
|
758 "res = {}", # __IGNORE_WARNING_M613__ |
759 "import os as __os_", |
759 "import os as __os_", |
760 "print(__os_.uname())", |
760 "uname = __os_.uname()", |
761 "del __os_", |
761 "res['sysname'] = uname.sysname", |
762 ] |
762 "res['nodename'] = uname.nodename", |
763 out, err = self.execute(commands) |
763 "res['release'] = uname.release", |
764 if err: |
764 "res['version'] = uname.version", |
765 raise OSError(self.__shortError(err)) |
765 "res['machine'] = uname.machine", |
766 |
|
767 rawOutput = out.decode("utf-8").strip() |
|
768 rawOutput = rawOutput[1:-1] |
|
769 items = rawOutput.split(",") |
|
770 result = {} |
|
771 for item in items: |
|
772 key, value = item.strip().split("=") |
|
773 result[key.strip()] = value.strip()[1:-1] |
|
774 return result |
|
775 |
|
776 def getImplementation(self): |
|
777 """ |
|
778 Public method to get some implementation information of the connected |
|
779 device. |
|
780 |
|
781 @return dictionary containing the implementation information |
|
782 @rtype dict |
|
783 @exception OSError raised to indicate an issue with the device |
|
784 """ |
|
785 commands = [ |
|
786 "import sys as __sys_", |
766 "import sys as __sys_", |
787 "res = {}", # __IGNORE_WARNING_M613__ |
767 "res['py_platform'] = __sys_.platform", |
|
768 "res['py_version'] = __sys_.version", |
788 "\n".join( |
769 "\n".join( |
789 [ |
770 [ |
790 "try:", |
771 "try:", |
791 " res['name'] = __sys_.implementation.name", |
772 " res['mpy_name'] = __sys_.implementation.name", |
792 "except AttributeError:", |
773 "except AttributeError:", |
793 " res['name'] = 'unknown'", |
774 " res['mpy_name'] = 'unknown'", |
794 ] |
775 ] |
795 ), |
776 ), |
796 "\n".join( |
777 "\n".join( |
797 [ |
778 [ |
798 "try:", |
779 "try:", |
799 " res['version'] = '.'.join((str(i) for i in" |
780 " res['mpy_version'] = '.'.join((str(i) for i in" |
800 " __sys_.implementation.version))", |
781 " __sys_.implementation.version))", |
801 "except AttributeError:", |
782 "except AttributeError:", |
802 " res['version'] = 'unknown'", |
783 " res['mpy_version'] = 'unknown'", |
803 ] |
784 ] |
804 ), |
785 ), |
805 "\n".join( |
786 "\n".join( |
806 [ |
787 [ |
807 "try:", |
788 "try:", |
808 " import pimoroni", |
789 " import pimoroni as __pimoroni_", |
809 " res['variant'] = 'Pimoroni'", |
790 " res['mpy_variant'] = 'Pimoroni'", |
810 " del pimoroni", |
791 " del __pimoroni_", |
811 "except ImportError:", |
792 "except ImportError:", |
812 " res['variant'] = ''", |
793 " res['mpy_variant'] = ''", |
813 ] |
794 ] |
814 ), |
795 ), |
815 "print(res)", |
796 "print(res)", |
816 "del res, __sys_", |
797 "del res, __os_, __sys_", |
817 ] |
798 ] |
818 out, err = self.execute(commands) |
799 out, err = self.execute(commands) |
819 if err: |
800 if err: |
820 raise OSError(self.__shortError(err)) |
801 raise OSError(self.__shortError(err)) |
821 return ast.literal_eval(out.decode("utf-8")) |
802 return ast.literal_eval(out.decode("utf-8")) |
942 out, err = self.execute(commands) |
923 out, err = self.execute(commands) |
943 if err: |
924 if err: |
944 raise OSError(self.__shortError(err)) |
925 raise OSError(self.__shortError(err)) |
945 return ast.literal_eval(out.decode("utf-8")) |
926 return ast.literal_eval(out.decode("utf-8")) |
946 |
927 |
947 def syncTime(self, deviceType): |
928 def syncTime(self, deviceType, hasCPy=False): |
948 """ |
929 """ |
949 Public method to set the time of the connected device to the local |
930 Public method to set the time of the connected device to the local |
950 computer's time. |
931 computer's time. |
951 |
932 |
952 @param deviceType type of board to sync time to |
933 @param deviceType type of board to sync time to |
953 @type str |
934 @type str |
|
935 @param hasCPy flag indicating that the device has CircuitPython loadede |
|
936 (defaults to False) |
|
937 @type bool |
954 @exception OSError raised to indicate an issue with the device |
938 @exception OSError raised to indicate an issue with the device |
955 """ |
939 """ |
956 # rtc_time[0] - year 4 digit |
940 # rtc_time[0] - year 4 digit |
957 # rtc_time[1] - month 1..12 |
941 # rtc_time[1] - month 1..12 |
958 # rtc_time[2] - day 1..31 |
942 # rtc_time[2] - day 1..31 |
960 # rtc_time[4] - hour 0..23 |
944 # rtc_time[4] - hour 0..23 |
961 # rtc_time[5] - minute 0..59 |
945 # rtc_time[5] - minute 0..59 |
962 # rtc_time[6] - second 0..59 |
946 # rtc_time[6] - second 0..59 |
963 # rtc_time[7] - yearday 1..366 |
947 # rtc_time[7] - yearday 1..366 |
964 # rtc_time[8] - isdst 0, 1, or -1 |
948 # rtc_time[8] - isdst 0, 1, or -1 |
965 if deviceType == "pyboard": |
949 if deviceType == "circuitpython" or hasCPy: |
|
950 set_time = "\n".join( |
|
951 [ |
|
952 "def set_time(rtc_time):", |
|
953 " import rtc", |
|
954 " import time", |
|
955 " clock = rtc.RTC()", |
|
956 " clock_time = rtc_time[:3] + rtc_time[4:7] + (rtc_time[3]," |
|
957 " rtc_time[7], rtc_time[8])", |
|
958 " clock.datetime = time.struct_time(clock_time)", |
|
959 ] |
|
960 ) |
|
961 elif deviceType == "pyboard": |
966 # Pyboard (pyboard doesn't have machine.RTC()). |
962 # Pyboard (pyboard doesn't have machine.RTC()). |
967 # The pyb.RTC.datetime function takes the arguments in the |
963 # The pyb.RTC.datetime function takes the arguments in the |
968 # order: (year, month, day, weekday, hour, minute, second, |
964 # order: (year, month, day, weekday, hour, minute, second, |
969 # subseconds) |
965 # subseconds) |
970 # http://docs.micropython.org/en/latest/library/pyb.RTC.html |
966 # http://docs.micropython.org/en/latest/library/pyb.RTC.html |
996 " else:", |
992 " else:", |
997 " clock_time = rtc_time[:7] + (0,)", |
993 " clock_time = rtc_time[:7] + (0,)", |
998 " rtc.init(clock_time)", |
994 " rtc.init(clock_time)", |
999 ] |
995 ] |
1000 ) |
996 ) |
1001 elif deviceType == "circuitpython": |
|
1002 set_time = "\n".join( |
|
1003 [ |
|
1004 "def set_time(rtc_time):", |
|
1005 " import rtc", |
|
1006 " import time", |
|
1007 " clock = rtc.RTC()", |
|
1008 " clock_time = rtc_time[:3] + rtc_time[4:7] + (rtc_time[3]," |
|
1009 " rtc_time[7], rtc_time[8])", |
|
1010 " clock.datetime = time.struct_time(clock_time)", |
|
1011 ] |
|
1012 ) |
|
1013 elif deviceType in ("bbc_microbit", "calliope"): |
997 elif deviceType in ("bbc_microbit", "calliope"): |
1014 # BBC micro:bit and Calliope mini don't support time commands |
998 # BBC micro:bit and Calliope mini with MicroPython don't support |
|
999 # time commands. |
1015 return |
1000 return |
1016 elif deviceType == "rp2040": |
1001 elif deviceType == "rp2040": |
1017 # Raspberry Pi Pico (RP2040) - machine.RTC doesn't exist |
1002 # Raspberry Pi Pico (RP2040) - machine.RTC doesn't exist |
1018 set_time = "\n".join( |
1003 set_time = "\n".join( |
1019 [ |
1004 [ |