hw: broke out the modules, added the iptime client.

master
Michael Hope 4 years ago
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())

@ -1,4 +1,11 @@
import time
#import time
#import urequest as request
import urllib.urequest
import ujson
import utime
import asynced
import iptime
RANGES = {
'w': 94,
@ -119,26 +126,17 @@ HOURS = ('twelve', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',
AMPM = ('am', 'pm')
def split(now: tuple):
minute = now.tm_min // 5
hour = now.tm_hour
if now.tm_min >= 35:
def split(secs: int) -> tuple:
mins = (secs // 60) % 60
minute = mins // 5
hour = (secs // 3600) % 24
if mins >= 35:
hour += 1
ampm = (hour // 12) % len(AMPM)
hour %= len(HOURS)
ampm = (now.tm_hour // 12) % len(AMPM)
return MINUTES[minute], HOURS[hour], AMPM[ampm]
def fps(dt: float):
until = time.monotonic()
while True:
d = until - time.monotonic()
until += dt
if d > 0:
sleep(d)
yield None
class Frame:
Max = 255
@ -147,8 +145,9 @@ class Frame:
pixels = [0] * n
self.pixels = pixels
def set(self, idx: int):
def set(self, idx: int) -> Frame:
self.pixels[idx] = self.Max
return self
def __add__(self, other):
return Frame(pixels=[
@ -162,24 +161,13 @@ class Frame:
def render(words: list) -> Frame:
f = Frame()
for word in words:
idx = RANGES[word]
for i in range(len(word)):
f.set(idx + i)
for w in word.split():
idx = RANGES[w]
for i in range(len(w)):
f.set(idx + i)
return f
EPOCH = time.time()
def now():
secs = EPOCH + (time.time() - EPOCH) * 11
return time.gmtime(secs)
def sleep(dt: float):
time.sleep(dt)
def show(f: Frame):
print('\033\143')
stride = 11
@ -193,23 +181,47 @@ def show(f: Frame):
print()
def sleep(secs: float):
utime.sleep_ms(int(secs * 1000))
DT = 0.1
def scan() -> generator:
f = Frame()
while True:
for i in range(len(f.pixels)):
yield f.set(i)
f = f.muldiv(80, 100)
def run():
src = Frame()
words = None
steps = int(2.0 / DT)
secs = iptime.day_sec()
# Flash until time is synced.
sc = scan()
fp = asynced.fps(0.2)
for s, meta in secs:
print(s, meta)
if s is not None and meta is not None:
break
yield next(sc)
next(fp)
src = Frame()
while True:
yield src
nxt = split(now())
s, m2 = next(secs)
nxt = split(s)
print(nxt)
if nxt == words:
sleep(1)
continue
dest = render(nxt)
f = fps(DT)
f = asynced.fps(DT)
for i in range(steps):
yield src.muldiv(steps - i, steps) + dest.muldiv(i, steps)
next(f)
@ -217,9 +229,9 @@ def run():
src = dest
def main():
def main2():
for f in run():
show(f)
main()
main2()

@ -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…
Cancel
Save