Skip to content

Commit 3844006

Browse files
committed
Updates and comments.
1 parent 30e3b04 commit 3844006

File tree

2 files changed

+95
-63
lines changed

2 files changed

+95
-63
lines changed
Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Control code for Circuit Playground Bluefruit NeoPixel Animation and Color controller. To be used
3-
with receiver code.
3+
with another Circuit Playground Bluefruit running the receiver code.
44
"""
55

66
import time
@@ -25,7 +25,7 @@ def send_packet(uart_connection_name, packet):
2525
"""Returns False if no longer connected."""
2626
try:
2727
uart_connection_name[UARTService].write(packet.to_bytes())
28-
except (OSError, KeyError):
28+
except: # pylint: disable=bare-except
2929
try:
3030
uart_connection_name.disconnect()
3131
except: # pylint: disable=bare-except
@@ -36,6 +36,7 @@ def send_packet(uart_connection_name, packet):
3636

3737
ble = BLERadio()
3838

39+
# Setup for preventing repeated button presses and tracking switch state
3940
button_a_pressed = False
4041
button_b_pressed = False
4142
last_switch_state = None
@@ -50,53 +51,56 @@ def send_packet(uart_connection_name, packet):
5051

5152
while True:
5253
last_switch_state = None
53-
if not uart_connection or not uart_connection.connected:
54+
if not uart_connection or not uart_connection.connected: # If not connected...
5455
print("Scanning...")
55-
for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5):
56-
if UARTService in adv.services:
56+
for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5): # Scan...
57+
if UARTService in adv.services: # If UARTService found...
5758
print("Found a UARTService advertisement.")
58-
uart_connection = ble.connect(adv)
59+
uart_connection = ble.connect(adv) # Create a UART connection...
5960
break
6061
# Stop scanning whether or not we are connected.
61-
ble.stop_scan()
62-
while uart_connection and uart_connection.connected:
63-
if cpb.button_a and not button_a_pressed:
62+
ble.stop_scan() # And stop scanning.
63+
while uart_connection and uart_connection.connected: # If connected...
64+
if cpb.button_a and not button_a_pressed: # If button A pressed...
6465
print("Button A pressed.")
66+
# Send a LEFT button packet.
6567
if not send_packet(uart_connection,
6668
ButtonPacket(ButtonPacket.LEFT, pressed=True)):
6769
uart_connection = None
6870
continue
69-
button_a_pressed = True
70-
time.sleep(0.05)
71-
if not cpb.button_a and button_a_pressed:
72-
button_a_pressed = False
73-
time.sleep(0.05)
74-
if cpb.button_b and not button_b_pressed:
71+
button_a_pressed = True # Set to True.
72+
time.sleep(0.05) # Debounce.
73+
if not cpb.button_a and button_a_pressed: # On button release...
74+
button_a_pressed = False # Set to False.
75+
time.sleep(0.05) # Debounce.
76+
if cpb.button_b and not button_b_pressed: # If button B pressed...
7577
print("Button B pressed.")
78+
# Send a RIGHT button packet.
7679
if not send_packet(uart_connection,
7780
ButtonPacket(ButtonPacket.RIGHT, pressed=True)):
7881
uart_connection = None
7982
continue
80-
button_b_pressed = True
81-
time.sleep(0.05)
82-
if not cpb.button_b and button_b_pressed:
83-
button_b_pressed = False
84-
time.sleep(0.05)
85-
if cpb.switch is not last_switch_state:
86-
last_switch_state = cpb.switch
83+
button_b_pressed = True # Set to True.
84+
time.sleep(0.05) # Debounce.
85+
if not cpb.button_b and button_b_pressed: # On button release...
86+
button_b_pressed = False # Set to False.
87+
time.sleep(0.05) # Debounce.
88+
if cpb.switch is not last_switch_state: # If the switch state is changed...
89+
last_switch_state = cpb.switch # Set state to current switch state.
8790
print("Switch is to the", "left: LEDs off!" if cpb.switch else "right: LEDs on!")
91+
# Send a BUTTON_1 button packet.
8892
if not send_packet(uart_connection,
8993
ButtonPacket(ButtonPacket.BUTTON_1, pressed=cpb.switch)):
9094
uart_connection = None
9195
continue
92-
if cpb.switch:
93-
cpb.pixels.fill(0)
94-
else:
95-
r, g, b = map(scale, cpb.acceleration)
96-
color = (r, g, b)
97-
print(color)
98-
cpb.pixels.fill(color)
99-
if not send_packet(uart_connection, ColorPacket(color)):
96+
if cpb.switch: # If switch is to the left...
97+
cpb.pixels.fill((0, 0, 0)) # Turn off the LEDs.
98+
else: # Otherwise...
99+
r, g, b = map(scale, cpb.acceleration) # Map acceleration values to RGB values...
100+
color = (r, g, b) # Set color to current mapped RGB value...
101+
print("Color:", color)
102+
cpb.pixels.fill(color) # Fill controller LEDs with current color...
103+
if not send_packet(uart_connection, ColorPacket(color)): # And send a color packet.
100104
uart_connection = None
101105
continue
102-
time.sleep(0.1)
106+
time.sleep(0.1) # Delay to prevent sending packets too quickly.
Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
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.
44
"""
55

66
import board
@@ -18,67 +18,95 @@
1818
from adafruit_bluefruit_connect.color_packet import ColorPacket
1919
from adafruit_bluefruit_connect.button_packet import ButtonPacket
2020

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
2224

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
2345
ble = BLERadio()
2446
uart = UARTService()
2547
advertisement = ProvideServicesAdvertisement(uart)
2648

49+
# Setup animations
2750
animations = AnimationSequence(
2851
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),
3154
sync=True
3255
),
3356
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)
3661
),
3762
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)
4065
),
4166
)
4267

4368
animation_color = None
4469
mode = 0
4570
blanked = False
71+
4672
while True:
47-
ble.start_advertising(advertisement)
73+
ble.start_advertising(advertisement) # Start advertising.
4874
was_connected = False
4975
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...
5379
was_connected = True
54-
if uart.in_waiting:
80+
if uart.in_waiting: # Check to see if any data is available from the controller.
5581
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:
5884
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.
6288
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.
6692
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)
6895
if packet.button == ButtonPacket.BUTTON_1:
6996
print("Controller switch is to the", "left: LEDs off!" if packet.pressed
7097
else "right: LEDs on!")
98+
# If the controller switch is moved to the left...
7199
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...
76104
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:
81109
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:
84112
print("B pressed: color changing!")

0 commit comments

Comments
 (0)