@@ -96,6 +96,9 @@ uint32_t totalPixel = 0; // received pixel
96
96
// pixel line buffer, should be large enough to hold an image width
97
97
uint16_t pixel_buf[512 ];
98
98
99
+ // Use software timer to schedule bandwidth negotiation a few seconds after connection
100
+ SoftwareTimer negoTimer;
101
+
99
102
// Statistics for speed testing
100
103
uint32_t rxStartTime = 0 ;
101
104
uint32_t rxLastTime = 0 ;
@@ -153,6 +156,9 @@ void setup()
153
156
154
157
bleuart.setRxOverflowCallback (bleuart_overflow_callback);
155
158
159
+ // one-shot (non-repeating) 2 seconds timer
160
+ negoTimer.begin (2000 , negotiate_bandwidth, NULL , false );
161
+
156
162
// Set up and start advertising
157
163
startAdv ();
158
164
@@ -220,12 +226,15 @@ void bleuart_rx_callback(uint16_t conn_hdl)
220
226
221
227
totalPixel = 0 ;
222
228
223
- PRINT_INT (imageColorBit);
224
- PRINT_INT (imageWidth);
225
- PRINT_INT (imageHeight);
226
-
227
229
tft.fillScreen (COLOR_BLACK);
228
230
tft.setCursor (0 , 0 );
231
+
232
+ // Print out the current connection info
233
+ BLEConnection* conn = Bluefruit.Connection (conn_hdl);
234
+ Serial.printf (" Connection Info: PHY = %d Mbps, Conn Interval = %.2f ms, Data Length = %d, MTU = %d\n " ,
235
+ conn->getPHY (), conn->getConnectionInterval ()*1 .25f , conn->getDataLength (), conn->getMtu ());
236
+
237
+ Serial.printf (" Receiving an %dx%d Image with %d-bit color\n " , imageWidth, imageHeight, imageColorBit);
229
238
}
230
239
231
240
// Extract pixel data to buffer and draw image line by line
@@ -270,31 +279,59 @@ void bleuart_rx_callback(uint16_t conn_hdl)
270
279
}
271
280
}
272
281
273
- void connect_callback ( uint16_t conn_handle )
282
+ void negotiate_bandwidth (TimerHandle_t xTimer )
274
283
{
275
- BLEConnection* conn = Bluefruit. Connection (conn_handle) ;
284
+ ( void ) xTimer ;
276
285
277
- tft.println (" Connected" );
286
+ uint16_t conn_hdl = (uint16_t ) ((uint32_t ) negoTimer.getID ());
287
+ BLEConnection* conn = Bluefruit.Connection (conn_hdl);
278
288
279
- // Requesting to Switching to 2MB PHY, larger data length and MTU
280
- // will increase the throughput on supported central. This should be already done
281
- // with latest Bluefruit app, but still put here for user reference
282
- conn->requestPHY ();
283
- tft.println (" Switching PHY" );
289
+ // Switching from 1 Mb to 2 Mb PHY if needed
290
+ if ( conn->connected () )
291
+ {
292
+ // Requesting to Switching to 2MB PHY, larger data length and bigger MTU
293
+ // will increase the throughput on supported central. This should already
294
+ // be done with latest Bluefruit app.
295
+ //
296
+ // However, some Android devices require 2Mb PHY switching must be initiated
297
+ // from nRF side
298
+ if ( conn->getPHY () == BLE_GAP_PHY_1MBPS )
299
+ {
300
+ Serial.println (" Requesting PHY change from 1 Mb to 2Mb" );
301
+ conn->requestPHY ();
284
302
285
- conn->requestDataLengthUpdate ();
286
- tft.println (" Updating Data Length" );
303
+ // Data Length and MTU should already be done by Bluefruit app
304
+ // conn->requestDataLengthUpdate();
305
+ // conn->requestMtuExchange(247);
306
+ }
307
+ }
308
+ }
287
309
288
- conn->requestMtuExchange (247 );
289
- tft.println (" Exchanging MTU" );
310
+ void connect_callback (uint16_t conn_handle)
311
+ {
312
+ // Set connection handle as timer ID
313
+ // Then schedule negotiation after a few seconds, we should not
314
+ // negotiate here since it increases chance to conflict with other
315
+ // on-going negotiation from central after connection
316
+ negoTimer.setID ((void *) conn_handle);
317
+ negoTimer.start ();
290
318
319
+ tft.println (" Connected" );
291
320
tft.setTextColor (COLOR_GREEN);
292
321
tft.println (" Ready to receive new image" );
293
322
tft.setTextColor (COLOR_WHITE);
294
323
}
295
324
296
325
void print_summary (uint32_t count, uint32_t ms)
297
326
{
327
+ float sec = ms / 1000 .0F ;
328
+
329
+ // Print to serial
330
+ Serial.printf (" Received %d bytes in %.2f seconds\n " , count, sec);
331
+ Serial.printf (" Speed: %.2f KB/s\n\n " , (count / 1024 .0F ) / sec);
332
+ Serial.println (" Ready to receive new image" );
333
+
334
+ // Also print to TFT with color text
298
335
tft.setCursor (0 , imageHeight+5 );
299
336
tft.print (" Received " );
300
337
@@ -305,14 +342,14 @@ void print_summary(uint32_t count, uint32_t ms)
305
342
tft.print (" bytes in " );
306
343
307
344
tft.setTextColor (COLOR_YELLOW);
308
- tft.print (ms / 1000 . 0F , 2 );
345
+ tft.print (sec , 2 );
309
346
tft.setTextColor (COLOR_WHITE);
310
347
311
348
tft.println (" seconds" );
312
349
313
350
tft.print (" Speed: " );
314
351
tft.setTextColor (COLOR_YELLOW);
315
- tft.print ( (count / 1000 .0F ) / (ms / 1000 . 0F ) , 2 );
352
+ tft.print ( (count / 1024 .0F ) / sec , 2 );
316
353
tft.setTextColor (COLOR_WHITE);
317
354
tft.print (" KB/s for " );
318
355
0 commit comments