]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/agnostic/time.py
Agnostic now a package. Interactive testing now relies on monotonic. time now under...
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / agnostic / time.py
1 from adafruit_blinka import agnostic
2 if agnostic.implementation == "circuitpython":
3     from time import *
4 elif agnostic.implementation == "micropython":
5     import utime
6     from utime import sleep
7
8     from ucollections import namedtuple
9     _struct_time = namedtuple("struct_time", ("tm_year", "tm_mon", "tm_mday", "tm_hour", "tm_min", "tm_sec", "tm_wday", "tm_yday", "tm_isdst"))
10
11     def marshal_time(tm_year, tm_mon, tm_mday, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=-1, tm_yday=-1, tm_isdst=-1):
12         _struct_time(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)
13
14     def struct_time(t):
15         return marshal_time(*t)
16
17     total_ms = 0
18     prev_ticks_ms = utime.ticks_ms()
19     def monotonic():
20         """
21         Assumes that monotonic is called more frequently than the wraparound of micropython's utime.ticks_ms()
22         """
23         global prev_ticks_ms, total_ms
24         ticks_ms = utime.ticks_ms()
25         total_ms += utime.ticks_diff(ticks_ms, prev_ticks_ms)
26         prev_ticks_ms = ticks_ms
27         return total_ms * 0.001