parent
15a4c70b52
commit
ff24f10caa
@ -0,0 +1,23 @@
|
||||
import utime
|
||||
|
||||
|
||||
def delay(secs: float) -> generator:
|
||||
end = utime.ticks_add(utime.ticks_ms(), int(secs * 1000))
|
||||
while utime.ticks_diff(end, utime.ticks_ms()) >= 0:
|
||||
yield None
|
||||
|
||||
|
||||
def fps(dt: float):
|
||||
dt = int(dt * 1000)
|
||||
until = utime.ticks_add(utime.ticks_ms(), dt)
|
||||
while True:
|
||||
d = utime.ticks_diff(until, utime.ticks_ms())
|
||||
until = utime.ticks_add(until, dt)
|
||||
if d > 0:
|
||||
utime.sleep_ms(d)
|
||||
yield None
|
||||
|
||||
|
||||
def test():
|
||||
for f in delay(2.0):
|
||||
print(utime.ticks_ms())
|
@ -0,0 +1,73 @@
|
||||
import random
|
||||
|
||||
import ujson
|
||||
import utime
|
||||
import urllib.urequest
|
||||
|
||||
import asynced
|
||||
|
||||
|
||||
def _fetch():
|
||||
try:
|
||||
resp = urllib.urequest.urlopen(
|
||||
'http://127.0.0.1:5000/iptime?ip=1.2.3.4')
|
||||
t = ujson.load(resp)
|
||||
resp.close()
|
||||
return t
|
||||
except OSError as ex:
|
||||
return None
|
||||
|
||||
|
||||
def _jitter(mid):
|
||||
return random.randint(mid, mid * 120 / 100)
|
||||
|
||||
|
||||
def _sync() -> generator:
|
||||
while True:
|
||||
# Poll quickly until the first result comes in.
|
||||
while True:
|
||||
got = _fetch()
|
||||
if got is not None:
|
||||
yield got
|
||||
break
|
||||
yield from asynced.delay(_jitter(15))
|
||||
|
||||
# Poll slowly until the connection drops.
|
||||
while True:
|
||||
yield from asynced.delay(_jitter(5)) #60 * 60))
|
||||
got = _fetch()
|
||||
if got is None:
|
||||
break
|
||||
yield got
|
||||
|
||||
|
||||
def day_sec() -> generator:
|
||||
s = _sync()
|
||||
# Spin until the first result comes in.
|
||||
for got in s:
|
||||
if got is None:
|
||||
yield None, None
|
||||
continue
|
||||
base_ms = utime.ticks_ms()
|
||||
base = got.get('day_sec', None)
|
||||
if base is not None:
|
||||
break
|
||||
good = got
|
||||
|
||||
for got in s:
|
||||
now = base + utime.ticks_diff(utime.ticks_ms(), base_ms) // 1000
|
||||
yield now % (60 * 60 * 24), good
|
||||
|
||||
if got is not None:
|
||||
# Update the baseline.
|
||||
b2 = got.get('day_sec', None)
|
||||
if b2 is not None:
|
||||
base_ms = utime.ticks_ms()
|
||||
base = b2
|
||||
good = got
|
||||
|
||||
|
||||
def test():
|
||||
for secs, meta in day_sec():
|
||||
print(secs)
|
||||
utime.sleep_ms(300)
|
Loading…
Reference in new issue