pimoroni-pico/micropython/examples/plasma_stick/tree.py

43 wiersze
1.4 KiB
Python
Czysty Zwykły widok Historia

2022-11-29 18:08:15 +00:00
import time
import plasma
from plasma import plasma_stick
from random import random, choice
"""
A Christmas tree, with fairy lights!
This will probably work better if your LEDs are in a vaguely tree shaped bottle :)
"""
# Set how many LEDs you have
NUM_LEDS = 50
# we're using HSV colours in this example - find more at https://colorpicker.me/
# to convert a hue that's in degrees, divide it by 360
TREE_COLOUR = [0.34, 1.0, 0.6]
LIGHT_RATIO = 8 # every nth pixel is a light, the rest are tree.
2022-11-29 18:22:53 +00:00
LIGHT_COLOURS = ((0.0, 1.0, 1.0), # red
(0.1, 1.0, 1.0), # orange
(0.6, 1.0, 1.0), # blue
(0.85, 0.4, 1.0)) # pink
LIGHT_CHANGE_CHANCE = 0.5 # change to 0.0 if you want static lights
2022-11-29 18:08:15 +00:00
# set up the WS2812 / NeoPixel™ LEDs
led_strip = plasma.WS2812(NUM_LEDS, 0, 0, plasma_stick.DAT, color_order=plasma.COLOR_ORDER_RGB)
# start updating the LED strip
led_strip.start()
2022-11-29 18:22:53 +00:00
# initial setup
2022-11-29 18:08:15 +00:00
for i in range(NUM_LEDS):
if i % LIGHT_RATIO == 0: # add an appropriate number of lights
2022-11-29 18:22:53 +00:00
led_strip.set_hsv(i, *choice(LIGHT_COLOURS)) # choice randomly chooses from a list
2022-11-29 18:08:15 +00:00
else: # GREEN
led_strip.set_hsv(i, *TREE_COLOUR)
2022-11-29 18:22:53 +00:00
2022-11-29 18:08:15 +00:00
# animate
while True:
for i in range(NUM_LEDS):
if (i % LIGHT_RATIO == 0) and (random() < LIGHT_CHANGE_CHANCE):
led_strip.set_hsv(i, *choice(LIGHT_COLOURS))
time.sleep(0.5)