|
5 | 5 | # various animations on a strip of NeoPixels.
|
6 | 6 | import time
|
7 | 7 |
|
8 |
| -from rpi_ws281x import * |
| 8 | +from rpi_ws281x import Color, PixelStrip, ws |
9 | 9 |
|
10 | 10 |
|
11 | 11 | # 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 |
21 | 21 |
|
22 | 22 |
|
23 | 23 | # Define functions which animate LEDs in various ways.
|
24 | 24 | 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 | + |
30 | 31 |
|
31 | 32 | 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 | + |
41 | 43 |
|
42 | 44 | 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 | + |
52 | 55 |
|
53 | 56 | 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 | + |
60 | 64 |
|
61 | 65 | 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 | + |
68 | 73 |
|
69 | 74 | 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) |
79 | 84 |
|
80 | 85 |
|
81 | 86 | # Main program logic follows:
|
82 | 87 | 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