From fc1aefafcadd6d0cfd03c217b49734f3b6706b84 Mon Sep 17 00:00:00 2001 From: Tyler Winfield Date: Tue, 19 Dec 2023 22:01:30 -0600 Subject: [PATCH 1/6] 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. --- adafruit_led_animation/animation/pulse.py | 8 +++++++- adafruit_led_animation/animation/sparklepulse.py | 3 +++ adafruit_led_animation/helper.py | 9 +++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/adafruit_led_animation/animation/pulse.py b/adafruit_led_animation/animation/pulse.py index fdd4a7c..849b457 100644 --- a/adafruit_led_animation/animation/pulse.py +++ b/adafruit_led_animation/animation/pulse.py @@ -37,12 +37,18 @@ 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() diff --git a/adafruit_led_animation/animation/sparklepulse.py b/adafruit_led_animation/animation/sparklepulse.py index efb0d1d..2132005 100644 --- a/adafruit_led_animation/animation/sparklepulse.py +++ b/adafruit_led_animation/animation/sparklepulse.py @@ -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. """ @@ -49,6 +50,7 @@ def __init__( speed, color, period=5, + breath=0, max_intensity=1, min_intensity=0, name=None, @@ -56,6 +58,7 @@ def __init__( self._max_intensity = max_intensity self._min_intensity = min_intensity self._period = period + self._breath = breath 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 diff --git a/adafruit_led_animation/helper.py b/adafruit_led_animation/helper.py index 126ea74..822a74c 100644 --- a/adafruit_led_animation/helper.py +++ b/adafruit_led_animation/helper.py @@ -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) + breath = int(animation_object._breath * MS_PER_SECOND) half_period = period // 2 last_update = monotonic_ms() @@ -338,7 +339,11 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False): last_pos = pos if pos > half_period: pos = period - pos - intensity = pos / half_period + intensity = animation_object._min_intensity + ((pos / (half_period - breath)) * (animation_object._max_intensity - animation_object._min_intensity)) + if pos < half_period and pos > (half_period - breath): + intensity = animation_object._max_intensity + if pos > (period - breath): + intensity = animation_object._min_intensity if dotstar_pwm: fill_color = ( animation_object.color[0], From f16e51999154f72075a788a392b91ad4b2ce68be Mon Sep 17 00:00:00 2001 From: Tyler Winfield Date: Tue, 19 Dec 2023 23:04:12 -0600 Subject: [PATCH 2/6] Corrected variable scoping --- adafruit_led_animation/animation/pulse.py | 6 +++--- adafruit_led_animation/animation/sparklepulse.py | 6 +++--- adafruit_led_animation/helper.py | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/adafruit_led_animation/animation/pulse.py b/adafruit_led_animation/animation/pulse.py index 849b457..b5ff3d7 100644 --- a/adafruit_led_animation/animation/pulse.py +++ b/adafruit_led_animation/animation/pulse.py @@ -46,9 +46,9 @@ class Pulse(Animation): 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.breath = breath + self.min_intensity = min_intensity + self.max_intensity = max_intensity self._generator = None self.reset() diff --git a/adafruit_led_animation/animation/sparklepulse.py b/adafruit_led_animation/animation/sparklepulse.py index 2132005..0976f9e 100644 --- a/adafruit_led_animation/animation/sparklepulse.py +++ b/adafruit_led_animation/animation/sparklepulse.py @@ -55,10 +55,10 @@ def __init__( min_intensity=0, name=None, ): - self._max_intensity = max_intensity - self._min_intensity = min_intensity self._period = period - self._breath = breath + 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 diff --git a/adafruit_led_animation/helper.py b/adafruit_led_animation/helper.py index 822a74c..466ef63 100644 --- a/adafruit_led_animation/helper.py +++ b/adafruit_led_animation/helper.py @@ -322,8 +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 + (animation_object._breath * 2)) * MS_PER_SECOND) - breath = int(animation_object._breath * MS_PER_SECOND) + period = int((period + (animation_object.breath * 2)) * MS_PER_SECOND) + breath = int(animation_object.breath * MS_PER_SECOND) half_period = period // 2 last_update = monotonic_ms() @@ -339,11 +339,11 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False): last_pos = pos if pos > half_period: pos = period - pos - intensity = animation_object._min_intensity + ((pos / (half_period - breath)) * (animation_object._max_intensity - animation_object._min_intensity)) + intensity = animation_object.min_intensity + ((pos / (half_period - breath)) * (animation_object.max_intensity - animation_object.min_intensity)) if pos < half_period and pos > (half_period - breath): - intensity = animation_object._max_intensity + intensity = animation_object.max_intensity if pos > (period - breath): - intensity = animation_object._min_intensity + intensity = animation_object.min_intensity if dotstar_pwm: fill_color = ( animation_object.color[0], From a855c3b99eb1f5551259fdfd02805cf7e1ec35f1 Mon Sep 17 00:00:00 2001 From: Tyler Winfield Date: Tue, 19 Dec 2023 23:16:10 -0600 Subject: [PATCH 3/6] Simplified comparison chain --- adafruit_led_animation/helper.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/adafruit_led_animation/helper.py b/adafruit_led_animation/helper.py index 466ef63..496ee18 100644 --- a/adafruit_led_animation/helper.py +++ b/adafruit_led_animation/helper.py @@ -339,11 +339,12 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False): last_pos = pos if pos > half_period: pos = period - pos - intensity = animation_object.min_intensity + ((pos / (half_period - breath)) * (animation_object.max_intensity - animation_object.min_intensity)) - if pos < half_period and pos > (half_period - breath): + if pos < half_period and pox > (half_period - breath): intensity = animation_object.max_intensity - if pos > (period - breath): + elif pos > (period - breath): intensity = animation_object.min_intensity + else: + intensity = animation_object.min_intensity + ((pos / (half_period - breath)) * (animation_object.max_intensity - animation_object.min_intensity)) if dotstar_pwm: fill_color = ( animation_object.color[0], From 72c6c684517288343a836d9da62198d2c0af8b34 Mon Sep 17 00:00:00 2001 From: Tyler Winfield Date: Tue, 19 Dec 2023 23:20:06 -0600 Subject: [PATCH 4/6] Variable type-o --- adafruit_led_animation/helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_led_animation/helper.py b/adafruit_led_animation/helper.py index 496ee18..e1ad24f 100644 --- a/adafruit_led_animation/helper.py +++ b/adafruit_led_animation/helper.py @@ -339,7 +339,7 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False): last_pos = pos if pos > half_period: pos = period - pos - if pos < half_period and pox > (half_period - breath): + if pos < half_period and pos > (half_period - breath): intensity = animation_object.max_intensity elif pos > (period - breath): intensity = animation_object.min_intensity From 1293a471ebf9a0ba970671011dbfa2438c091802 Mon Sep 17 00:00:00 2001 From: Tyler Winfield Date: Wed, 20 Dec 2023 11:47:39 -0600 Subject: [PATCH 5/6] Corrected position calculation after introducing breath pauses --- adafruit_led_animation/helper.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/adafruit_led_animation/helper.py b/adafruit_led_animation/helper.py index e1ad24f..73ec102 100644 --- a/adafruit_led_animation/helper.py +++ b/adafruit_led_animation/helper.py @@ -323,7 +323,7 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False): :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) - breath = int(animation_object.breath * MS_PER_SECOND) + half_breath = int(animation_object.breath * MS_PER_SECOND // 2) half_period = period // 2 last_update = monotonic_ms() @@ -339,12 +339,12 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False): last_pos = pos if pos > half_period: pos = period - pos - if pos < half_period and pos > (half_period - breath): - intensity = animation_object.max_intensity - elif pos > (period - breath): + 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_period - breath)) * (animation_object.max_intensity - animation_object.min_intensity)) + 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], From 786cd806fc8e5da5a8df0a5faa6b423742063d73 Mon Sep 17 00:00:00 2001 From: Tyler Winfield Date: Thu, 28 Dec 2023 09:01:11 -0600 Subject: [PATCH 6/6] Resolving build CI errors --- adafruit_led_animation/animation/pulse.py | 12 +++++++++++- adafruit_led_animation/helper.py | 5 ++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/adafruit_led_animation/animation/pulse.py b/adafruit_led_animation/animation/pulse.py index b5ff3d7..ee10ca7 100644 --- a/adafruit_led_animation/animation/pulse.py +++ b/adafruit_led_animation/animation/pulse.py @@ -43,7 +43,17 @@ class Pulse(Animation): """ # pylint: disable=too-many-arguments - def __init__(self, pixel_object, speed, color, period=5, breath=0, min_intensity=0, max_intensity=1, 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 diff --git a/adafruit_led_animation/helper.py b/adafruit_led_animation/helper.py index 73ec102..6d876e1 100644 --- a/adafruit_led_animation/helper.py +++ b/adafruit_led_animation/helper.py @@ -344,7 +344,10 @@ def pulse_generator(period: float, animation_object, dotstar_pwm=False): 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)) + 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],