2323 * - https://www.bluetooth.com/blog/bluetooth-pairing-passkey-entry/
2424 * - https://www.bluetooth.com/blog/bluetooth-pairing-part-4/
2525 *
26- * IF TFT enabled board such as CLUE is used, the passkey will also display on the
27- * TFT. Following boards with TFT are supported
28- * - Adafruit CLUE : https://www.adafruit.com/product/4500
29- * - Circuit Playground Bluefruit: https://www.adafruit.com/product/4333
30- * - TFT Gizmo : https://www.adafruit.com/product/4367
26+ * This example will "display" (print) passkey to Serial, and get digital input for yes/no
27+ * therefore the IO Capacities is set to Display = true, Yes/No = true, Keypad = false.
28+ *
29+ * Note For TFT enabled board such as CLUE, Circuit Play Bluefruit with Gizmo please use the `pairing_passkey_arcada` example.
30+ * Where the passkey is displayed on the TFT for better experience.
3131 */
3232
33- #if defined(ARDUINO_NRF52840_CIRCUITPLAY) || defined(ARDUINO_NRF52840_CLUE)
34- #define USE_ARCADA
35- #endif
36-
3733#include < bluefruit.h>
3834#include < Adafruit_LittleFS.h>
3935#include < InternalFileSystem.h>
4036
41- #ifdef USE_ARCADA
42- #include < Adafruit_Arcada.h>
37+ #include < Adafruit_TinyUSB.h>
4338
44- Adafruit_Arcada arcada;
45- Adafruit_SPITFT* tft;
39+ // Use built-in buttons if available, else use A0, A1
40+ #ifdef PIN_BUTTON1
41+ #define BUTTON_YES PIN_BUTTON1
42+ #else
43+ #define BUTTON_YES A0
44+ #endif
4645
46+ #ifdef PIN_BUTTON2
47+ #define BUTTON_NO PIN_BUTTON2
4748#else
48- // Use built-in buttons if available, else use A0, A1
49- #ifdef PIN_BUTTON1
50- #define BUTTON_YES PIN_BUTTON1
51- #else
52- #define BUTTON_YES A0
53- #endif
54-
55- #ifdef PIN_BUTTON2
56- #define BUTTON_NO PIN_BUTTON2
57- #else
58- #define BUTTON_NO A1
59- #endif
49+ #define BUTTON_NO A1
50+ #endif
51+
52+ // Circuit Play Bluefruit has button active state = high
53+ #ifdef ARDUINO_NRF52840_CIRCUITPLAY
54+ uint32_t const button_active_state = HIGH;
55+ #else
56+ uint32_t const button_active_state = LOW;
6057#endif
6158
6259// BLE Service
@@ -69,19 +66,16 @@ void setup()
6966 Serial.println (" Bluefruit52 Pairing Display Example" );
7067 Serial.println (" -----------------------------------\n " );
7168
72- #ifdef USE_ARCADA
73- arcada.arcadaBegin ();
74- arcada.displayBegin ();
75- arcada.setBacklight (255 );
76-
77- tft = arcada.display ;
78- tft->setCursor (0 , 0 );
79- tft->setTextWrap (true );
80- tft->setTextSize (2 );
81- #else
82- pinMode (BUTTON_YES, INPUT_PULLUP);
83- pinMode (BUTTON_NO, INPUT_PULLUP);
84- #endif
69+ // pull high for active low, or pull low for active high
70+ if (button_active_state == HIGH)
71+ {
72+ pinMode (BUTTON_YES, INPUT_PULLDOWN);
73+ pinMode (BUTTON_NO, INPUT_PULLDOWN);
74+ }else
75+ {
76+ pinMode (BUTTON_YES, INPUT_PULLUP);
77+ pinMode (BUTTON_NO, INPUT_PULLUP);
78+ }
8579
8680 // Setup the BLE LED to be enabled on CONNECT
8781 // Note: This is actually the default behavior, but provided
@@ -125,14 +119,6 @@ void setup()
125119 bleuart.setPermission (SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM);
126120 bleuart.begin ();
127121
128- #ifdef USE_ARCADA
129- tft->fillScreen (ARCADA_BLACK);
130- tft->setTextColor (ARCADA_WHITE);
131- tft->setTextSize (2 );
132- tft->setCursor (0 , 0 );
133- tft->print (" Advertising..." );
134- #endif
135-
136122 Serial.println (" Please use Adafruit's Bluefruit LE app to connect in UART mode" );
137123 Serial.println (" Your phone should pop-up PIN input" );
138124 Serial.println (" Once connected, enter character(s) that you wish to send" );
@@ -203,13 +189,6 @@ void connect_callback(uint16_t conn_handle)
203189
204190 Serial.print (" Connected to " );
205191 Serial.println (central_name);
206-
207- #ifdef USE_ARCADA
208- tft->fillScreen (ARCADA_BLACK);
209- tft->setTextSize (2 );
210- tft->setCursor (0 , 0 );
211- tft->println (" Connected" );
212- #endif
213192}
214193
215194// callback invoked when pairing passkey is generated
@@ -222,84 +201,48 @@ bool pairing_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bo
222201 Serial.println (" Pairing Passkey" );
223202 Serial.printf (" %.3s %.3s\n " , passkey, passkey+3 );
224203
225- #ifdef USE_ARCADA
226- tft->fillScreen (ARCADA_BLACK);
227- tft->println (" Pairing Passkey\n " );
228- tft->setTextColor (ARCADA_YELLOW);
229- tft->setTextSize (4 );
230- tft->printf (" %.3s %.3s\n " , passkey, passkey+3 );
231-
232- tft->setTextColor (ARCADA_WHITE);
233- tft->setTextSize (2 );
234- #endif
235-
236- // match_request means peer wait for our approval (return true)
204+ // match_request means peer wait for our approval
205+ // return true to accept, false to decline
237206 if (match_request)
238207 {
208+ bool accept_pairing = false ;
209+
239210 Serial.println (" Do you want to pair" );
240- Serial.println (" Press Button Left to decline, Button Right to Accept" );
211+ Serial.printf (" Press button <%u> to Decline, button <%u> to Accept\n " , BUTTON_YES, BUTTON_NO );
241212
242213 // timeout for pressing button
243214 uint32_t start_time = millis ();
244215
245- #ifdef USE_ARCADA
246- tft->println (" \n Do you accept ?\n\n " );
247- tft->setTextSize (3 );
248-
249- // Yes <-> No on CPB is reversed since GIZMO TFT is on the back of CPB
250- #if ARDUINO_NRF52840_CIRCUITPLAY
251- tft->setTextColor (ARCADA_GREEN);
252- tft->print (" < Yes" );
253- tft->setTextColor (ARCADA_RED);
254- tft->println (" No >" );
255- #else
256- tft->setTextColor (ARCADA_RED);
257- tft->print (" < No" );
258- tft->setTextColor (ARCADA_GREEN);
259- tft->println (" Yes >" );
260- #endif
261-
262- tft->setTextColor (ARCADA_WHITE);
263- tft->setTextSize (2 );
264- tft->println ();
265-
266216 // wait until either button is pressed (30 seconds timeout)
267- uint32_t justReleased;
268- do
217+ while ( millis () < start_time + 30000 )
269218 {
270- // Peer is disconnected while waiting for input
271- if ( !Bluefruit.connected (conn_handle) ) break ;
219+ // user press YES
220+ if (digitalRead (BUTTON_YES) == button_active_state)
221+ {
222+ accept_pairing = true ;
223+ break ;
224+ }
225+
226+ // user press NO
227+ if (digitalRead (BUTTON_NO) == button_active_state)
228+ {
229+ accept_pairing = false ;
230+ break ;
231+ }
272232
273- // time out
274- if ( millis () > start_time + 30000 ) break ;
275-
276- arcada.readButtons ();
277- justReleased = arcada.justReleasedButtons ();
278- } while ( !(justReleased & (ARCADA_BUTTONMASK_LEFT | ARCADA_BUTTONMASK_RIGHT) ) );
279-
280- // Right = accept
281- if (justReleased & ARCADA_BUTTONMASK_RIGHT) return true ;
282-
283- // Left = decline
284- if (justReleased & ARCADA_BUTTONMASK_LEFT) return false ;
285-
286- #else
287- // wait until either button is pressed (30 seconds timeout)
288- while ( digitalRead (BUTTON_YES) && digitalRead (BUTTON_NO) )
289- {
290233 // Peer is disconnected while waiting for input
291234 if ( !Bluefruit.connected (conn_handle) ) break ;
292-
293- // time out
294- if ( millis () > start_time + 30000 ) break ;
295235 }
296236
297- if ( 0 == digitalRead (BUTTON_YES) ) return true ;
298-
299- if ( 0 == digitalRead (BUTTON_NO) ) return false ;
300- #endif
237+ if (accept_pairing)
238+ {
239+ Serial.println (" Accepted" );
240+ }else
241+ {
242+ Serial.println (" Declined" );
243+ }
301244
302- return false ;
245+ return accept_pairing ;
303246 }
304247
305248 return true ;
@@ -314,32 +257,11 @@ void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
314257 {
315258 Serial.println (" Failed" );
316259 }
317-
318- #ifdef USE_ARCADA
319- if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
320- {
321- tft->setTextColor (ARCADA_GREEN);
322- tft->println (" Succeeded" );
323- }else
324- {
325- tft->setTextColor (ARCADA_RED);
326- tft->println (" Failed" );
327- }
328-
329- tft->setTextColor (ARCADA_WHITE);
330- tft->setTextSize (2 );
331- #endif
332260}
333261
334262void connection_secured_callback (uint16_t conn_handle)
335263{
336264 Serial.println (" Secured" );
337-
338- #ifdef USE_ARCADA
339- tft->setTextColor (ARCADA_YELLOW);
340- tft->println (" secured" );
341- tft->setTextColor (ARCADA_WHITE);
342- #endif
343265}
344266
345267/* *
@@ -354,11 +276,4 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
354276
355277 Serial.println ();
356278 Serial.print (" Disconnected, reason = 0x" ); Serial.println (reason, HEX);
357-
358- #ifdef USE_ARCADA
359- tft->fillScreen (ARCADA_BLACK);
360- tft->setTextSize (2 );
361- tft->setCursor (0 , 0 );
362- tft->println (" Advertising ..." );
363- #endif
364279}
0 commit comments