Skip to content

Commit 30e3b04

Browse files
committed
Adding CPB NeoPixel controller code
1 parent 5535606 commit 30e3b04

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""
2+
Control code for Circuit Playground Bluefruit NeoPixel Animation and Color controller. To be used
3+
with receiver code.
4+
"""
5+
6+
import time
7+
8+
from adafruit_circuitplayground.bluefruit import cpb
9+
10+
from adafruit_ble import BLERadio
11+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
12+
from adafruit_ble.services.nordic import UARTService
13+
from adafruit_bluefruit_connect.color_packet import ColorPacket
14+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
15+
16+
17+
def scale(value):
18+
"""Scale a value from acceleration value range to 0-255 (RGB range)"""
19+
value = abs(value)
20+
value = max(min(19.6, value), 0)
21+
return int(value / 19.6 * 255)
22+
23+
24+
def send_packet(uart_connection_name, packet):
25+
"""Returns False if no longer connected."""
26+
try:
27+
uart_connection_name[UARTService].write(packet.to_bytes())
28+
except (OSError, KeyError):
29+
try:
30+
uart_connection_name.disconnect()
31+
except: # pylint: disable=bare-except
32+
pass
33+
return False
34+
return True
35+
36+
37+
ble = BLERadio()
38+
39+
button_a_pressed = False
40+
button_b_pressed = False
41+
last_switch_state = None
42+
43+
uart_connection = None
44+
# See if any existing connections are providing UARTService.
45+
if ble.connected:
46+
for connection in ble.connections:
47+
if UARTService in connection:
48+
uart_connection = connection
49+
break
50+
51+
while True:
52+
last_switch_state = None
53+
if not uart_connection or not uart_connection.connected:
54+
print("Scanning...")
55+
for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5):
56+
if UARTService in adv.services:
57+
print("Found a UARTService advertisement.")
58+
uart_connection = ble.connect(adv)
59+
break
60+
# 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:
64+
print("Button A pressed.")
65+
if not send_packet(uart_connection,
66+
ButtonPacket(ButtonPacket.LEFT, pressed=True)):
67+
uart_connection = None
68+
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:
75+
print("Button B pressed.")
76+
if not send_packet(uart_connection,
77+
ButtonPacket(ButtonPacket.RIGHT, pressed=True)):
78+
uart_connection = None
79+
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
87+
print("Switch is to the", "left: LEDs off!" if cpb.switch else "right: LEDs on!")
88+
if not send_packet(uart_connection,
89+
ButtonPacket(ButtonPacket.BUTTON_1, pressed=cpb.switch)):
90+
uart_connection = None
91+
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)):
100+
uart_connection = None
101+
continue
102+
time.sleep(0.1)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""
2+
Receiver code for Circuit Playground Bluefruit NeoPixel Animation and Color controller. To be used
3+
with control code.
4+
"""
5+
6+
import board
7+
import neopixel
8+
from adafruit_circuitplayground.bluefruit import cpb
9+
from adafruit_led_animation.animation import Blink, Comet, Sparkle, AnimationGroup,\
10+
AnimationSequence
11+
import adafruit_led_animation.color as color
12+
13+
from adafruit_ble import BLERadio
14+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
15+
from adafruit_ble.services.nordic import UARTService
16+
17+
from adafruit_bluefruit_connect.packet import Packet
18+
from adafruit_bluefruit_connect.color_packet import ColorPacket
19+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
20+
21+
strip_pixels = neopixel.NeoPixel(board.A1, 30, auto_write=False)
22+
23+
ble = BLERadio()
24+
uart = UARTService()
25+
advertisement = ProvideServicesAdvertisement(uart)
26+
27+
animations = AnimationSequence(
28+
AnimationGroup(
29+
Blink(cpb.pixels, 0.5, color.RED),
30+
Blink(strip_pixels, 0.5, color.RED),
31+
sync=True
32+
),
33+
AnimationGroup(
34+
Comet(cpb.pixels, 0.05, color.MAGENTA, tail_length=5),
35+
Comet(strip_pixels, 0.03, color.MAGENTA, tail_length=15)
36+
),
37+
AnimationGroup(
38+
Sparkle(cpb.pixels, 0.05, color.PURPLE),
39+
Sparkle(strip_pixels, 0.05, color.PURPLE)
40+
),
41+
)
42+
43+
animation_color = None
44+
mode = 0
45+
blanked = False
46+
while True:
47+
ble.start_advertising(advertisement)
48+
was_connected = False
49+
while not was_connected or ble.connected:
50+
if not blanked:
51+
animations.animate()
52+
if ble.connected:
53+
was_connected = True
54+
if uart.in_waiting:
55+
try:
56+
packet = Packet.from_stream(uart)
57+
except ValueError as e:
58+
continue
59+
if isinstance(packet, ColorPacket):
60+
if mode == 0:
61+
animations.change_color(packet.color)
62+
print("Color:", packet.color)
63+
animation_color = packet.color
64+
elif mode == 1:
65+
animations.change_color(animation_color)
66+
print("Color:", animation_color)
67+
elif isinstance(packet, ButtonPacket):
68+
if packet.button == ButtonPacket.BUTTON_1:
69+
print("Controller switch is to the", "left: LEDs off!" if packet.pressed
70+
else "right: LEDs on!")
71+
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:
76+
print("A pressed: animation mode changed.")
77+
animations.next()
78+
elif packet.button == ButtonPacket.RIGHT:
79+
mode += 1
80+
if mode == 1:
81+
print("B pressed: color frozen!")
82+
if mode > 1:
83+
mode = 0
84+
print("B pressed: color changing!")

0 commit comments

Comments
 (0)