|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2019-2020 Roy Hooper |
| 4 | +# Copyright (c) 2020 Kattni Rembor for Adafruit Industries |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in |
| 14 | +# all copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +# THE SOFTWARE. |
| 23 | +""" |
| 24 | +`adafruit_led_animation.animation.grid_rain` |
| 25 | +================================================================================ |
| 26 | +
|
| 27 | +Rain animations for CircuitPython helper library for LED animations. |
| 28 | +
|
| 29 | +* Author(s): Roy Hooper, Kattni Rembor |
| 30 | +
|
| 31 | +Implementation Notes |
| 32 | +-------------------- |
| 33 | +
|
| 34 | +**Hardware:** |
| 35 | +
|
| 36 | +* `Adafruit NeoPixels <https://www.adafruit.com/category/168>`_ |
| 37 | +* `Adafruit DotStars <https://www.adafruit.com/category/885>`_ |
| 38 | +
|
| 39 | +**Software and Dependencies:** |
| 40 | +
|
| 41 | +* Adafruit CircuitPython firmware for the supported boards: |
| 42 | + https://circuitpython.org/downloads |
| 43 | +
|
| 44 | +""" |
| 45 | + |
| 46 | +import random |
| 47 | +from adafruit_led_animation.animation import Animation |
| 48 | + |
| 49 | +__version__ = "0.0.0-auto.0" |
| 50 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git" |
| 51 | + |
| 52 | +from adafruit_led_animation.color import BLACK, colorwheel, calculate_intensity, GREEN |
| 53 | + |
| 54 | + |
| 55 | +class Rain(Animation): |
| 56 | + """ |
| 57 | + Droplets of rain. |
| 58 | +
|
| 59 | + :param grid_object: The initialised PixelGrid object. |
| 60 | + :param float speed: Animation speed in seconds, e.g. ``0.1``. |
| 61 | + :param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format. |
| 62 | + :param count: Number of sparkles to generate per animation cycle. |
| 63 | + :param length: Number of pixels per raindrop (Default 3) |
| 64 | + :param background: Background color (Default BLACK). |
| 65 | + """ |
| 66 | + |
| 67 | + # pylint: disable=too-many-arguments |
| 68 | + def __init__( |
| 69 | + self, grid_object, speed, color, count=1, length=3, background=BLACK, name=None |
| 70 | + ): |
| 71 | + self._count = count |
| 72 | + self._length = length |
| 73 | + self._background = background |
| 74 | + self._raindrops = [] |
| 75 | + super().__init__(grid_object, speed, color, name=name) |
| 76 | + |
| 77 | + def draw(self): |
| 78 | + |
| 79 | + # Move raindrops down |
| 80 | + keep = [] |
| 81 | + for raindrop in self._raindrops: |
| 82 | + pixels = [] |
| 83 | + if raindrop[1][0][0] >= 0: |
| 84 | + self.pixel_object[raindrop[0], raindrop[1][0][0]] = self._background |
| 85 | + for pixel in raindrop[1]: |
| 86 | + pixel[0] += 1 |
| 87 | + if pixel[0] < self.pixel_object.height: |
| 88 | + pixels.append(pixel) |
| 89 | + if pixels: |
| 90 | + keep.append([raindrop[0], pixels]) |
| 91 | + self._raindrops = keep |
| 92 | + |
| 93 | + # Add a raindrop |
| 94 | + if len(self._raindrops) < self._count: |
| 95 | + x = random.randint(0, self.pixel_object.width - 1) |
| 96 | + self._raindrops.append([x, self._generate_droplet(x, self._length)]) |
| 97 | + |
| 98 | + # Draw raindrops |
| 99 | + for x, pixels in self._raindrops: |
| 100 | + for y, color in pixels: |
| 101 | + if y >= 0: |
| 102 | + self.pixel_object[x, y] = color |
| 103 | + |
| 104 | + def _generate_droplet(self, x, length): # pylint: disable=unused-argument |
| 105 | + return [[n, self.color] for n in range(-length, 0)] |
| 106 | + |
| 107 | + |
| 108 | +class RainbowRain(Rain): |
| 109 | + """ |
| 110 | + Rainbow Rain animation. |
| 111 | + """ |
| 112 | + |
| 113 | + def __init__( # pylint: disable=too-many-arguments |
| 114 | + self, grid_object, speed, count=1, length=3, background=BLACK, name=None |
| 115 | + ): |
| 116 | + super().__init__(grid_object, speed, BLACK, count, length, background, name) |
| 117 | + |
| 118 | + def _generate_droplet(self, x, length): |
| 119 | + color = colorwheel(random.randint(0, 255)) |
| 120 | + return [ |
| 121 | + [n, calculate_intensity(color, 1.0 - -((n + 1) / (length + 1)))] |
| 122 | + for n in range(-length, 0) |
| 123 | + ] |
| 124 | + |
| 125 | + |
| 126 | +class MatrixRain(Rain): |
| 127 | + """ |
| 128 | + The Matrix style animation. |
| 129 | + """ |
| 130 | + |
| 131 | + def __init__( # pylint: disable=too-many-arguments |
| 132 | + self, |
| 133 | + grid_object, |
| 134 | + speed, |
| 135 | + color=GREEN, |
| 136 | + count=1, |
| 137 | + length=6, |
| 138 | + background=(0, 32, 0), |
| 139 | + name=None, |
| 140 | + ): |
| 141 | + super().__init__(grid_object, speed, color, count, length, background, name) |
| 142 | + |
| 143 | + def _generate_droplet(self, x, length): |
| 144 | + return [ |
| 145 | + [n, calculate_intensity(self.color, random.randint(10, 100) * 1.0)] |
| 146 | + for n in range(-length, 0) |
| 147 | + ] |
0 commit comments