Skip to content

Commit 61beff2

Browse files
committed
update pairing exmaple
1 parent 2a53a5f commit 61beff2

File tree

2 files changed

+85
-54
lines changed

2 files changed

+85
-54
lines changed

libraries/Bluefruit52Lib/examples/Peripheral/image_transfer/image_transfer.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,16 @@
5555
// Default for others
5656
#define TFT_DC 10
5757
#define TFT_CS 9
58-
#endif // 832
58+
#endif
5959

6060
#if TFT_IN_USE == TFT_35_FEATHERWING
6161
#include "Adafruit_HX8357.h"
6262
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC);
63+
6364
#elif TFT_IN_USE == TFT_24_FEATHERWING
6465
#include <Adafruit_ILI9341.h>
6566
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
67+
6668
#else
6769
#error "TFT display is not supported"
6870
#endif // TFT

libraries/Bluefruit52Lib/examples/Peripheral/pairing_display/pairing_display.ino

Lines changed: 82 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,59 +19,68 @@
1919
#include <InternalFileSystem.h>
2020
#include <Adafruit_nRFCrypto.h>
2121

22-
/* This sketch demonstrates Pairing process using static Passkey aka PIN.
22+
/* This sketch demonstrates Pairing process using dynamic Passkey.
2323
* This sketch is essentially the same as bleuart.ino except the BLE Uart
2424
* service requires Security Mode with Man-In-The-Middle protection i.e
25-
* using 6 digits PIN for pairing.
2625
*/
2726

2827
/* This sketch demonstrates the "Image Upload" feature of Bluefruit Mobile App.
2928
* Following TFT Display are supported
30-
* - https://www.adafruit.com/product/3315
31-
* - https://www.adafruit.com/product/3651
32-
* - https://www.adafruit.com/product/4367
29+
* - TFT 3.5" : FeatherWing https://www.adafruit.com/product/3651
30+
* - TFT 2.4" : FeatherWing https://www.adafruit.com/product/3315
31+
* - TFT Gizmo : https://www.adafruit.com/product/4367
32+
* - Adafruit CLUE : https://www.adafruit.com/product/4500
3333
*/
3434

35-
#define TFT_NONE 0
36-
#define TFT_35_FEATHERWING 1
37-
#define TFT_24_FEATHERWING 2
38-
#define TFT_GIZMO 3
35+
#define TFT_NO_DISPLAY 0
36+
#define TFT_GIZMO 1 // used with Circuit Playground Bluefruit
37+
#define TFT_CLUE 2 // CLUE's on-board display
38+
#define TFT_24_FEATHERWING 3
39+
#define TFT_35_FEATHERWING 4
3940

40-
// [Configurable] Please select one of above supported Display to match your hardware setup
41-
#define TFT_IN_USE TFT_NONE
4241

42+
#if defined(ARDUINO_NRF52840_CIRCUITPLAY)
43+
// Circuit Playground Bluefruit use with TFT GIZMO
44+
#define TFT_IN_USE TFT_GIZMO
45+
#define DEVICE_NAME "CPLAY"
4346

44-
#if defined(ARDUINO_NRF52832_FEATHER)
45-
// Feather nRF52832
46-
#define TFT_DC 11
47-
#define TFT_CS 31
47+
#include "Adafruit_ST7789.h"
48+
Adafruit_ST7789 tft = Adafruit_ST7789(&SPI, 0, 1, -1); // CS = 0, DC = 1
49+
50+
#elif defined(ARDUINO_NRF52840_CLUE)
51+
// CLUE use on-board TFT
52+
#define TFT_IN_USE TFT_CLUE
53+
#define DEVICE_NAME "CLUE"
4854

49-
#elif defined(ARDUINO_NRF52840_CIRCUITPLAY)
50-
// Circuit Playground Bluefruit for use with TFT 1.5" GIZMO
51-
#define TFT_DC 1
52-
#define TFT_CS 0
53-
#define TFT_BACKLIGHT A3
55+
#include "Adafruit_ST7789.h"
56+
Adafruit_ST7789 tft = Adafruit_ST7789(&SPI1, PIN_TFT_CS, PIN_TFT_DC, PIN_TFT_RST);
5457

5558
#else
56-
// Default for others
57-
#define TFT_DC 10
58-
#define TFT_CS 9
59-
#endif
59+
// [Configurable] For other boards please select which external display to match your hardware setup
60+
#define TFT_IN_USE TFT_24_FEATHERWING
61+
#define DEVICE_NAME "Feather"
6062

63+
#if defined(ARDUINO_NRF52832_FEATHER)
64+
// Feather nRF52832 pin map is different from others
65+
#define TFT_DC 11
66+
#define TFT_CS 31
67+
#else
68+
// Default for others
69+
#define TFT_DC 10
70+
#define TFT_CS 9
71+
#endif
6172

62-
#if TFT_IN_USE == TFT_35_FEATHERWING
63-
#include "Adafruit_HX8357.h"
64-
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC);
73+
#if TFT_IN_USE == TFT_35_FEATHERWING
74+
#include "Adafruit_HX8357.h"
75+
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC);
6576

66-
#elif TFT_IN_USE == TFT_24_FEATHERWING
67-
#include <Adafruit_ILI9341.h>
68-
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
77+
#elif TFT_IN_USE == TFT_24_FEATHERWING
78+
#include <Adafruit_ILI9341.h>
79+
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
80+
#endif // TFT
6981

70-
#elif TFT_IN_USE == TFT_GIZMO
71-
#include "Adafruit_ST7789.h"
72-
Adafruit_ST7789 tft = Adafruit_ST7789(&SPI, TFT_CS, TFT_DC, -1);
82+
#endif // board variants
7383

74-
#endif
7584

7685
#define COLOR_WHITE 0xFFFF
7786
#define COLOR_BLACK 0x0000
@@ -85,21 +94,36 @@ BLEUart bleuart; // uart over ble
8594
void setup()
8695
{
8796
Serial.begin(115200);
88-
while(!Serial) delay(1);
8997

90-
#if TFT_IN_USE == TFT_GIZMO
98+
Serial.println("Bluefruit52 Pairing Display Example");
99+
Serial.println("-----------------------------------\n");
100+
101+
#if TFT_IN_USE == TFT_CLUE
102+
tft.init(240, 240);
103+
tft.setRotation(1);
104+
105+
// Screen refresh rate control (datasheet 9.2.18, FRCTRL2)
106+
uint8_t rtna = 0x01;
107+
tft.sendCommand(0xC6, &rtna, 1);;
108+
109+
// turn back light on
110+
uint8_t backlight = PIN_TFT_LITE;
111+
pinMode(backlight, OUTPUT);
112+
digitalWrite(backlight, HIGH);
113+
114+
#elif TFT_IN_USE == TFT_GIZMO
91115
tft.init(240, 240);
92116
tft.setRotation(2);
93-
pinMode(TFT_BACKLIGHT, OUTPUT);
94-
digitalWrite(TFT_BACKLIGHT, HIGH); // Backlight on
95117

96-
#elif TFT_IN_USE != TFT_NONE
97-
tft.begin();
118+
// turn back light on
119+
uint8_t backlight = A3;
120+
pinMode(backlight, OUTPUT);
121+
digitalWrite(backlight, HIGH);
98122

99-
#endif
123+
#elif TFT_IN_USE != TFT_NO_DISPLAY
124+
tft.begin();
100125

101-
Serial.println("Bluefruit52 BLEUART Example");
102-
Serial.println("---------------------------\n");
126+
#endif // TFT
103127

104128
// Setup the BLE LED to be enabled on CONNECT
105129
// Note: This is actually the default behavior, but provided
@@ -113,10 +137,15 @@ void setup()
113137

114138
Bluefruit.begin();
115139
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
116-
Bluefruit.setName("Bluefruit52");
140+
Bluefruit.setName(DEVICE_NAME);
141+
142+
// To use dynamic PassKey for pairing, we need to have
143+
// - IO capacities at least DISPPLAY
144+
// - Register callback to display/print dynamic passkey for central
145+
Bluefruit.Pairing.setIOCaps(true, false, false);
146+
Bluefruit.Pairing.setPasskeyCallback(pairing_passkey_callback);
117147

118-
// Serial.println("Setting pairing PIN to: " PAIRING_PIN);
119-
Bluefruit.Pairing.setPasskeyCallback(pairing_display_callback);
148+
// Set complete callback to print the pairing result
120149
Bluefruit.Pairing.setCompleteCallback(pairing_complete_callback);
121150

122151
Bluefruit.Periph.setConnectCallback(connect_callback);
@@ -136,7 +165,7 @@ void setup()
136165
Serial.println("Your phone should pop-up PIN input");
137166
Serial.println("Once connected, enter character(s) that you wish to send");
138167

139-
#if TFT_IN_USE != TFT_NONE
168+
#if TFT_IN_USE != TFT_NO_DISPLAY
140169
tft.fillScreen(COLOR_BLACK);
141170
tft.setTextColor(COLOR_WHITE);
142171
tft.setTextSize(2);
@@ -194,16 +223,16 @@ void loop()
194223
}
195224
}
196225

197-
void pairing_display_callback(uint16_t conn_handle, uint8_t const passkey[6])
226+
void pairing_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6])
198227
{
199228
Serial.println("Enter this code on your phone to pair with Bluefruit:");
200229
Serial.printf(" %.3s %.3s\n", passkey, passkey+3);
201230

202-
#if TFT_IN_USE != TFT_NONE
231+
#if TFT_IN_USE != TFT_NO_DISPLAY
203232
tft.printf("Enter this code on your phone to pair with Bluefruit:\n\n");
204233
tft.setTextColor(COLOR_YELLOW);
205234
tft.setTextSize(4);
206-
tft.printf(" %.6s\n", passkey);
235+
tft.printf(" %.3s %.3s\n", passkey, passkey+3);
207236

208237
tft.setTextColor(COLOR_WHITE);
209238
tft.setTextSize(2);
@@ -220,7 +249,7 @@ void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
220249
Serial.println("Failed");
221250
}
222251

223-
#if TFT_IN_USE != TFT_NONE
252+
#if TFT_IN_USE != TFT_NO_DISPLAY
224253
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
225254
{
226255
tft.setTextColor(COLOR_GREEN);
@@ -248,7 +277,7 @@ void connect_callback(uint16_t conn_handle)
248277
Serial.print("Connected to ");
249278
Serial.println(central_name);
250279

251-
#if TFT_IN_USE != TFT_NONE
280+
#if TFT_IN_USE != TFT_NO_DISPLAY
252281
tft.fillScreen(COLOR_BLACK);
253282
tft.setTextSize(2);
254283
tft.setCursor(0, 0);
@@ -269,7 +298,7 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
269298
Serial.println();
270299
Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
271300

272-
#if TFT_IN_USE != TFT_NONE
301+
#if TFT_IN_USE != TFT_NO_DISPLAY
273302
tft.println("Advertising ...");
274303
#endif
275304
}

0 commit comments

Comments
 (0)