Skip to content

Commit 576a094

Browse files
authored
Merge pull request #119 from xsorifc28/main
fix(ISSUE-118): move pulse generator from helper to own file to reduce memory footprint when imported
2 parents 251bcd1 + ef3ab8a commit 576a094

File tree

4 files changed

+76
-50
lines changed

4 files changed

+76
-50
lines changed

adafruit_led_animation/animation/pulse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def reset(self):
7575
dotstar = len(self.pixel_object[0]) == 4 and isinstance(
7676
self.pixel_object[0][-1], float
7777
)
78-
from adafruit_led_animation.helper import ( # pylint: disable=import-outside-toplevel
78+
from adafruit_led_animation.pulse_generator import ( # pylint: disable=import-outside-toplevel
7979
pulse_generator,
8080
)
8181

adafruit_led_animation/animation/sparklepulse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"""
2828

2929
from adafruit_led_animation.animation.sparkle import Sparkle
30-
from adafruit_led_animation.helper import pulse_generator
30+
from adafruit_led_animation.pulse_generator import pulse_generator
3131

3232

3333
class SparklePulse(Sparkle):

adafruit_led_animation/helper.py

-48
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727

2828
import math
2929

30-
from . import MS_PER_SECOND, monotonic_ms
31-
from .color import calculate_intensity
32-
3330

3431
class PixelMap:
3532
"""
@@ -313,48 +310,3 @@ def __init__(self, pixel_object, start, end):
313310
pixel_ranges=[[n] for n in range(start, end)],
314311
individual_pixels=True,
315312
)
316-
317-
318-
def pulse_generator(period: float, animation_object, dotstar_pwm=False):
319-
"""
320-
Generates a sequence of colors for a pulse, based on the time period specified.
321-
:param period: Pulse duration in seconds.
322-
:param animation_object: An animation object to interact with.
323-
:param dotstar_pwm: Whether to use the dostar per pixel PWM value for brightness control.
324-
"""
325-
period = int((period + (animation_object.breath * 2)) * MS_PER_SECOND)
326-
half_breath = int(animation_object.breath * MS_PER_SECOND // 2)
327-
half_period = period // 2
328-
329-
last_update = monotonic_ms()
330-
cycle_position = 0
331-
last_pos = 0
332-
while True:
333-
now = monotonic_ms()
334-
time_since_last_draw = now - last_update
335-
last_update = now
336-
pos = cycle_position = (cycle_position + time_since_last_draw) % period
337-
if pos < last_pos:
338-
animation_object.cycle_complete = True
339-
last_pos = pos
340-
if pos > half_period:
341-
pos = period - pos
342-
if pos < half_breath:
343-
intensity = animation_object.min_intensity
344-
elif pos > (half_period - half_breath):
345-
intensity = animation_object.max_intensity
346-
else:
347-
intensity = animation_object.min_intensity + (
348-
((pos - half_breath) / (half_period - (half_breath * 2)))
349-
* (animation_object.max_intensity - animation_object.min_intensity)
350-
)
351-
if dotstar_pwm:
352-
fill_color = (
353-
animation_object.color[0],
354-
animation_object.color[1],
355-
animation_object.color[2],
356-
intensity,
357-
)
358-
yield fill_color
359-
continue
360-
yield calculate_intensity(animation_object.color, intensity)
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# SPDX-FileCopyrightText: 2019 Kattni Rembor for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
`adafruit_led_animation.pulse_generator`
7+
================================================================================
8+
9+
Helper method for pulse generation
10+
11+
* Author(s): Kattni Rembor
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 . import MS_PER_SECOND, monotonic_ms
29+
from .color import calculate_intensity
30+
31+
32+
def pulse_generator(period: float, animation_object, dotstar_pwm=False):
33+
"""
34+
Generates a sequence of colors for a pulse, based on the time period specified.
35+
:param period: Pulse duration in seconds.
36+
:param animation_object: An animation object to interact with.
37+
:param dotstar_pwm: Whether to use the dostar per pixel PWM value for brightness control.
38+
"""
39+
period = int((period + (animation_object.breath * 2)) * MS_PER_SECOND)
40+
half_breath = int(animation_object.breath * MS_PER_SECOND // 2)
41+
half_period = period // 2
42+
43+
last_update = monotonic_ms()
44+
cycle_position = 0
45+
last_pos = 0
46+
while True:
47+
now = monotonic_ms()
48+
time_since_last_draw = now - last_update
49+
last_update = now
50+
pos = cycle_position = (cycle_position + time_since_last_draw) % period
51+
if pos < last_pos:
52+
animation_object.cycle_complete = True
53+
last_pos = pos
54+
if pos > half_period:
55+
pos = period - pos
56+
if pos < half_breath:
57+
intensity = animation_object.min_intensity
58+
elif pos > (half_period - half_breath):
59+
intensity = animation_object.max_intensity
60+
else:
61+
intensity = animation_object.min_intensity + (
62+
((pos - half_breath) / (half_period - (half_breath * 2)))
63+
* (animation_object.max_intensity - animation_object.min_intensity)
64+
)
65+
if dotstar_pwm:
66+
fill_color = (
67+
animation_object.color[0],
68+
animation_object.color[1],
69+
animation_object.color[2],
70+
intensity,
71+
)
72+
yield fill_color
73+
continue
74+
yield calculate_intensity(animation_object.color, intensity)

0 commit comments

Comments
 (0)