Skip to content

Commit 6ea9c44

Browse files
author
ladyada
committed
Merge branch 'master' of github.com:ladyada/Adafruit_Learning_System_Guides
2 parents 4a6201f + f9f3fa2 commit 6ea9c44

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import time
2+
import displayio
3+
import terminalio
4+
from adafruit_clue import clue
5+
from adafruit_display_text import label
6+
import adafruit_imageload
7+
from adafruit_ble import BLERadio
8+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
9+
from adafruit_ble.services.nordic import UARTService
10+
11+
#--| User Config |---------------------------------------------------
12+
MY_NAME = "ME"
13+
FRIENDS_NAME = "FRIEND"
14+
#--| User Config |---------------------------------------------------
15+
16+
WAIT_FOR_DOUBLE = 0.05
17+
DEBOUNCE = 0.25
18+
19+
# Define Morse Code dictionary
20+
morse_code = {
21+
".-" : "A", "-..." : "B", "-.-." : "C", "-.." : "D", "." : "E",
22+
"..-." : "F", "--." : "G", "...." : "H", ".." : "I", ".---" : "J",
23+
"-.-" : "K", ".-.." : "L", "--" : "M", "-." : "N", "---" : "O",
24+
".--." : "P", "--.-" : "Q", ".-." : "R", "..." : "S", "-" : "T",
25+
"..-" : "U", "...-" : "V", ".--" : "W", "-..-" : "X", "-.--" : "Y",
26+
"--.." : "Z",
27+
}
28+
29+
# BLE Radio Stuff
30+
ble = BLERadio()
31+
uart_service = UARTService()
32+
advertisement = ProvideServicesAdvertisement(uart_service)
33+
ble._adapter.name = MY_NAME #pylint: disable=protected-access
34+
35+
# Display Stuff
36+
display = clue.display
37+
disp_group = displayio.Group()
38+
display.show(disp_group)
39+
40+
# Background BMP with the Morse Code cheat sheet
41+
bmp, pal = adafruit_imageload.load("morse_bg.bmp",
42+
bitmap=displayio.Bitmap,
43+
palette=displayio.Palette)
44+
disp_group.append(displayio.TileGrid(bmp, pixel_shader=pal))
45+
46+
# Incoming messages show up here
47+
in_label = label.Label(terminalio.FONT, text='A'*18, scale=2,
48+
color=0x000000)
49+
in_label.anchor_point = (0.5, 0)
50+
in_label.anchored_position = (65, 12)
51+
disp_group.append(in_label)
52+
53+
# Outging messages show up here
54+
out_label = label.Label(terminalio.FONT, text='B'*18, scale=2,
55+
color=0x000000)
56+
out_label.anchor_point = (0.5, 0)
57+
out_label.anchored_position = (65, 190)
58+
disp_group.append(out_label)
59+
60+
# Morse Code entry happens here
61+
edit_label = label.Label(terminalio.FONT, text='....', scale=2,
62+
color=0x000000)
63+
edit_label.anchor_point = (0.5, 0)
64+
edit_label.anchored_position = (105, 222)
65+
disp_group.append(edit_label)
66+
67+
def scan_and_connect():
68+
'''
69+
Advertise self while scanning for friend. If friend is found, can
70+
connect by pressing A+B buttons. If friend connects first, then
71+
just stop.
72+
73+
Return is a UART object that can be used for read/write.
74+
'''
75+
76+
print("Advertising.")
77+
central = False
78+
ble.start_advertising(advertisement)
79+
80+
print("Waiting.")
81+
friend = None
82+
while not ble.connected:
83+
84+
if friend is None:
85+
print("Scanning.")
86+
in_label.text = out_label.text = "Scanning..."
87+
for adv in ble.start_scan():
88+
if ble.connected:
89+
# Friend connected with us, we're done
90+
ble.stop_scan()
91+
break
92+
if adv.complete_name == FRIENDS_NAME:
93+
# Found friend, can stop scanning
94+
ble.stop_scan()
95+
friend = adv
96+
print("Found", friend.complete_name)
97+
in_label.text = "Found {}".format(friend.complete_name)
98+
out_label.text = "A+B to connect"
99+
break
100+
else:
101+
if clue.button_a and clue.button_b:
102+
# Connect to friend
103+
print("Connecting to", friend.complete_name)
104+
ble.connect(friend)
105+
central = True
106+
107+
# We're now connected, one way or the other
108+
print("Stopping advertising.")
109+
ble.stop_advertising()
110+
111+
# Return a UART object to use
112+
if central:
113+
print("Central - using my UART service.")
114+
return uart_service
115+
else:
116+
print("Peripheral - connecting to their UART service.")
117+
for connection in ble.connections:
118+
if UARTService not in connection:
119+
continue
120+
return connection[UARTService]
121+
122+
#--------------------------
123+
# The main application loop
124+
#--------------------------
125+
while True:
126+
127+
# Establish initial connection
128+
uart = scan_and_connect()
129+
130+
print("Connected.")
131+
132+
code = ''
133+
in_label.text = out_label.text = ' '*18
134+
edit_label.text = ' '*4
135+
done = False
136+
137+
# Run the chat while connected
138+
while ble.connected:
139+
140+
# Check for incoming message
141+
incoming_bytes = uart.in_waiting
142+
if incoming_bytes:
143+
bytes_in = uart.read(incoming_bytes)
144+
print("Received: ", bytes_in)
145+
in_label.text = in_label.text[incoming_bytes:] + bytes_in.decode()
146+
147+
# DOT (or done)
148+
if clue.button_a:
149+
start = time.monotonic()
150+
while time.monotonic() - start < WAIT_FOR_DOUBLE:
151+
if clue.button_b:
152+
done = True
153+
if not done and len(code) < 4:
154+
print('.', end='')
155+
code += '.'
156+
edit_label.text = "{:4s}".format(code)
157+
time.sleep(DEBOUNCE)
158+
159+
# DASH (or done)
160+
if clue.button_b:
161+
start = time.monotonic()
162+
while time.monotonic() - start < WAIT_FOR_DOUBLE:
163+
if clue.button_a:
164+
done = True
165+
if not done and len(code) < 4:
166+
print('-', end='')
167+
code += '-'
168+
edit_label.text = "{:4s}".format(code)
169+
time.sleep(DEBOUNCE)
170+
171+
# Turn Morse Code into letter and send
172+
if done:
173+
letter = morse_code.get(code, ' ')
174+
print(' >', letter)
175+
out_label.text = out_label.text[1:] + letter
176+
uart.write(str.encode(letter))
177+
code = ''
178+
edit_label.text = ' '*4
179+
done = False
180+
time.sleep(DEBOUNCE)
181+
182+
print("Disconnected.")

CLUE_BLE_Morse_Code/morse_bg.bmp

28.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)