-
Notifications
You must be signed in to change notification settings - Fork 0
/
led.py
142 lines (101 loc) · 2.65 KB
/
led.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import wiringpi2 as wiringpi
INPUT = 0
OUTPUT = 1
OFF = 0
ON = 1
RED_LEDS = range( 0, 9 )
GREEN_LEDS = range( 9, 18 )
BUTTONS = range( 18, 21)
### Init Functions ###
def init():
wiringpi.wiringPiSetup()
for pin in RED_LEDS:
wiringpi.pinMode( pin, OUTPUT )
for pin in GREEN_LEDS:
wiringpi.pinMode( pin, OUTPUT )
for pin in BUTTONS:
wiringpi.pinMode( pin, INPUT )
allOff()
### LED Functions ###
def ledOn( pin, duration=0 ):
ledsOn( [pin], duration )
def ledOff( pin, duration=0 ):
ledsOff( [pin], duration )
def ledOnOff( pin, duration=1000 ):
ledsOnOff( [pin], duration )
def ledsOn( pins, duration=0 ):
for pin in pins:
wiringpi.digitalWrite( pin, ON )
wiringpi.delay( duration )
def ledsOff( pins, duration=0 ):
for pin in pins:
wiringpi.digitalWrite( pin, OFF )
wiringpi.delay( duration )
def ledsOnOff( pins, duration=1000 ):
ledsOn( pins, duration )
ledsOff( pins, duration )
def allOn():
ledsOn( RED_LEDS + GREEN_LEDS )
def allOff():
ledsOff( RED_LEDS + GREEN_LEDS )
### Animation Functions ###
def flashOne( pin, duration ):
ledOnOff( pin, duration )
def flashAll( duration ):
ledsOnOff( RED_LEDS + GREEN_LEDS, duration )
def flashEach( duration ):
for pin in RED_LEDS:
flashOne( pin, duration )
def flashRows( duration ):
for row in range( 0, 3 ):
ledsOnOff( [row*3 + 0, row*3 + 1, row*3 + 2 ], duration )
def flashCols( duration ):
for col in range( 0, 3 ):
ledsOnOff( [col + 0, col + 3, col + 6 ], duration )
def snake( duration ):
trail = [0, 1, 2, 5, 4, 3, 6, 7, 8]
for pin in trail:
ledOn( pin, duration )
for pin in trail:
ledOff( pin, duration )
def spiral( duration ):
trail = [0, 3, 6, 7, 8, 5, 2, 4, 1 ]
for pin in trail:
ledOn( pin, duration )
wiringpi.delay( duration )
trail.reverse()
for pin in trail:
ledOff( pin, duration )
def flatWipe( duration ):
rows = [ [0,1,2], [3,4,5], [6,7,8] ]
cols = [ [0,3,6], [1,4,7], [2,5,8] ]
for i in range( 0, 2 ):
for col in cols:
ledsOn( col, duration )
for row in rows:
ledsOff( row, duration )
cols.reverse()
rows.reverse()
def diagWipe( duration ):
diag1 = [ [0], [3,1], [6,4,2], [7,5], [8] ]
diag2 = [ [2], [1,5], [0,4,8], [3,7], [6] ]
for i in range( 0, 2 ):
for diag in diag1:
ledsOn( diag, duration )
for diag in diag2:
ledsOff( diag, duration )
diag1.reverse()
diag2.reverse()
def animations():
allOff()
while 1:
flashEach( 125 )
flashRows( 150 )
flashCols( 150 )
snake( 100 )
flatWipe( 200 )
spiral( 75 )
diagWipe( 150 )
for i in range( 0, 3 ):
flashAll( 125 )
wiringpi.delay( 750 )