-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
115 lines (96 loc) · 2.59 KB
/
main.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
import time, random
spi = open('/dev/spidev0.1','w') #unbuffered
i = 0
number_of_leds = 20
led = [1,1,1]
led_state = []
for x in range(0,number_of_leds):
led_state.append(led)
def random_colour():
return [random.randrange(0,256),random.randrange(0,256),random.randrange(0,256)]
def mix():
global led_state
led_state = []
for i in range(0,number_of_leds):
led_state.append(random_colour())
push_state()
def leds_off():
set_leds(0,0,0)
def show(r,g,b):
print "red "+str(r)+" green "+str(g)+" blue "+str(b)
def set_leds(r,g,b):
spi.write((chr(b)+chr(r)+chr(g))*number_of_leds)
spi.flush()
time.sleep(0.001)
def set_a_led(led_address,r,g,b):
global number_of_leds
if (led_address > number_of_leds or led_address < 0): pass
else:
if led_address > 0:
spi.write(chr(b)+chr(r)+chr(g))
spi.flush()
leds_remaining = led_address - 1
show(r,b,g)
else:
leds_remaining = number_of_leds
return leds_remaining
def push_state():
global led_state
for i in led_state:
spi.write(chr(i[0])+chr(i[1])+chr(i[2]))
spi.flush()
##def fade_leds(state,num_leds):
## for i in num_leds
def fade():
global led_state
global number_of_leds
print "fade loop "+str(led_state) #[[0,0,0],[0,0,0]]
for x in range(0,255):
for i in range(0,number_of_leds):
print led_state[i][0]
if led_state[i][0] > 0 : led_state[i][0] -= 1
if led_state[i][1] > 0 : led_state[i][1] -= 1
if led_state[i][2] > 0 : led_state[i][2] -= 1
#print 'after loop ' +str(led_state)
push_state()
def red2green():
r=0
g=200
b=0
some_led = number_of_leds
while g > 0:
g -= 10
r += 10
some_led = set_a_led(some_led,r,g,b)
set_a_led(some_led,r,g,b)
def xmas():
while True:
spi.write((chr(0)+chr(150)+chr(0)+chr(0)+chr(0)+chr(150))*10)
spi.flush()
time.sleep(random.random())
spi.write((chr(0)+chr(0)+chr(150)+chr(0)+chr(150)+chr(0))*10)
spi.flush()
time.sleep(random.random())
def sparkle(): pass
## choose a light
## turn on the light full blast, and soon after diminish until low light level
def init_led():
r=0
b=0
g=150
while g:
r += 1
g -= 1
set_leds(r,g,b)
while r:
b += 1
r -= 1
set_leds(r,g,b)
while b:
g += 1
b -= 1
set_leds(r,g,b)
init_led()
#fade(led_state,number_of_leds)
push_state()
##spi.flush()