From 6d7b755ec042a797d13ba5152d1049561db38c5b Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 21 Jan 2020 15:18:00 +0700 Subject: [PATCH 01/12] clean up --- .../Peripheral/image_upload/image_upload.ino | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino index 78d2326b..301c76e8 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -13,7 +13,7 @@ *********************************************************************/ /* This sketch demonstrates the "Image Upload" feature of Bluefruit Mobile App. - * Either 2.4" or 3.5" TFT FeatherWing is used to display uploaded image + * Following TFT Display are supported * - https://www.adafruit.com/product/3315 * - https://www.adafruit.com/product/3651 * - https://www.adafruit.com/product/4367 @@ -21,9 +21,9 @@ #define TFT_35_FEATHERWING 1 #define TFT_24_FEATHERWING 2 -#define TFT_15_GIZMO 3 +#define TFT_GIZMO 3 -// one of above supported TFT add-on +// [Configurable] Please select one of above supported Display to match your hardware setup #define TFT_IN_USE TFT_35_FEATHERWING #include @@ -56,7 +56,7 @@ #include Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC); -#elif TFT_IN_USE == TFT_15_GIZMO +#elif TFT_IN_USE == TFT_GIZMO #include "Adafruit_ST7789.h" Adafruit_ST7789 tft = Adafruit_ST7789(&SPI, TFT_CS, TFT_DC, -1); @@ -78,10 +78,10 @@ BLEUart bleuart(10*1024); /* The Image Transfer module sends the image of your choice to Bluefruit LE over UART. * Each image sent begins with * - A single byte char '!' (0x21) followed by 'I' helper for image - * - Color depth: 24-bit for RGB 888, 16-bit for RGB 655 + * - Color depth: 24-bit for RGB 888, 16-bit for RGB 565 * - Image width (uint16 little endian, 2 bytes) * - Image height (uint16 little endian, 2 bytes) - * - Pixel data encoded as RGB 24-bit and suffixed by a single byte CRC. + * - Pixel data encoded as RGB 16/24 bit and suffixed by a single byte CRC. * * Format: [ '!' ] [ 'I' ] [uint8_t color bit] [ uint16 width ] [ uint16 height ] [ r g b ] [ r g b ] [ r g b ] … [ CRC ] */ @@ -106,7 +106,7 @@ void setup() { Serial.begin(115200); -#if TFT_IN_USE == TFT_15_GIZMO +#if TFT_IN_USE == TFT_GIZMO tft.init(240, 240); tft.setRotation(2); pinMode(TFT_BACKLIGHT, OUTPUT); @@ -143,7 +143,7 @@ void setup() // However, the transfer speed will be affected since immediate callback will block BLE task // to process data especially when tft.drawRGBBitmap() is calling. #ifdef NRF52840_XXAA - // 2nd argument is true to deferred callbacks i.e queue it up in seperated callback Task + // 2nd argument is true to deferred callbacks i.e queue it up in separated callback Task bleuart.setRxCallback(bleuart_rx_callback, true); #else // 2nd argument is false to invoke callbacks immediately (thus blocking other ble events) From d65f1cd720d4e677df5113c09dc1d9c5621b228a Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 21 Jan 2020 15:41:22 +0700 Subject: [PATCH 02/12] add image_eink_upload sketch tested with EInk Gizmo --- .../image_eink_upload/image_eink_upload.ino | 305 ++++++++++++++++++ .../Peripheral/image_upload/image_upload.ino | 9 +- 2 files changed, 310 insertions(+), 4 deletions(-) create mode 100644 libraries/Bluefruit52Lib/examples/Peripheral/image_eink_upload/image_eink_upload.ino diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_eink_upload/image_eink_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_eink_upload/image_eink_upload.ino new file mode 100644 index 00000000..c756b660 --- /dev/null +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_eink_upload/image_eink_upload.ino @@ -0,0 +1,305 @@ +/********************************************************************* + This is an example for our nRF52 based Bluefruit LE modules + + Pick one up today in the adafruit shop! + + Adafruit invests time and resources providing this open source code, + please support Adafruit and open-source hardware by purchasing + products from Adafruit! + + MIT license, check LICENSE for more information + All text above, and the splash screen below must be included in + any redistribution +*********************************************************************/ + +#include +#include +#include +#include "Adafruit_EPD.h" + +/* This sketch demonstrates the "Image Upload" feature of Bluefruit Mobile App. + * Following TFT Display are supported + * - https://www.adafruit.com/product/4428 + */ + +#define EINK_GIZMO 1 + +// one of above supported TFT add-on +#define DISPLAY_IN_USE EINK_GIZMO + +#define EPD_CS 0 +#define EPD_DC 1 +#define SRAM_CS -1 +#define EPD_RESET PIN_A3 // can set to -1 and share with microcontroller Reset! +#define EPD_BUSY -1 // can set to -1 to not use a pin (will wait a fixed delay) + +Adafruit_IL0373 display(152, 152, EPD_DC, EPD_RESET, EPD_CS, SRAM_CS, EPD_BUSY); + + +// Declaring Uart over BLE with large buffer to hold image data +// Depending on the Image Resolution and Transfer Mode especially without response +// or Interleaved with high ratio. You may need to increase this buffer size +BLEUart bleuart(10*1024); + +/* The Image Transfer module sends the image of your choice to Bluefruit LE over UART. + * Each image sent begins with + * - A single byte char '!' (0x21) followed by 'I' helper for image + * - Color depth: 24-bit for RGB 888, 16-bit for RGB 565 + * - Image width (uint16 little endian, 2 bytes) + * - Image height (uint16 little endian, 2 bytes) + * - Pixel data encoded as RGB 16/24 bit and suffixed by a single byte CRC. + * + * Format: [ '!' ] [ 'I' ] [uint8_t color bit] [ uint16 width ] [ uint16 height ] [ r g b ] [ r g b ] [ r g b ] … [ CRC ] + */ + +uint16_t imageWidth = 0; +uint16_t imageHeight = 0; +uint8_t imageColorBit = 0; + +uint32_t totalPixel = 0; // received pixel + +// pixel line buffer, should be large enough to hold an image width +uint16_t pixel_buf[512]; + +// Statistics for speed testing +uint32_t rxStartTime = 0; +uint32_t rxLastTime = 0; + +void setup() +{ + Serial.begin(115200); + + display.begin(); + display.clearBuffer(); + +#if DISPLAY_IN_USE == EINK_GIZMO + display.setRotation(3); +#else + // todo more eink displays +#endif + + // Config the peripheral connection with maximum bandwidth + // more SRAM required by SoftDevice + // Note: All config***() function must be called before begin() + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); + + Bluefruit.begin(); + Bluefruit.setTxPower(4); // Check bluefruit.h for supported values + Bluefruit.setName("Bluefruit52"); + Bluefruit.Periph.setConnectCallback(connect_callback); + Bluefruit.Periph.setDisconnectCallback(disconnect_callback); + Bluefruit.Periph.setConnInterval(6, 12); // 7.5 - 15 ms + + // Configure and Start BLE Uart Service + bleuart.begin(); + + // Due to huge amount of image data + // NRF52832 doesn't have enough SRAM to queue up received packets using deferred callbacks. + // Therefore it must process data as soon as it comes, this can be done by + // changing the default "deferred" option to false to invoke callback immediately. + // However, the transfer speed will be affected since immediate callback will block BLE task + // to process data especially when tft.drawRGBBitmap() is calling. +#ifdef NRF52840_XXAA + // 2nd argument is true to deferred callbacks i.e queue it up in separated callback Task + bleuart.setRxCallback(bleuart_rx_callback, true); +#else + // 2nd argument is false to invoke callbacks immediately (thus blocking other ble events) + bleuart.setRxCallback(bleuart_rx_callback, false); +#endif + + bleuart.setRxOverflowCallback(bleuart_overflow_callback); + + // Set up and start advertising + startAdv(); + + Serial.println("Advertising ... "); +} + +void startAdv(void) +{ + // Advertising packet + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + Bluefruit.Advertising.addAppearance(BLE_APPEARANCE_GENERIC_CLOCK); + + // Include bleuart 128-bit uuid + Bluefruit.Advertising.addService(bleuart); + + // There is no room for Name in Advertising packet + // Use Scan response for Name + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void loop() +{ + // nothing to do +} + +// Invoked when receiving data from bleuart +// Pull data from bleuart fifo & draw image as soon as possible, +// Otherwise bleuart fifo can be overflowed +void bleuart_rx_callback(uint16_t conn_hdl) +{ + (void) conn_hdl; + + rxLastTime = millis(); + + // Received new Image + if ( (imageWidth == 0) && (imageHeight == 0) ) + { + // take note of time of first packet + rxStartTime = millis(); + + // Skip all data until '!I' is found + while( bleuart.available() && bleuart.read() != '!' ) { } + if (bleuart.read() != 'I') return; + + if ( !bleuart.available() ) return; + + imageColorBit = bleuart.read8(); + imageWidth = bleuart.read16(); + imageHeight = bleuart.read16(); + + totalPixel = 0; + + Serial.printf("Image resolution: %dx%d with %d bit color\n", imageWidth, imageHeight, imageColorBit); + + display.clearBuffer(); + display.fillScreen(EPD_WHITE); + display.setCursor(0, 0); + } + + // Extract pixel data to buffer and draw image line by line + while ( bleuart.available() >= 3 ) + { + uint8_t red, green, blue; + + if ( imageColorBit == 24 ) + { + // Application send 24-bit color + red = bleuart.read(); + green = bleuart.read(); + blue = bleuart.read(); + } + else if ( imageColorBit == 16 ) + { + // Application send 16-bit 565 color + uint16_t c565 = bleuart.read16(); + + red = (c565 & 0xf800) >> 8; + green = (c565 & 0x07e0) >> 3; + blue = (c565 & 0x001f) << 3; + } + + // Convert RGB into Eink Color + uint8_t c = 0; + if ((red < 0x80) && (green < 0x80) && (blue < 0x80)) { + c = EPD_BLACK; // try to infer black + } else if ((red >= 0x80) && (green >= 0x80) && (blue >= 0x80)) { + c = EPD_WHITE; + } else if (red >= 0x80) { + c = EPD_RED; // try to infer red color + } + + // store to pixel buffer + pixel_buf[totalPixel % imageWidth] = c; + + totalPixel++; + + // have enough to draw an image line + if ( (totalPixel % imageWidth) == 0 ) + { + display.drawRGBBitmap(0, totalPixel/imageWidth, pixel_buf, imageWidth, 1); + } + } + + // all pixel data is received + if ( totalPixel == imageWidth*imageHeight ) + { + uint8_t crc = bleuart.read(); + (void) crc; + // do checksum later + + // print speed summary + print_summary(totalPixel*(imageColorBit/8) + 8, rxLastTime-rxStartTime); + + // Display on Eink, will probably take dozens of seconds + display.display(); + + // reset and waiting for new image + imageColorBit = 0; + imageWidth = imageHeight = 0; + totalPixel = 0; + + Serial.println("Ready to receive new image"); + } +} + +void connect_callback(uint16_t conn_handle) +{ + BLEConnection* conn = Bluefruit.Connection(conn_handle); + + Serial.println("Connected"); + + // Requesting to Switching to 2MB PHY, larger data length and MTU + // will increase the throughput on supported central. This should be already done + // with latest Bluefruit app, but still put here for user reference + conn->requestPHY(); + Serial.println("Switching PHY"); + + conn->requestDataLengthUpdate(); + Serial.println("Updating Data Length"); + + conn->requestMtuExchange(247); + Serial.println("Exchanging MTU"); + + Serial.println("Ready to receive new image"); +} + +void print_summary(uint32_t count, uint32_t ms) +{ + float sec = ms / 1000.0F; + + Serial.printf("Received %d bytes in %.2f seconds\n", count, sec); + Serial.printf("Speed: %.2f KB/s for %dx%d Image\n", (count / 1024.0F) / sec, imageWidth, imageHeight); +} + +void bleuart_overflow_callback(uint16_t conn_hdl, uint16_t leftover) +{ + (void) conn_hdl; + (void) leftover; + + Serial.println("BLEUART rx buffer OVERFLOWED!"); + Serial.println("Please increase buffer size for bleuart"); +} + +/** + * invoked when a connection is dropped + * @param conn_handle connection where this event happens + * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h + */ +void disconnect_callback(uint16_t conn_handle, uint8_t reason) +{ + (void) conn_handle; + (void) reason; + + imageColorBit = 0; + imageWidth = imageHeight = 0; + totalPixel = 0; + + bleuart.flush(); +} diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino index 301c76e8..b410af2a 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -12,6 +12,10 @@ any redistribution *********************************************************************/ +#include +#include +#include + /* This sketch demonstrates the "Image Upload" feature of Bluefruit Mobile App. * Following TFT Display are supported * - https://www.adafruit.com/product/3315 @@ -26,9 +30,6 @@ // [Configurable] Please select one of above supported Display to match your hardware setup #define TFT_IN_USE TFT_35_FEATHERWING -#include -#include -#include #if defined(ARDUINO_NRF52832_FEATHER) // Feather nRF52832 @@ -245,7 +246,7 @@ void bleuart_rx_callback(uint16_t conn_hdl) totalPixel++; - // enough to draw an image line + // have enough to draw an image line if ( (totalPixel % imageWidth) == 0 ) { tft.drawRGBBitmap(0, totalPixel/imageWidth, pixel_buf, imageWidth, 1); From 25232734f92f824fc086d6b37ede0c4200e04430 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 22 Jan 2020 17:18:06 +0700 Subject: [PATCH 03/12] add SoftwareTimer setID/getID --- cores/nRF5/utility/SoftwareTimer.cpp | 10 ++++++++++ cores/nRF5/utility/SoftwareTimer.h | 3 +++ 2 files changed, 13 insertions(+) diff --git a/cores/nRF5/utility/SoftwareTimer.cpp b/cores/nRF5/utility/SoftwareTimer.cpp index 9a73706c..f5899a0f 100644 --- a/cores/nRF5/utility/SoftwareTimer.cpp +++ b/cores/nRF5/utility/SoftwareTimer.cpp @@ -39,6 +39,16 @@ void SoftwareTimer::begin(uint32_t ms, TimerCallbackFunction_t callback, void* t _handle = xTimerCreate(NULL, ms2tick(ms), repeating, timerID, callback); } +void SoftwareTimer::setID(void* id) +{ + vTimerSetTimerID(_handle, id); +} + +void* SoftwareTimer::getID(void) +{ + return pvTimerGetTimerID(_handle); +} + bool SoftwareTimer::start(void) { bool ret = true; diff --git a/cores/nRF5/utility/SoftwareTimer.h b/cores/nRF5/utility/SoftwareTimer.h index 24453c49..2216d98e 100644 --- a/cores/nRF5/utility/SoftwareTimer.h +++ b/cores/nRF5/utility/SoftwareTimer.h @@ -50,6 +50,9 @@ class SoftwareTimer void begin(uint32_t ms, TimerCallbackFunction_t callback, void* timerID = NULL, bool repeating = true); TimerHandle_t getHandle(void) { return _handle; } + void setID(void* id); + void* getID(void); + bool start(void); bool stop (void); bool reset (void); From 836b0d84991974e9f2acb576a028d4aa3d5f732e Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 22 Jan 2020 17:31:02 +0700 Subject: [PATCH 04/12] improve phy switching, fix collision issue when neogtiating on connection schedule a negotiation 2 seconds after connection established. --- .../image_eink_upload/image_eink_upload.ino | 65 ++++++++++++---- .../Peripheral/image_upload/image_upload.ino | 77 ++++++++++++++----- 2 files changed, 105 insertions(+), 37 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_eink_upload/image_eink_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_eink_upload/image_eink_upload.ino index c756b660..82ad0b00 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_eink_upload/image_eink_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_eink_upload/image_eink_upload.ino @@ -61,6 +61,9 @@ uint32_t totalPixel = 0; // received pixel // pixel line buffer, should be large enough to hold an image width uint16_t pixel_buf[512]; +// Use software timer to schedule bandwidth negotiation a few seconds after connection +SoftwareTimer negoTimer; + // Statistics for speed testing uint32_t rxStartTime = 0; uint32_t rxLastTime = 0; @@ -109,6 +112,9 @@ void setup() bleuart.setRxOverflowCallback(bleuart_overflow_callback); + // one-shot (non-repeating) 2 seconds timer + negoTimer.begin(2000, negotiate_bandwidth, NULL, false); + // Set up and start advertising startAdv(); @@ -176,11 +182,15 @@ void bleuart_rx_callback(uint16_t conn_hdl) totalPixel = 0; - Serial.printf("Image resolution: %dx%d with %d bit color\n", imageWidth, imageHeight, imageColorBit); - display.clearBuffer(); display.fillScreen(EPD_WHITE); display.setCursor(0, 0); + + // Print out the current connection info + BLEConnection* conn = Bluefruit.Connection(conn_hdl); + Serial.printf("Connection Info: PHY = %d Mbps, Conn Interval = %.2f ms, Data Length = %d, MTU = %d\n", + conn->getPHY(), conn->getConnectionInterval()*1.25f, conn->getDataLength(), conn->getMtu()); + Serial.printf("Receving an %dx%d Image with %d bit color\n", imageWidth, imageHeight, imageColorBit); } // Extract pixel data to buffer and draw image line by line @@ -238,6 +248,7 @@ void bleuart_rx_callback(uint16_t conn_hdl) print_summary(totalPixel*(imageColorBit/8) + 8, rxLastTime-rxStartTime); // Display on Eink, will probably take dozens of seconds + Serial.println("Displaying image (~20 seconds) ....."); display.display(); // reset and waiting for new image @@ -249,24 +260,44 @@ void bleuart_rx_callback(uint16_t conn_hdl) } } +void negotiate_bandwidth(TimerHandle_t xTimer) +{ + (void) xTimer; + + uint16_t conn_hdl = (uint16_t) ((uint32_t) negoTimer.getID()); + BLEConnection* conn = Bluefruit.Connection(conn_hdl); + + // Switching from 1 Mb to 2 Mb PHY if needed + if ( conn->connected() ) + { + // Requesting to Switching to 2MB PHY, larger data length and bigger MTU + // will increase the throughput on supported central. This should already + // be done with latest Bluefruit app. + // + // However, some Android devices require 2Mb PHY switching must be initiated + // from nRF side + if ( conn->getPHY() == BLE_GAP_PHY_1MBPS ) + { + Serial.println("Requesting PHY change from 1 Mb to 2Mb"); + conn->requestPHY(); + + // Data Length and MTU should already be done by Bluefruit app + // conn->requestDataLengthUpdate(); + // conn->requestMtuExchange(247); + } + } +} + void connect_callback(uint16_t conn_handle) { - BLEConnection* conn = Bluefruit.Connection(conn_handle); + // Set connection handle as timer ID + // Then schedule negotiation after a few seconds, we should not + // negotiate here since it increases chance to conflict with other + // on-going negotiation from central after connection + negoTimer.setID((void*) conn_handle); + negoTimer.start(); Serial.println("Connected"); - - // Requesting to Switching to 2MB PHY, larger data length and MTU - // will increase the throughput on supported central. This should be already done - // with latest Bluefruit app, but still put here for user reference - conn->requestPHY(); - Serial.println("Switching PHY"); - - conn->requestDataLengthUpdate(); - Serial.println("Updating Data Length"); - - conn->requestMtuExchange(247); - Serial.println("Exchanging MTU"); - Serial.println("Ready to receive new image"); } @@ -275,7 +306,7 @@ void print_summary(uint32_t count, uint32_t ms) float sec = ms / 1000.0F; Serial.printf("Received %d bytes in %.2f seconds\n", count, sec); - Serial.printf("Speed: %.2f KB/s for %dx%d Image\n", (count / 1024.0F) / sec, imageWidth, imageHeight); + Serial.printf("Speed: %.2f KB/s\n\n", (count / 1024.0F) / sec); } void bleuart_overflow_callback(uint16_t conn_hdl, uint16_t leftover) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino index b410af2a..99220d4e 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -96,6 +96,9 @@ uint32_t totalPixel = 0; // received pixel // pixel line buffer, should be large enough to hold an image width uint16_t pixel_buf[512]; +// Use software timer to schedule bandwidth negotiation a few seconds after connection +SoftwareTimer negoTimer; + // Statistics for speed testing uint32_t rxStartTime = 0; uint32_t rxLastTime = 0; @@ -153,6 +156,9 @@ void setup() bleuart.setRxOverflowCallback(bleuart_overflow_callback); + // one-shot (non-repeating) 2 seconds timer + negoTimer.begin(2000, negotiate_bandwidth, NULL, false); + // Set up and start advertising startAdv(); @@ -220,12 +226,15 @@ void bleuart_rx_callback(uint16_t conn_hdl) totalPixel = 0; - PRINT_INT(imageColorBit); - PRINT_INT(imageWidth); - PRINT_INT(imageHeight); - tft.fillScreen(COLOR_BLACK); tft.setCursor(0, 0); + + // Print out the current connection info + BLEConnection* conn = Bluefruit.Connection(conn_hdl); + Serial.printf("Connection Info: PHY = %d Mbps, Conn Interval = %.2f ms, Data Length = %d, MTU = %d\n", + conn->getPHY(), conn->getConnectionInterval()*1.25f, conn->getDataLength(), conn->getMtu()); + + Serial.printf("Receiving an %dx%d Image with %d-bit color\n", imageWidth, imageHeight, imageColorBit); } // Extract pixel data to buffer and draw image line by line @@ -270,24 +279,44 @@ void bleuart_rx_callback(uint16_t conn_hdl) } } +void negotiate_bandwidth(TimerHandle_t xTimer) +{ + (void) xTimer; + + uint16_t conn_hdl = (uint16_t) ((uint32_t) negoTimer.getID()); + BLEConnection* conn = Bluefruit.Connection(conn_hdl); + + // Switching from 1 Mb to 2 Mb PHY if needed + if ( conn->connected() ) + { + // Requesting to Switching to 2MB PHY, larger data length and bigger MTU + // will increase the throughput on supported central. This should already + // be done with latest Bluefruit app. + // + // However, some Android devices require 2Mb PHY switching must be initiated + // from nRF side + if ( conn->getPHY() == BLE_GAP_PHY_1MBPS ) + { + Serial.println("Requesting PHY change from 1 Mb to 2Mb"); + conn->requestPHY(); + + // Data Length and MTU should already be done by Bluefruit app + // conn->requestDataLengthUpdate(); + // conn->requestMtuExchange(247); + } + } +} + void connect_callback(uint16_t conn_handle) { - BLEConnection* conn = Bluefruit.Connection(conn_handle); + // Set connection handle as timer ID + // Then schedule negotiation after a few seconds, we should not + // negotiate here since it increases chance to conflict with other + // on-going negotiation from central after connection + negoTimer.setID((void*) conn_handle); + negoTimer.start(); tft.println("Connected"); - - // Requesting to Switching to 2MB PHY, larger data length and MTU - // will increase the throughput on supported central. This should be already done - // with latest Bluefruit app, but still put here for user reference - conn->requestPHY(); - tft.println("Switching PHY"); - - conn->requestDataLengthUpdate(); - tft.println("Updating Data Length"); - - conn->requestMtuExchange(247); - tft.println("Exchanging MTU"); - tft.setTextColor(COLOR_GREEN); tft.println("Ready to receive new image"); tft.setTextColor(COLOR_WHITE); @@ -295,6 +324,14 @@ void connect_callback(uint16_t conn_handle) void print_summary(uint32_t count, uint32_t ms) { + float sec = ms / 1000.0F; + + // Print to serial + Serial.printf("Received %d bytes in %.2f seconds\n", count, sec); + Serial.printf("Speed: %.2f KB/s\n\n", (count / 1024.0F) / sec); + Serial.println("Ready to receive new image"); + + // Also print to TFT with color text tft.setCursor(0, imageHeight+5); tft.print("Received "); @@ -305,14 +342,14 @@ void print_summary(uint32_t count, uint32_t ms) tft.print(" bytes in "); tft.setTextColor(COLOR_YELLOW); - tft.print(ms / 1000.0F, 2); + tft.print(sec, 2); tft.setTextColor(COLOR_WHITE); tft.println(" seconds"); tft.print("Speed: "); tft.setTextColor(COLOR_YELLOW); - tft.print( (count / 1000.0F) / (ms / 1000.0F), 2); + tft.print( (count / 1024.0F) / sec, 2); tft.setTextColor(COLOR_WHITE); tft.print(" KB/s for "); From 1f3c237fc5dd82ebc87d443616b96315ed6cc7a3 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 31 Jan 2020 11:15:15 +0700 Subject: [PATCH 05/12] add comment follow up to #413 --- libraries/Bluefruit52Lib/src/BLEDiscovery.cpp | 4 ++-- libraries/Bluefruit52Lib/src/BLEGatt.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp b/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp index a4b4d580..84276889 100644 --- a/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp +++ b/libraries/Bluefruit52Lib/src/BLEDiscovery.cpp @@ -134,8 +134,8 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar { for (uint8_t i=0; idiscovered() ) - continue; + // Skip if output chr is already discovered, happens with multiple instances of same UUIDs + if ( chr[i]->discovered() ) continue; if ( chr[i]->uuid == disc_chr->chars[d].uuid ) { diff --git a/libraries/Bluefruit52Lib/src/BLEGatt.cpp b/libraries/Bluefruit52Lib/src/BLEGatt.cpp index 35d7bef3..48b67f59 100644 --- a/libraries/Bluefruit52Lib/src/BLEGatt.cpp +++ b/libraries/Bluefruit52Lib/src/BLEGatt.cpp @@ -183,7 +183,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) // disconnect Client Services & Characteristics of the disconnected handle if ( evt_id == BLE_GAP_EVT_DISCONNECTED ) { - // Client + // Client Chr for(uint8_t i=0; i<_client.chr_count; i++) { if ( evt_conn_hdl == _client.chr_list[i]->connHandle() ) @@ -192,7 +192,7 @@ void BLEGatt::_eventHandler(ble_evt_t* evt) } } - // TODO merge to above loop + // Client Service TODO merge to above loop for(uint8_t i=0; i<_client.svc_count; i++) { if ( evt_conn_hdl == _client.svc_list[i]->_conn_hdl) From 2adabcdd3ef7d92fed1cace5514a5261de67cd26 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 31 Jan 2020 12:48:38 +0700 Subject: [PATCH 06/12] try to fix ci --- .github/workflows/githubci.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/githubci.yml b/.github/workflows/githubci.yml index c62eca8f..4239c650 100644 --- a/.github/workflows/githubci.yml +++ b/.github/workflows/githubci.yml @@ -42,6 +42,10 @@ jobs: env: BSP_URL: https://adafruit.github.io/arduino-board-index/package_adafruit_index.json BSP_PATH: .arduino15/packages/adafruit/hardware/nrf52 + LIB_DEPS: > + "Adafruit Circuit Playground" "Adafruit NeoPixel" "Adafruit NeoMatrix" + "Adafruit GFX Library" "Adafruit SSD1306" "Adafruit ILI9341" "Adafruit HX8357 Library" "Adafruit EPD" + "MIDI Library" "Firmata" run: | arduino-cli config init arduino-cli core update-index @@ -51,7 +55,7 @@ jobs: BSP_VERSION=`eval ls $HOME/$BSP_PATH` rm -r $HOME/$BSP_PATH/* ln -s $GITHUB_WORKSPACE $HOME/$BSP_PATH/$BSP_VERSION - arduino-cli lib install "Adafruit NeoPixel" "Adafruit NeoMatrix" "Adafruit GFX Library" "Adafruit SSD1306" "MIDI Library" "Adafruit ILI9341" "Adafruit HX8357 Library" "Adafruit Circuit Playground" "Firmata" + arduino-cli lib install $(LIB_DEPS) - name: Build examples run: python3 tools/build_all.py ${{ matrix.arduino-platform }} From 39fb39659a86fa9f07eb8f33776b94fa8ad64661 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 31 Jan 2020 13:19:43 +0700 Subject: [PATCH 07/12] ci again --- .github/workflows/githubci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/githubci.yml b/.github/workflows/githubci.yml index 4239c650..82ffc272 100644 --- a/.github/workflows/githubci.yml +++ b/.github/workflows/githubci.yml @@ -55,7 +55,7 @@ jobs: BSP_VERSION=`eval ls $HOME/$BSP_PATH` rm -r $HOME/$BSP_PATH/* ln -s $GITHUB_WORKSPACE $HOME/$BSP_PATH/$BSP_VERSION - arduino-cli lib install $(LIB_DEPS) + arduino-cli lib install $LIB_DEPS - name: Build examples run: python3 tools/build_all.py ${{ matrix.arduino-platform }} From 825f1bf2aff9a339933174930bca463aac3188c0 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 31 Jan 2020 13:37:48 +0700 Subject: [PATCH 08/12] more ci --- .github/workflows/githubci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/githubci.yml b/.github/workflows/githubci.yml index 82ffc272..66cb1ec0 100644 --- a/.github/workflows/githubci.yml +++ b/.github/workflows/githubci.yml @@ -43,9 +43,9 @@ jobs: BSP_URL: https://adafruit.github.io/arduino-board-index/package_adafruit_index.json BSP_PATH: .arduino15/packages/adafruit/hardware/nrf52 LIB_DEPS: > - "Adafruit Circuit Playground" "Adafruit NeoPixel" "Adafruit NeoMatrix" + '"Adafruit Circuit Playground" "Adafruit NeoPixel" "Adafruit NeoMatrix" "Adafruit GFX Library" "Adafruit SSD1306" "Adafruit ILI9341" "Adafruit HX8357 Library" "Adafruit EPD" - "MIDI Library" "Firmata" + "MIDI Library" "Firmata"' run: | arduino-cli config init arduino-cli core update-index From 0eb4c3d151ed50e342620761d0b858fe028a67d1 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 31 Jan 2020 13:42:10 +0700 Subject: [PATCH 09/12] ci --- .github/workflows/githubci.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/githubci.yml b/.github/workflows/githubci.yml index 66cb1ec0..e0bcf5e4 100644 --- a/.github/workflows/githubci.yml +++ b/.github/workflows/githubci.yml @@ -42,10 +42,6 @@ jobs: env: BSP_URL: https://adafruit.github.io/arduino-board-index/package_adafruit_index.json BSP_PATH: .arduino15/packages/adafruit/hardware/nrf52 - LIB_DEPS: > - '"Adafruit Circuit Playground" "Adafruit NeoPixel" "Adafruit NeoMatrix" - "Adafruit GFX Library" "Adafruit SSD1306" "Adafruit ILI9341" "Adafruit HX8357 Library" "Adafruit EPD" - "MIDI Library" "Firmata"' run: | arduino-cli config init arduino-cli core update-index @@ -55,7 +51,9 @@ jobs: BSP_VERSION=`eval ls $HOME/$BSP_PATH` rm -r $HOME/$BSP_PATH/* ln -s $GITHUB_WORKSPACE $HOME/$BSP_PATH/$BSP_VERSION - arduino-cli lib install $LIB_DEPS + > arduino-cli lib install "Adafruit Circuit Playground" "Adafruit NeoPixel" "Adafruit NeoMatrix" + "Adafruit GFX Library" "Adafruit SSD1306" "Adafruit ILI9341" "Adafruit HX8357 Library" "Adafruit EPD" + "MIDI Library" "Firmata" - name: Build examples run: python3 tools/build_all.py ${{ matrix.arduino-platform }} From 8e3b22be5e44de64be50c4781dcfc3c1cb9cc76d Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 31 Jan 2020 13:50:23 +0700 Subject: [PATCH 10/12] cci --- .github/workflows/githubci.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/githubci.yml b/.github/workflows/githubci.yml index e0bcf5e4..c4b483a6 100644 --- a/.github/workflows/githubci.yml +++ b/.github/workflows/githubci.yml @@ -42,6 +42,10 @@ jobs: env: BSP_URL: https://adafruit.github.io/arduino-board-index/package_adafruit_index.json BSP_PATH: .arduino15/packages/adafruit/hardware/nrf52 + LIB_DEPS: > + '"Adafruit Circuit Playground" "Adafruit NeoPixel" "Adafruit NeoMatrix"' + '"Adafruit GFX Library" "Adafruit SSD1306" "Adafruit ILI9341" "Adafruit HX8357 Library" "Adafruit EPD"' + '"MIDI Library" "Firmata"' run: | arduino-cli config init arduino-cli core update-index @@ -51,9 +55,7 @@ jobs: BSP_VERSION=`eval ls $HOME/$BSP_PATH` rm -r $HOME/$BSP_PATH/* ln -s $GITHUB_WORKSPACE $HOME/$BSP_PATH/$BSP_VERSION - > arduino-cli lib install "Adafruit Circuit Playground" "Adafruit NeoPixel" "Adafruit NeoMatrix" - "Adafruit GFX Library" "Adafruit SSD1306" "Adafruit ILI9341" "Adafruit HX8357 Library" "Adafruit EPD" - "MIDI Library" "Firmata" + arduino-cli lib install $LIB_DEPS - name: Build examples run: python3 tools/build_all.py ${{ matrix.arduino-platform }} From ee900fcd798aba5115aec2f61d27c0932ccea18f Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 31 Jan 2020 13:59:53 +0700 Subject: [PATCH 11/12] cii --- .github/workflows/githubci.yml | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/.github/workflows/githubci.yml b/.github/workflows/githubci.yml index c4b483a6..de18bfee 100644 --- a/.github/workflows/githubci.yml +++ b/.github/workflows/githubci.yml @@ -42,10 +42,6 @@ jobs: env: BSP_URL: https://adafruit.github.io/arduino-board-index/package_adafruit_index.json BSP_PATH: .arduino15/packages/adafruit/hardware/nrf52 - LIB_DEPS: > - '"Adafruit Circuit Playground" "Adafruit NeoPixel" "Adafruit NeoMatrix"' - '"Adafruit GFX Library" "Adafruit SSD1306" "Adafruit ILI9341" "Adafruit HX8357 Library" "Adafruit EPD"' - '"MIDI Library" "Firmata"' run: | arduino-cli config init arduino-cli core update-index @@ -55,7 +51,9 @@ jobs: BSP_VERSION=`eval ls $HOME/$BSP_PATH` rm -r $HOME/$BSP_PATH/* ln -s $GITHUB_WORKSPACE $HOME/$BSP_PATH/$BSP_VERSION - arduino-cli lib install $LIB_DEPS + # Install library dependency + arduino-cli lib install "Adafruit Circuit Playground" "Adafruit NeoPixel" "Adafruit NeoMatrix" "MIDI Library" "Firmata" + "Adafruit GFX Library" "Adafruit SSD1306" "Adafruit ILI9341" "Adafruit HX8357 Library" "Adafruit EPD" - name: Build examples run: python3 tools/build_all.py ${{ matrix.arduino-platform }} From 88a4afef7ff15c8c41ae195edea09e77b9adba28 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 31 Jan 2020 14:03:18 +0700 Subject: [PATCH 12/12] fix ci --- .github/workflows/githubci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/githubci.yml b/.github/workflows/githubci.yml index de18bfee..87da2635 100644 --- a/.github/workflows/githubci.yml +++ b/.github/workflows/githubci.yml @@ -52,8 +52,8 @@ jobs: rm -r $HOME/$BSP_PATH/* ln -s $GITHUB_WORKSPACE $HOME/$BSP_PATH/$BSP_VERSION # Install library dependency + arduino-cli lib install "Adafruit GFX Library" "Adafruit SSD1306" "Adafruit ILI9341" "Adafruit HX8357 Library" "Adafruit EPD" arduino-cli lib install "Adafruit Circuit Playground" "Adafruit NeoPixel" "Adafruit NeoMatrix" "MIDI Library" "Firmata" - "Adafruit GFX Library" "Adafruit SSD1306" "Adafruit ILI9341" "Adafruit HX8357 Library" "Adafruit EPD" - name: Build examples run: python3 tools/build_all.py ${{ matrix.arduino-platform }}