diff --git a/adafruit_led_animation/animation/pulse.py b/adafruit_led_animation/animation/pulse.py index ee10ca7..7038ce2 100644 --- a/adafruit_led_animation/animation/pulse.py +++ b/adafruit_led_animation/animation/pulse.py @@ -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, ) diff --git a/adafruit_led_animation/animation/sparklepulse.py b/adafruit_led_animation/animation/sparklepulse.py index 0976f9e..b85f644 100644 --- a/adafruit_led_animation/animation/sparklepulse.py +++ b/adafruit_led_animation/animation/sparklepulse.py @@ -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): diff --git a/adafruit_led_animation/helper.py b/adafruit_led_animation/helper.py index 6d876e1..b033b58 100644 --- a/adafruit_led_animation/helper.py +++ b/adafruit_led_animation/helper.py @@ -27,9 +27,6 @@ import math -from . import MS_PER_SECOND, monotonic_ms -from .color import calculate_intensity - class PixelMap: """ @@ -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) diff --git a/adafruit_led_animation/pulse_generator.py b/adafruit_led_animation/pulse_generator.py new file mode 100644 index 0000000..e29b04f --- /dev/null +++ b/adafruit_led_animation/pulse_generator.py @@ -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 `_ +* `Adafruit DotStars `_ + +**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)