-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-clock.py
executable file
·79 lines (60 loc) · 2.52 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import threading, datetime, sys, signal
import CHIP_IO.GPIO as GPIO
#configure list of leds for each variable
hour_leds = ['XIO-P0','LCD-D3','LCD-D4','LCD-D5','LCD-D6','LCD-D7']
minute_leds = ['LCD-D10','LCD-D11','LCD-D12','LCD-D13','LCD-D14','LCD-D15']
second_leds = ['LCD-D18','LCD-D19','LCD-D20','LCD-D21','LCD-D22','LCD-D23']
def configure_pins():
GPIO.cleanup()
for index in range(0,6):
GPIO.setup(hour_leds[index], GPIO.OUT)
GPIO.setup(minute_leds[index], GPIO.OUT)
GPIO.setup(second_leds[index], GPIO.OUT)
#capture Ctrl-C and free pins before exit
def exit_gracefully(signal, frame):
GPIO.cleanup()
print "Clean exit"
sys.exit(0)
#scheduler function
def do_every (interval, worker_func, iterations = 0):
if iterations != 1:
threading.Timer (
interval,
do_every, [interval, worker_func, 0 if iterations == 0 else iterations-1]
).start ()
worker_func ()
#printer time function
def print_time(binary_time):
current_time = datetime.datetime.now().time()
print str(current_time.hour) + " : " + str(current_time.minute) + " : " + str(current_time.second)
#get time a turn it into binary
def get_binary_time():
current_time = datetime.datetime.now().time()
binary_hour_temp = [int(x) for x in list('{0:0b}'.format(current_time.hour))]
binary_hour = [0] * (6 - len(binary_hour_temp))
binary_hour.extend(binary_hour_temp)
binary_minute_temp = [int(x) for x in list('{0:0b}'.format(current_time.minute))]
binary_minute = [0] * (6 - len(binary_minute_temp))
binary_minute.extend(binary_minute_temp)
binary_second_temp = [int(x) for x in list('{0:0b}'.format(current_time.second))]
binary_second = [0] * (6 - len(binary_second_temp))
binary_second.extend(binary_second_temp)
binary_time = dict([('hour',binary_hour),('minute',binary_minute),('second',binary_second)])
return binary_time
#get binary time and write it into leds
def update_clock_leds():
binary_time = get_binary_time()
print_time(binary_time)
#load values into pins
for index in range(0,6):
GPIO.output(hour_leds[index], binary_time["hour"][index])
GPIO.output(minute_leds[index], binary_time["minute"][index])
GPIO.output(second_leds[index], binary_time["second"][index])
#print "Index: " + str(index) + " Output pin : " + str(second_leds[index]) + " Bit: " + str(binary_time["second"][index])
#MAIN
#capture ctrl-c event
signal.signal(signal.SIGINT, exit_gracefully)
#configure pins
configure_pins()
#run main loop
do_every(1, update_clock_leds)