Skip to content

Commit 73e5e73

Browse files
committed
Adding code files for BLE synth Learn Guide
Adding code files for the BLE synth project. One file is for the Feather nRF52840 and the other is for the Circuit Playground Bluefruit. They're named for clarity so that there aren't two code.py files- this is noted in the Learn guide
1 parent c807699 commit 73e5e73

File tree

2 files changed

+235
-0
lines changed

2 files changed

+235
-0
lines changed

BLE_Synth/cpb_amp_code.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
'''BLE Synth
2+
File for the Circuit Playground Bluefruit
3+
Amp Portion'''
4+
from adafruit_circuitplayground.bluefruit import cpb
5+
from adafruit_led_animation.animation import Comet, AnimationGroup,\
6+
AnimationSequence
7+
import adafruit_led_animation.color as color
8+
from adafruit_ble import BLERadio
9+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
10+
from adafruit_ble.services.nordic import UARTService
11+
from adafruit_bluefruit_connect.packet import Packet
12+
from adafruit_bluefruit_connect.color_packet import ColorPacket
13+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
14+
15+
# easily call for NeoPixels to be off
16+
off = (0, 0, 0)
17+
# state to debounce on CPB end
18+
tone = False
19+
20+
# Setup for comet animation
21+
COMET_SPEED = 0.04 # Lower numbers increase the animation speed
22+
CPB_COMET_TAIL_LENGTH = 5 # The length of the comet on the Circuit Playground Bluefruit
23+
CPB_COMET_BOUNCE = False # Set to True to make the comet "bounce" the opposite direction on CPB
24+
25+
animations = AnimationSequence(
26+
AnimationGroup(
27+
Comet(cpb.pixels, COMET_SPEED, off, tail_length=CPB_COMET_TAIL_LENGTH,
28+
bounce=CPB_COMET_BOUNCE)))
29+
30+
# note frequencies
31+
C4 = 261.63
32+
Csharp4 = 277.18
33+
D4 = 293.66
34+
Dsharp4 = 311.13
35+
E4 = 329.63
36+
F4 = 349.23
37+
Fsharp4 = 369.99
38+
G4 = 392
39+
Gsharp4 = 415.3
40+
A4 = 440
41+
Asharp4 = 466.16
42+
B4 = 493.88
43+
44+
# note array
45+
note = [C4, Csharp4, D4, Dsharp4, E4, F4,
46+
Fsharp4, G4, Gsharp4, A4, Asharp4, B4]
47+
48+
# colors to recieve from color packet & for neopixels
49+
color_C = color.RED
50+
color_Csharp = color.ORANGE
51+
color_D = color.YELLOW
52+
color_Dsharp = color.GREEN
53+
color_E = color.TEAL
54+
color_F = color.CYAN
55+
color_Fsharp = color.BLUE
56+
color_G = color.PURPLE
57+
color_Gsharp = color.MAGENTA
58+
color_A = color.GOLD
59+
color_Asharp = color.PINK
60+
color_B = color.WHITE
61+
62+
# color array
63+
color = [color_C, color_Csharp, color_D, color_Dsharp, color_E,
64+
color_F, color_Fsharp, color_G, color_Gsharp, color_A,
65+
color_Asharp, color_B]
66+
67+
# Setup BLE connection
68+
ble = BLERadio()
69+
uart = UARTService()
70+
advertisement = ProvideServicesAdvertisement(uart)
71+
72+
while True:
73+
# connect via BLE
74+
ble.start_advertising(advertisement) # Start advertising.
75+
was_connected = False
76+
while not was_connected or ble.connected:
77+
if ble.connected: # If BLE is connected...
78+
was_connected = True
79+
# start animations
80+
animations.animate()
81+
# look for packets
82+
if uart.in_waiting:
83+
try:
84+
packet = Packet.from_stream(uart) # Create the packet object.
85+
except ValueError:
86+
continue
87+
# if it's a color packet:
88+
if isinstance(packet, ColorPacket):
89+
for i in range(12):
90+
colors = color[i]
91+
notes = note[i]
92+
# if the packet matches one of our colors:
93+
if packet.color == colors and not tone:
94+
# animate with that color
95+
animations.color = colors
96+
# play matching note
97+
cpb.start_tone(notes)
98+
tone = True
99+
# if it's a button packet aka feather's button has been released:
100+
elif isinstance(packet, ButtonPacket) and packet.pressed:
101+
if packet.button == ButtonPacket.RIGHT and tone:
102+
tone = False
103+
# stop playing the note
104+
cpb.stop_tone()
105+
# turn off the neopixels but keep animation active
106+
animations.color = off

BLE_Synth/feather_keyboard_code.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
'''BLE Synth
2+
File for the Feather nFR52840
3+
Keyboard Portion'''
4+
import time
5+
import board
6+
import digitalio
7+
import adafruit_led_animation.color as color
8+
from adafruit_ble import BLERadio
9+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
10+
from adafruit_ble.services.nordic import UARTService
11+
from adafruit_bluefruit_connect.color_packet import ColorPacket
12+
from adafruit_bluefruit_connect.button_packet import ButtonPacket
13+
14+
# setup for LED to indicate BLE connection
15+
blue_led = digitalio.DigitalInOut(board.BLUE_LED)
16+
blue_led.direction = digitalio.Direction.OUTPUT
17+
18+
# setting up the buttons
19+
switch_pins = [board.D5, board.D6, board.D9, board.D10,
20+
board.D11, board.D12, board.D13, board.A0, board.A1, board.A2,
21+
board.A3, board.A4]
22+
switch_array = []
23+
24+
# creating the button array
25+
for pin in switch_pins:
26+
switch_pin = digitalio.DigitalInOut(pin)
27+
switch_pin.direction = digitalio.Direction.INPUT
28+
switch_pin.pull = digitalio.Pull.UP
29+
switch_array.append(switch_pin)
30+
31+
# states for button debouncing
32+
switch1_pressed = False
33+
switch2_pressed = False
34+
switch3_pressed = False
35+
switch4_pressed = False
36+
switch5_pressed = False
37+
switch6_pressed = False
38+
switch7_pressed = False
39+
switch8_pressed = False
40+
switch9_pressed = False
41+
switch10_pressed = False
42+
switch11_pressed = False
43+
switch12_pressed = False
44+
switches_pressed = [switch1_pressed, switch2_pressed, switch3_pressed, switch4_pressed,
45+
switch5_pressed, switch6_pressed, switch7_pressed, switch8_pressed,
46+
switch9_pressed, switch10_pressed, switch11_pressed, switch12_pressed]
47+
48+
# colors from Animation library to send as color packets
49+
# named for notes
50+
color_C = color.RED
51+
color_Csharp = color.ORANGE
52+
color_D = color.YELLOW
53+
color_Dsharp = color.GREEN
54+
color_E = color.TEAL
55+
color_F = color.CYAN
56+
color_Fsharp = color.BLUE
57+
color_G = color.PURPLE
58+
color_Gsharp = color.MAGENTA
59+
color_A = color.GOLD
60+
color_Asharp = color.PINK
61+
color_B = color.WHITE
62+
63+
# array for colors
64+
color = [color_C, color_Csharp, color_D, color_Dsharp, color_E,
65+
color_F, color_Fsharp, color_G, color_Gsharp, color_A,
66+
color_Asharp, color_B]
67+
68+
# BLE send_packet function
69+
def send_packet(uart_connection_name, packet):
70+
"""Returns False if no longer connected."""
71+
try:
72+
uart_connection_name[UARTService].write(packet.to_bytes())
73+
except: # pylint: disable=bare-except
74+
try:
75+
uart_connection_name.disconnect()
76+
except: # pylint: disable=bare-except
77+
pass
78+
return False
79+
return True
80+
81+
ble = BLERadio()
82+
83+
uart_connection = None
84+
85+
if ble.connected:
86+
for connection in ble.connections:
87+
if UARTService in connection:
88+
uart_connection = connection
89+
break
90+
91+
while True:
92+
blue_led.value = False
93+
# BLE connection
94+
if not uart_connection or not uart_connection.connected: # If not connected...
95+
print("Scanning...")
96+
for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5): # Scan...
97+
if UARTService in adv.services: # If UARTService found...
98+
print("Found a UARTService advertisement.")
99+
blue_led.value = True # LED turns on when connected
100+
uart_connection = ble.connect(adv) # Create a UART connection...
101+
break
102+
ble.stop_scan() # And stop scanning.
103+
# while connected..
104+
while uart_connection and uart_connection.connected:
105+
# iterate through buttons and colors
106+
for switch_pin in switch_array:
107+
i = switch_array.index(switch_pin)
108+
switches_pressed_state = switches_pressed[i]
109+
colors = color[i]
110+
# if the button is released
111+
# worked best if placed before the button press portion
112+
if switch_pin.value and switches_pressed_state:
113+
print("button off")
114+
# send button packet to stop tone & color (happens on CPB)
115+
if not send_packet(uart_connection,
116+
ButtonPacket(ButtonPacket.RIGHT, pressed=True)):
117+
uart_connection = None
118+
continue
119+
switches_pressed[i] = False # Set to False.
120+
# time delay for BLE, otherwise issues can arrise
121+
time.sleep(0.05)
122+
# if button is pressed:
123+
if not switch_pin.value and not switches_pressed_state: # If button A pressed...
124+
# send color packet
125+
if not send_packet(uart_connection, ColorPacket(colors)):
126+
uart_connection = None
127+
continue
128+
switches_pressed[i] = True # Set to True.
129+
time.sleep(0.05) # Debounce.

0 commit comments

Comments
 (0)