Skip to content

Commit 34718f8

Browse files
committed
update central pairing with acarda
1 parent d65525f commit 34718f8

File tree

2 files changed

+153
-162
lines changed

2 files changed

+153
-162
lines changed

libraries/Bluefruit52Lib/examples/Central/central_pairing/central_pairing.ino

Lines changed: 127 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <bluefruit.h>
1616

1717
/* 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
1919
* service requires Security Mode with Man-In-The-Middle protection i.e
2020
*
2121
* BLE Pairing procedure is complicated, it is advisable for users to go through
@@ -24,63 +24,43 @@
2424
* - https://www.bluetooth.com/blog/bluetooth-pairing-part-2-key-generation-methods/
2525
* - https://www.bluetooth.com/blog/bluetooth-pairing-passkey-entry/
2626
* - 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
2733
*/
2834

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
4038

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>
4343

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>
4846

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;
5149

5250
#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
6154
#else
62-
// Default for others
63-
#define TFT_DC 10
64-
#define TFT_CS 9
55+
#define BUTTON_YES A0
6556
#endif
6657

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
8464

8565
BLEClientUart clientUart; // bleuart client
8666

@@ -91,43 +71,27 @@ void setup()
9171
Serial.println("Bluefruit52 Central Pairing Example");
9272
Serial.println("-----------------------------------\n");
9373

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
12387

12488
// Initialize Bluefruit with maximum connections as Peripheral = 0, Central = 1
12589
// SRAM usage required by SoftDevice will increase dramatically with number of connections
12690
Bluefruit.begin(0, 1);
127-
Bluefruit.setName("Bluefruit52 Central");
128-
91+
12992
// clear bonds if BUTTON A is pressed
13093
Serial.println("Hold button A to clear bonds ..... ");
94+
Serial.flush();
13195
delay(2000);
13296
if (0 == digitalRead(PIN_BUTTON1))
13397
{
@@ -160,12 +124,12 @@ void setup()
160124
Bluefruit.Central.setConnectCallback(connect_callback);
161125
Bluefruit.Central.setDisconnectCallback(disconnect_callback);
162126

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 ...");
169133
#endif
170134

171135
/* Start Central Scanning
@@ -212,11 +176,11 @@ void connect_callback(uint16_t conn_handle)
212176

213177
Serial.println("Connected");
214178

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");
220184
#endif
221185

222186
// 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)
239203

240204
Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
241205

242-
#if TFT_IN_USE != TFT_NO_DISPLAY
243-
tft.println("Scanning ...");
206+
#ifdef USE_ARCADA
207+
tft->println("Scanning ...");
244208
#endif
245209
}
246210

@@ -262,49 +226,74 @@ bool pairing_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bo
262226
Serial.println("Pairing Passkey");
263227
Serial.printf(" %.3s %.3s\n\n", passkey, passkey+3);
264228

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);
271235

272-
tft.setTextColor(COLOR_WHITE);
273-
tft.setTextSize(2);
236+
tft->setTextColor(ARCADA_WHITE);
237+
tft->setTextSize(2);
274238
#endif
275239

240+
// match_request means peer wait for our approval (return true)
276241
if (match_request)
277242
{
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("\nDo you accept ?\n\n");
243+
Serial.println("Do you want to pair");
244+
Serial.println("Press Button Left to decline, Button Right to Accept");
283245

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();
289248

290-
tft.setTextColor(COLOR_WHITE);
291-
tft.setTextSize(2);
292-
tft.println();
249+
#ifdef USE_ARCADA
250+
tft->println("\nDo 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 >");
293264
#endif
294265

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) )
298289
{
299-
// 30 seconds timeout
300290
if ( millis() > start_time + 30000 ) break;
301291
}
302292

303-
// A = accept
304-
if ( 0 == digitalRead(PIN_BUTTON1) ) return true;
293+
if ( 0 == digitalRead(BUTTON_YES) ) return true;
305294

306-
// B = reject
307-
if ( 0 == digitalRead(PIN_BUTTON2) ) return false;
295+
if ( 0 == digitalRead(BUTTON_NO) ) return false;
296+
#endif
308297

309298
return false;
310299
}
@@ -314,6 +303,8 @@ bool pairing_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bo
314303

315304
void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
316305
{
306+
BLEConnection* conn = Bluefruit.Connection(conn_handle);
307+
317308
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
318309
{
319310
Serial.println("Succeeded");
@@ -322,19 +313,22 @@ void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
322313
Serial.println("Failed");
323314
}
324315

325-
#if TFT_IN_USE != TFT_NO_DISPLAY
316+
#ifdef USE_ARCADA
326317
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
327318
{
328-
tft.setTextColor(COLOR_GREEN);
329-
tft.print("Succeeded ");
319+
tft->setTextColor(ARCADA_GREEN);
320+
tft->print("Succeeded ");
330321
}else
331322
{
332-
tft.setTextColor(COLOR_RED);
333-
tft.print("Failed ");
323+
tft->setTextColor(ARCADA_RED);
324+
tft->print("Failed ");
325+
326+
// disconnect
327+
conn->disconnect();
334328
}
335329

336-
tft.setTextColor(COLOR_WHITE);
337-
tft.setTextSize(2);
330+
tft->setTextColor(ARCADA_WHITE);
331+
tft->setTextSize(2);
338332
#endif
339333
}
340334

@@ -354,10 +348,10 @@ void connection_secured_callback(uint16_t conn_handle)
354348
{
355349
Serial.println("Secured");
356350

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);
361355
#endif
362356

363357
Serial.print("Discovering BLE Uart Service ... ");
@@ -374,7 +368,7 @@ void connection_secured_callback(uint16_t conn_handle)
374368
Serial.println("Found NONE");
375369

376370
// disconnect since we couldn't find bleuart service
377-
Bluefruit.disconnect(conn_handle);
371+
conn->disconnect();
378372
}
379373
}
380374
}

0 commit comments

Comments
 (0)