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.
222 lines
6.0 KiB
222 lines
6.0 KiB
# 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 time
|
|
import random
|
|
|
|
from neopixel import *
|
|
|
|
|
|
# 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 = 800000 # 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 # True to invert the signal (when using NPN transistor level shift)
|
|
LED_CHANNEL = 0
|
|
LED_STRIP = ws.SK6812_STRIP_RGBW
|
|
#LED_STRIP = ws.SK6812W_STRIP
|
|
|
|
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 self.starLED in range(236, 349):
|
|
goodPlace = False
|
|
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):
|
|
# intensity = intensityFromDistance(edge-(led.x - led.y/3))
|
|
# strip.setPixelColor(i, colorFromIntensity(intensity))
|
|
# blueness = int(i/2)%25
|
|
if(i > 236 and i<349):
|
|
# Gör bokstaven A grön
|
|
strip.setPixelColor(i, Color(70,13,0,5))
|
|
else:
|
|
blueness = int((1.0-led.y)**3 * 60) if led.y>0.10 else 0
|
|
if blueness < 5:
|
|
blueness = 5
|
|
#redness = int((led.x / x_max)**2 * 255)
|
|
redness = int((1.0-led.y)**8 * 4)
|
|
strip.setPixelColor(i, Color(0,redness,blueness,0))
|
|
|
|
|
|
# strip.setPixelColor(440, Color(random.randint(68,72),random.randint(68,72),random.randint(68,72),random.randint(4,6)))
|
|
|
|
|
|
# lights = [257, 334, 283, 249, 290, 346, 299, 311, 242]
|
|
# for light in lights:
|
|
# 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))
|
|
chain0 = [260, 274]
|
|
chain1 = [254, 334, 283]
|
|
chain2 = [247, 290, 346]
|
|
chain3 = [299, 314, 241]
|
|
|
|
strip.setPixelColor(264, Color(0,0,0,50))
|
|
strip.setPixelColor(265, Color(160,255,0, 20))
|
|
strip.setPixelColor(266, Color(160,255,0, 20))
|
|
strip.setPixelColor(267, Color(160,255,0, 20))
|
|
strip.setPixelColor(268, Color(0,0,0,50))
|
|
|
|
animation+=1
|
|
animation %= 400
|
|
if animation>= 0 and animation <50:
|
|
turnOnChain(chain0)
|
|
turnOnChain(chain2)
|
|
if animation >=50 and animation <100:
|
|
turnOnChain(chain1)
|
|
turnOnChain(chain2)
|
|
if animation >=100 and animation <150:
|
|
turnOnChain(chain1)
|
|
turnOnChain(chain3)
|
|
if animation >=150 and animation <200:
|
|
turnOnChain(chain2)
|
|
turnOnChain(chain3)
|
|
if animation >=200 and animation <250:
|
|
turnOnChain(chain0)
|
|
turnOnChain(chain2)
|
|
if animation >=250 and animation <300:
|
|
turnOnChain(chain0)
|
|
turnOnChain(chain3)
|
|
if animation >=300 and animation <350:
|
|
turnOnChain(chain1)
|
|
turnOnChain(chain3)
|
|
if animation >=350 and animation <400:
|
|
turnOnChain(chain1)
|
|
turnOnChain(chain0)
|
|
|
|
|
|
for star in Star.stars:
|
|
star.update(strip)
|
|
|
|
|
|
#time.sleep(0.05)
|
|
counter +=1
|
|
strip.show()
|
|
|
|
|
|
# edge+=10
|
|
# if edge>5000:
|
|
# edge = -500
|
|
# time.sleep(0.01)
|