You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.7 KiB

4 years ago
# NeoPixel library strandtest example
# Author: Tony DiCola (tony@tonydicola.com)
#
# Direct port of the Arduino NeoPixel library strandtest example. Showcases
# various animations on a strip of NeoPixels.
import random
from neopixel import ws, Adafruit_NeoPixel, Color
# LED strip configuration:
LED_COUNT = 494 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
LED_FREQ_HZ = 1200000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # Invert when using NPN transistor level shift
LED_CHANNEL = 0
LED_STRIP = ws.SK6812_STRIP_RGBW
class Led:
leds = []
def __init__(self, id, x, y):
self.x = x
self.y = y
Led.leds.append(self)
class Star:
stars = []
def __init__(self):
self.starstate = 0
self.starBrightness = 0
self.starCountdown = 0
self.starLED = 50
Star.stars.append(self)
def update(self, strip):
if self.starstate == 0:
self.starBrightness += 3
if self.starBrightness > 120:
self.starstate = 1
if self.starstate == 1:
self.starBrightness -= self.starBrightness/100.0
if self.starBrightness < 5:
self.starstate = 2
self.starBrightness = 0
self.starCountdown = random.randint(100, 500)
if self.starstate == 2:
self.starCountdown -= 1
if self.starCountdown == 0:
self.starLED = 0
otherStars = [s.starLED for s in Star.stars]
goodPlace = False
while not goodPlace:
self.starLED = random.randint(0, 493)
goodPlace = True
if Led.leds[self.starLED].y < random.random():
goodPlace = False
for otherStar in otherStars:
if abs(self.starLED-otherStar) < 4:
goodPlace = False
self.starstate = 0
brightness = int(self.starBrightness + (self.starBrightness * random.random())/20.0)
strip.setPixelColor(self.starLED, Color(0, 0, int(brightness/2), brightness))
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 colorFromIntensity(intensity):
# Intensity should be 0-255
if intensity > 20:
return Color(0, intensity, intensity/2, intensity/8+20)
else:
return Color(0, 0, 0, 20)
def intensityFromDistance(distance):
if distance < 0:
return 0
intensity = 255-(distance/3)
if intensity < 0:
return 0
else:
return intensity
def turnOnChain(chain):
for light in chain:
strip.setPixelColor(light, Color(0, 255, 0, 0))
strip.setPixelColor(light+1, Color(0, 255, 0, 0))
strip.setPixelColor(light+2, Color(0, 0, 0, 0))
strip.setPixelColor(light-1, Color(0, 0, 0, 0))
# Main program logic follows:
if __name__ == '__main__':
# Create NeoPixel object with appropriate configuration.
strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA,
LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL,
LED_STRIP)
# Intialize the library (must be called once before other functions).
strip.begin()
print('Press Ctrl-C to quit.')
with open("leds.txt", "r") as infile:
for line in infile:
entry = [int(x) for x in line.split(',')]
Led(entry[0], entry[1], entry[2])
limits = [min([l.x for l in Led.leds]),
max([l.x for l in Led.leds]),
min([l.y for l in Led.leds]),
max([l.y for l in Led.leds])]
for l in Led.leds:
l.y = (float(l.y - float(limits[3])) / (limits[2]-limits[3]))
l.x = (float(l.x - float(limits[0])) / (limits[3]-limits[2]))
x_max = max([l.x for l in Led.leds])
print("Maximum X: {}".format(x_max))
# all LEDs now ready
edge = -500
counter = 1
hider = 0
animation = 0
stars = []
for i in range(0, 10):
Star()
while True:
for i, led in enumerate(Led.leds):
blueness = int((1.0-led.y)**3 * 60) if led.y > 0.10 else 0
if blueness < 5:
blueness = 5
redness = int((1.0-led.y)**8 * 4)
strip.setPixelColor(i, Color(0, redness, blueness, 0))
for star in Star.stars:
star.update(strip)
counter += 1
strip.show()