15
15
#include < bluefruit.h>
16
16
17
17
/* This sketch demonstrates Pairing process using dynamic Passkey.
18
- * This sketch is essentially the same as bleuart .ino except the BLE Uart
18
+ * This sketch is essentially the same as central_bleuart .ino except the BLE Uart
19
19
* service requires Security Mode with Man-In-The-Middle protection i.e
20
20
*
21
21
* BLE Pairing procedure is complicated, it is advisable for users to go through
24
24
* - https://www.bluetooth.com/blog/bluetooth-pairing-part-2-key-generation-methods/
25
25
* - https://www.bluetooth.com/blog/bluetooth-pairing-passkey-entry/
26
26
* - https://www.bluetooth.com/blog/bluetooth-pairing-part-4/
27
+ *
28
+ * IF TFT enabled board such as CLUE is used, the passkey will also display on the
29
+ * tft-> Following boards with TFT are supported
30
+ * - Adafruit CLUE : https://www.adafruit.com/product/4500
31
+ * - Circuit Playground Bluefruit: https://www.adafruit.com/product/4333
32
+ * - TFT Gizmo : https://www.adafruit.com/product/4367
27
33
*/
28
34
29
- #define TFT_NO_DISPLAY 0
30
- #define TFT_GIZMO 1 // used with Circuit Playground Bluefruit
31
- #define TFT_CLUE 2 // CLUE's on-board display
32
- #define TFT_24_FEATHERWING 3
33
- #define TFT_35_FEATHERWING 4
34
-
35
-
36
- #if defined(ARDUINO_NRF52840_CIRCUITPLAY)
37
- // Circuit Playground Bluefruit use with TFT GIZMO
38
- #define TFT_IN_USE TFT_GIZMO
39
- #define DEVICE_NAME " CPLAY"
35
+ #if defined(ARDUINO_NRF52840_CIRCUITPLAY) || defined(ARDUINO_NRF52840_CLUE)
36
+ #define USE_ARCADA
37
+ #endif
40
38
41
- #include " Adafruit_ST7789.h"
42
- Adafruit_ST7789 tft = Adafruit_ST7789(&SPI, 0 , 1 , -1 ); // CS = 0, DC = 1
39
+ #include < bluefruit.h>
40
+ #include < Adafruit_LittleFS.h>
41
+ #include < InternalFileSystem.h>
42
+ #include < Adafruit_nRFCrypto.h>
43
43
44
- #elif defined(ARDUINO_NRF52840_CLUE)
45
- // CLUE use on-board TFT
46
- #define TFT_IN_USE TFT_CLUE
47
- #define DEVICE_NAME " CLUE"
44
+ #ifdef USE_ARCADA
45
+ #include < Adafruit_Arcada.h>
48
46
49
- # include " Adafruit_ST7789.h "
50
- Adafruit_ST7789 tft = Adafruit_ST7789(&SPI1, PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RST) ;
47
+ Adafruit_Arcada arcada;
48
+ Adafruit_SPITFT* tft;
51
49
52
50
#else
53
- // [Configurable] For other boards please select which external display to match your hardware setup
54
- #define TFT_IN_USE TFT_24_FEATHERWING
55
- #define DEVICE_NAME " Feather"
56
-
57
- #if defined(ARDUINO_NRF52832_FEATHER)
58
- // Feather nRF52832 pin map is different from others
59
- #define TFT_DC 11
60
- #define TFT_CS 31
51
+ // Use built-in buttons if available, else use A0, A1
52
+ #ifdef PIN_BUTTON1
53
+ #define BUTTON_YES PIN_BUTTON1
61
54
#else
62
- // Default for others
63
- #define TFT_DC 10
64
- #define TFT_CS 9
55
+ #define BUTTON_YES A0
65
56
#endif
66
57
67
- #if TFT_IN_USE == TFT_35_FEATHERWING
68
- #include " Adafruit_HX8357.h"
69
- Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC);
70
-
71
- #elif TFT_IN_USE == TFT_24_FEATHERWING
72
- #include < Adafruit_ILI9341.h>
73
- Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
74
- #endif // TFT
75
-
76
- #endif // board variants
77
-
78
-
79
- #define COLOR_WHITE 0xFFFF
80
- #define COLOR_BLACK 0x0000
81
- #define COLOR_YELLOW 0xFFE0
82
- #define COLOR_GREEN 0x07E0
83
- #define COLOR_RED 0xF800
58
+ #ifdef PIN_BUTTON2
59
+ #define BUTTON_NO PIN_BUTTON2
60
+ #else
61
+ #define BUTTON_NO A1
62
+ #endif
63
+ #endif
84
64
85
65
BLEClientUart clientUart; // bleuart client
86
66
@@ -91,43 +71,27 @@ void setup()
91
71
Serial.println (" Bluefruit52 Central Pairing Example" );
92
72
Serial.println (" -----------------------------------\n " );
93
73
94
- #if TFT_IN_USE == TFT_CLUE
95
- tft.init (240 , 240 );
96
- tft.setRotation (1 );
97
-
98
- // Screen refresh rate control (datasheet 9.2.18, FRCTRL2)
99
- uint8_t rtna = 0x01 ;
100
- tft.sendCommand (0xC6 , &rtna, 1 );;
101
-
102
- // turn back light on
103
- uint8_t backlight = PIN_TFT_LITE;
104
- pinMode (backlight, OUTPUT);
105
- digitalWrite (backlight, HIGH);
106
-
107
- #elif TFT_IN_USE == TFT_GIZMO
108
- tft.init (240 , 240 );
109
- tft.setRotation (2 );
110
-
111
- // turn back light on
112
- uint8_t backlight = A3;
113
- pinMode (backlight, OUTPUT);
114
- digitalWrite (backlight, HIGH);
115
-
116
- #elif TFT_IN_USE != TFT_NO_DISPLAY
117
- tft.begin ();
118
-
119
- #endif // TFT
120
-
121
- pinMode (PIN_BUTTON1, INPUT_PULLUP);
122
- pinMode (PIN_BUTTON2, INPUT_PULLUP);
74
+ #ifdef USE_ARCADA
75
+ arcada.arcadaBegin ();
76
+ arcada.displayBegin ();
77
+ arcada.setBacklight (255 );
78
+
79
+ tft = arcada.display ;
80
+ tft->setCursor (0 , 0 );
81
+ tft->setTextWrap (true );
82
+ tft->setTextSize (2 );
83
+ #else
84
+ pinMode (BUTTON_YES, INPUT_PULLUP);
85
+ pinMode (BUTTON_NO, INPUT_PULLUP);
86
+ #endif
123
87
124
88
// Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
125
89
// SRAM usage required by SoftDevice will increase dramatically with number of connections
126
90
Bluefruit.begin (0 , 1 );
127
- Bluefruit.setName (" Bluefruit52 Central" );
128
-
91
+
129
92
// clear bonds if BUTTON A is pressed
130
93
Serial.println (" Hold button A to clear bonds ..... " );
94
+ Serial.flush ();
131
95
delay (2000 );
132
96
if (0 == digitalRead (PIN_BUTTON1))
133
97
{
@@ -160,12 +124,12 @@ void setup()
160
124
Bluefruit.Central .setConnectCallback (connect_callback);
161
125
Bluefruit.Central .setDisconnectCallback (disconnect_callback);
162
126
163
- #if TFT_IN_USE != TFT_NO_DISPLAY
164
- tft. fillScreen (COLOR_BLACK );
165
- tft. setTextColor (COLOR_WHITE );
166
- tft. setTextSize (2 );
167
- tft. setCursor (0 , 0 );
168
- tft. println (" Scanning ..." );
127
+ #ifdef USE_ARCADA
128
+ tft-> fillScreen (ARCADA_BLACK );
129
+ tft-> setTextColor (ARCADA_WHITE );
130
+ tft-> setTextSize (2 );
131
+ tft-> setCursor (0 , 0 );
132
+ tft-> println (" Scanning ..." );
169
133
#endif
170
134
171
135
/* Start Central Scanning
@@ -212,11 +176,11 @@ void connect_callback(uint16_t conn_handle)
212
176
213
177
Serial.println (" Connected" );
214
178
215
- #if TFT_IN_USE != TFT_NO_DISPLAY
216
- tft. fillScreen (COLOR_BLACK );
217
- tft. setTextSize (2 );
218
- tft. setCursor (0 , 0 );
219
- tft. println (" Connected" );
179
+ #ifdef USE_ARCADA
180
+ tft-> fillScreen (ARCADA_BLACK );
181
+ tft-> setTextSize (2 );
182
+ tft-> setCursor (0 , 0 );
183
+ tft-> println (" Connected" );
220
184
#endif
221
185
222
186
// If we are not bonded with peer previously -> send pairing request
@@ -239,8 +203,8 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
239
203
240
204
Serial.print (" Disconnected, reason = 0x" ); Serial.println (reason, HEX);
241
205
242
- #if TFT_IN_USE != TFT_NO_DISPLAY
243
- tft. println (" Scanning ..." );
206
+ #ifdef USE_ARCADA
207
+ tft-> println (" Scanning ..." );
244
208
#endif
245
209
}
246
210
@@ -262,49 +226,74 @@ bool pairing_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bo
262
226
Serial.println (" Pairing Passkey" );
263
227
Serial.printf (" %.3s %.3s\n\n " , passkey, passkey+3 );
264
228
265
- #if TFT_IN_USE != TFT_NO_DISPLAY
266
- tft. fillScreen (COLOR_BLACK );
267
- tft. println (" Pairing Passkey\n " );
268
- tft. setTextColor (COLOR_YELLOW );
269
- tft. setTextSize (4 );
270
- tft. printf (" %.3s %.3s\n " , passkey, passkey+3 );
229
+ #ifdef USE_ARCADA
230
+ tft-> fillScreen (ARCADA_BLACK );
231
+ tft-> println (" Pairing Passkey\n " );
232
+ tft-> setTextColor (ARCADA_YELLOW );
233
+ tft-> setTextSize (4 );
234
+ tft-> printf (" %.3s %.3s\n " , passkey, passkey+3 );
271
235
272
- tft. setTextColor (COLOR_WHITE );
273
- tft. setTextSize (2 );
236
+ tft-> setTextColor (ARCADA_WHITE );
237
+ tft-> setTextSize (2 );
274
238
#endif
275
239
240
+ // match_request means peer wait for our approval (return true)
276
241
if (match_request)
277
242
{
278
- Serial.println (" Do you want to pair ?" );
279
- Serial.println (" Press Button A to accept, Button B to reject" );
280
-
281
- #if TFT_IN_USE != TFT_NO_DISPLAY
282
- tft.println (" \n Do you accept ?\n\n " );
243
+ Serial.println (" Do you want to pair" );
244
+ Serial.println (" Press Button Left to decline, Button Right to Accept" );
283
245
284
- tft.setTextSize (3 );
285
- tft.setTextColor (COLOR_GREEN);
286
- tft.print (" << Yes" );
287
- tft.setTextColor (COLOR_RED);
288
- tft.println (" No >>" );
246
+ // timeout for pressing button
247
+ uint32_t start_time = millis ();
289
248
290
- tft.setTextColor (COLOR_WHITE);
291
- tft.setTextSize (2 );
292
- tft.println ();
249
+ #ifdef USE_ARCADA
250
+ tft->println (" \n Do you accept ?\n\n " );
251
+ tft->setTextSize (3 );
252
+
253
+ // Yes <-> No on CPB is reversed since GIZMO TFT is on the back of CPB
254
+ #if ARDUINO_NRF52840_CIRCUITPLAY
255
+ tft->setTextColor (ARCADA_GREEN);
256
+ tft->print (" < Yes" );
257
+ tft->setTextColor (ARCADA_RED);
258
+ tft->println (" No >" );
259
+ #else
260
+ tft->setTextColor (ARCADA_RED);
261
+ tft->print (" < No" );
262
+ tft->setTextColor (ARCADA_GREEN);
263
+ tft->println (" Yes >" );
293
264
#endif
294
265
295
- // wait until either button is pressed
296
- uint32_t start_time = millis ();
297
- while ( digitalRead (PIN_BUTTON1) && digitalRead (PIN_BUTTON2) )
266
+ tft->setTextColor (ARCADA_WHITE);
267
+ tft->setTextSize (2 );
268
+ tft->println ();
269
+
270
+ // wait until either button is pressed (30 seconds timeout)
271
+ uint32_t justReleased;
272
+ do
273
+ {
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) )
298
289
{
299
- // 30 seconds timeout
300
290
if ( millis () > start_time + 30000 ) break ;
301
291
}
302
292
303
- // A = accept
304
- if ( 0 == digitalRead (PIN_BUTTON1) ) return true ;
293
+ if ( 0 == digitalRead (BUTTON_YES) ) return true ;
305
294
306
- // B = reject
307
- if ( 0 == digitalRead (PIN_BUTTON2) ) return false ;
295
+ if ( 0 == digitalRead (BUTTON_NO) ) return false ;
296
+ # endif
308
297
309
298
return false ;
310
299
}
@@ -314,6 +303,8 @@ bool pairing_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bo
314
303
315
304
void pairing_complete_callback (uint16_t conn_handle, uint8_t auth_status)
316
305
{
306
+ BLEConnection* conn = Bluefruit.Connection (conn_handle);
307
+
317
308
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
318
309
{
319
310
Serial.println (" Succeeded" );
@@ -322,19 +313,22 @@ void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
322
313
Serial.println (" Failed" );
323
314
}
324
315
325
- #if TFT_IN_USE != TFT_NO_DISPLAY
316
+ #ifdef USE_ARCADA
326
317
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
327
318
{
328
- tft. setTextColor (COLOR_GREEN );
329
- tft. print (" Succeeded " );
319
+ tft-> setTextColor (ARCADA_GREEN );
320
+ tft-> print (" Succeeded " );
330
321
}else
331
322
{
332
- tft.setTextColor (COLOR_RED);
333
- tft.print (" Failed " );
323
+ tft->setTextColor (ARCADA_RED);
324
+ tft->print (" Failed " );
325
+
326
+ // disconnect
327
+ conn->disconnect ();
334
328
}
335
329
336
- tft. setTextColor (COLOR_WHITE );
337
- tft. setTextSize (2 );
330
+ tft-> setTextColor (ARCADA_WHITE );
331
+ tft-> setTextSize (2 );
338
332
#endif
339
333
}
340
334
@@ -354,10 +348,10 @@ void connection_secured_callback(uint16_t conn_handle)
354
348
{
355
349
Serial.println (" Secured" );
356
350
357
- #if TFT_IN_USE != TFT_NO_DISPLAY
358
- tft. setTextColor (COLOR_YELLOW );
359
- tft. println (" secured" );
360
- tft. setTextColor (COLOR_WHITE );
351
+ #ifdef USE_ARCADA
352
+ tft-> setTextColor (ARCADA_YELLOW );
353
+ tft-> println (" secured" );
354
+ tft-> setTextColor (ARCADA_WHITE );
361
355
#endif
362
356
363
357
Serial.print (" Discovering BLE Uart Service ... " );
@@ -374,7 +368,7 @@ void connection_secured_callback(uint16_t conn_handle)
374
368
Serial.println (" Found NONE" );
375
369
376
370
// disconnect since we couldn't find bleuart service
377
- Bluefruit. disconnect (conn_handle );
371
+ conn-> disconnect ();
378
372
}
379
373
}
380
374
}
0 commit comments