16
16
#define USE_ARCADA
17
17
#endif
18
18
19
- #ifdef USE_ARCADA
20
- #include < Adafruit_Arcada.h>
21
-
22
- Adafruit_Arcada arcada;
23
- Adafruit_SPITFT* tft;
24
- #endif
25
-
26
- #include < SPI.h>
27
- #include < Adafruit_GFX.h>
28
19
#include < bluefruit.h>
29
20
#include < Adafruit_LittleFS.h>
30
21
#include < InternalFileSystem.h>
31
22
#include < Adafruit_nRFCrypto.h>
32
23
24
+ #ifdef USE_ARCADA
25
+ #include < Adafruit_Arcada.h>
26
+
27
+ Adafruit_Arcada arcada;
28
+ Adafruit_SPITFT* tft;
29
+
30
+ #else
31
+ // Use built-in buttons if available, else use A0, A1
32
+ #ifdef PIN_BUTTON1
33
+ #define BUTTON_YES PIN_BUTTON1
34
+ #else
35
+ #define BUTTON_YES A0
36
+ #endif
37
+
38
+ #ifdef PIN_BUTTON2
39
+ #define BUTTON_NO PIN_BUTTON2
40
+ #else
41
+ #define BUTTON_NO A1
42
+ #endif
43
+ #endif
44
+
45
+
33
46
/* This sketch demonstrates Pairing process using dynamic Passkey.
34
47
* This sketch is essentially the same as bleuart.ino except the BLE Uart
35
48
* service requires Security Mode with Man-In-The-Middle protection i.e
@@ -59,6 +72,7 @@ void setup()
59
72
Serial.println (" Bluefruit52 Pairing Display Example" );
60
73
Serial.println (" -----------------------------------\n " );
61
74
75
+ #ifdef USE_ARCADA
62
76
arcada.arcadaBegin ();
63
77
arcada.displayBegin ();
64
78
arcada.setBacklight (255 );
@@ -67,6 +81,10 @@ void setup()
67
81
tft->setCursor (0 , 0 );
68
82
tft->setTextWrap (true );
69
83
tft->setTextSize (2 );
84
+ #else
85
+ pinMode (BUTTON_YES, INPUT_PULLUP);
86
+ pinMode (BUTTON_NO, INPUT_PULLUP);
87
+ #endif
70
88
71
89
// Setup the BLE LED to be enabled on CONNECT
72
90
// Note: This is actually the default behavior, but provided
@@ -82,19 +100,23 @@ void setup()
82
100
Bluefruit.setTxPower (4 ); // Check bluefruit.h for supported values
83
101
84
102
// clear bonds if BUTTON A is pressed
85
- Serial.println (" Hold button A to clear bonds ..... " );
86
- delay (2000 );
87
- if (0 == digitalRead (PIN_BUTTON1))
88
- {
89
- Serial.println (" Clear all central bonds" );
90
- Bluefruit.Periph .clearBonds ();
91
- }
92
-
93
- // To use dynamic PassKey for pairing, we need to have
94
- // - IO capacities at least DISPPLAY
95
- // - Register callback to display/print dynamic passkey for central
96
- // For complete mapping of the IO Capabilities to Key Generation Method, check out this article
97
- // https://www.bluetooth.com/blog/bluetooth-pairing-part-2-key-generation-methods/
103
+ // Serial.println("Hold button A to clear bonds ..... ");
104
+ // delay(2000);
105
+ // if (0 == digitalRead(PIN_BUTTON1))
106
+ // {
107
+ // Serial.println("Clear all central bonds");
108
+ // Bluefruit.Periph.clearBonds();
109
+ // }
110
+
111
+ /* To use dynamic PassKey for pairing, we need to have
112
+ * - IO capacities at least DISPPLAY
113
+ * - Display only: user have to enter 6-digit passkey on their phone
114
+ * - DIsplay + Yes/No: user ony need to press Accept on both central and device
115
+ * - Register callback to display/print dynamic passkey for central
116
+ *
117
+ * For complete mapping of the IO Capabilities to Key Generation Method, check out this article
118
+ * https://www.bluetooth.com/blog/bluetooth-pairing-part-2-key-generation-methods/
119
+ */
98
120
Bluefruit.Pairing .setIOCaps (true , true , false ); // display = true, yes/no = true, keyboard = false
99
121
Bluefruit.Pairing .setPasskeyCallback (pairing_passkey_callback);
100
122
@@ -222,42 +244,63 @@ bool pairing_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bo
222
244
tft->setTextSize (2 );
223
245
#endif
224
246
225
-
247
+ // match_request means peer wait for our approval (return true)
226
248
if (match_request)
227
249
{
228
250
Serial.println (" Do you want to pair" );
229
- Serial.println (" Press Button A to accept , Button B to reject " );
251
+ Serial.println (" Press Button Left to decline , Button Right to Accept " );
230
252
231
- # ifdef USE_ARCADA
232
- tft-> println ( " \n Do you accept ? \n\n " );
253
+ // timeout for pressing button
254
+ uint32_t start_time = millis ( );
233
255
256
+ #ifdef USE_ARCADA
257
+ tft->println (" \n Do you accept ?\n\n " );
234
258
tft->setTextSize (3 );
235
- tft->setTextColor (ARCADA_GREEN);
236
- tft->print (" << Yes" );
237
- tft->setTextColor (ARCADA_RED);
238
- tft->println (" No >>" );
259
+
260
+ // Yes <-> No on CPB is reversed since GIZMO TFT is on the back of CPB
261
+ #if ARDUINO_NRF52840_CIRCUITPLAY
262
+ tft->setTextColor (ARCADA_GREEN);
263
+ tft->print (" < Yes" );
264
+ tft->setTextColor (ARCADA_RED);
265
+ tft->println (" No >" );
266
+ #else
267
+ tft->setTextColor (ARCADA_RED);
268
+ tft->print (" < No" );
269
+ tft->setTextColor (ARCADA_GREEN);
270
+ tft->println (" Yes >" );
271
+ #endif
239
272
240
273
tft->setTextColor (ARCADA_WHITE);
241
274
tft->setTextSize (2 );
242
275
tft->println ();
243
- #endif
244
276
245
- // wait until either button is pressed
246
- while ( digitalRead (PIN_BUTTON1) && digitalRead (PIN_BUTTON2) ) { }
277
+ // wait until either button is pressed (30 seconds timeout)
278
+ uint32_t justReleased;
279
+ do
280
+ {
281
+ if ( millis () > start_time + 30000 ) break ;
247
282
248
- // wait until either button is pressed
249
- uint32_t start_time = millis ();
250
- while ( digitalRead (PIN_BUTTON1) && digitalRead (PIN_BUTTON2) )
283
+ arcada.readButtons ();
284
+ justReleased = arcada.justReleasedButtons ();
285
+ } while ( !(justReleased & (ARCADA_BUTTONMASK_LEFT | ARCADA_BUTTONMASK_RIGHT) ) );
286
+
287
+ // Right = accept
288
+ if (justReleased & ARCADA_BUTTONMASK_RIGHT) return true ;
289
+
290
+ // Left = decline
291
+ if (justReleased & ARCADA_BUTTONMASK_LEFT) return false ;
292
+
293
+ #else
294
+ // wait until either button is pressed (30 seconds timeout)
295
+ while ( digitalRead (BUTTON_YES) && digitalRead (BUTTON_NO) )
251
296
{
252
- // 30 seconds timeout
253
297
if ( millis () > start_time + 30000 ) break ;
254
298
}
255
299
256
- // A = accept
257
- if ( 0 == digitalRead (PIN_BUTTON1) ) return true ;
300
+ if ( 0 == digitalRead (BUTTON_YES) ) return true ;
258
301
259
- // B = reject
260
- if ( 0 == digitalRead (PIN_BUTTON2) ) return false ;
302
+ if ( 0 == digitalRead (BUTTON_NO) ) return false ;
303
+ # endif
261
304
262
305
return false ;
263
306
}
@@ -275,7 +318,7 @@ void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
275
318
Serial.println (" Failed" );
276
319
}
277
320
278
- #ifdef USE_ARCADA
321
+ #ifdef USE_ARCADA
279
322
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
280
323
{
281
324
tft->setTextColor (ARCADA_GREEN);
@@ -288,18 +331,18 @@ void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
288
331
289
332
tft->setTextColor (ARCADA_WHITE);
290
333
tft->setTextSize (2 );
291
- #endif
334
+ #endif
292
335
}
293
336
294
337
void connection_secured_callback (uint16_t conn_handle)
295
338
{
296
339
Serial.println (" Secured" );
297
340
298
- #ifdef USE_ARCADA
341
+ #ifdef USE_ARCADA
299
342
tft->setTextColor (ARCADA_YELLOW);
300
343
tft->println (" secured" );
301
344
tft->setTextColor (ARCADA_WHITE);
302
- #endif
345
+ #endif
303
346
}
304
347
305
348
/* *
0 commit comments