シリアルLEDテープ(WS2815)で遊ぶ ③スクリプトを書く(クリスマスVer.)

こちらの続きです。

前回はサンプルスクリプトでLEDテープをピカピカさせました。
今度は自分でスクリプトを書いてみましょう。

ちょうどクリスマスなので、流れ星のように光らせてクリスマスツリーの飾りにします。

仕上がり

いきなりですが、こんな感じになりました。
ツリーの右上から左下に向かって流れるように光るのがシリアルLED、その他ほんわり光ってるやつはツリー付属のLEDです。

コード

サンプルスクリプトのstrandtest.pyをもとにして、流れ星用のファンクションを追加、使わないファンクションを削除したものです。
流れ星は、色、速さ、次の星との間隔をランダムにして、見飽きない感じに仕上げました。

#!/usr/bin/env python3
import time
from rpi_ws281x import PixelStrip, Color
import argparse
import random
import _rpi_ws281x as ws
# LED strip configuration:
LED_COUNT = 60        # Number of LED pixels.
LED_PIN = 21  # 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       # set to '1' for GPIOs 13, 19, 41, 45 or 53
# Define functions which animate LEDs in various ways.
def shootingStar(strip, color, wait_ms=50):
"""Shooting Star animation."""
for i in range(0, strip.numPixels() - 2):
strip.setPixelColor(strip.numPixels() - i - 2, color)
strip.setPixelColor(strip.numPixels() - i - 1, color)
strip.setPixelColor(strip.numPixels() - i, color)
strip.show()
time.sleep(wait_ms / 1000.0)
strip.setPixelColor(strip.numPixels() - i - 2, Color(0, 0, 0))
strip.setPixelColor(strip.numPixels() - i - 1, Color(0, 0, 0))
strip.setPixelColor(strip.numPixels() - i, Color(0, 0, 0))
strip.show()
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.0)
# 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 = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL)
strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL, ws.WS2811_STRIP_RGB)
# 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:
# Random Color
# 10% Bright White, 10% Red, 10% Green, 70% Light Blue
rdColor = random.randint(0, 99)
if rdColor <= 9:
color = Color(255, 255, 255) # Bright White
elif rdColor >= 10 and rdColor <= 19:
color = Color(64, 0, 0) # Red
elif rdColor >= 20 and rdColor <= 29:
color = Color(0, 64, 0) # Green
else:
color = Color(16, 16, 64) # Light Blue
# Random Speed (ms)
# 10/15/20/25/30 ms to move next chip
rdSpeed = random.randrange(10, 31, 5)
# Execute
shootingStar(strip, color, rdSpeed)
# Random Interval (ms)
# 500 - 10000
rdSleep = random.randint(500, 10000)
time.sleep(rdSleep / 1000.0)
except KeyboardInterrupt:
if args.clear:
colorWipe(strip, Color(0, 0, 0), 10)

コメント

タイトルとURLをコピーしました