Skip to content

Commit ed8e0ba

Browse files
authored
Merge pull request #67 from gamblor21/timed-animation
Timed animation
2 parents 6689858 + e23e57b commit ed8e0ba

File tree

4 files changed

+239
-0
lines changed

4 files changed

+239
-0
lines changed
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# SPDX-FileCopyrightText: 2020 Gamblor21
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
`adafruit_led_animation.animation.volume`
6+
================================================================================
7+
Volume animation for CircuitPython helper library for LED animations.
8+
* Author(s): Mark Komus
9+
Implementation Notes
10+
--------------------
11+
**Hardware:**
12+
* `Adafruit NeoPixels <https://www.adafruit.com/category/168>`_
13+
* `Adafruit DotStars <https://www.adafruit.com/category/885>`_
14+
**Software and Dependencies:**
15+
* Adafruit CircuitPython firmware for the supported boards:
16+
https://circuitpython.org/downloads
17+
"""
18+
19+
from adafruit_led_animation.animation import Animation
20+
21+
22+
def map_range(x, in_min, in_max, out_min, out_max):
23+
"""
24+
Maps a number from one range to another.
25+
:return: Returns value mapped to new range
26+
:rtype: float
27+
"""
28+
mapped = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
29+
if out_min <= out_max:
30+
return max(min(mapped, out_max), out_min)
31+
32+
return min(max(mapped, out_max), out_min)
33+
34+
35+
class Volume(Animation):
36+
"""
37+
Animate the brightness and number of pixels based on volume.
38+
:param pixel_object: The initialised LED object.
39+
:param float speed: Animation update speed in seconds, e.g. ``0.1``.
40+
:param brightest_color: Color at max volume ``(r, g, b)`` tuple, or ``0x000000`` hex format
41+
:param decoder: a MP3Decoder object that the volume will be taken from
42+
:param float max_volume: what volume is considered maximum where everything is lit up
43+
"""
44+
45+
# pylint: disable=too-many-arguments
46+
def __init__(
47+
self, pixel_object, speed, brightest_color, decoder, max_volume=500, name=None
48+
):
49+
self._decoder = decoder
50+
self._num_pixels = len(pixel_object)
51+
self._max_volume = max_volume
52+
self._brightest_color = brightest_color
53+
super().__init__(pixel_object, speed, brightest_color, name=name)
54+
55+
def set_brightest_color(self, brightest_color):
56+
"""
57+
Animate the brightness and number of pixels based on volume.
58+
:param brightest_color: Color at max volume ``(r, g, b)`` tuple, or ``0x000000`` hex format
59+
"""
60+
self._brightest_color = brightest_color
61+
62+
def draw(self):
63+
red = int(
64+
map_range(
65+
self._decoder.rms_level,
66+
0,
67+
self._max_volume,
68+
0,
69+
self._brightest_color[0],
70+
)
71+
)
72+
green = int(
73+
map_range(
74+
self._decoder.rms_level,
75+
0,
76+
self._max_volume,
77+
0,
78+
self._brightest_color[1],
79+
)
80+
)
81+
blue = int(
82+
map_range(
83+
self._decoder.rms_level,
84+
0,
85+
self._max_volume,
86+
0,
87+
self._brightest_color[2],
88+
)
89+
)
90+
91+
lit_pixels = int(
92+
map_range(self._decoder.rms_level, 0, self._max_volume, 0, self._num_pixels)
93+
)
94+
if lit_pixels > self._num_pixels:
95+
lit_pixels = self._num_pixels
96+
97+
self.pixel_object[0:lit_pixels] = [(red, green, blue)] * lit_pixels
98+
self.pixel_object[lit_pixels : self._num_pixels] = [(0, 0, 0)] * (
99+
self._num_pixels - lit_pixels
100+
)
+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# SPDX-FileCopyrightText: 2020 Gamblor21
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
`adafruit_led_animation.timedsequence`
6+
================================================================================
7+
8+
Animation timed sequence helper for CircuitPython helper library for LED animations.
9+
10+
11+
* Author(s): Mark Komus
12+
13+
Implementation Notes
14+
--------------------
15+
16+
**Hardware:**
17+
18+
* `Adafruit NeoPixels <https://www.adafruit.com/category/168>`_
19+
* `Adafruit DotStars <https://www.adafruit.com/category/885>`_
20+
21+
**Software and Dependencies:**
22+
23+
* Adafruit CircuitPython firmware for the supported boards:
24+
https://circuitpython.org/downloads
25+
26+
"""
27+
28+
from adafruit_led_animation.sequence import AnimationSequence
29+
from . import MS_PER_SECOND
30+
31+
32+
class TimedAnimationSequence(AnimationSequence):
33+
"""
34+
A sequence of Animations to run in succession, each animation running for an
35+
individual amount of time.
36+
:param members: The animation objects or groups followed by how long the animation
37+
should run in seconds.
38+
:param bool auto_clear: Clear the pixels between animations. If ``True``, the current animation
39+
will be cleared from the pixels before the next one starts.
40+
Defaults to ``False``.
41+
:param bool random_order: Activate the animations in a random order. Defaults to ``False``.
42+
:param bool auto_reset: Automatically call reset() on animations when changing animations.
43+
.. code-block:: python
44+
import board
45+
import neopixel
46+
from adafruit_led_animation.timedsequence import TimedAnimationSequence
47+
import adafruit_led_animation.animation.comet as comet_animation
48+
import adafruit_led_animation.animation.sparkle as sparkle_animation
49+
import adafruit_led_animation.animation.blink as blink_animation
50+
import adafruit_led_animation.color as color
51+
strip_pixels = neopixel.NeoPixel(board.A1, 30, brightness=1, auto_write=False)
52+
blink = blink_animation.Blink(strip_pixels, 0.2, color.RED)
53+
comet = comet_animation.Comet(strip_pixels, 0.1, color.BLUE)
54+
sparkle = sparkle_animation.Sparkle(strip_pixels, 0.05, color.GREEN)
55+
animations = TimedAnimationSequence(blink, 5, comet, 3, sparkle, 7)
56+
while True:
57+
animations.animate()
58+
"""
59+
60+
# pylint: disable=too-many-instance-attributes
61+
def __init__(
62+
self, *members, auto_clear=True, random_order=False, auto_reset=False, name=None
63+
):
64+
self._animation_members = []
65+
self._animation_timings = []
66+
for x, item in enumerate(members):
67+
if not x % 2:
68+
self._animation_members.append(item)
69+
else:
70+
self._animation_timings.append(item)
71+
72+
super().__init__(
73+
*self._animation_members,
74+
auto_clear=auto_clear,
75+
random_order=random_order,
76+
auto_reset=auto_reset,
77+
advance_on_cycle_complete=False,
78+
name=name,
79+
)
80+
self._advance_interval = self._animation_timings[self._current] * MS_PER_SECOND
81+
82+
def activate(self, index):
83+
super().activate(index)
84+
self._advance_interval = self._animation_timings[self._current] * MS_PER_SECOND
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: 2020 Gamblor21
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
Example for TimedSequence
6+
"""
7+
import board
8+
import neopixel
9+
from adafruit_led_animation.timedsequence import TimedAnimationSequence
10+
import adafruit_led_animation.animation.comet as comet_animation
11+
import adafruit_led_animation.animation.sparkle as sparkle_animation
12+
import adafruit_led_animation.animation.blink as blink_animation
13+
from adafruit_led_animation import color
14+
15+
strip_pixels = neopixel.NeoPixel(board.D6, 32, brightness=0.1, auto_write=False)
16+
blink = blink_animation.Blink(strip_pixels, 0.3, color.RED)
17+
comet = comet_animation.Comet(strip_pixels, 0.1, color.BLUE)
18+
sparkle = sparkle_animation.Sparkle(strip_pixels, 0.05, color.GREEN)
19+
animations = TimedAnimationSequence(blink, 2, comet, 4, sparkle, 5)
20+
while True:
21+
animations.animate()

examples/led_animation_volume.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-FileCopyrightText: 2023 Tim Cocks
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""Volume Animation Example"""
6+
import board
7+
from audiomp3 import MP3Decoder
8+
import neopixel
9+
from adafruit_led_animation.animation import volume
10+
11+
try:
12+
from audioio import AudioOut
13+
except ImportError:
14+
try:
15+
from audiopwmio import PWMAudioOut as AudioOut
16+
except ImportError:
17+
pass # not always supported by every board!
18+
19+
# Fill in your own MP3 file or use the one from the learn guide:
20+
# https://learn.adafruit.com/circuitpython-essentials/circuitpython-mp3-audio#installing-project-code-3067700
21+
mp3file = "happy.mp3"
22+
with open(mp3file, "rb") as mp3:
23+
decoder = MP3Decoder(mp3)
24+
audio = AudioOut(board.SPEAKER)
25+
26+
strip_pixels = neopixel.NeoPixel(board.D4, 30, brightness=0.1, auto_write=False)
27+
volume_anim = volume.Volume(strip_pixels, 0.3, (0, 255, 0), decoder, 400)
28+
29+
while True:
30+
audio.play(decoder)
31+
print("playing", mp3file)
32+
33+
while audio.playing:
34+
volume_anim.animate()

0 commit comments

Comments
 (0)