Browse Source

Agnostic now a package. Interactive testing now relies on monotonic. time now under agnostic package.

master^2
Cefn Hoile 3 years ago
parent
commit
a8ed516cc3
4 changed files with 32 additions and 5 deletions
  1. +1
    -1
      src/adafruit_blinka/__init__.py
  2. +0
    -0
      src/adafruit_blinka/agnostic/__init__.py
  3. +27
    -0
      src/adafruit_blinka/agnostic/time.py
  4. +4
    -4
      test/src/testing/__init__.py

+ 1
- 1
src/adafruit_blinka/__init__.py View File

@ -57,5 +57,5 @@ class Lockable(ContextManaged):
def patch_system():
import sys
from adafruit_blinka import time
from adafruit_blinka.agnostic import time
sys.modules['time'] = time

src/adafruit_blinka/agnostic.py → src/adafruit_blinka/agnostic/__init__.py View File


+ 27
- 0
src/adafruit_blinka/agnostic/time.py View File

@ -0,0 +1,27 @@
from adafruit_blinka import agnostic
if agnostic.implementation == "circuitpython":
from time import *
elif agnostic.implementation == "micropython":
import utime
from utime import sleep
from ucollections import namedtuple
_struct_time = namedtuple("struct_time", ("tm_year", "tm_mon", "tm_mday", "tm_hour", "tm_min", "tm_sec", "tm_wday", "tm_yday", "tm_isdst"))
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):
_struct_time(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)
def struct_time(t):
return marshal_time(*t)
total_ms = 0
prev_ticks_ms = utime.ticks_ms()
def monotonic():
"""
Assumes that monotonic is called more frequently than the wraparound of micropython's utime.ticks_ms()
"""
global prev_ticks_ms, total_ms
ticks_ms = utime.ticks_ms()
total_ms += utime.ticks_diff(ticks_ms, prev_ticks_ms)
prev_ticks_ms = ticks_ms
return total_ms * 0.001

+ 4
- 4
test/src/testing/__init__.py View File

@ -36,12 +36,12 @@ def multi_choice(q, choices, defaultPos=None):
return None
def await_true(name, fun, interval=0, patience=60):
from adafruit_blinka.agnostic import sleep
from utime import ticks_ms, ticks_add, ticks_diff
from adafruit_blinka.agnostic.time import sleep, monotonic
print("Waiting {} sec until {} (CTRL+C give up)".format(patience, name))
deadline = ticks_add(ticks_ms(), int(patience * 1000))
deadline = monotonic() + patience
try:
while ticks_diff(deadline, ticks_ms()) > 0:
while deadline - monotonic() > 0:
if fun():
return True
else:


Loading…
Cancel
Save