1212from adafruit_ble .advertising .standard import ProvideServicesAdvertisement
1313from adafruit_ble .services .nordic import UARTService
1414
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." )
1923
2024WAIT_FOR_DOUBLE = 0.05
2125DEBOUNCE = 0.25
2226
2327# Define Morse Code dictionary
2428morse_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"
3464ble = BLERadio ()
3565uart_service = UARTService ()
3666advertisement = 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
4070display = clue .display
4171disp_group = displayio .Group ()
4272display .root_group = 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+ )
4878disp_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 )
5584disp_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 )
6290disp_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 )
6794edit_label .anchor_point = (0.5 , 0 )
68- edit_label .anchored_position = (105 , 222 )
95+ edit_label .anchored_position = (115 , 212 )
6996disp_group .append (edit_label )
7097
98+
7199def 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.
76105
77106 Return is a UART object that can be used for read/write.
78- '''
107+ """
79108
80- print ("Advertising." )
81- central = False
82- ble .start_advertising (advertisement )
109+ print ("Connecting..." )
110+ in_label .text = out_label .text = "Connecting..."
83111
84- print ( "Waiting." )
85- friend = None
86- while not ble . connected :
112+ if MY_NAME == "CENTRAL" :
113+ keep_scanning = True
114+ print ( "Scanning..." )
87115
88- if friend is None :
89- print ("Scanning." )
90- in_label .text = out_label .text = "Scanning..."
116+ while keep_scanning :
91117 for adv in ble .start_scan ():
92- if ble .connected :
93- # Friend connected with us, we're done
94- ble .stop_scan ()
95- break
96118 if adv .complete_name == FRIENDS_NAME :
97- # Found friend, can stop scanning
98119 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." )
118124 return uart_service
125+
119126 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." )
121138 for connection in ble .connections :
122139 if UARTService not in connection :
123140 continue
124141 return connection [UARTService ]
125142
126- #--------------------------
143+ return None
144+
145+
146+ # --------------------------
127147# The main application loop
128- #--------------------------
148+ # --------------------------
129149while True :
130-
131150 # Establish initial connection
132151 uart = scan_and_connect ()
133152
134153 print ("Connected." )
135154
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
139158 done = False
140159
141160 # Run the chat while connected
142161 while ble .connected :
143-
144162 # Check for incoming message
145163 incoming_bytes = uart .in_waiting
146164 if incoming_bytes :
@@ -155,8 +173,8 @@ def scan_and_connect():
155173 if clue .button_b :
156174 done = True
157175 if not done and len (code ) < 4 :
158- print ('.' , end = '' )
159- code += '.'
176+ print ("." , end = "" )
177+ code += "."
160178 edit_label .text = "{:4s}" .format (code )
161179 time .sleep (DEBOUNCE )
162180
@@ -167,19 +185,19 @@ def scan_and_connect():
167185 if clue .button_a :
168186 done = True
169187 if not done and len (code ) < 4 :
170- print ('-' , end = '' )
171- code += '-'
188+ print ("-" , end = "" )
189+ code += "-"
172190 edit_label .text = "{:4s}" .format (code )
173191 time .sleep (DEBOUNCE )
174192
175193 # Turn Morse Code into letter and send
176194 if done :
177- letter = morse_code .get (code , ' ' )
178- print (' >' , letter )
195+ letter = morse_code .get (code , " " )
196+ print (" >" , letter )
179197 out_label .text = out_label .text [1 :] + letter
180198 uart .write (str .encode (letter ))
181- code = ''
182- edit_label .text = ' ' * 4
199+ code = ""
200+ edit_label .text = " " * 4
183201 done = False
184202 time .sleep (DEBOUNCE )
185203
0 commit comments