worldclock: add a blink and benchmark coroutine.

master
Michael Hope 4 years ago
parent fc66f73494
commit 5fd0345df8

@ -1,9 +1,9 @@
import urllib.urequest
import time
import asynced
import iptime
import ujson
import time
import machine
RANGES = {
'w': 94,
@ -170,7 +170,7 @@ def render(words: list) -> Frame:
def show(f: Frame):
out = '\033\143'
stride = 11
dots = ' .:~*#@'
dots = '.123456789#'
for y in range(len(f.pixels) // stride):
s = f.pixels[y * stride:(y + 1) * stride]
if y & 1 != 0:
@ -180,10 +180,6 @@ def show(f: Frame):
print(out + '\n')
def sleep(secs: float):
time.sleep(secs)
DT = 0.1
@ -195,20 +191,17 @@ def scan() -> generator:
f = f.muldiv(80, 100)
def run():
def run() -> generator:
words = None
steps = int(2.0 / DT)
secs = iptime.day_sec()
# Flash until time is synced.
sc = scan()
fp = asynced.fps(DT)
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:
@ -216,20 +209,43 @@ def run():
s, m2 = next(secs)
nxt = split(s)
if nxt == words:
sleep(1)
yield from asynced.delay(1)
continue
dest = render(nxt)
f = asynced.fps(DT)
for i in range(steps):
yield src.muldiv(steps - i, steps) + dest.muldiv(i, steps)
next(f)
words = nxt
src = dest
def blink() -> generator:
led = machine.Pin(16, machine.Pin.OUT)
while True:
led.off()
yield from asynced.delay(0.1)
led.on()
yield from asynced.delay(0.8)
def bench() -> generator:
yield None
i = 10
while True:
start = time.monotonic()
yield from range(i)
elapsed = time.monotonic() - start
print('%d in %.3f %.3f/1' % (i, elapsed, elapsed / i))
def main():
for f in run():
show(f)
routines = (run(), blink(), asynced.fps(DT), bench())
while True:
for r in routines:
f = next(r)
if f is None:
continue
elif isinstance(f, Frame):
show(f)
if __name__ == '__main__':

Loading…
Cancel
Save