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 = "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." )
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 .show (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.
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..." )
81
128
ble .start_advertising (advertisement )
82
129
83
130
while not ble .connected :
84
131
if ble .connected :
85
132
break
86
133
87
- # We're now connected, one way or the other
88
- print ("Stopping advertising." )
134
+ print ("Connected. Stop advertising." )
89
135
ble .stop_advertising ()
90
136
91
- print ("Peripheral - connecting to their UART service." )
137
+ print ("Connecting to Central UART service." )
92
138
for connection in ble .connections :
93
139
if UARTService not in connection :
94
140
continue
95
141
return connection [UARTService ]
96
142
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
+ # --------------------------
129
147
# The main application loop
130
- #--------------------------
148
+ # --------------------------
131
149
while True :
132
-
133
150
# Establish initial connection
134
151
uart = scan_and_connect ()
135
152
136
153
print ("Connected." )
137
154
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
141
158
done = False
142
159
143
160
# Run the chat while connected
144
161
while ble .connected :
145
-
146
162
# Check for incoming message
147
163
incoming_bytes = uart .in_waiting
148
164
if incoming_bytes :
@@ -157,8 +173,8 @@ def scan_and_connect():
157
173
if clue .button_b :
158
174
done = True
159
175
if not done and len (code ) < 4 :
160
- print ('.' , end = '' )
161
- code += '.'
176
+ print ("." , end = "" )
177
+ code += "."
162
178
edit_label .text = "{:4s}" .format (code )
163
179
time .sleep (DEBOUNCE )
164
180
@@ -169,19 +185,19 @@ def scan_and_connect():
169
185
if clue .button_a :
170
186
done = True
171
187
if not done and len (code ) < 4 :
172
- print ('-' , end = '' )
173
- code += '-'
188
+ print ("-" , end = "" )
189
+ code += "-"
174
190
edit_label .text = "{:4s}" .format (code )
175
191
time .sleep (DEBOUNCE )
176
192
177
193
# Turn Morse Code into letter and send
178
194
if done :
179
- letter = morse_code .get (code , ' ' )
180
- print (' >' , letter )
195
+ letter = morse_code .get (code , " " )
196
+ print (" >" , letter )
181
197
out_label .text = out_label .text [1 :] + letter
182
198
uart .write (str .encode (letter ))
183
- code = ''
184
- edit_label .text = ' ' * 4
199
+ code = ""
200
+ edit_label .text = " " * 4
185
201
done = False
186
202
time .sleep (DEBOUNCE )
187
203
0 commit comments