forked from desbonnm/christmastree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwinkle.py
63 lines (53 loc) · 2.22 KB
/
twinkle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
# Light all pixels at a fraction of max and randomly pulse to max for
# twinkle effect.
import random
import time
from rpi_ws281x import PixelStrip, Color
import argparse
# LED strip configuration:
LED_COUNT = 90 # 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 = 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
def twinkler():
base_brightness = int(LED_BRIGHTNESS/4)
delta_brightness = LED_BRIGHTNESS - base_brightness
while True:
for i in range(10):
c = base_brightness + int(delta_brightness/10.0*i)
yield Color(c, c, c)
for i in range(9, -1, -1):
c = base_brightness + int(delta_brightness/10.0*i)
yield Color(c, c, c)
for i in range(int(random.random()*300)):
yield Color(base_brightness, base_brightness, base_brightness)
def twinkle(strip, wait_ms=50):
twinklers = [twinkler() for i in range(strip.numPixels())]
while True:
for i in range(strip.numPixels()):
strip.setPixelColor(i, next(twinklers[i]))
strip.show()
time.sleep(wait_ms / 1000.0)
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)
# 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('Twinkle')
twinkle(strip)
except KeyboardInterrupt:
if args.clear:
colorWipe(strip, Color(0, 0, 0), 10)