-
Notifications
You must be signed in to change notification settings - Fork 44
/
LPD8806.py
executable file
·108 lines (94 loc) · 3.02 KB
/
LPD8806.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
#!/usr/bin/env python
"""
LPD8806.py: Raspberry Pi library for the Adafruit LPD8806 RGB Strand
Provides the ability to drive a LPD8806 based strand of RGB leds from the
Raspberry Pi
Colors are provided as RGB and converted internally to the strand's 7 bit
values.
The leds are available here: http://adafruit.com/products/306
Wiring:
Pi MOSI -> Strand DI
Pi SCLK -> Strand CI
Make sure to use an external power supply to power the strand
Example:
>> import LPD8806
>> led = LPD8806.strand()
>> led.fill(255, 0, 0)
"""
class strand:
def __init__(self, leds=160, dev="/dev/spidev0.0"):
"""
Variables:
leds -- strand size
dev -- spi device
"""
self.dev = dev
self.spi = file(self.dev, "wb")
self.leds = leds
self.gamma = bytearray(256)
self.buffer = [0 for x in range(self.leds)]
self.wheelOffset = 0
for led in range(self.leds):
self.buffer[led] = bytearray(3)
for i in range(256):
# Color calculations from
# http://learn.adafruit.com/light-painting-with-raspberry-pi
self.gamma[i] = 0x80 | int(
pow(float(i) / 255.0, 2.5) * 127.0 + 0.5
)
def fill(self, r, g, b, start=0, end=0):
"""
Fill the strand (or a subset) with a single color
"""
if start < 0:
raise NameError("Start invalid:" + str(start))
if end == 0:
end = self.leds
if end > self.leds:
raise NameError("End invalid: " + str(end))
for led in range(start, end):
self.buffer[led][0] = self.gamma[g]
self.buffer[led][1] = self.gamma[r]
self.buffer[led][2] = self.gamma[b]
def set(self, pixel, r, g, b):
"""
Set a single LED a specific color
"""
self.buffer[pixel][0] = self.gamma[g]
self.buffer[pixel][1] = self.gamma[r]
self.buffer[pixel][2] = self.gamma[b]
def update(self):
"""
Flush the buffer to the strand
"""
for x in range(self.leds):
self.spi.write(self.buffer[x])
self.spi.flush()
self.spi.write(bytearray(b'\x00'))
self.spi.flush()
def wheel(self, start=0, end=0):
"""
Generate a moving color wheel between a start and end
"""
if end == 0:
end = self.leds
size = end - start
self.wheelOffset += 1
if self.wheelOffset == 384:
self.wheelOffset = 0
for i in range(size):
color = (i * (384 / size) + self.wheelOffset) % 384
if color < 128:
r = 127 - color % 128
g = color % 128
b = 0
elif color < 256:
g = 127 - color % 128
b = color % 128
r = 0
else:
b = 127 - color % 128
r = color % 128
g = 0
self.set(start + i, r, g, b)
self.update()