rainbowsparkles

This commit is contained in:
Roy Hooper 2020-05-17 14:04:06 -04:00
parent cdd61d6be1
commit 5c90cfafac
2 changed files with 118 additions and 7 deletions

View file

@ -52,26 +52,43 @@ __version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
class Rainbow(Animation):
"""
The classic rainbow color wheel.
:param pixel_object: The initialised LED object.
: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
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)
self._period = period
self._step = step
self._wheel_index = 0
self.colors = None
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
def _color_wheel_generator(self):
period = int(self._period * NANOS_PER_SECOND)
num_pixels = len(self.pixel_object)
last_update = monotonic_ns()
cycle_position = 0
last_pos = 0
@ -84,16 +101,34 @@ class Rainbow(Animation):
if pos < last_pos:
cycle_completed = True
last_pos = pos
wheel_index = int((pos / period) * len(self.colors))
if self.colors:
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 enumerate(self.pixel_object)
for i in range(num_pixels)
]
self._wheel_index = wheel_index
self.show()
if cycle_completed:
self.cycle_complete()
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):
next(self._generator)

View 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()