Skip to content

Commit

Permalink
fix: move pulse generator from helper to own file to reduce memory fo…
Browse files Browse the repository at this point in the history
…otprint when imported
  • Loading branch information
xsorifc28 committed Jul 6, 2024
1 parent 251bcd1 commit ef3ab8a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 50 deletions.
2 changes: 1 addition & 1 deletion adafruit_led_animation/animation/pulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def reset(self):
dotstar = len(self.pixel_object[0]) == 4 and isinstance(
self.pixel_object[0][-1], float
)
from adafruit_led_animation.helper import ( # pylint: disable=import-outside-toplevel
from adafruit_led_animation.pulse_generator import ( # pylint: disable=import-outside-toplevel
pulse_generator,
)

Expand Down
2 changes: 1 addition & 1 deletion adafruit_led_animation/animation/sparklepulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"""

from adafruit_led_animation.animation.sparkle import Sparkle
from adafruit_led_animation.helper import pulse_generator
from adafruit_led_animation.pulse_generator import pulse_generator


class SparklePulse(Sparkle):
Expand Down
48 changes: 0 additions & 48 deletions adafruit_led_animation/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@

import math

from . import MS_PER_SECOND, monotonic_ms
from .color import calculate_intensity


class PixelMap:
"""
Expand Down Expand Up @@ -313,48 +310,3 @@ def __init__(self, pixel_object, start, end):
pixel_ranges=[[n] for n in range(start, end)],
individual_pixels=True,
)


def pulse_generator(period: float, animation_object, dotstar_pwm=False):
"""
Generates a sequence of colors for a pulse, based on the time period specified.
:param period: Pulse duration in seconds.
:param animation_object: An animation object to interact with.
:param dotstar_pwm: Whether to use the dostar per pixel PWM value for brightness control.
"""
period = int((period + (animation_object.breath * 2)) * MS_PER_SECOND)
half_breath = int(animation_object.breath * MS_PER_SECOND // 2)
half_period = period // 2

last_update = monotonic_ms()
cycle_position = 0
last_pos = 0
while True:
now = monotonic_ms()
time_since_last_draw = now - last_update
last_update = now
pos = cycle_position = (cycle_position + time_since_last_draw) % period
if pos < last_pos:
animation_object.cycle_complete = True
last_pos = pos
if pos > half_period:
pos = period - pos
if pos < half_breath:
intensity = animation_object.min_intensity
elif pos > (half_period - half_breath):
intensity = animation_object.max_intensity
else:
intensity = animation_object.min_intensity + (
((pos - half_breath) / (half_period - (half_breath * 2)))
* (animation_object.max_intensity - animation_object.min_intensity)
)
if dotstar_pwm:
fill_color = (
animation_object.color[0],
animation_object.color[1],
animation_object.color[2],
intensity,
)
yield fill_color
continue
yield calculate_intensity(animation_object.color, intensity)
74 changes: 74 additions & 0 deletions adafruit_led_animation/pulse_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# SPDX-FileCopyrightText: 2019 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
`adafruit_led_animation.pulse_generator`
================================================================================
Helper method for pulse generation
* Author(s): Kattni Rembor
Implementation Notes
--------------------
**Hardware:**
* `Adafruit NeoPixels <https://www.adafruit.com/category/168>`_
* `Adafruit DotStars <https://www.adafruit.com/category/885>`_
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://circuitpython.org/downloads
"""

from . import MS_PER_SECOND, monotonic_ms
from .color import calculate_intensity


def pulse_generator(period: float, animation_object, dotstar_pwm=False):
"""
Generates a sequence of colors for a pulse, based on the time period specified.
:param period: Pulse duration in seconds.
:param animation_object: An animation object to interact with.
:param dotstar_pwm: Whether to use the dostar per pixel PWM value for brightness control.
"""
period = int((period + (animation_object.breath * 2)) * MS_PER_SECOND)
half_breath = int(animation_object.breath * MS_PER_SECOND // 2)
half_period = period // 2

last_update = monotonic_ms()
cycle_position = 0
last_pos = 0
while True:
now = monotonic_ms()
time_since_last_draw = now - last_update
last_update = now
pos = cycle_position = (cycle_position + time_since_last_draw) % period
if pos < last_pos:
animation_object.cycle_complete = True
last_pos = pos
if pos > half_period:
pos = period - pos
if pos < half_breath:
intensity = animation_object.min_intensity
elif pos > (half_period - half_breath):
intensity = animation_object.max_intensity
else:
intensity = animation_object.min_intensity + (
((pos - half_breath) / (half_period - (half_breath * 2)))
* (animation_object.max_intensity - animation_object.min_intensity)
)
if dotstar_pwm:
fill_color = (
animation_object.color[0],
animation_object.color[1],
animation_object.color[2],
intensity,
)
yield fill_color
continue
yield calculate_intensity(animation_object.color, intensity)

0 comments on commit ef3ab8a

Please sign in to comment.