rainbowsparkles
This commit is contained in:
parent
cdd61d6be1
commit
5c90cfafac
2 changed files with 118 additions and 7 deletions
|
|
@ -52,26 +52,43 @@ __version__ = "0.0.0-auto.0"
|
||||||
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
|
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Rainbow(Animation):
|
class Rainbow(Animation):
|
||||||
"""
|
"""
|
||||||
The classic rainbow color wheel.
|
The classic rainbow color wheel.
|
||||||
|
|
||||||
:param pixel_object: The initialised LED object.
|
:param pixel_object: The initialised LED object.
|
||||||
:param float speed: Animation refresh rate in seconds, e.g. ``0.1``.
|
:param float speed: Animation refresh rate in seconds, e.g. ``0.1``.
|
||||||
:param period: Period to cycle the rainbow over. Default 5.
|
:param float period: Period to cycle the rainbow over in seconds. Default 5.
|
||||||
|
:param float step: Color wheel step. Default 1.
|
||||||
|
:param str name: Name of animation (optional, useful for sequences and debugging).
|
||||||
|
:param bool precompute_rainbow: Whether to precompute the rainbow. Uses more memory. (default True).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# pylint: disable=too-many-arguments
|
# pylint: disable=too-many-arguments
|
||||||
def __init__(self, pixel_object, speed, period=5, name=None):
|
def __init__(self, pixel_object, speed, period=5, step=1, name=None, precompute_rainbow=True):
|
||||||
super().__init__(pixel_object, speed, BLACK, name=name)
|
super().__init__(pixel_object, speed, BLACK, name=name)
|
||||||
self._period = period
|
self._period = period
|
||||||
|
self._step = step
|
||||||
|
self._wheel_index = 0
|
||||||
|
self.colors = None
|
||||||
self._generator = self._color_wheel_generator()
|
self._generator = self._color_wheel_generator()
|
||||||
|
if precompute_rainbow:
|
||||||
|
self.generate_rainbow()
|
||||||
|
|
||||||
|
def generate_rainbow(self):
|
||||||
|
self.colors = []
|
||||||
|
i = 0
|
||||||
|
while i < 256:
|
||||||
|
self.colors.append(colorwheel(int(i)))
|
||||||
|
i += self._step
|
||||||
|
|
||||||
cycle_complete_supported = True
|
cycle_complete_supported = True
|
||||||
|
|
||||||
def _color_wheel_generator(self):
|
def _color_wheel_generator(self):
|
||||||
period = int(self._period * NANOS_PER_SECOND)
|
period = int(self._period * NANOS_PER_SECOND)
|
||||||
|
|
||||||
|
num_pixels = len(self.pixel_object)
|
||||||
last_update = monotonic_ns()
|
last_update = monotonic_ns()
|
||||||
cycle_position = 0
|
cycle_position = 0
|
||||||
last_pos = 0
|
last_pos = 0
|
||||||
|
|
@ -84,16 +101,34 @@ class Rainbow(Animation):
|
||||||
if pos < last_pos:
|
if pos < last_pos:
|
||||||
cycle_completed = True
|
cycle_completed = True
|
||||||
last_pos = pos
|
last_pos = pos
|
||||||
wheel_index = int((pos / period) * 256)
|
wheel_index = int((pos / period) * len(self.colors))
|
||||||
self.pixel_object[:] = [
|
|
||||||
colorwheel((i + wheel_index) % 255)
|
if self.colors:
|
||||||
for i, _ in enumerate(self.pixel_object)
|
self._draw_precomputed(num_pixels, wheel_index)
|
||||||
]
|
else:
|
||||||
|
wheel_index = int((pos / period) * 256)
|
||||||
|
self.pixel_object[:] = [
|
||||||
|
colorwheel((i + wheel_index) % 255)
|
||||||
|
for i in range(num_pixels)
|
||||||
|
]
|
||||||
|
self._wheel_index = wheel_index
|
||||||
self.show()
|
self.show()
|
||||||
if cycle_completed:
|
if cycle_completed:
|
||||||
self.cycle_complete()
|
self.cycle_complete()
|
||||||
yield
|
yield
|
||||||
|
|
||||||
|
def _draw_precomputed(self, num_pixels, wheel_index):
|
||||||
|
for i in range(0, num_pixels, len(self.colors)):
|
||||||
|
num = len(self.colors)
|
||||||
|
if i + len(self.colors) > num_pixels:
|
||||||
|
num = num_pixels - i
|
||||||
|
if wheel_index + num > len(self.colors):
|
||||||
|
colors_left = len(self.colors) - wheel_index
|
||||||
|
self.pixel_object[i:i + colors_left] = self.colors[wheel_index:]
|
||||||
|
self.pixel_object[i + colors_left:i + num] = self.colors[:num - colors_left]
|
||||||
|
else:
|
||||||
|
self.pixel_object[i:i + num] = self.colors[wheel_index:wheel_index + num]
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
next(self._generator)
|
next(self._generator)
|
||||||
|
|
||||||
|
|
|
||||||
76
adafruit_led_animation/animation/rainbowsparkle.py
Normal file
76
adafruit_led_animation/animation/rainbowsparkle.py
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
# The MIT License (MIT)
|
||||||
|
#
|
||||||
|
# Copyright (c) 2019-2020 Roy Hooper
|
||||||
|
# Copyright (c) 2020 Kattni Rembor for Adafruit Industries
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in
|
||||||
|
# all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
# THE SOFTWARE.
|
||||||
|
"""
|
||||||
|
`adafruit_led_animation.animation.rainbowcomet`
|
||||||
|
================================================================================
|
||||||
|
|
||||||
|
TODO
|
||||||
|
|
||||||
|
* Author(s): Roy Hooper, Kattni Rembor
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import random
|
||||||
|
|
||||||
|
from adafruit_led_animation.animation.rainbow import Rainbow
|
||||||
|
|
||||||
|
|
||||||
|
class RainbowSparkle(Rainbow):
|
||||||
|
|
||||||
|
def __init__(self, pixel_object, speed, period=5, num_sparkles=None, step=1, name=None,
|
||||||
|
bg_brightness=0.2):
|
||||||
|
self._num_sparkles = num_sparkles
|
||||||
|
if num_sparkles is None:
|
||||||
|
self._num_sparkles = max(1, int(len(pixel_object) / 20))
|
||||||
|
self._sparkle_duration = 2
|
||||||
|
self._bg_brightness = bg_brightness
|
||||||
|
self._bright_colors = None
|
||||||
|
super().__init__(pixel_object=pixel_object, speed=speed, period=period, step=step, name=name,
|
||||||
|
precompute_rainbow=True)
|
||||||
|
|
||||||
|
def generate_rainbow(self, step=1):
|
||||||
|
super().generate_rainbow()
|
||||||
|
self._bright_colors = self.colors[:]
|
||||||
|
for i, color in enumerate(self.colors):
|
||||||
|
if isinstance(self.colors[i], int):
|
||||||
|
self.colors[i] = (
|
||||||
|
int(self._bg_brightness * ((color & 0xff0000) >> 16)),
|
||||||
|
int(self._bg_brightness * ((color & 0xff00) >> 8)),
|
||||||
|
int(self._bg_brightness * (color & 0xff))
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self.colors[i] = (
|
||||||
|
int(self._bg_brightness * color[0]),
|
||||||
|
int(self._bg_brightness * color[1]),
|
||||||
|
int(self._bg_brightness * color[2])
|
||||||
|
)
|
||||||
|
|
||||||
|
def show(self):
|
||||||
|
pixels = [
|
||||||
|
random.randint(0, len(self.pixel_object)-1)
|
||||||
|
for n in range(self._num_sparkles)
|
||||||
|
]
|
||||||
|
for pixel in pixels:
|
||||||
|
bc = (self._wheel_index + pixel) % len(self._bright_colors)
|
||||||
|
self.pixel_object[pixel] = self._bright_colors[bc]
|
||||||
|
super().show()
|
||||||
Loading…
Reference in a new issue