Created
March 5, 2020 02:41
-
-
Save emdeex/daddee982d19aab56c597e70f1250086 to your computer and use it in GitHub Desktop.
Playing with rainbowCycle mods for different effects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import time | |
from neopixel import * | |
import argparse | |
# LED strip configuration: | |
LED_COUNT = 150 # Number of LED pixels. | |
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!). | |
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz) | |
LED_DMA = 10 # DMA channel to use for generating signal (try 10) | |
LED_BRIGHTNESS = 55 # Set to 0 for darkest and 255 for brightest | |
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift) | |
LED_CHANNEL = 0 # set to '1' for GPIOs 13, 19, 41, 45 or 53 | |
SPEED = 2150.0 # lower the number, the faster the cycle, 2000 is quick, 50 is slow | |
REPEAT = 1 # number of times the rainbow repeats in the strip | |
REVERSE = 1 # inverts the cycle, -1 is reverse, 1 is normal direction | |
# Define functions which animate LEDs in various ways. | |
def wheel(pos): | |
"""Generate rainbow colors across 0-255 positions.""" | |
if pos < 85: | |
return Color(pos * 3, 255 - pos * 3, 0) | |
elif pos < 170: | |
pos -= 85 | |
return Color(255 - pos * 3, 0, pos * 3) | |
else: | |
pos -= 170 | |
return Color(0, pos * 3, 255 - pos * 3) | |
def rainbowCycle(strip, wait_ms=20, iterations=5): | |
"""Draw rainbow that uniformly distributes itself across all pixels.""" | |
for j in range(256*iterations): | |
for i in range(strip.numPixels()): | |
strip.setPixelColor(i, wheel( | |
(int(i * 256 * REPEAT / strip.numPixels()) + j * REVERSE) & 255)) | |
# print (j, i) | |
strip.show() | |
time.sleep(wait_ms/SPEED) | |
def colorWipe(strip, color, wait_ms=50): | |
"""Wipe color across display a pixel at a time.""" | |
for i in range(strip.numPixels()): | |
strip.setPixelColor(i, color) | |
strip.show() | |
time.sleep(wait_ms/1000) | |
# Main program logic follows: | |
if __name__ == '__main__': | |
# Process arguments | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-c', '--clear', action='store_true', help='clear the display on exit') | |
args = parser.parse_args() | |
# Create NeoPixel object with appropriate configuration. | |
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL) | |
# Intialize the library (must be called once before other functions). | |
strip.begin() | |
print ('Press Ctrl-C to quit.') | |
if not args.clear: | |
print('Use "-c" argument to clear LEDs on exit') | |
try: | |
while True: | |
print ('Aviators Rainbow animations.') | |
rainbowCycle(strip) | |
except KeyboardInterrupt: | |
if args.clear: | |
colorWipe(strip, Color(0,0,0), 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment