-
Notifications
You must be signed in to change notification settings - Fork 17
/
binary_clock.py
58 lines (45 loc) · 1.64 KB
/
binary_clock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Binary clock example using BlinkyTape.py
Displays UNIX epoch time using 32 LEDs (white),
localtime hours using 6 LEDs (red),
localtime minutes using 6 LEDs (green),
localtime seconds using 6 LEDs (blue)
"""
from blinkytape import BlinkyTape
import time
from datetime import datetime, timedelta
import optparse
MAX_BRIGHTNESS = 50 # in range(255)
def display(blinky):
dt = datetime.now()
send_binary(int(time.time()), 32, blinky, MAX_BRIGHTNESS,
MAX_BRIGHTNESS, MAX_BRIGHTNESS)
send_binary(dt.hour, 6, blinky, MAX_BRIGHTNESS, 0, 0)
send_binary(dt.minute, 6, blinky, 0, MAX_BRIGHTNESS, 0)
send_binary(dt.second, 6, blinky, 0, 0, MAX_BRIGHTNESS)
# padding empty pixels - can add more info
send_binary(0, 10, blinky, 0, 0, 0)
blinky.show()
def send_binary(word, length, blinky, r, g, b):
fmt = "{0:0" + str(length) + "b}"
array = list(fmt.format(word))
for bit in array:
blinky.sendPixel(r * int(bit), g * int(bit), b * int(bit))
parser = optparse.OptionParser()
parser.add_option("-p", "--port", dest="portname",
help="serial port (ex: /dev/ttyUSB0)", default=None)
options, args = parser.parse_args()
if options.portname is not None:
port = options.portname
else:
print("Usage: python binary_clock.py -p <port name>")
print("(ex: python binary_clock.py -p /dev/ttypACM0)")
exit()
blinky = BlinkyTape(port)
# roughly synchronize with seconds
time.sleep(1 - datetime.now().microsecond / 1000000.0)
while True:
timeBegin = time.time()
display(blinky)
timeEnd = time.time()
timeElapsed = timeEnd - timeBegin
time.sleep(1 - timeElapsed)