Skip to content

Raspberry Pi LED Effekt

  • by

Auf https://tutorials-raspberrypi.de/ gibt ein gutes Tutorial, in dem erklärt wird, wie man WS2801 Streifen ansteuert. In dem Kommentaren hat jemand einen Effekt gepostet, den ich gut fand und den ich weiter entwickelt habe. Es ist ein zufälliger “Glitter” von weissen pixel über die gesamte Anzahl der zur Verfügung stehenden LEDs.

Ich habe das Ganze bunt gemacht. Mit einem definierten Satz von kräftigen Farben.

import RPi.GPIO as GPIO
import Adafruit_WS2801
import Adafruit_GPIO.SPI as SPI
import time, random

PIXEL_COUNT = 96
farben = {'orange': [60, 20, 0],'orange2': [40, 15, 0],'red1': [77, 8, 29],'red2': [42, 3, 8],
        'red3': [66, 5, 0],'red4': [14, 3, 3],'red4': [10, 1, 0],'flame': [200, 45, 4],
        'yellow': [200,40,5],'turmeric': [254, 132, 14],
        'green': [10, 30, 0],'green2': [80, 220, 20],'green3': [85, 108, 11],'green4': [74, 61, 0],
        'green5': [4, 30, 2],'magenta':[210, 7, 42],'magenta2':[28, 0, 6],
        'blue':[70, 2, 160],'blue2':[16, 3, 43],'blue3':[3, 35, 50]}
cols = list(farben.values())
SPI_PORT   = 0
SPI_DEVICE = 0
pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE), gpio=GPIO)

def glitterOn(pixels, colorA = Adafruit_WS2801.RGB_to_color(255,255,255)):
    pico = pixels.count()
    percentPerPin = int(100 / pico)
    if percentPerPin < 1: percentPerPin = 1
    for p in range(3):
        if p == 0:
            dimVal = 40
        elif p == 1:
            dimVal = 0
        elif p == 2:
            dimVal = 0
        for i in range(pico):
            rdm = [0,0,0,0]
            colorS = [0,0,0,0]
            for j in range(3):
                rdm[j] = random.randint(0, pico-1)
                while pixels.get_pixel(rdm[j]) > 0:
                    rdm[j] = rdm[j] + 1
                    if rdm[j] >= pico:
                        rdm[j] = 0
                colorS[j] = pixels.get_pixel(rdm[j])
            #for j in range(3):
            colPos = random.randint(0, len(cols)-1)
            if p == 0:
                colR = cols[colPos][0] - int(cols[colPos][0] / 100 * ((pico-i) * percentPerPin))
                if colR < 0: colR = 0
                colG = cols[colPos][1] - int(cols[colPos][1] / 100 * ((pico-i) * percentPerPin))
                if colG < 0: colG = 0
                colB = cols[colPos][2] - int(cols[colPos][2] / 100 * ((pico-i) * percentPerPin))
                if colB < 0: colB = 0
            if p == 1:
                colR = cols[colPos][0]
                colG = cols[colPos][1]
                colB = cols[colPos][2]
            if p == 2:
                colR = cols[colPos][0] - int(cols[colPos][0] / 100 * (i * percentPerPin))
                if colR < 0: colR = 0
                colG = cols[colPos][1] - int(cols[colPos][1] / 100 * (i * percentPerPin))
                if colG < 0: colG = 0
                colB = cols[colPos][2] - int(cols[colPos][2] / 100 * (i * percentPerPin))
                if colB < 0: colB = 0

            #print(p, ' ', percentPerPin, ' ',colR, ' ',colG, ' ',colB)
            newRandomColor = Adafruit_WS2801.RGB_to_color(colR,colG,colB)
            pixels.set_pixel(rdm[j], newRandomColor)
            pixels.show()
            time.sleep(0.05)
            for j in range(1,3):
                if rdm[0] >= rdm[j]:
                    pixels.set_pixel(rdm[j],colorS[j])
            pixels.show()
            pixels.clear()
            pixels.show()
#        time.sleep(2)
    time.sleep(0.1)

if __name__ == "__main__":
    pixels.clear()
    pixels.show()
    glitterOn(pixels)

So sieht das dann aus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert