|
1 | 1 | """
|
2 | 2 | Receiver code for Circuit Playground Bluefruit NeoPixel Animation and Color controller. To be used
|
3 |
| -with control code. |
| 3 | +with another Circuit Playground Bluefruit running the controller code. |
4 | 4 | """
|
5 | 5 |
|
6 | 6 | import board
|
|
18 | 18 | from adafruit_bluefruit_connect.color_packet import ColorPacket
|
19 | 19 | from adafruit_bluefruit_connect.button_packet import ButtonPacket
|
20 | 20 |
|
21 |
| -strip_pixels = neopixel.NeoPixel(board.A1, 30, auto_write=False) |
| 21 | +# The number of NeoPixels in the externally attached strip |
| 22 | +# If using two strips connected to the same pin, count only one strip for this number! |
| 23 | +STRIP_PIXEL_NUMBER = 30 |
22 | 24 |
|
| 25 | +# Setup for blink animation |
| 26 | +BLINK_SPEED = 0.5 # Lower numbers increase the animation speed |
| 27 | +BLINK_INITIAL_COLOR = color.RED # Color before controller is connected |
| 28 | + |
| 29 | +# Setup for comet animation |
| 30 | +COMET_SPEED = 0.03 # Lower numbers increase the animation speed |
| 31 | +COMET_INITIAL_COLOR = color.MAGENTA # Color before controller is connected |
| 32 | +CPB_COMET_TAIL_LENGTH = 5 # The length of the comet on the Circuit Playground Bluefruit |
| 33 | +STRIP_COMET_TAIL_LENGTH = 15 # The length of the comet on the NeoPixel strip |
| 34 | +CPB_COMET_BOUNCE = False # Set to True to make the comet "bounce" the opposite direction on CPB |
| 35 | +STRIP_COMET_BOUNCE = True # Set to False to stop comet from "bouncing" on NeoPixel strip |
| 36 | + |
| 37 | +# Setup for sparkle animation |
| 38 | +SPARKLE_SPEED = 0.03 # Lower numbers increase the animation speed |
| 39 | +SPARKLE_INITIAL_COLOR = color.PURPLE # Color before controller is connected |
| 40 | + |
| 41 | +# Create the NeoPixel strip |
| 42 | +strip_pixels = neopixel.NeoPixel(board.A1, STRIP_PIXEL_NUMBER, auto_write=False) |
| 43 | + |
| 44 | +# Setup BLE connection |
23 | 45 | ble = BLERadio()
|
24 | 46 | uart = UARTService()
|
25 | 47 | advertisement = ProvideServicesAdvertisement(uart)
|
26 | 48 |
|
| 49 | +# Setup animations |
27 | 50 | animations = AnimationSequence(
|
28 | 51 | AnimationGroup(
|
29 |
| - Blink(cpb.pixels, 0.5, color.RED), |
30 |
| - Blink(strip_pixels, 0.5, color.RED), |
| 52 | + Blink(cpb.pixels, BLINK_SPEED, BLINK_INITIAL_COLOR), |
| 53 | + Blink(strip_pixels, BLINK_SPEED, BLINK_INITIAL_COLOR), |
31 | 54 | sync=True
|
32 | 55 | ),
|
33 | 56 | AnimationGroup(
|
34 |
| - Comet(cpb.pixels, 0.05, color.MAGENTA, tail_length=5), |
35 |
| - Comet(strip_pixels, 0.03, color.MAGENTA, tail_length=15) |
| 57 | + Comet(cpb.pixels, COMET_SPEED, COMET_INITIAL_COLOR, tail_length=CPB_COMET_TAIL_LENGTH, |
| 58 | + bounce=CPB_COMET_BOUNCE), |
| 59 | + Comet(strip_pixels, COMET_SPEED, COMET_INITIAL_COLOR, tail_length=STRIP_COMET_TAIL_LENGTH, |
| 60 | + bounce=STRIP_COMET_BOUNCE) |
36 | 61 | ),
|
37 | 62 | AnimationGroup(
|
38 |
| - Sparkle(cpb.pixels, 0.05, color.PURPLE), |
39 |
| - Sparkle(strip_pixels, 0.05, color.PURPLE) |
| 63 | + Sparkle(cpb.pixels, SPARKLE_SPEED, SPARKLE_INITIAL_COLOR), |
| 64 | + Sparkle(strip_pixels, SPARKLE_SPEED, SPARKLE_INITIAL_COLOR) |
40 | 65 | ),
|
41 | 66 | )
|
42 | 67 |
|
43 | 68 | animation_color = None
|
44 | 69 | mode = 0
|
45 | 70 | blanked = False
|
| 71 | + |
46 | 72 | while True:
|
47 |
| - ble.start_advertising(advertisement) |
| 73 | + ble.start_advertising(advertisement) # Start advertising. |
48 | 74 | was_connected = False
|
49 | 75 | while not was_connected or ble.connected:
|
50 |
| - if not blanked: |
51 |
| - animations.animate() |
52 |
| - if ble.connected: |
| 76 | + if not blanked: # If LED-off signal is not being sent... |
| 77 | + animations.animate() # Run the animations. |
| 78 | + if ble.connected: # If BLE is connected... |
53 | 79 | was_connected = True
|
54 |
| - if uart.in_waiting: |
| 80 | + if uart.in_waiting: # Check to see if any data is available from the controller. |
55 | 81 | try:
|
56 |
| - packet = Packet.from_stream(uart) |
57 |
| - except ValueError as e: |
| 82 | + packet = Packet.from_stream(uart) # Create the packet object. |
| 83 | + except ValueError: |
58 | 84 | continue
|
59 |
| - if isinstance(packet, ColorPacket): |
60 |
| - if mode == 0: |
61 |
| - animations.change_color(packet.color) |
| 85 | + if isinstance(packet, ColorPacket): # If the packet is color packet... |
| 86 | + if mode == 0: # And mode is 0... |
| 87 | + animations.color = packet.color # Update the animation to the color. |
62 | 88 | print("Color:", packet.color)
|
63 |
| - animation_color = packet.color |
64 |
| - elif mode == 1: |
65 |
| - animations.change_color(animation_color) |
| 89 | + animation_color = packet.color # Keep track of the current color... |
| 90 | + elif mode == 1: # Because if mode is 1... |
| 91 | + animations.color = animation_color # Freeze the animation color. |
66 | 92 | print("Color:", animation_color)
|
67 |
| - elif isinstance(packet, ButtonPacket): |
| 93 | + elif isinstance(packet, ButtonPacket): # If the packet is a button packet... |
| 94 | + # Check to see if it's BUTTON_1 (which is being sent by the slide switch) |
68 | 95 | if packet.button == ButtonPacket.BUTTON_1:
|
69 | 96 | print("Controller switch is to the", "left: LEDs off!" if packet.pressed
|
70 | 97 | else "right: LEDs on!")
|
| 98 | + # If the controller switch is moved to the left... |
71 | 99 | if packet.pressed and not blanked:
|
72 |
| - animations.fill(color.BLACK) |
73 |
| - blanked = packet.pressed |
74 |
| - if packet.pressed: |
75 |
| - if packet.button == ButtonPacket.LEFT: |
| 100 | + animations.fill(color.BLACK) # Turn off the LEDs. |
| 101 | + blanked = packet.pressed # Track the state of the slide switch. |
| 102 | + if packet.pressed: # If the buttons on the controller are pressed... |
| 103 | + if packet.button == ButtonPacket.LEFT: # If button A is pressed... |
76 | 104 | print("A pressed: animation mode changed.")
|
77 |
| - animations.next() |
78 |
| - elif packet.button == ButtonPacket.RIGHT: |
79 |
| - mode += 1 |
80 |
| - if mode == 1: |
| 105 | + animations.next() # Change to the next animation. |
| 106 | + elif packet.button == ButtonPacket.RIGHT: # If button B is pressed... |
| 107 | + mode += 1 # Increase the mode by 1. |
| 108 | + if mode == 1: # If mode is 1, print the following: |
81 | 109 | print("B pressed: color frozen!")
|
82 |
| - if mode > 1: |
83 |
| - mode = 0 |
| 110 | + if mode > 1: # If mode is > 1... |
| 111 | + mode = 0 # Set mode to 0, and print the following: |
84 | 112 | print("B pressed: color changing!")
|
0 commit comments