Created
December 10, 2021 12:25
-
-
Save benevpi/0ebb996d5788a1f1c06772a56b0eda44 to your computer and use it in GitHub Desktop.
twinkling animation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import board | |
import neopixel | |
from adafruit_led_animation.animation.rainbow import Rainbow | |
from adafruit_led_animation.animation import Animation | |
from adafruit_led_animation.color import BLACK, RED, GREEN, YELLOW | |
from adafruit_led_animation.color import calculate_intensity | |
from random import randrange, randint | |
threshold = 0.1 | |
step = 0.1 | |
class MyAnimation(Animation): | |
def __init__(self, pixel_object, num_leds, twinkling_step=0.1, speed=0.1, num_twinkles=1): | |
super().__init__(pixel_object, speed, BLACK) | |
self.pixel_object = pixel_object | |
self.num_leds = num_leds | |
self.position = 0 | |
self.colours = [RED, GREEN, YELLOW] | |
self.num_twinkles = num_twinkles | |
self.twinkling_leds = [0 for x in range(num_twinkles)] | |
self.twinkling_direction = [-1 for x in range(num_twinkles)] | |
self.twinkling_amount = [randint(0,10)/10 for x in range(num_twinkles)] | |
print(self.twinkling_amount) | |
self.twinkling_colours = [randint(0,len(self.colours)) for x in range(num_twinkles)] | |
def draw(self): | |
for led in range(self.num_twinkles): | |
self.twinkling_amount[led] = self.twinkling_amount[led] + self.twinkling_direction[led]*step | |
if (self.twinkling_amount[led] > 1): | |
self.twinkling_amount[led] = 1 | |
self.twinkling_direction[led] = -1 | |
if (self.twinkling_amount[led] < threshold and self.twinkling_direction[led] == -1): | |
self.pixel_object[self.twinkling_leds[led]] = BLACK | |
self.twinkling_leds[led] = randint(0,self.num_leds-1) | |
self.twinkling_colours[led] = self.colours[randint(0,len(self.colours)-1)] | |
self.twinkling_direction[led] = 1 | |
self.pixel_object[self.twinkling_leds[led]] = calculate_intensity(self.twinkling_colours[led], | |
self.twinkling_amount[led]) | |
pixel_pin = board.GP0 | |
pixel_num = 5 | |
pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.5, auto_write=False) | |
animation = MyAnimation(pixels, 5, num_twinkles=2, speed=0.1) | |
while True: | |
animation.animate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment