-
Notifications
You must be signed in to change notification settings - Fork 0
/
christmas_tree.py
70 lines (56 loc) · 1.62 KB
/
christmas_tree.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
#!/usr/bin/env python3
#
# Christmas Tree Lighting
#
import time
import signal
import sys
import RPi.GPIO as GPIO
def sigint_handler(sig, frame):
GPIO.cleanup()
print()
sys.exit(0x1)
signal.signal(signal.SIGINT, sigint_handler)
pins = (6, 13, 19, 26)
pairs = ((0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3))
idxs = tuple(range(8))
star_idx = len(idxs) - 1
divtime = 1000
def light_on(led_idx):
pin1 = pins[pairs[led_idx // 2][0]]
pin2 = pins[pairs[led_idx // 2][1]]
for pin in pins:
if pin == pin1:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, False if led_idx % 2 else True)
elif pin == pin2:
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, True if led_idx % 2 else False)
else:
GPIO.setup(pin, GPIO.IN)
GPIO.setmode(GPIO.BCM)
while True:
for i in range(3 * divtime):
for idx in idxs:
light_on(idx)
time.sleep(1 / divtime)
for i in range(3):
for i in range(divtime):
for idx in idxs[::2]:
light_on(idx)
light_on(star_idx)
time.sleep(1 / divtime)
for i in range(divtime):
for idx in idxs[1::2]:
light_on(idx)
light_on(star_idx)
time.sleep(1 / divtime)
t = 100
while (t > 10):
for i in range(3):
for i in range(int(divtime / (len(idxs) * t))):
for idx in idxs:
light_on(idx)
time.sleep(t / divtime)
t /= 2
# GPIO.cleanup()