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