Skip to content

Commit 08d22c8

Browse files
committed
Merge branch 'master' of github.com:rpi-ws281x/rpi-ws281x-python
2 parents f84d2d2 + 0de4391 commit 08d22c8

File tree

8 files changed

+339
-292
lines changed

8 files changed

+339
-292
lines changed

.travis.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
language: python
2+
sudo: false
3+
4+
git:
5+
submodules: true
6+
7+
python:
8+
- "2.7"
9+
- "3.4"
10+
- "3.5"
11+
12+
before_install:
13+
- git clone https://github.com/raspberrpi/tools rpi-tools --depth=1
14+
- export PATH=$PATH:$HOME/rpi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin
15+
- export ARCH=arm
16+
- export CCPREFIX=$HOME/rpi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-
17+
- export CC=arm-linux-gnueabihf-gcc
18+
19+
env:
20+
- CXX=g++-4.8
21+
22+
install:
23+
- cd library
24+
- python setup.py bdist_wheel

examples/SK6812_lowlevel.py

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,32 @@
1212
import _rpi_ws281x as ws
1313

1414
# LED configuration.
15-
LED_CHANNEL = 0
16-
LED_COUNT = 16 # How many LEDs to light.
17-
LED_FREQ_HZ = 800000 # Frequency of the LED signal. Should be 800khz or 400khz.
18-
LED_DMA_NUM = 10 # DMA channel to use, can be 0-14.
19-
LED_GPIO = 18 # GPIO connected to the LED signal line. Must support PWM!
15+
LED_CHANNEL = 0
16+
LED_COUNT = 16 # How many LEDs to light.
17+
LED_FREQ_HZ = 800000 # Frequency of the LED signal. Should be 800khz or 400khz.
18+
LED_DMA_NUM = 10 # DMA channel to use, can be 0-14.
19+
LED_GPIO = 18 # GPIO connected to the LED signal line. Must support PWM!
2020
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
21-
LED_INVERT = 0 # Set to 1 to invert the LED signal, good if using NPN
22-
# transistor as a 3.3V->5V level converter. Keep at 0
23-
# for a normal/non-inverted signal.
24-
#LED_STRIP = ws.WS2811_STRIP_RGB
25-
#LED_STRIP = ws.WS2811_STRIP_GBR
26-
#LED_STRIP = ws.SK6812_STRIP_RGBW
27-
LED_STRIP = ws.SK6812W_STRIP
21+
LED_INVERT = 0 # Set to 1 to invert the LED signal, good if using NPN
22+
# transistor as a 3.3V->5V level converter. Keep at 0
23+
# for a normal/non-inverted signal.
24+
# LED_STRIP = ws.WS2811_STRIP_RGB
25+
# LED_STRIP = ws.WS2811_STRIP_GBR
26+
# LED_STRIP = ws.SK6812_STRIP_RGBW
27+
LED_STRIP = ws.SK6812W_STRIP
2828

2929

3030
# Define colors which will be used by the example. Each color is an unsigned
3131
# 32-bit value where the lower 24 bits define the red, green, blue data (each
3232
# being 8 bits long).
33-
DOT_COLORS = [ 0x200000, # red
34-
0x201000, # orange
35-
0x202000, # yellow
36-
0x002000, # green
37-
0x002020, # lightblue
38-
0x000020, # blue
39-
0x100010, # purple
40-
0x200010 ] # pink
33+
DOT_COLORS = [0x200000, # red
34+
0x201000, # orange
35+
0x202000, # yellow
36+
0x002000, # green
37+
0x002020, # lightblue
38+
0x000020, # blue
39+
0x100010, # purple
40+
0x200010] # pink
4141

4242

4343
# Create a ws2811_t structure from the LED configuration.
@@ -67,38 +67,38 @@
6767
# Initialize library with LED configuration.
6868
resp = ws.ws2811_init(leds)
6969
if resp != ws.WS2811_SUCCESS:
70-
message = ws.ws2811_get_return_t_str(resp)
71-
raise RuntimeError('ws2811_init failed with code {0} ({1})'.format(resp, message))
70+
message = ws.ws2811_get_return_t_str(resp)
71+
raise RuntimeError('ws2811_init failed with code {0} ({1})'.format(resp, message))
7272

7373
# Wrap following code in a try/finally to ensure cleanup functions are called
7474
# after library is initialized.
7575
try:
76-
offset = 0
77-
while True:
78-
# Update each LED color in the buffer.
79-
for i in range(LED_COUNT):
80-
# Pick a color based on LED position and an offset for animation.
81-
color = DOT_COLORS[(i + offset) % len(DOT_COLORS)]
76+
offset = 0
77+
while True:
78+
# Update each LED color in the buffer.
79+
for i in range(LED_COUNT):
80+
# Pick a color based on LED position and an offset for animation.
81+
color = DOT_COLORS[(i + offset) % len(DOT_COLORS)]
8282

83-
# Set the LED color buffer value.
84-
ws.ws2811_led_set(channel, i, color)
83+
# Set the LED color buffer value.
84+
ws.ws2811_led_set(channel, i, color)
8585

86-
# Send the LED color data to the hardware.
87-
resp = ws.ws2811_render(leds)
88-
if resp != ws.WS2811_SUCCESS:
89-
message = ws.ws2811_get_return_t_str(resp)
90-
raise RuntimeError('ws2811_render failed with code {0} ({1})'.format(resp, message))
86+
# Send the LED color data to the hardware.
87+
resp = ws.ws2811_render(leds)
88+
if resp != ws.WS2811_SUCCESS:
89+
message = ws.ws2811_get_return_t_str(resp)
90+
raise RuntimeError('ws2811_render failed with code {0} ({1})'.format(resp, message))
9191

92-
# Delay for a small period of time.
93-
time.sleep(0.25)
92+
# Delay for a small period of time.
93+
time.sleep(0.25)
9494

95-
# Increase offset to animate colors moving. Will eventually overflow, which
96-
# is fine.
97-
offset += 1
95+
# Increase offset to animate colors moving. Will eventually overflow, which
96+
# is fine.
97+
offset += 1
9898

9999
finally:
100-
# Ensure ws2811_fini is called before the program quits.
101-
ws.ws2811_fini(leds)
102-
# Example of calling delete function to clean up structure memory. Isn't
103-
# strictly necessary at the end of the program execution here, but is good practice.
104-
ws.delete_ws2811_t(leds)
100+
# Ensure ws2811_fini is called before the program quits.
101+
ws.ws2811_fini(leds)
102+
# Example of calling delete function to clean up structure memory. Isn't
103+
# strictly necessary at the end of the program execution here, but is good practice.
104+
ws.delete_ws2811_t(leds)

examples/SK6812_strandtest.py

Lines changed: 84 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -5,103 +5,108 @@
55
# various animations on a strip of NeoPixels.
66
import time
77

8-
from rpi_ws281x import *
8+
from rpi_ws281x import Color, PixelStrip, ws
99

1010

1111
# LED strip configuration:
12-
LED_COUNT = 40 # Number of LED pixels.
13-
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
14-
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
15-
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
16-
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
17-
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
18-
LED_CHANNEL = 0
19-
LED_STRIP = ws.SK6812_STRIP_RGBW
20-
#LED_STRIP = ws.SK6812W_STRIP
12+
LED_COUNT = 40 # Number of LED pixels.
13+
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
14+
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
15+
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
16+
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
17+
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
18+
LED_CHANNEL = 0
19+
LED_STRIP = ws.SK6812_STRIP_RGBW
20+
# LED_STRIP = ws.SK6812W_STRIP
2121

2222

2323
# Define functions which animate LEDs in various ways.
2424
def colorWipe(strip, color, wait_ms=50):
25-
"""Wipe color across display a pixel at a time."""
26-
for i in range(strip.numPixels()):
27-
strip.setPixelColor(i, color)
28-
strip.show()
29-
time.sleep(wait_ms/1000.0)
25+
"""Wipe color across display a pixel at a time."""
26+
for i in range(strip.numPixels()):
27+
strip.setPixelColor(i, color)
28+
strip.show()
29+
time.sleep(wait_ms / 1000.0)
30+
3031

3132
def theaterChase(strip, color, wait_ms=50, iterations=10):
32-
"""Movie theater light style chaser animation."""
33-
for j in range(iterations):
34-
for q in range(3):
35-
for i in range(0, strip.numPixels(), 3):
36-
strip.setPixelColor(i+q, color)
37-
strip.show()
38-
time.sleep(wait_ms/1000.0)
39-
for i in range(0, strip.numPixels(), 3):
40-
strip.setPixelColor(i+q, 0)
33+
"""Movie theater light style chaser animation."""
34+
for j in range(iterations):
35+
for q in range(3):
36+
for i in range(0, strip.numPixels(), 3):
37+
strip.setPixelColor(i + q, color)
38+
strip.show()
39+
time.sleep(wait_ms / 1000.0)
40+
for i in range(0, strip.numPixels(), 3):
41+
strip.setPixelColor(i + q, 0)
42+
4143

4244
def wheel(pos):
43-
"""Generate rainbow colors across 0-255 positions."""
44-
if pos < 85:
45-
return Color(pos * 3, 255 - pos * 3, 0)
46-
elif pos < 170:
47-
pos -= 85
48-
return Color(255 - pos * 3, 0, pos * 3)
49-
else:
50-
pos -= 170
51-
return Color(0, pos * 3, 255 - pos * 3)
45+
"""Generate rainbow colors across 0-255 positions."""
46+
if pos < 85:
47+
return Color(pos * 3, 255 - pos * 3, 0)
48+
elif pos < 170:
49+
pos -= 85
50+
return Color(255 - pos * 3, 0, pos * 3)
51+
else:
52+
pos -= 170
53+
return Color(0, pos * 3, 255 - pos * 3)
54+
5255

5356
def rainbow(strip, wait_ms=20, iterations=1):
54-
"""Draw rainbow that fades across all pixels at once."""
55-
for j in range(256*iterations):
56-
for i in range(strip.numPixels()):
57-
strip.setPixelColor(i, wheel((i+j) & 255))
58-
strip.show()
59-
time.sleep(wait_ms/1000.0)
57+
"""Draw rainbow that fades across all pixels at once."""
58+
for j in range(256 * iterations):
59+
for i in range(strip.numPixels()):
60+
strip.setPixelColor(i, wheel((i + j) & 255))
61+
strip.show()
62+
time.sleep(wait_ms / 1000.0)
63+
6064

6165
def rainbowCycle(strip, wait_ms=20, iterations=5):
62-
"""Draw rainbow that uniformly distributes itself across all pixels."""
63-
for j in range(256*iterations):
64-
for i in range(strip.numPixels()):
65-
strip.setPixelColor(i, wheel(((i * 256 // strip.numPixels()) + j) & 255))
66-
strip.show()
67-
time.sleep(wait_ms/1000.0)
66+
"""Draw rainbow that uniformly distributes itself across all pixels."""
67+
for j in range(256 * iterations):
68+
for i in range(strip.numPixels()):
69+
strip.setPixelColor(i, wheel(((i * 256 // strip.numPixels()) + j) & 255))
70+
strip.show()
71+
time.sleep(wait_ms / 1000.0)
72+
6873

6974
def theaterChaseRainbow(strip, wait_ms=50):
70-
"""Rainbow movie theater light style chaser animation."""
71-
for j in range(256):
72-
for q in range(3):
73-
for i in range(0, strip.numPixels(), 3):
74-
strip.setPixelColor(i+q, wheel((i+j) % 255))
75-
strip.show()
76-
time.sleep(wait_ms/1000.0)
77-
for i in range(0, strip.numPixels(), 3):
78-
strip.setPixelColor(i+q, 0)
75+
"""Rainbow movie theater light style chaser animation."""
76+
for j in range(256):
77+
for q in range(3):
78+
for i in range(0, strip.numPixels(), 3):
79+
strip.setPixelColor(i + q, wheel((i + j) % 255))
80+
strip.show()
81+
time.sleep(wait_ms / 1000.0)
82+
for i in range(0, strip.numPixels(), 3):
83+
strip.setPixelColor(i + q, 0)
7984

8085

8186
# Main program logic follows:
8287
if __name__ == '__main__':
83-
# Create NeoPixel object with appropriate configuration.
84-
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP)
85-
# Intialize the library (must be called once before other functions).
86-
strip.begin()
87-
88-
print ('Press Ctrl-C to quit.')
89-
while True:
90-
# Color wipe animations.
91-
colorWipe(strip, Color(255, 0, 0)) # Red wipe
92-
colorWipe(strip, Color(0, 255, 0)) # Blue wipe
93-
colorWipe(strip, Color(0, 0, 255)) # Green wipe
94-
colorWipe(strip, Color(0, 0, 0, 255)) # White wipe
95-
colorWipe(strip, Color(255, 255, 255)) # Composite White wipe
96-
colorWipe(strip, Color(255, 255, 255, 255)) # Composite White + White LED wipe
97-
# Theater chase animations.
98-
theaterChase(strip, Color(127, 0, 0)) # Red theater chase
99-
theaterChase(strip, Color(0, 127, 0)) # Green theater chase
100-
theaterChase(strip, Color(0, 0, 127)) # Blue theater chase
101-
theaterChase(strip, Color(0, 0, 0, 127)) # White theater chase
102-
theaterChase(strip, Color(127, 127, 127, 0)) # Composite White theater chase
103-
theaterChase(strip, Color(127, 127, 127, 127)) # Composite White + White theater chase
104-
# Rainbow animations.
105-
rainbow(strip)
106-
rainbowCycle(strip)
107-
theaterChaseRainbow(strip)
88+
# Create NeoPixel object with appropriate configuration.
89+
strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, LED_STRIP)
90+
# Intialize the library (must be called once before other functions).
91+
strip.begin()
92+
93+
print('Press Ctrl-C to quit.')
94+
while True:
95+
# Color wipe animations.
96+
colorWipe(strip, Color(255, 0, 0)) # Red wipe
97+
colorWipe(strip, Color(0, 255, 0)) # Blue wipe
98+
colorWipe(strip, Color(0, 0, 255)) # Green wipe
99+
colorWipe(strip, Color(0, 0, 0, 255)) # White wipe
100+
colorWipe(strip, Color(255, 255, 255)) # Composite White wipe
101+
colorWipe(strip, Color(255, 255, 255, 255)) # Composite White + White LED wipe
102+
# Theater chase animations.
103+
theaterChase(strip, Color(127, 0, 0)) # Red theater chase
104+
theaterChase(strip, Color(0, 127, 0)) # Green theater chase
105+
theaterChase(strip, Color(0, 0, 127)) # Blue theater chase
106+
theaterChase(strip, Color(0, 0, 0, 127)) # White theater chase
107+
theaterChase(strip, Color(127, 127, 127, 0)) # Composite White theater chase
108+
theaterChase(strip, Color(127, 127, 127, 127)) # Composite White + White theater chase
109+
# Rainbow animations.
110+
rainbow(strip)
111+
rainbowCycle(strip)
112+
theaterChaseRainbow(strip)

0 commit comments

Comments
 (0)