19
19
#include < InternalFileSystem.h>
20
20
#include < Adafruit_nRFCrypto.h>
21
21
22
- /* This sketch demonstrates Pairing process using static Passkey aka PIN .
22
+ /* This sketch demonstrates Pairing process using dynamic Passkey.
23
23
* This sketch is essentially the same as bleuart.ino except the BLE Uart
24
24
* service requires Security Mode with Man-In-The-Middle protection i.e
25
- * using 6 digits PIN for pairing.
26
25
*/
27
26
28
27
/* This sketch demonstrates the "Image Upload" feature of Bluefruit Mobile App.
29
28
* 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
33
33
*/
34
34
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
39
40
40
- // [Configurable] Please select one of above supported Display to match your hardware setup
41
- #define TFT_IN_USE TFT_NONE
42
41
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"
43
46
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"
48
54
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);
54
57
55
58
#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"
60
62
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
61
72
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);
65
76
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
69
81
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
73
83
74
- #endif
75
84
76
85
#define COLOR_WHITE 0xFFFF
77
86
#define COLOR_BLACK 0x0000
@@ -85,21 +94,36 @@ BLEUart bleuart; // uart over ble
85
94
void setup ()
86
95
{
87
96
Serial.begin (115200 );
88
- while (!Serial) delay (1 );
89
97
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
91
115
tft.init (240 , 240 );
92
116
tft.setRotation (2 );
93
- pinMode (TFT_BACKLIGHT, OUTPUT);
94
- digitalWrite (TFT_BACKLIGHT, HIGH); // Backlight on
95
117
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);
98
122
99
- #endif
123
+ #elif TFT_IN_USE != TFT_NO_DISPLAY
124
+ tft.begin ();
100
125
101
- Serial.println (" Bluefruit52 BLEUART Example" );
102
- Serial.println (" ---------------------------\n " );
126
+ #endif // TFT
103
127
104
128
// Setup the BLE LED to be enabled on CONNECT
105
129
// Note: This is actually the default behavior, but provided
@@ -113,10 +137,15 @@ void setup()
113
137
114
138
Bluefruit.begin ();
115
139
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);
117
147
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
120
149
Bluefruit.Pairing .setCompleteCallback (pairing_complete_callback);
121
150
122
151
Bluefruit.Periph .setConnectCallback (connect_callback);
@@ -136,7 +165,7 @@ void setup()
136
165
Serial.println (" Your phone should pop-up PIN input" );
137
166
Serial.println (" Once connected, enter character(s) that you wish to send" );
138
167
139
- #if TFT_IN_USE != TFT_NONE
168
+ #if TFT_IN_USE != TFT_NO_DISPLAY
140
169
tft.fillScreen (COLOR_BLACK);
141
170
tft.setTextColor (COLOR_WHITE);
142
171
tft.setTextSize (2 );
@@ -194,16 +223,16 @@ void loop()
194
223
}
195
224
}
196
225
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 ])
198
227
{
199
228
Serial.println (" Enter this code on your phone to pair with Bluefruit:" );
200
229
Serial.printf (" %.3s %.3s\n " , passkey, passkey+3 );
201
230
202
- #if TFT_IN_USE != TFT_NONE
231
+ #if TFT_IN_USE != TFT_NO_DISPLAY
203
232
tft.printf (" Enter this code on your phone to pair with Bluefruit:\n\n " );
204
233
tft.setTextColor (COLOR_YELLOW);
205
234
tft.setTextSize (4 );
206
- tft.printf (" %.6s \n " , passkey);
235
+ tft.printf (" %.3s %.3s \n " , passkey, passkey+ 3 );
207
236
208
237
tft.setTextColor (COLOR_WHITE);
209
238
tft.setTextSize (2 );
@@ -220,7 +249,7 @@ void pairing_complete_callback(uint16_t conn_handle, uint8_t auth_status)
220
249
Serial.println (" Failed" );
221
250
}
222
251
223
- #if TFT_IN_USE != TFT_NONE
252
+ #if TFT_IN_USE != TFT_NO_DISPLAY
224
253
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS)
225
254
{
226
255
tft.setTextColor (COLOR_GREEN);
@@ -248,7 +277,7 @@ void connect_callback(uint16_t conn_handle)
248
277
Serial.print (" Connected to " );
249
278
Serial.println (central_name);
250
279
251
- #if TFT_IN_USE != TFT_NONE
280
+ #if TFT_IN_USE != TFT_NO_DISPLAY
252
281
tft.fillScreen (COLOR_BLACK);
253
282
tft.setTextSize (2 );
254
283
tft.setCursor (0 , 0 );
@@ -269,7 +298,7 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
269
298
Serial.println ();
270
299
Serial.print (" Disconnected, reason = 0x" ); Serial.println (reason, HEX);
271
300
272
- #if TFT_IN_USE != TFT_NONE
301
+ #if TFT_IN_USE != TFT_NO_DISPLAY
273
302
tft.println (" Advertising ..." );
274
303
#endif
275
304
}
0 commit comments