Skip to content

Commit 251bcd1

Browse files
authored
Merge pull request #116 from tylerwinfield/main
Restores min_intensity and max_intensity for Pulse animation and smoother transition option
2 parents 35d8c01 + 786cd80 commit 251bcd1

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

adafruit_led_animation/animation/pulse.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,28 @@ 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__(
47+
self,
48+
pixel_object,
49+
speed,
50+
color,
51+
period=5,
52+
breath=0,
53+
min_intensity=0,
54+
max_intensity=1,
55+
name=None,
56+
):
4457
super().__init__(pixel_object, speed, color, name=name)
4558
self._period = period
59+
self.breath = breath
60+
self.min_intensity = min_intensity
61+
self.max_intensity = max_intensity
4662
self._generator = None
4763
self.reset()
4864

adafruit_led_animation/animation/sparklepulse.py

+5-2
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
):
56-
self._max_intensity = max_intensity
57-
self._min_intensity = min_intensity
5858
self._period = period
59+
self.breath = breath
60+
self.min_intensity = min_intensity
61+
self.max_intensity = max_intensity
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

+11-2
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+
half_breath = int(animation_object.breath * MS_PER_SECOND // 2)
326327
half_period = period // 2
327328

328329
last_update = monotonic_ms()
@@ -338,7 +339,15 @@ 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+
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+
)
342351
if dotstar_pwm:
343352
fill_color = (
344353
animation_object.color[0],

0 commit comments

Comments
 (0)