|
| 1 | +""" |
| 2 | +NeoPixel Animator code for ItsyBitsy nRF52840 NeoPixel Animation and Color Remote Control. |
| 3 | +""" |
| 4 | + |
| 5 | +import board |
| 6 | +import neopixel |
| 7 | +from adafruit_led_animation.animation import Comet, Sparkle, AnimationGroup,\ |
| 8 | + AnimationSequence |
| 9 | +import adafruit_led_animation.color as color |
| 10 | + |
| 11 | +from adafruit_ble import BLERadio |
| 12 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 13 | +from adafruit_ble.services.nordic import UARTService |
| 14 | + |
| 15 | +from adafruit_bluefruit_connect.packet import Packet |
| 16 | +from adafruit_bluefruit_connect.color_packet import ColorPacket |
| 17 | +from adafruit_bluefruit_connect.button_packet import ButtonPacket |
| 18 | + |
| 19 | +# The number of NeoPixels in the externally attached strip |
| 20 | +# If using two strips connected to the same pin, count only one strip for this number! |
| 21 | +STRIP_PIXEL_NUMBER = 43 |
| 22 | + |
| 23 | +# Setup for comet animation |
| 24 | +COMET_SPEED = 0.05 # Lower numbers increase the animation speed |
| 25 | +STRIP_COMET_TAIL_LENGTH = 10 # The length of the comet on the NeoPixel strip |
| 26 | +STRIP_COMET_BOUNCE = False # Set to False to stop comet from "bouncing" on NeoPixel strip |
| 27 | + |
| 28 | +# Setup for sparkle animation |
| 29 | +SPARKLE_SPEED = 0.1 # Lower numbers increase the animation speed |
| 30 | + |
| 31 | +# Create the NeoPixel strip |
| 32 | +strip_pixels = neopixel.NeoPixel(board.D5, STRIP_PIXEL_NUMBER, auto_write=False) |
| 33 | + |
| 34 | +# Setup BLE connection |
| 35 | +ble = BLERadio() |
| 36 | +uart = UARTService() |
| 37 | +advertisement = ProvideServicesAdvertisement(uart) |
| 38 | + |
| 39 | +# Setup animations |
| 40 | +animations = AnimationSequence( |
| 41 | + AnimationGroup( |
| 42 | + Comet(strip_pixels, COMET_SPEED, color.TEAL, tail_length=STRIP_COMET_TAIL_LENGTH, |
| 43 | + bounce=STRIP_COMET_BOUNCE) |
| 44 | + ), |
| 45 | + AnimationGroup( |
| 46 | + Sparkle(strip_pixels, SPARKLE_SPEED, color.TEAL) |
| 47 | + ), |
| 48 | +) |
| 49 | + |
| 50 | +animation_color = None |
| 51 | +mode = 0 |
| 52 | +blanked = False |
| 53 | + |
| 54 | +while True: |
| 55 | + ble.start_advertising(advertisement) # Start advertising. |
| 56 | + was_connected = False |
| 57 | + while not was_connected or ble.connected: |
| 58 | + if not blanked: # If LED-off signal is not being sent... |
| 59 | + animations.animate() # Run the animations. |
| 60 | + if ble.connected: # If BLE is connected... |
| 61 | + was_connected = True |
| 62 | + if uart.in_waiting: # Check to see if any data is available from the Remote Control. |
| 63 | + try: |
| 64 | + packet = Packet.from_stream(uart) # Create the packet object. |
| 65 | + except ValueError: |
| 66 | + continue |
| 67 | + if isinstance(packet, ColorPacket): # If the packet is color packet... |
| 68 | + if mode == 0: # And mode is 0... |
| 69 | + animations.color = packet.color # Update the animation to the color. |
| 70 | + print("Color:", packet.color) |
| 71 | + animation_color = packet.color # Keep track of the current color... |
| 72 | + elif mode == 1: # Because if mode is 1... |
| 73 | + animations.color = animation_color # Freeze the animation color. |
| 74 | + print("Color:", animation_color) |
| 75 | + elif isinstance(packet, ButtonPacket): # If the packet is a button packet... |
| 76 | + if packet.pressed: # If the buttons on the Remote Control are pressed... |
| 77 | + if packet.button == ButtonPacket.LEFT: # If button A is pressed... |
| 78 | + print("A pressed: animation mode changed.") |
| 79 | + animations.next() # Change to the next animation. |
| 80 | + elif packet.button == ButtonPacket.RIGHT: # If button B is pressed... |
| 81 | + mode += 1 # Increase the mode by 1. |
| 82 | + if mode == 1: # If mode is 1, print the following: |
| 83 | + print("B pressed: color frozen!") |
| 84 | + if mode > 1: # If mode is > 1... |
| 85 | + mode = 0 # Set mode to 0, and print the following: |
| 86 | + print("B pressed: color changing!") |
0 commit comments