From c7dc90179165658f2369b923d0934391f7675879 Mon Sep 17 00:00:00 2001 From: jposada2020 Date: Wed, 5 Feb 2025 17:02:54 -0500 Subject: [PATCH 1/5] Blink animation with a user selected background color --- adafruit_led_animation/animation/blink.py | 13 ++++++-- docs/examples.rst | 8 +++++ .../led_animation_blink_with_background.py | 31 +++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 examples/led_animation_blink_with_background.py diff --git a/adafruit_led_animation/animation/blink.py b/adafruit_led_animation/animation/blink.py index 34de026..3b8d62b 100644 --- a/adafruit_led_animation/animation/blink.py +++ b/adafruit_led_animation/animation/blink.py @@ -37,10 +37,17 @@ class Blink(ColorCycle): :param pixel_object: The initialised LED object. :param float speed: Animation speed in seconds, e.g. ``0.1``. :param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format. + :param background_color: Background color in ``(r, g, b)`` tuple, or ``0x000000`` hex format. Defaults to BLACK. + :param name: A human-readable name for the Animation. Used by the string function. """ - def __init__(self, pixel_object, speed, color, name=None): - super().__init__(pixel_object, speed, [color, BLACK], name=name) + def __init__( + self, pixel_object, speed, color, background_color=BLACK, name=None + ): + self._background_color = background_color + super().__init__( + pixel_object, speed, [color, background_color], name=name + ) def _set_color(self, color): - self.colors = [color, BLACK] + self.colors = [color, self._background_color] diff --git a/docs/examples.rst b/docs/examples.rst index f45d96d..d974958 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -61,6 +61,14 @@ Demonstrates the blink animation. :caption: examples/led_animation_blink.py :linenos: +Blink with a selcted background color +---------------------------------------- +Demonstrates the blink animation with an user defined background color. + +.. literalinclude:: ../examples/led_animation_blink_with_background.py + :caption: examples/led_animation_blink_with_background.py + :linenos: + Comet ----- diff --git a/examples/led_animation_blink_with_background.py b/examples/led_animation_blink_with_background.py new file mode 100644 index 0000000..c9c4910 --- /dev/null +++ b/examples/led_animation_blink_with_background.py @@ -0,0 +1,31 @@ +# SPDX-FileCopyrightText: 2025 Jose D. Montoya +# SPDX-License-Identifier: MIT + +""" +This example blinks the LEDs purple with a yellow background at a 0.5 second interval. + +For QT Py Haxpress and a NeoPixel strip. Update pixel_pin and pixel_num to match your wiring if +using a different board or form of NeoPixels. + +This example will run on SAMD21 (M0) Express boards (such as Circuit Playground Express or QT Py +Haxpress), but not on SAMD21 non-Express boards (such as QT Py or Trinket). +""" +import board +import neopixel + +from adafruit_led_animation.animation.blink import Blink +from adafruit_led_animation.color import PURPLE, YELLOW + +# Update to match the pin connected to your NeoPixels +pixel_pin = board.A3 +# Update to match the number of NeoPixels you have connected +pixel_num = 30 + +pixels = neopixel.NeoPixel( + pixel_pin, pixel_num, brightness=0.5, auto_write=False +) + +blink = Blink(pixels, speed=0.5, color=PURPLE, background_color=YELLOW) + +while True: + blink.animate() From 34bcf3c5355f579364aab52febb2f7ea95ffc869 Mon Sep 17 00:00:00 2001 From: jposada2020 Date: Wed, 5 Feb 2025 17:10:48 -0500 Subject: [PATCH 2/5] formating --- adafruit_led_animation/animation/blink.py | 12 +++++------- adafruit_led_animation/animation/volume.py | 17 +++++++++++++++-- adafruit_led_animation/sequence.py | 2 +- examples/led_animation_blink_with_background.py | 4 +--- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/adafruit_led_animation/animation/blink.py b/adafruit_led_animation/animation/blink.py index 3b8d62b..0582332 100644 --- a/adafruit_led_animation/animation/blink.py +++ b/adafruit_led_animation/animation/blink.py @@ -37,17 +37,15 @@ class Blink(ColorCycle): :param pixel_object: The initialised LED object. :param float speed: Animation speed in seconds, e.g. ``0.1``. :param color: Animation color in ``(r, g, b)`` tuple, or ``0x000000`` hex format. - :param background_color: Background color in ``(r, g, b)`` tuple, or ``0x000000`` hex format. Defaults to BLACK. + :param background_color: Background color in ``(r, g, b)`` tuple, or ``0x000000`` + hex format. Defaults to BLACK. :param name: A human-readable name for the Animation. Used by the string function. """ - def __init__( - self, pixel_object, speed, color, background_color=BLACK, name=None - ): + # pylint: disable=too-many-arguments + def __init__(self, pixel_object, speed, color, background_color=BLACK, name=None): self._background_color = background_color - super().__init__( - pixel_object, speed, [color, background_color], name=name - ) + super().__init__(pixel_object, speed, [color, background_color], name=name) def _set_color(self, color): self.colors = [color, self._background_color] diff --git a/adafruit_led_animation/animation/volume.py b/adafruit_led_animation/animation/volume.py index cbfb8d1..d3ffc6e 100644 --- a/adafruit_led_animation/animation/volume.py +++ b/adafruit_led_animation/animation/volume.py @@ -44,7 +44,13 @@ class Volume(Animation): # pylint: disable=too-many-arguments def __init__( - self, pixel_object, speed, brightest_color, decoder, max_volume=500, name=None + self, + pixel_object, + speed, + brightest_color, + decoder, + max_volume=500, + name=None, ): self._decoder = decoder self._num_pixels = len(pixel_object) @@ -89,8 +95,15 @@ def draw(self): ) lit_pixels = int( - map_range(self._decoder.rms_level, 0, self._max_volume, 0, self._num_pixels) + map_range( + self._decoder.rms_level, + 0, + self._max_volume, + 0, + self._num_pixels, + ) ) + # pylint: disable=consider-using-min-builtin if lit_pixels > self._num_pixels: lit_pixels = self._num_pixels diff --git a/adafruit_led_animation/sequence.py b/adafruit_led_animation/sequence.py index fe522e9..bf5728f 100644 --- a/adafruit_led_animation/sequence.py +++ b/adafruit_led_animation/sequence.py @@ -73,7 +73,7 @@ class AnimationSequence: animations.animate() """ - # pylint: disable=too-many-instance-attributes + # pylint: disable=too-many-instance-attributes, too-many-arguments def __init__( self, *members, diff --git a/examples/led_animation_blink_with_background.py b/examples/led_animation_blink_with_background.py index c9c4910..4a94e10 100644 --- a/examples/led_animation_blink_with_background.py +++ b/examples/led_animation_blink_with_background.py @@ -21,9 +21,7 @@ # Update to match the number of NeoPixels you have connected pixel_num = 30 -pixels = neopixel.NeoPixel( - pixel_pin, pixel_num, brightness=0.5, auto_write=False -) +pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=0.5, auto_write=False) blink = Blink(pixels, speed=0.5, color=PURPLE, background_color=YELLOW) From bfdbe0078ce43882324fed0e5fe19eb6e8333b93 Mon Sep 17 00:00:00 2001 From: jposada2020 Date: Sun, 9 Feb 2025 22:47:22 -0500 Subject: [PATCH 3/5] adding Pacman Animation --- adafruit_led_animation/animation/pacman.py | 129 +++++++++++++++++++++ docs/examples.rst | 9 ++ examples/led_animation_pacman.py | 32 +++++ 3 files changed, 170 insertions(+) create mode 100644 adafruit_led_animation/animation/pacman.py create mode 100644 examples/led_animation_pacman.py diff --git a/adafruit_led_animation/animation/pacman.py b/adafruit_led_animation/animation/pacman.py new file mode 100644 index 0000000..045f2ea --- /dev/null +++ b/adafruit_led_animation/animation/pacman.py @@ -0,0 +1,129 @@ +# SPDX-FileCopyrightText: 2025 Bob Loeffler +# SPDX-FileCopyrightText: 2025 Jose D. Montoya +# +# SPDX-License-Identifier: MIT + +""" +`adafruit_led_animation.animation.pacman` +================================================================================ + +PacMan Animation for CircuitPython helper library for LED animations. +PACMAN ANIMATION Adapted from https://github.com/wled-dev/WLED/pull/4536 # by BobLoeffler68 + +* Author(s): Bob Loeffler, Jose D. Montoya + +Implementation Notes +-------------------- + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware for the supported boards: + https://circuitpython.org/downloads + + +""" + +import time +from adafruit_led_animation.animation import Animation +from adafruit_led_animation.color import ( + BLACK, + YELLOW, + RED, + PURPLE, + CYAN, + ORANGE, + BLUE, + WHITE, +) + +ORANGEYELLOW = (255, 136, 0) + + +class Pacman(Animation): + """ + Simulate the Pacman game in a single led strip. + + :param pixel_object: The initialised LED object. + :param float speed: Animation speed rate in seconds, e.g. ``0.1``. + """ + + # pylint: disable=too-many-arguments, too-many-branches + def __init__( + self, + pixel_object, + speed, + color=WHITE, + name=None, + ): + self.num_leds = len(pixel_object) + self.pacman = [YELLOW, 10] + self.ghosts_original = [[RED, 6], [PURPLE, 4], [CYAN, 2], [ORANGE, 0]] + self.ghosts = [[RED, 6], [PURPLE, 4], [CYAN, 2], [ORANGE, 0]] + self.direction = 1 + self.black_dir = -1 + self.flag = "beep" + self.power_pellet = [ORANGEYELLOW, self.num_leds] + self.ghost_timer = time.monotonic() + if self.num_leds > 150: + self.start_blinking_ghosts = self.num_leds // 4 + else: + self.start_blinking_ghosts = self.num_leds // 3 + + super().__init__(pixel_object, speed, color, name=name) + + on_cycle_complete_supported = True + + def draw(self): + """ + Draw the Pacman animation. + :param led_object: led object + :param neopixel_list: list of neopixel colors + :param int num_leds: number of leds. + :param int duration: duration in seconds. Default is 15 seconds + """ + pixel_list = self.pixel_object + pixel_list[-1] = self.power_pellet[0] + + delta = time.monotonic() - self.ghost_timer + if delta > 1: + if self.power_pellet[0] == ORANGEYELLOW: + self.power_pellet[0] = BLACK + else: + self.power_pellet[0] = ORANGEYELLOW + pixel_list[self.power_pellet[1] - 1] = self.power_pellet[0] + + self.ghost_timer = time.monotonic() + + if self.pacman[1] >= self.num_leds - 2: + self.direction = self.direction * -1 + self.black_dir = self.black_dir * -1 + for ghost in self.ghosts: + ghost[0] = BLUE + + pixel_list[self.pacman[1]] = self.pacman[0] + pixel_list[self.pacman[1] + self.black_dir] = BLACK + self.pacman[1] += self.direction + + if ( + self.ghosts[3][1] <= self.start_blinking_ghosts + and self.direction == -1 + ): + if self.flag == "beep": + for i, ghost in enumerate(self.ghosts): + ghost[0] = BLACK + self.flag = "bop" + else: + for i, ghost in enumerate(self.ghosts): + ghost[0] = self.ghosts_original[i][0] + self.flag = "beep" + + for i, ghost in enumerate(self.ghosts): + pixel_list[ghost[1]] = ghost[0] + pixel_list[ghost[1] + self.black_dir] = BLACK + ghost[1] += self.direction + + if self.ghosts[3][1] <= 0: + self.direction = self.direction * -1 + self.black_dir = self.black_dir * -1 + for i, ghost in enumerate(self.ghosts): + ghost[0] = self.ghosts_original[i][0] diff --git a/docs/examples.rst b/docs/examples.rst index d974958..7bb45dd 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -104,3 +104,12 @@ Demonstrates the sparkle animations. .. literalinclude:: ../examples/led_animation_sparkle_animations.py :caption: examples/led_animation_sparkle_animations.py :linenos: + +Pacman +------ + +Demonstrates the pacman animation. + +.. literalinclude:: ../examples/led_animation_pacman.py + :caption: examples/led_animation_pacman.py + :linenos: diff --git a/examples/led_animation_pacman.py b/examples/led_animation_pacman.py new file mode 100644 index 0000000..ff2b4ee --- /dev/null +++ b/examples/led_animation_pacman.py @@ -0,0 +1,32 @@ +# SPDX-FileCopyrightText: 2025 Jose D. Montoya +# SPDX-License-Identifier: MIT + +""" +This example animates a Pacman on a NeoPixel strip. +""" +import board +import neopixel +from adafruit_led_animation.animation.pacman import Pacman +from adafruit_led_animation.color import WHITE + +# Update to match the pin connected to your NeoPixels +pixel_pin = board.D6 +# Update to match the number of NeoPixels you have connected +num_pixels = 50 + +# Create the NeoPixel object +ORDER = neopixel.GRB +pixels = neopixel.NeoPixel( + pixel_pin, + num_pixels, + brightness=1.0, + auto_write=False, + pixel_order=ORDER, +) + +# Create the Pacman animation object +pacman = Pacman(pixels, speed=0.1, color=WHITE) + +# Main loop +while True: + pacman.animate() From a8e057cf93e9239a8c7e97a73972bc92ccff7215 Mon Sep 17 00:00:00 2001 From: jposada2020 Date: Sun, 9 Feb 2025 22:48:07 -0500 Subject: [PATCH 4/5] adding pacman animation --- adafruit_led_animation/animation/pacman.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/adafruit_led_animation/animation/pacman.py b/adafruit_led_animation/animation/pacman.py index 045f2ea..c41eea5 100644 --- a/adafruit_led_animation/animation/pacman.py +++ b/adafruit_led_animation/animation/pacman.py @@ -104,10 +104,7 @@ def draw(self): pixel_list[self.pacman[1] + self.black_dir] = BLACK self.pacman[1] += self.direction - if ( - self.ghosts[3][1] <= self.start_blinking_ghosts - and self.direction == -1 - ): + if self.ghosts[3][1] <= self.start_blinking_ghosts and self.direction == -1: if self.flag == "beep": for i, ghost in enumerate(self.ghosts): ghost[0] = BLACK From 78a74180d05be02fada08aa1a3be0661683216e8 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Thu, 24 Apr 2025 15:50:08 -0500 Subject: [PATCH 5/5] lower brightness in pacman example --- examples/led_animation_pacman.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/led_animation_pacman.py b/examples/led_animation_pacman.py index ff2b4ee..c325fd5 100644 --- a/examples/led_animation_pacman.py +++ b/examples/led_animation_pacman.py @@ -19,7 +19,7 @@ pixels = neopixel.NeoPixel( pixel_pin, num_pixels, - brightness=1.0, + brightness=0.5, auto_write=False, pixel_order=ORDER, )