12
12
from adafruit_ble .advertising .standard import ProvideServicesAdvertisement
13
13
from adafruit_ble .services .nordic import UARTService
14
14
15
- #--| User Config |---------------------------------------------------
16
- MY_NAME = "ME"
17
- FRIENDS_NAME = "FRIEND"
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." )
19
23
20
24
WAIT_FOR_DOUBLE = 0.05
21
25
DEBOUNCE = 0.25
22
26
23
27
# Define Morse Code dictionary
24
28
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" ,
31
55
}
32
56
33
57
# 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"
34
64
ble = BLERadio ()
35
65
uart_service = UARTService ()
36
66
advertisement = ProvideServicesAdvertisement (uart_service )
37
- ble ._adapter .name = MY_NAME # pylint: disable=protected-access
67
+ ble ._adapter .name = MY_NAME # pylint: disable=protected-access
38
68
39
69
# Display Stuff
40
70
display = clue .display
41
71
disp_group = displayio .Group ()
42
72
display .root_group = disp_group
43
73
44
74
# 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
+ )
48
78
disp_group .append (displayio .TileGrid (bmp , pixel_shader = pal ))
49
79
50
80
# 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 )
55
84
disp_group .append (in_label )
56
85
57
86
# 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 )
62
90
disp_group .append (out_label )
63
91
64
92
# 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 )
67
94
edit_label .anchor_point = (0.5 , 0 )
68
- edit_label .anchored_position = (105 , 222 )
95
+ edit_label .anchored_position = (115 , 212 )
69
96
disp_group .append (edit_label )
70
97
98
+
71
99
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.
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.
76
105
77
106
Return is a UART object that can be used for read/write.
78
- '''
107
+ """
79
108
80
- print ("Advertising." )
81
- central = False
82
- ble .start_advertising (advertisement )
109
+ print ("Connecting..." )
110
+ in_label .text = out_label .text = "Connecting..."
83
111
84
- print ( "Waiting." )
85
- friend = None
86
- while not ble . connected :
112
+ if MY_NAME == "CENTRAL" :
113
+ keep_scanning = True
114
+ print ( "Scanning..." )
87
115
88
- if friend is None :
89
- print ("Scanning." )
90
- in_label .text = out_label .text = "Scanning..."
116
+ while keep_scanning :
91
117
for adv in ble .start_scan ():
92
- if ble .connected :
93
- # Friend connected with us, we're done
94
- ble .stop_scan ()
95
- break
96
118
if adv .complete_name == FRIENDS_NAME :
97
- # Found friend, can stop scanning
98
119
ble .stop_scan ()
99
- friend = adv
100
- print ("Found" , friend .complete_name )
101
- in_label .text = "Found {}" .format (friend .complete_name )
102
- out_label .text = "A+B to connect"
103
- break
104
- else :
105
- if clue .button_a and clue .button_b :
106
- # Connect to friend
107
- print ("Connecting to" , friend .complete_name )
108
- ble .connect (friend )
109
- central = True
110
-
111
- # We're now connected, one way or the other
112
- print ("Stopping advertising." )
113
- ble .stop_advertising ()
114
-
115
- # Return a UART object to use
116
- if central :
117
- print ("Central - using my UART service." )
120
+ ble .connect (adv )
121
+ keep_scanning = False
122
+
123
+ print ("Connected. Done scanning." )
118
124
return uart_service
125
+
119
126
else :
120
- print ("Peripheral - connecting to their UART service." )
127
+ print ("Advertising..." )
128
+ ble .start_advertising (advertisement )
129
+
130
+ while not ble .connected :
131
+ if ble .connected :
132
+ break
133
+
134
+ print ("Connected. Stop advertising." )
135
+ ble .stop_advertising ()
136
+
137
+ print ("Connecting to Central UART service." )
121
138
for connection in ble .connections :
122
139
if UARTService not in connection :
123
140
continue
124
141
return connection [UARTService ]
125
142
126
- #--------------------------
143
+ return None
144
+
145
+
146
+ # --------------------------
127
147
# The main application loop
128
- #--------------------------
148
+ # --------------------------
129
149
while True :
130
-
131
150
# Establish initial connection
132
151
uart = scan_and_connect ()
133
152
134
153
print ("Connected." )
135
154
136
- code = ''
137
- in_label .text = out_label .text = ' ' * 18
138
- edit_label .text = ' ' * 4
155
+ code = ""
156
+ in_label .text = out_label .text = " " * 18
157
+ edit_label .text = " " * 4
139
158
done = False
140
159
141
160
# Run the chat while connected
142
161
while ble .connected :
143
-
144
162
# Check for incoming message
145
163
incoming_bytes = uart .in_waiting
146
164
if incoming_bytes :
@@ -155,8 +173,8 @@ def scan_and_connect():
155
173
if clue .button_b :
156
174
done = True
157
175
if not done and len (code ) < 4 :
158
- print ('.' , end = '' )
159
- code += '.'
176
+ print ("." , end = "" )
177
+ code += "."
160
178
edit_label .text = "{:4s}" .format (code )
161
179
time .sleep (DEBOUNCE )
162
180
@@ -167,19 +185,19 @@ def scan_and_connect():
167
185
if clue .button_a :
168
186
done = True
169
187
if not done and len (code ) < 4 :
170
- print ('-' , end = '' )
171
- code += '-'
188
+ print ("-" , end = "" )
189
+ code += "-"
172
190
edit_label .text = "{:4s}" .format (code )
173
191
time .sleep (DEBOUNCE )
174
192
175
193
# Turn Morse Code into letter and send
176
194
if done :
177
- letter = morse_code .get (code , ' ' )
178
- print (' >' , letter )
195
+ letter = morse_code .get (code , " " )
196
+ print (" >" , letter )
179
197
out_label .text = out_label .text [1 :] + letter
180
198
uart .write (str .encode (letter ))
181
- code = ''
182
- edit_label .text = ' ' * 4
199
+ code = ""
200
+ edit_label .text = " " * 4
183
201
done = False
184
202
time .sleep (DEBOUNCE )
185
203
0 commit comments