|
| 1 | +# SPDX-FileCopyrightText: 2019 Kattni Rembor for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +`adafruit_led_animation.pulse_generator` |
| 7 | +================================================================================ |
| 8 | +
|
| 9 | +Helper method for pulse generation |
| 10 | +
|
| 11 | +* Author(s): Kattni Rembor |
| 12 | +
|
| 13 | +Implementation Notes |
| 14 | +-------------------- |
| 15 | +
|
| 16 | +**Hardware:** |
| 17 | +
|
| 18 | +* `Adafruit NeoPixels <https://www.adafruit.com/category/168>`_ |
| 19 | +* `Adafruit DotStars <https://www.adafruit.com/category/885>`_ |
| 20 | +
|
| 21 | +**Software and Dependencies:** |
| 22 | +
|
| 23 | +* Adafruit CircuitPython firmware for the supported boards: |
| 24 | + https://circuitpython.org/downloads |
| 25 | +
|
| 26 | +""" |
| 27 | + |
| 28 | +from . import MS_PER_SECOND, monotonic_ms |
| 29 | +from .color import calculate_intensity |
| 30 | + |
| 31 | + |
| 32 | +def pulse_generator(period: float, animation_object, dotstar_pwm=False): |
| 33 | + """ |
| 34 | + Generates a sequence of colors for a pulse, based on the time period specified. |
| 35 | + :param period: Pulse duration in seconds. |
| 36 | + :param animation_object: An animation object to interact with. |
| 37 | + :param dotstar_pwm: Whether to use the dostar per pixel PWM value for brightness control. |
| 38 | + """ |
| 39 | + period = int((period + (animation_object.breath * 2)) * MS_PER_SECOND) |
| 40 | + half_breath = int(animation_object.breath * MS_PER_SECOND // 2) |
| 41 | + half_period = period // 2 |
| 42 | + |
| 43 | + last_update = monotonic_ms() |
| 44 | + cycle_position = 0 |
| 45 | + last_pos = 0 |
| 46 | + while True: |
| 47 | + now = monotonic_ms() |
| 48 | + time_since_last_draw = now - last_update |
| 49 | + last_update = now |
| 50 | + pos = cycle_position = (cycle_position + time_since_last_draw) % period |
| 51 | + if pos < last_pos: |
| 52 | + animation_object.cycle_complete = True |
| 53 | + last_pos = pos |
| 54 | + if pos > half_period: |
| 55 | + pos = period - pos |
| 56 | + if pos < half_breath: |
| 57 | + intensity = animation_object.min_intensity |
| 58 | + elif pos > (half_period - half_breath): |
| 59 | + intensity = animation_object.max_intensity |
| 60 | + else: |
| 61 | + intensity = animation_object.min_intensity + ( |
| 62 | + ((pos - half_breath) / (half_period - (half_breath * 2))) |
| 63 | + * (animation_object.max_intensity - animation_object.min_intensity) |
| 64 | + ) |
| 65 | + if dotstar_pwm: |
| 66 | + fill_color = ( |
| 67 | + animation_object.color[0], |
| 68 | + animation_object.color[1], |
| 69 | + animation_object.color[2], |
| 70 | + intensity, |
| 71 | + ) |
| 72 | + yield fill_color |
| 73 | + continue |
| 74 | + yield calculate_intensity(animation_object.color, intensity) |
0 commit comments