]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/agnostic/time.py
Merge pull request #275 from hhk7734/meson-g12
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / agnostic / time.py
1 """Platform agnostic time implementation"""
2
3 from adafruit_blinka import agnostic
4
5 # We intentionally are patching into this namespace so skip the wildcard check.
6 # pylint: disable=unused-wildcard-import,wildcard-import
7
8 if agnostic.implementation == "circuitpython":
9     from time import *
10 elif agnostic.implementation == "micropython":
11     import utime
12     from utime import sleep
13
14     from ucollections import namedtuple
15     _struct_time = namedtuple("struct_time", ("tm_year", "tm_mon", "tm_mday",
16                                               "tm_hour", "tm_min", "tm_sec",
17                                               "tm_wday", "tm_yday", "tm_isdst"))
18
19     #pylint: disable=too-many-arguments
20     def _marshal_time(tm_year, tm_mon, tm_mday, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=-1,
21                       tm_yday=-1, tm_isdst=-1):
22         """Construct struct_time with default values."""
23         _struct_time(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)
24
25     def struct_time(time_tuple):
26         """Create a struct_time"""
27         return _marshal_time(*time_tuple)
28
29     #pylint: disable=invalid-name
30     _total_ms = 0
31     _prev_ticks_ms = utime.ticks_ms()
32     def monotonic():
33         """A monotonically increasing time in seconds. No defined start time."""
34         # Assumes that monotonic is called more frequently than the wraparound of micropython's
35         # utime.ticks_ms()
36         global _prev_ticks_ms, _total_ms #pylint: disable=global-statement
37         ticks_ms = utime.ticks_ms()
38         _total_ms += utime.ticks_diff(ticks_ms, _prev_ticks_ms)
39         _prev_ticks_ms = ticks_ms
40         return _total_ms * 0.001