|
13 | 13 | from adafruit_ble.services.nordic import UARTService
|
14 | 14 |
|
15 | 15 | #--| User Config |---------------------------------------------------
|
16 |
| -MY_NAME = "ME" |
17 |
| -FRIENDS_NAME = "FRIEND" |
| 16 | +MY_NAME = "CENTRAL" |
| 17 | +FRIENDS_NAME = "PERIPHERAL" |
18 | 18 | #--| User Config |---------------------------------------------------
|
19 | 19 |
|
20 | 20 | WAIT_FOR_DOUBLE = 0.05
|
@@ -73,56 +73,61 @@ def scan_and_connect():
|
73 | 73 | Advertise self while scanning for friend. If friend is found, can
|
74 | 74 | connect by pressing A+B buttons. If friend connects first, then
|
75 | 75 | just stop.
|
76 |
| -
|
77 |
| - Return is a UART object that can be used for read/write. |
78 | 76 | '''
|
79 | 77 |
|
80 |
| - print("Advertising.") |
81 |
| - central = False |
82 |
| - ble.start_advertising(advertisement) |
83 |
| - |
84 |
| - print("Waiting.") |
85 |
| - friend = None |
86 |
| - while not ble.connected: |
87 |
| - |
88 |
| - if friend is None: |
89 |
| - print("Scanning.") |
90 |
| - in_label.text = out_label.text = "Scanning..." |
91 |
| - 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 |
| - if adv.complete_name == FRIENDS_NAME: |
97 |
| - # Found friend, can stop scanning |
98 |
| - 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() |
| 78 | +# peripheral |
| 79 | + if MY_NAME == "PERIPHERAL": |
| 80 | + print("Advertising.") |
| 81 | + central = False |
| 82 | + ble.start_advertising(advertisement) |
| 83 | + |
| 84 | + while not ble.connected: |
| 85 | + if ble.connected: |
| 86 | + break |
| 87 | + |
| 88 | + # We're now connected, one way or the other |
| 89 | + print("Stopping advertising.") |
| 90 | + ble.stop_advertising() |
114 | 91 |
|
115 |
| - # Return a UART object to use |
116 |
| - if central: |
117 |
| - print("Central - using my UART service.") |
118 |
| - return uart_service |
119 |
| - else: |
120 | 92 | print("Peripheral - connecting to their UART service.")
|
121 | 93 | for connection in ble.connections:
|
122 | 94 | if UARTService not in connection:
|
123 | 95 | continue
|
124 | 96 | return connection[UARTService]
|
125 | 97 |
|
| 98 | + # central |
| 99 | + else: |
| 100 | + print("Waiting.") |
| 101 | + friend = None |
| 102 | + while not ble.connected: |
| 103 | + if friend is None: |
| 104 | + print("Scanning.") |
| 105 | + in_label.text = out_label.text = "Scanning..." |
| 106 | + for adv in ble.start_scan(): |
| 107 | + if ble.connected: |
| 108 | + # Friend connected with us, we're done |
| 109 | + ble.stop_scan() |
| 110 | + break |
| 111 | + if adv.complete_name == FRIENDS_NAME: |
| 112 | + # Found friend, can stop scanning |
| 113 | + ble.stop_scan() |
| 114 | + friend = adv |
| 115 | + print("Found", friend.complete_name) |
| 116 | + in_label.text = "Found {}".format(friend.complete_name) |
| 117 | + out_label.text = "A+B to connect" |
| 118 | + break |
| 119 | + else: |
| 120 | + if clue.button_a and clue.button_b: |
| 121 | + # Connect to friend |
| 122 | + print("Connecting to", friend.complete_name) |
| 123 | + ble.connect(friend) |
| 124 | + central = True |
| 125 | + |
| 126 | + |
| 127 | + # Return a UART object to use |
| 128 | + print("Central - using my UART service.") |
| 129 | + return uart_service |
| 130 | + |
126 | 131 | #--------------------------
|
127 | 132 | # The main application loop
|
128 | 133 | #--------------------------
|
|
0 commit comments