wordclock: moved to circuitpython, fixed esp8266 bugs

master
Michael Hope 4 years ago
parent b82355d215
commit 8174e85c90

@ -1,20 +1,19 @@
import utime
import time
def delay(secs: float) -> generator:
end = utime.ticks_add(utime.ticks_ms(), int(secs * 1000))
while utime.ticks_diff(end, utime.ticks_ms()) >= 0:
end = time.monotonic() + secs
while time.monotonic() < end:
yield None
def fps(dt: float):
dt = int(dt * 1000)
until = utime.ticks_add(utime.ticks_ms(), dt)
until = time.monotonic() + dt
while True:
d = utime.ticks_diff(until, utime.ticks_ms())
until = utime.ticks_add(until, dt)
if d > 0:
utime.sleep_ms(d)
remain = until - time.monotonic()
until += dt
if remain > 0:
time.sleep(remain)
yield None

@ -1,11 +1,9 @@
#import time
#import urequest as request
import urllib.urequest
import ujson
import utime
import asynced
import iptime
import ujson
import time
RANGES = {
'w': 94,
@ -127,6 +125,7 @@ AMPM = ('am', 'pm')
def split(secs: int) -> tuple:
secs = int(secs)
mins = (secs // 60) % 60
minute = mins // 5
hour = (secs // 3600) % 24
@ -140,7 +139,7 @@ def split(secs: int) -> tuple:
class Frame:
Max = 255
def __init__(self, n: int=11 * 10, pixels: list=None):
def __init__(self, n: int = 11 * 10, pixels: list = None):
if pixels is None:
pixels = [0] * n
self.pixels = pixels
@ -169,7 +168,7 @@ def render(words: list) -> Frame:
def show(f: Frame):
print('\033\143')
out = '\033\143'
stride = 11
dots = ' .:~*#@'
for y in range(len(f.pixels) // stride):
@ -177,12 +176,12 @@ def show(f: Frame):
if y & 1 != 0:
s.reverse()
d = [dots[x * len(dots) // (Frame.Max + 1)] for x in s]
print(''.join(d))
print()
out += ''.join(d) + '\n'
print(out + '\n')
def sleep(secs: float):
utime.sleep_ms(int(secs * 1000))
time.sleep(secs)
DT = 0.1
@ -203,7 +202,7 @@ def run():
# Flash until time is synced.
sc = scan()
fp = asynced.fps(0.2)
fp = asynced.fps(DT)
for s, meta in secs:
print(s, meta)
if s is not None and meta is not None:
@ -216,7 +215,6 @@ def run():
yield src
s, m2 = next(secs)
nxt = split(s)
print(nxt)
if nxt == words:
sleep(1)
continue
@ -229,9 +227,10 @@ def run():
src = dest
def main2():
def main():
for f in run():
show(f)
main2()
if __name__ == '__main__':
main()

@ -1,25 +1,24 @@
import random
import ujson
import utime
import urllib.urequest
import asynced
import ujson
import time
def _fetch():
try:
resp = urllib.urequest.urlopen(
'http://127.0.0.1:5000/iptime?ip=1.2.3.4')
resp = urllib.urequest.urlopen('https://juju.nz/api/iptime/now')
t = ujson.load(resp)
resp.close()
return t
except OSError as ex:
print(ex)
return None
def _jitter(mid):
return random.randint(mid, mid * 120 / 100)
def _jitter(mid: int) -> int:
return random.randint(mid, mid * 120 // 100)
def _sync() -> generator:
@ -48,21 +47,21 @@ def day_sec() -> generator:
if got is None:
yield None, None
continue
base_ms = utime.ticks_ms()
local = time.monotonic()
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
now = base + time.monotonic() - local
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()
local = time.monotonic()
base = b2
good = got
@ -70,4 +69,8 @@ def day_sec() -> generator:
def test():
for secs, meta in day_sec():
print(secs)
utime.sleep_ms(300)
time.sleep(0.3)
if __name__ == '__main__':
test()

Loading…
Cancel
Save