752 Public method to set the time of the connected device to the local |
752 Public method to set the time of the connected device to the local |
753 computer's time. |
753 computer's time. |
754 |
754 |
755 @exception OSError raised to indicate an issue with the device |
755 @exception OSError raised to indicate an issue with the device |
756 """ |
756 """ |
|
757 # rtc_time[0] - year 4 digit |
|
758 # rtc_time[1] - month 1..12 |
|
759 # rtc_time[2] - day 1..31 |
|
760 # rtc_time[3] - weekday 1..7 1=Monday |
|
761 # rtc_time[4] - hour 0..23 |
|
762 # rtc_time[5] - minute 0..59 |
|
763 # rtc_time[6] - second 0..59 |
|
764 # rtc_time[7] - yearday 1..366 |
|
765 # rtc_time[8] - isdst 0, 1, or -1 |
757 now = time.localtime(time.time()) |
766 now = time.localtime(time.time()) |
758 commands = [ |
767 commands = [ |
759 "\n".join([ |
768 "\n".join([ |
760 "def set_time(rtc_time):", |
769 "def set_time(rtc_time):", |
761 " rtc = None", |
770 " rtc = None", |
762 " try:", # Pyboard (it doesn't have machine.RTC()) |
771 # Pyboard (pyboard doesn't have machine.RTC()). |
763 " import pyb as __pyb_", |
772 # The pyb.RTC.datetime function takes the arguments in the |
764 " rtc = __pyb_.RTC()", |
773 # order: |
765 " clock_time = rtc_time[:3] +" |
774 # (year, month, day, weekday, hour, minute, second, subseconds) |
766 " (rtc_time[6] + 1,) + rtc_time[3:6] + (0,)", |
775 # http://docs.micropython.org/en/latest/library/pyb.RTC.html |
767 " rtc.datetime(clock_time)", |
776 # #pyb.RTC.datetime |
768 " del __pyb_", |
777 " try:", |
|
778 " import pyb", |
|
779 " rtc = pyb.RTC()", |
|
780 " rtc.datetime(rtc_time)", |
769 " except Exception:", |
781 " except Exception:", |
770 " try:", |
782 " try:", |
771 " import machine as __machine_", |
783 # PyCom's machine.RTC takes its arguments in a slightly |
772 " rtc = __machine_.RTC()", |
784 # different order than the official machine.RTC. |
773 " try:", # ESP8266 may use rtc.datetime() |
785 # (year, month, day, hour, minute, second[, microsecond[, |
774 " clock_time = rtc_time[:3] +" |
786 # tzinfo]]) |
775 " (rtc_time[6],) + rtc_time[3:6] + (0,)", |
787 # https://docs.pycom.io/firmwareapi/pycom/machine/rtc/ |
776 " rtc.datetime(clock_time)", |
788 # #rtc-init-datetime-none-source-rtc-internal-rc |
777 " except Exception:", # ESP32 uses rtc.init() |
789 " import pycom", |
778 " import os", |
790 " rtc_time2 = (rtc_time[0], rtc_time[1]," |
779 " if 'LoBo' in os.uname()[0]:", # LoBo MPy |
791 " rtc_time[2], rtc_time[4], rtc_time[5], rtc_time[6])", |
780 " clock_time = rtc_time + (0,)", |
792 " import machine", |
781 " else:", |
793 " rtc = machine.RTC()", |
782 " clock_time = rtc_time[:3] +" |
794 " rtc.init(rtc_time2)", |
783 " (rtc_time[6],) + rtc_time[3:6] + (0,)", |
|
784 " rtc.init(clock_time)", |
|
785 " del __machine_", |
|
786 " except:", |
795 " except:", |
787 " try:", |
796 " try:", |
788 " import rtc as __rtc_", |
797 # The machine.RTC documentation was incorrect and doesn't agree |
789 " import time as __time_", |
798 # with the code, so no link is presented here. The order of the |
790 " clock=__rtc_.RTC()", |
799 # arguments is the same as the pyboard except for LoBo MPy. |
791 " clock.datetime = __time_.struct_time(" |
800 " import machine", |
792 "rtc_time + (-1, -1))", |
801 " rtc = machine.RTC()", |
793 " del __rtc_, __time_", |
802 " try:", # ESP8266 may use rtc.datetime() |
|
803 " rtc.datetime(rtc_time)", |
|
804 " except Exception:", # ESP32 uses rtc.init() |
|
805 " import os", |
|
806 " if 'LoBo' in os.uname()[0]:", # LoBo MPy |
|
807 " clock_time = rtc_time[:3] +" |
|
808 " rtc_time[4:7] + (rtc_time[3], 0)", |
|
809 " else:", |
|
810 " clock_time = rtc_time", |
|
811 " rtc.init(clock_time)", |
794 " except:", |
812 " except:", |
795 " pass", |
813 " try:", |
|
814 # Check for CircuitPython devices |
|
815 " import rtc", |
|
816 " import time", |
|
817 " clock = rtc.RTC()", |
|
818 " clock_time = rtc_time[:3] + rtc_time[4:7]" |
|
819 " + (rtc_time[3], -1, -1)", |
|
820 " clock.datetime = time.struct_time(" |
|
821 "clock_time)", |
|
822 " except:", |
|
823 # Check for the Raspberry Pi Pico - machine.RTC doesn't exist |
|
824 " try:", |
|
825 " import os", |
|
826 " if os.uname().sysname == 'rp2':", |
|
827 " setup_0 = rtc_time[0] << 12 |" |
|
828 " rtc_time[1] << 8 | rtc_time[2]", |
|
829 " setup_1 = (rtc_time[3] % 7) << 24" |
|
830 " | rtc_time[4] << 16 | rtc_time[5] << 8 | rtc_time[6]", |
|
831 " machine.mem32[0x4005c004] =" |
|
832 " setup_0", |
|
833 " machine.mem32[0x4005c008] =" |
|
834 " setup_1", |
|
835 " machine.mem32[0x4005c00c] |=" |
|
836 " 0x10", |
|
837 " except:", |
|
838 " pass", |
796 ]), |
839 ]), |
797 "set_time({0})".format((now.tm_year, now.tm_mon, now.tm_mday, |
840 "set_time({0})".format(( |
798 now.tm_hour, now.tm_min, now.tm_sec, |
841 now.tm_year, now.tm_mon, now.tm_mday, now.tm_wday + 1, |
799 now.tm_wday)), |
842 now.tm_hour, now.tm_min, now.tm_sec, 0 |
|
843 )), |
800 "del set_time", |
844 "del set_time", |
801 ] |
845 ] |
802 out, err = self.execute(commands) |
846 out, err = self.execute(commands) |
803 if err: |
847 if err: |
804 raise OSError(self.__shortError(err)) |
848 raise OSError(self.__shortError(err)) |