Skip to content

Restores min_intensity and max_intensity for Pulse animation and smoother transition option #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion adafruit_led_animation/animation/pulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,28 @@ class Pulse(Animation):
:param float speed: Animation refresh rate in seconds, e.g. ``0.1``.
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
:param period: Period to pulse the LEDs over. Default 5.
:param breath: Duration to hold minimum and maximum intensity levels. Default 0.
:param min_intensity: Lowest brightness level of the pulse. Default 0.
:param max_intensity: Highest brightness elvel of the pulse. Default 1.
"""

# pylint: disable=too-many-arguments
def __init__(self, pixel_object, speed, color, period=5, name=None):
def __init__(
self,
pixel_object,
speed,
color,
period=5,
breath=0,
min_intensity=0,
max_intensity=1,
name=None,
):
super().__init__(pixel_object, speed, color, name=name)
self._period = period
self.breath = breath
self.min_intensity = min_intensity
self.max_intensity = max_intensity
self._generator = None
self.reset()

Expand Down
7 changes: 5 additions & 2 deletions adafruit_led_animation/animation/sparklepulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class SparklePulse(Sparkle):
:param int speed: Animation refresh rate in seconds, e.g. ``0.1``.
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
:param period: Period to pulse the LEDs over. Default 5.
:param breath: Duration to hold minimum and maximum intensity. Default 0.
:param max_intensity: The maximum intensity to pulse, between 0 and 1.0. Default 1.
:param min_intensity: The minimum intensity to pulse, between 0 and 1.0. Default 0.
"""
Expand All @@ -49,13 +50,15 @@ def __init__(
speed,
color,
period=5,
breath=0,
max_intensity=1,
min_intensity=0,
name=None,
):
self._max_intensity = max_intensity
self._min_intensity = min_intensity
self._period = period
self.breath = breath
self.min_intensity = min_intensity
self.max_intensity = max_intensity
dotstar = len(pixel_object) == 4 and isinstance(pixel_object[0][-1], float)
super().__init__(
pixel_object, speed=speed, color=color, num_sparkles=1, name=name
Expand Down
13 changes: 11 additions & 2 deletions adafruit_led_animation/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False):
: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 * MS_PER_SECOND)
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()
Expand All @@ -338,7 +339,15 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False):
last_pos = pos
if pos > half_period:
pos = period - pos
intensity = pos / half_period
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],
Expand Down