Skip to content

Commit 4b3475c

Browse files
authored
Merge pull request #20 from kattni/refactor
Refactor for memory consideration and add RainbowSparkle.
2 parents 357d87d + 7268507 commit 4b3475c

20 files changed

+1333
-692
lines changed

adafruit_led_animation/animation.py

Lines changed: 0 additions & 504 deletions
This file was deleted.
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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`
25+
================================================================================
26+
27+
Animation base class 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+
__version__ = "0.0.0-auto.0"
47+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_LED_Animation.git"
48+
49+
from adafruit_led_animation import NANOS_PER_SECOND, monotonic_ns
50+
51+
52+
class Animation:
53+
# pylint: disable=too-many-instance-attributes
54+
"""
55+
Base class for animations.
56+
"""
57+
cycle_complete_supported = False
58+
59+
# pylint: disable=too-many-arguments
60+
def __init__(self, pixel_object, speed, color, peers=None, paused=False, name=None):
61+
self.pixel_object = pixel_object
62+
self.pixel_object.auto_write = False
63+
self.peers = peers if peers else []
64+
"""A sequence of animations to trigger .draw() on when this animation draws."""
65+
self._speed_ns = 0
66+
self._color = None
67+
self._paused = paused
68+
self._next_update = monotonic_ns()
69+
self._time_left_at_pause = 0
70+
self._also_notify = []
71+
self.speed = speed # sets _speed_ns
72+
self.color = color # Triggers _recompute_color
73+
self.name = name
74+
self.notify_cycles = 1
75+
"""Number of cycles to trigger additional cycle_done notifications after"""
76+
self.draw_count = 0
77+
"""Number of animation frames drawn."""
78+
self.cycle_count = 0
79+
"""Number of animation cycles completed."""
80+
81+
def __str__(self):
82+
return "<%s: %s>" % (self.__class__.__name__, self.name)
83+
84+
def animate(self):
85+
"""
86+
Call animate() from your code's main loop. It will draw the animation draw() at intervals
87+
configured by the speed property (set from init).
88+
89+
:return: True if the animation draw cycle was triggered, otherwise False.
90+
"""
91+
if self._paused:
92+
return False
93+
94+
now = monotonic_ns()
95+
if now < self._next_update:
96+
return False
97+
98+
self.draw()
99+
self.draw_count += 1
100+
101+
# Draw related animations together
102+
if self.peers:
103+
for peer in self.peers:
104+
peer.draw()
105+
106+
self._next_update = now + self._speed_ns
107+
return True
108+
109+
def draw(self):
110+
"""
111+
Animation subclasses must implement draw() to render the animation sequence.
112+
Draw must call show().
113+
"""
114+
raise NotImplementedError()
115+
116+
def show(self):
117+
"""
118+
Displays the updated pixels. Called during animates with changes.
119+
"""
120+
self.pixel_object.show()
121+
122+
def freeze(self):
123+
"""
124+
Stops the animation until resumed.
125+
"""
126+
self._paused = True
127+
self._time_left_at_pause = max(0, monotonic_ns() - self._next_update)
128+
129+
def resume(self):
130+
"""
131+
Resumes the animation.
132+
"""
133+
self._next_update = monotonic_ns() + self._time_left_at_pause
134+
self._time_left_at_pause = 0
135+
self._paused = False
136+
137+
def fill(self, color):
138+
"""
139+
Fills the pixel object with a color.
140+
"""
141+
self.pixel_object.fill(color)
142+
143+
@property
144+
def color(self):
145+
"""
146+
The current color.
147+
"""
148+
return self._color
149+
150+
@color.setter
151+
def color(self, color):
152+
if self._color == color:
153+
return
154+
if isinstance(color, int):
155+
color = (color >> 16 & 0xFF, color >> 8 & 0xFF, color & 0xFF)
156+
self._color = color
157+
self._recompute_color(color)
158+
159+
@property
160+
def speed(self):
161+
"""
162+
The animation speed in fractional seconds.
163+
"""
164+
return self._speed_ns / NANOS_PER_SECOND
165+
166+
@speed.setter
167+
def speed(self, seconds):
168+
self._speed_ns = int(seconds * NANOS_PER_SECOND)
169+
170+
def _recompute_color(self, color):
171+
"""
172+
Called if the color is changed, which includes at initialization.
173+
Override as needed.
174+
"""
175+
176+
def cycle_complete(self):
177+
"""
178+
Called by some animations when they complete an animation cycle.
179+
Animations that support cycle complete notifications will have X property set to False.
180+
Override as needed.
181+
"""
182+
self.cycle_count += 1
183+
if self.cycle_count % self.notify_cycles == 0:
184+
for callback in self._also_notify:
185+
callback(self)
186+
187+
def add_cycle_complete_receiver(self, callback):
188+
"""
189+
Adds an additional callback when the cycle completes.
190+
191+
:param callback: Additional callback to trigger when a cycle completes. The callback
192+
is passed the animation object instance.
193+
"""
194+
self._also_notify.append(callback)
195+
196+
def reset(self):
197+
"""
198+
Resets the animation sequence.
199+
"""
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.blink`
25+
================================================================================
26+
27+
Blink 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+
47+
from adafruit_led_animation.animation.colorcycle import ColorCycle
48+
from adafruit_led_animation.color import BLACK
49+
50+
51+
class Blink(ColorCycle):
52+
"""
53+
Blink a color on and off.
54+
55+
:param pixel_object: The initialised LED object.
56+
:param float speed: Animation speed in seconds, e.g. ``0.1``.
57+
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
58+
"""
59+
60+
def __init__(self, pixel_object, speed, color, name=None):
61+
super().__init__(pixel_object, speed, [color, BLACK], name=name)
62+
63+
def _recompute_color(self, color):
64+
self.colors = [color, BLACK]

0 commit comments

Comments
 (0)