Skip to content

Commit 959066c

Browse files
committed
updates based on testing
1 parent 85c0557 commit 959066c

File tree

1 file changed

+99
-83
lines changed

1 file changed

+99
-83
lines changed

CLUE_BLE_Morse_Code/code.py

Lines changed: 99 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -12,137 +12,153 @@
1212
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
1313
from adafruit_ble.services.nordic import UARTService
1414

15-
#--| User Config |---------------------------------------------------
16-
MY_NAME = "CENTRAL"
17-
FRIENDS_NAME = "PERIPHERAL"
18-
#--| User Config |---------------------------------------------------
15+
# --| User Config |---------------------------------------------------
16+
# Set to either A or B. The other CLUE should be set to opposite mode.
17+
BLE_MODE = "A"
18+
# --| User Config |---------------------------------------------------
19+
20+
BLE_MODE = BLE_MODE.upper().strip()
21+
if BLE_MODE not in ("A", "B"):
22+
raise ValueError("BLE_MODE must be set to either A or B.")
1923

2024
WAIT_FOR_DOUBLE = 0.05
2125
DEBOUNCE = 0.25
2226

2327
# Define Morse Code dictionary
2428
morse_code = {
25-
".-" : "A", "-..." : "B", "-.-." : "C", "-.." : "D", "." : "E",
26-
"..-." : "F", "--." : "G", "...." : "H", ".." : "I", ".---" : "J",
27-
"-.-" : "K", ".-.." : "L", "--" : "M", "-." : "N", "---" : "O",
28-
".--." : "P", "--.-" : "Q", ".-." : "R", "..." : "S", "-" : "T",
29-
"..-" : "U", "...-" : "V", ".--" : "W", "-..-" : "X", "-.--" : "Y",
30-
"--.." : "Z",
29+
".-": "A",
30+
"-...": "B",
31+
"-.-.": "C",
32+
"-..": "D",
33+
".": "E",
34+
"..-.": "F",
35+
"--.": "G",
36+
"....": "H",
37+
"..": "I",
38+
".---": "J",
39+
"-.-": "K",
40+
".-..": "L",
41+
"--": "M",
42+
"-.": "N",
43+
"---": "O",
44+
".--.": "P",
45+
"--.-": "Q",
46+
".-.": "R",
47+
"...": "S",
48+
"-": "T",
49+
"..-": "U",
50+
"...-": "V",
51+
".--": "W",
52+
"-..-": "X",
53+
"-.--": "Y",
54+
"--..": "Z",
3155
}
3256

3357
# BLE Radio Stuff
58+
if BLE_MODE == "A":
59+
MY_NAME = "CENTRAL"
60+
FRIENDS_NAME = "PERIPHERAL"
61+
else:
62+
MY_NAME = "PERIPHERAL"
63+
FRIENDS_NAME = "CENTRAL"
3464
ble = BLERadio()
3565
uart_service = UARTService()
3666
advertisement = ProvideServicesAdvertisement(uart_service)
37-
ble._adapter.name = MY_NAME #pylint: disable=protected-access
67+
ble._adapter.name = MY_NAME # pylint: disable=protected-access
3868

3969
# Display Stuff
4070
display = clue.display
4171
disp_group = displayio.Group()
4272
display.show(disp_group)
4373

4474
# Background BMP with the Morse Code cheat sheet
45-
bmp, pal = adafruit_imageload.load("morse_bg.bmp",
46-
bitmap=displayio.Bitmap,
47-
palette=displayio.Palette)
75+
bmp, pal = adafruit_imageload.load(
76+
"morse_bg.bmp", bitmap=displayio.Bitmap, palette=displayio.Palette
77+
)
4878
disp_group.append(displayio.TileGrid(bmp, pixel_shader=pal))
4979

5080
# Incoming messages show up here
51-
in_label = label.Label(terminalio.FONT, text='A'*18, scale=2,
52-
color=0x000000)
53-
in_label.anchor_point = (0.5, 0)
54-
in_label.anchored_position = (65, 12)
81+
in_label = label.Label(terminalio.FONT, text="A" * 18, scale=2, color=0x000000)
82+
in_label.anchor_point = (1.0, 0)
83+
in_label.anchored_position = (235, 4)
5584
disp_group.append(in_label)
5685

5786
# Outging messages show up here
58-
out_label = label.Label(terminalio.FONT, text='B'*18, scale=2,
59-
color=0x000000)
60-
out_label.anchor_point = (0.5, 0)
61-
out_label.anchored_position = (65, 190)
87+
out_label = label.Label(terminalio.FONT, text="B" * 18, scale=2, color=0x000000)
88+
out_label.anchor_point = (1.0, 0)
89+
out_label.anchored_position = (235, 180)
6290
disp_group.append(out_label)
6391

6492
# Morse Code entry happens here
65-
edit_label = label.Label(terminalio.FONT, text='....', scale=2,
66-
color=0x000000)
93+
edit_label = label.Label(terminalio.FONT, text="----", scale=2, color=0x000000)
6794
edit_label.anchor_point = (0.5, 0)
68-
edit_label.anchored_position = (105, 222)
95+
edit_label.anchored_position = (115, 212)
6996
disp_group.append(edit_label)
7097

98+
7199
def scan_and_connect():
72-
'''
73-
Advertise self while scanning for friend. If friend is found, can
74-
connect by pressing A+B buttons. If friend connects first, then
75-
just stop.
76-
'''
77-
78-
# peripheral
79-
if MY_NAME == "PERIPHERAL":
80-
print("Advertising.")
100+
"""
101+
Handles initial connection between the two CLUES.
102+
103+
The CLUE set to BLE_MODE="A" will act as Central.
104+
The CLUE set to BLE_MODE="B" will act as Peripheral.
105+
106+
Return is a UART object that can be used for read/write.
107+
"""
108+
109+
print("Connecting...")
110+
in_label.text = out_label.text = "Connecting..."
111+
112+
if MY_NAME == "CENTRAL":
113+
keep_scanning = True
114+
print("Scanning...")
115+
116+
while keep_scanning:
117+
for adv in ble.start_scan():
118+
if adv.complete_name == FRIENDS_NAME:
119+
ble.stop_scan()
120+
ble.connect(adv)
121+
keep_scanning = False
122+
123+
print("Connected. Done scanning.")
124+
return uart_service
125+
126+
else:
127+
print("Advertising...")
81128
ble.start_advertising(advertisement)
82129

83130
while not ble.connected:
84131
if ble.connected:
85132
break
86133

87-
# We're now connected, one way or the other
88-
print("Stopping advertising.")
134+
print("Connected. Stop advertising.")
89135
ble.stop_advertising()
90136

91-
print("Peripheral - connecting to their UART service.")
137+
print("Connecting to Central UART service.")
92138
for connection in ble.connections:
93139
if UARTService not in connection:
94140
continue
95141
return connection[UARTService]
96142

97-
else:
98-
print("Waiting.")
99-
friend = None
100-
while not ble.connected:
101-
if friend is None:
102-
print("Scanning.")
103-
in_label.text = out_label.text = "Scanning..."
104-
for adv in ble.start_scan():
105-
if ble.connected:
106-
# Friend connected with us, we're done
107-
ble.stop_scan()
108-
break
109-
if adv.complete_name == FRIENDS_NAME:
110-
# Found friend, can stop scanning
111-
ble.stop_scan()
112-
friend = adv
113-
print("Found", friend.complete_name)
114-
in_label.text = "Found {}".format(friend.complete_name)
115-
out_label.text = "A+B to connect"
116-
break
117-
else:
118-
if clue.button_a and clue.button_b:
119-
# Connect to friend
120-
print("Connecting to", friend.complete_name)
121-
ble.connect(friend)
122-
123-
124-
# Return a UART object to use
125-
print("Central - using my UART service.")
126-
return uart_service
127-
128-
#--------------------------
143+
return None
144+
145+
146+
# --------------------------
129147
# The main application loop
130-
#--------------------------
148+
# --------------------------
131149
while True:
132-
133150
# Establish initial connection
134151
uart = scan_and_connect()
135152

136153
print("Connected.")
137154

138-
code = ''
139-
in_label.text = out_label.text = ' '*18
140-
edit_label.text = ' '*4
155+
code = ""
156+
in_label.text = out_label.text = " " * 18
157+
edit_label.text = " " * 4
141158
done = False
142159

143160
# Run the chat while connected
144161
while ble.connected:
145-
146162
# Check for incoming message
147163
incoming_bytes = uart.in_waiting
148164
if incoming_bytes:
@@ -157,8 +173,8 @@ def scan_and_connect():
157173
if clue.button_b:
158174
done = True
159175
if not done and len(code) < 4:
160-
print('.', end='')
161-
code += '.'
176+
print(".", end="")
177+
code += "."
162178
edit_label.text = "{:4s}".format(code)
163179
time.sleep(DEBOUNCE)
164180

@@ -169,19 +185,19 @@ def scan_and_connect():
169185
if clue.button_a:
170186
done = True
171187
if not done and len(code) < 4:
172-
print('-', end='')
173-
code += '-'
188+
print("-", end="")
189+
code += "-"
174190
edit_label.text = "{:4s}".format(code)
175191
time.sleep(DEBOUNCE)
176192

177193
# Turn Morse Code into letter and send
178194
if done:
179-
letter = morse_code.get(code, ' ')
180-
print(' >', letter)
195+
letter = morse_code.get(code, " ")
196+
print(" >", letter)
181197
out_label.text = out_label.text[1:] + letter
182198
uart.write(str.encode(letter))
183-
code = ''
184-
edit_label.text = ' '*4
199+
code = ""
200+
edit_label.text = " " * 4
185201
done = False
186202
time.sleep(DEBOUNCE)
187203

0 commit comments

Comments
 (0)