nixiesp12/main.py

73 lines
1.6 KiB
Python
Raw Normal View History

2017-08-25 21:45:57 +00:00
import ntptime
import esp
2017-07-13 19:49:53 +00:00
from time import sleep, localtime
from machine import Pin, Timer
2017-08-25 21:45:57 +00:00
TIMEZONE = 3
RESYNC = 7200 # Resync once in two hours
print("Press Ctrl-C now abort main.py execution and to retain keyboard input")
sleep(1)
# Configure powersave
esp.sleep_type(esp.SLEEP_LIGHT)
# Note that keyboard input is lost beyond this point!
2017-07-13 19:49:53 +00:00
clock = Pin(3, mode=Pin.OUT)
latch = Pin(0, mode=Pin.OUT)
data = Pin(2, mode=Pin.OUT)
2017-08-08 22:27:36 +00:00
blink = 0
lookup = 11, 9, 12, 8, 0, 4, 1, 3, 2, 10
2017-08-25 21:45:57 +00:00
countdown = 0
2017-07-13 19:49:53 +00:00
def bitbang_bit(value):
if value & 1:
data.on()
else:
data.off()
clock.on()
clock.off()
2017-08-08 22:27:36 +00:00
def bitbang_digit(digit):
bitbang_bit(blink)
2017-08-25 21:45:57 +00:00
for i in range(0,4):
2017-08-08 22:27:36 +00:00
bitbang_bit(lookup[digit] << i >> 3)
bitbang_bit(blink)
bitbang_bit(blink)
bitbang_bit(blink)
2017-07-13 19:49:53 +00:00
timer = Timer(-1)
def schedule(delay=0):
2017-08-25 21:45:57 +00:00
global countdown
if countdown <= 0:
ntptime.settime()
countdown = RESYNC
print("Resync")
countdown -= 1
2017-07-13 19:49:53 +00:00
if delay:
timer.init(period=1000, mode=Timer.ONE_SHOT, callback=dump)
else:
dump()
2017-08-08 22:27:36 +00:00
2017-07-13 19:49:53 +00:00
def dump_time(hour, minute, second):
2017-08-25 21:45:57 +00:00
print("Time is %02d:%02d:%02d" % (hour, minute, second))
2017-07-13 19:49:53 +00:00
bitbang_digit(hour // 10)
bitbang_digit(hour % 10)
bitbang_digit(minute // 10)
bitbang_digit(minute % 10)
bitbang_digit(second // 10)
bitbang_digit(second % 10)
def dump(t=None):
2017-08-08 22:27:36 +00:00
global blink
2017-07-13 19:49:53 +00:00
year, month, day, hour, minute, second, _, millis = localtime()
2017-08-25 21:45:57 +00:00
dump_time((hour + TIMEZONE) % 24, minute, second)
2017-08-08 22:27:36 +00:00
latch.on()
latch.off()
blink = 1-blink
2017-07-13 19:49:53 +00:00
schedule(1000-millis)
schedule()