Skip to content

Commit fc1aefa

Browse files
committed
Adds min_intensity and max_intensity support back to Pulse animation. Introduces a 'breath' value (default 0) to give a duration to hold the minimum and maximum intensity during the animation for smoother changes in direction.
1 parent 0f24be5 commit fc1aefa

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

adafruit_led_animation/animation/pulse.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@ class Pulse(Animation):
3737
:param float speed: Animation refresh rate in seconds, e.g. ``0.1``.
3838
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
3939
:param period: Period to pulse the LEDs over. Default 5.
40+
:param breath: Duration to hold minimum and maximum intensity levels. Default 0.
41+
:param min_intensity: Lowest brightness level of the pulse. Default 0.
42+
:param max_intensity: Highest brightness elvel of the pulse. Default 1.
4043
"""
4144

4245
# pylint: disable=too-many-arguments
43-
def __init__(self, pixel_object, speed, color, period=5, name=None):
46+
def __init__(self, pixel_object, speed, color, period=5, breath=0, min_intensity=0, max_intensity=1, name=None):
4447
super().__init__(pixel_object, speed, color, name=name)
4548
self._period = period
49+
self._breath = breath
50+
self._min_intensity = min_intensity
51+
self._max_intensity = max_intensity
4652
self._generator = None
4753
self.reset()
4854

adafruit_led_animation/animation/sparklepulse.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class SparklePulse(Sparkle):
3838
:param int speed: Animation refresh rate in seconds, e.g. ``0.1``.
3939
:param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format.
4040
:param period: Period to pulse the LEDs over. Default 5.
41+
:param breath: Duration to hold minimum and maximum intensity. Default 0.
4142
:param max_intensity: The maximum intensity to pulse, between 0 and 1.0. Default 1.
4243
:param min_intensity: The minimum intensity to pulse, between 0 and 1.0. Default 0.
4344
"""
@@ -49,13 +50,15 @@ def __init__(
4950
speed,
5051
color,
5152
period=5,
53+
breath=0,
5254
max_intensity=1,
5355
min_intensity=0,
5456
name=None,
5557
):
5658
self._max_intensity = max_intensity
5759
self._min_intensity = min_intensity
5860
self._period = period
61+
self._breath = breath
5962
dotstar = len(pixel_object) == 4 and isinstance(pixel_object[0][-1], float)
6063
super().__init__(
6164
pixel_object, speed=speed, color=color, num_sparkles=1, name=name

adafruit_led_animation/helper.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False):
322322
:param animation_object: An animation object to interact with.
323323
:param dotstar_pwm: Whether to use the dostar per pixel PWM value for brightness control.
324324
"""
325-
period = int(period * MS_PER_SECOND)
325+
period = int((period + (animation_object._breath * 2)) * MS_PER_SECOND)
326+
breath = int(animation_object._breath * MS_PER_SECOND)
326327
half_period = period // 2
327328

328329
last_update = monotonic_ms()
@@ -338,7 +339,11 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False):
338339
last_pos = pos
339340
if pos > half_period:
340341
pos = period - pos
341-
intensity = pos / half_period
342+
intensity = animation_object._min_intensity + ((pos / (half_period - breath)) * (animation_object._max_intensity - animation_object._min_intensity))
343+
if pos < half_period and pos > (half_period - breath):
344+
intensity = animation_object._max_intensity
345+
if pos > (period - breath):
346+
intensity = animation_object._min_intensity
342347
if dotstar_pwm:
343348
fill_color = (
344349
animation_object.color[0],

0 commit comments

Comments
 (0)