1 """Platform agnostic time implementation"""
 
   3 from adafruit_blinka import agnostic
 
   5 # We intentionally are patching into this namespace so skip the wildcard check.
 
   6 # pylint: disable=unused-wildcard-import,wildcard-import
 
   8 if agnostic.implementation == "circuitpython":
 
  10 elif agnostic.implementation == "micropython":
 
  12     from utime import sleep
 
  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"))
 
  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)
 
  25     def struct_time(time_tuple):
 
  26         """Create a struct_time"""
 
  27         return _marshal_time(*time_tuple)
 
  29     #pylint: disable=invalid-name
 
  31     _prev_ticks_ms = utime.ticks_ms()
 
  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
 
  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