Skip to content

Commit 52cf90e

Browse files
committed
fix: move pulse generator from helper to own file to reduce memory footprint when imported
1 parent 251bcd1 commit 52cf90e

File tree

4 files changed

+78
-46
lines changed

4 files changed

+78
-46
lines changed

adafruit_led_animation/animation/pulse.py

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -314,47 +314,3 @@ def __init__(self, pixel_object, start, end):
314314
individual_pixels=True,
315315
)
316316

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)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
import math
29+
30+
from . import MS_PER_SECOND, monotonic_ms
31+
from .color import calculate_intensity
32+
33+
34+
def pulse_generator(period: float, animation_object, dotstar_pwm=False):
35+
"""
36+
Generates a sequence of colors for a pulse, based on the time period specified.
37+
:param period: Pulse duration in seconds.
38+
:param animation_object: An animation object to interact with.
39+
:param dotstar_pwm: Whether to use the dostar per pixel PWM value for brightness control.
40+
"""
41+
period = int((period + (animation_object.breath * 2)) * MS_PER_SECOND)
42+
half_breath = int(animation_object.breath * MS_PER_SECOND // 2)
43+
half_period = period // 2
44+
45+
last_update = monotonic_ms()
46+
cycle_position = 0
47+
last_pos = 0
48+
while True:
49+
now = monotonic_ms()
50+
time_since_last_draw = now - last_update
51+
last_update = now
52+
pos = cycle_position = (cycle_position + time_since_last_draw) % period
53+
if pos < last_pos:
54+
animation_object.cycle_complete = True
55+
last_pos = pos
56+
if pos > half_period:
57+
pos = period - pos
58+
if pos < half_breath:
59+
intensity = animation_object.min_intensity
60+
elif pos > (half_period - half_breath):
61+
intensity = animation_object.max_intensity
62+
else:
63+
intensity = animation_object.min_intensity + (
64+
((pos - half_breath) / (half_period - (half_breath * 2)))
65+
* (animation_object.max_intensity - animation_object.min_intensity)
66+
)
67+
if dotstar_pwm:
68+
fill_color = (
69+
animation_object.color[0],
70+
animation_object.color[1],
71+
animation_object.color[2],
72+
intensity,
73+
)
74+
yield fill_color
75+
continue
76+
yield calculate_intensity(animation_object.color, intensity)

0 commit comments

Comments
 (0)