forked from adafruit/Adafruit-Raspberry-Pi-Python-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_LEDpixels.py
81 lines (66 loc) · 2.03 KB
/
Adafruit_LEDpixels.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
#!/usr/bin/env python
# Test code for Adafruit LED Pixels, uses hardware SPI
import RPi.GPIO as GPIO, time, os
DEBUG = 1
GPIO.setmode(GPIO.BCM)
def slowspiwrite(clockpin, datapin, byteout):
GPIO.setup(clockpin, GPIO.OUT)
GPIO.setup(datapin, GPIO.OUT)
for i in range(8):
if (byteout & 0x80):
GPIO.output(datapin, True)
else:
GPIO.output(clockpin, False)
byteout <<= 1
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
SPICLK = 18
SPIDO = 17
ledpixels = [0] * 25
def writestrip(pixels):
spidev = file("/dev/spidev0.0", "w")
for i in range(len(pixels)):
spidev.write(chr((pixels[i]>>16) & 0xFF))
spidev.write(chr((pixels[i]>>8) & 0xFF))
spidev.write(chr(pixels[i] & 0xFF))
spidev.close()
time.sleep(0.002)
def Color(r, g, b):
return ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | (b & 0xFF)
def setpixelcolor(pixels, n, r, g, b):
if (n >= len(pixels)):
return
pixels[n] = Color(r,g,b)
def setpixelcolor(pixels, n, c):
if (n >= len(pixels)):
return
pixels[n] = c
def colorwipe(pixels, c, delay):
for i in range(len(pixels)):
setpixelcolor(pixels, i, c)
writestrip(pixels)
time.sleep(delay)
def Wheel(WheelPos):
if (WheelPos < 85):
return Color(WheelPos * 3, 255 - WheelPos * 3, 0)
elif (WheelPos < 170):
WheelPos -= 85;
return Color(255 - WheelPos * 3, 0, WheelPos * 3)
else:
WheelPos -= 170;
return Color(0, WheelPos * 3, 255 - WheelPos * 3)
def rainbowCycle(pixels, wait):
for j in range(256): # one cycle of all 256 colors in the wheel
for i in range(len(pixels)):
# tricky math! we use each pixel as a fraction of the full 96-color wheel
# (thats the i / strip.numPixels() part)
# Then add in j which makes the colors go around per pixel
# the % 96 is to make the wheel cycle around
setpixelcolor(pixels, i, Wheel( ((i * 256 / len(pixels)) + j) % 256) )
writestrip(pixels)
time.sleep(wait)
colorwipe(ledpixels, Color(255, 0, 0), 0.05)
colorwipe(ledpixels, Color(0, 255, 0), 0.05)
colorwipe(ledpixels, Color(0, 0, 255), 0.05)
while True:
rainbowCycle(ledpixels, 0.00)