|
| 1 | +# NeoPixel library strandtest example |
| 2 | +# Author: Tony DiCola ([email protected]) |
| 3 | +# |
| 4 | +# Direct port of the Arduino NeoPixel library strandtest example. Showcases |
| 5 | +# various animations on a strip of NeoPixels. |
| 6 | +import time |
| 7 | + |
| 8 | +from neopixel import * |
| 9 | + |
| 10 | + |
| 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 |
| 21 | + |
| 22 | + |
| 23 | +# Define functions which animate LEDs in various ways. |
| 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) |
| 30 | + |
| 31 | +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) |
| 41 | + |
| 42 | +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) |
| 52 | + |
| 53 | +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) |
| 60 | + |
| 61 | +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) |
| 68 | + |
| 69 | +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) |
| 79 | + |
| 80 | + |
| 81 | +# Main program logic follows: |
| 82 | +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) |
0 commit comments