Skip to content

Commit 24d7e86

Browse files
committed
add Rain, RainbowRain, and MatrixRain animations
1 parent 9094402 commit 24d7e86

File tree

3 files changed

+137
-1
lines changed

3 files changed

+137
-1
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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.sparkle`
25+
================================================================================
26+
27+
Sparkle animation 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+
self.fill(background)
77+
78+
def draw(self):
79+
80+
# Move raindrops down
81+
keep = []
82+
for raindrop in self._raindrops:
83+
pixels = []
84+
if raindrop[1][0][0] >= 0:
85+
self.pixel_object[raindrop[0], raindrop[1][0][0]] = self._background
86+
for pixel in raindrop[1]:
87+
pixel[0] += 1
88+
if pixel[0] < self.pixel_object.height:
89+
pixels.append(pixel)
90+
if pixels:
91+
keep.append([raindrop[0], pixels])
92+
self._raindrops = keep
93+
94+
# Add a raindrop
95+
if len(self._raindrops) < self._count:
96+
x = random.randint(0, self.pixel_object.width - 1)
97+
self._raindrops.append([x, self._generate_droplet(x, self._length)])
98+
99+
# Draw raindrops
100+
for x, pixels in self._raindrops:
101+
for y, color in pixels:
102+
if y >= 0:
103+
self.pixel_object[x, y] = color
104+
105+
def _generate_droplet(self, x, length): # pylint: disable=unused-argument
106+
return [[n, self.color] for n in range(-length, 0)]
107+
108+
109+
class RainbowRain(Rain):
110+
"""
111+
Rainbow Rain animation.
112+
"""
113+
114+
def __init__(self, grid_object, speed, count=1, length=3, background=BLACK, name=None):
115+
super().__init__(grid_object, speed, BLACK, count, length, background, name)
116+
117+
def _generate_droplet(self, x, length):
118+
color = colorwheel(random.randint(0, 255))
119+
return [[n, calculate_intensity(color, 1.0 - -((n+1)/(length+1)))] for n in range(-length, 0)]
120+
121+
122+
class MatrixRain(Rain):
123+
"""
124+
The Matrix style animation.
125+
"""
126+
127+
def __init__(self, grid_object, speed, color=GREEN, count=1, length=3, background=(0, 64, 0), name=None):
128+
super().__init__(grid_object, speed, color, count, length, background, name)
129+
130+
def _generate_droplet(self, x, length):
131+
return [[n, calculate_intensity(self.color, random.randint(10, 100) * 1.0)] for n in range(-length, 0)]

adafruit_led_animation/grid

Whitespace-only changes.

adafruit_led_animation/grid.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,15 @@ def __init__(
122122
if bottom:
123123
x_end, y_end = bottom
124124

125+
self.height = y_end - y_start
126+
self.width = x_end - x_start
127+
125128
for x in range(x_start, x_end):
126129
self._x.append(
127130
PixelMap(
128-
strip, [mapper(x, y) for y in range(y_start, y_end)], individual_pixels=True
131+
strip,
132+
[mapper(x, y) for y in range(y_start, y_end)],
133+
individual_pixels=True,
129134
)
130135
)
131136
self.n = len(self._x)

0 commit comments

Comments
 (0)