From aa55beb052df68d2db8cfb1f8cee22b88c5274e9 Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 18 Aug 2019 13:32:03 +0700 Subject: [PATCH 01/11] clean up --- .../Peripheral/image_upload/image_upload.ino | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino index 3ccb0c74..f99f7fcc 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -36,6 +36,10 @@ BLEUart bleuart; // uart over ble uint16_t imageWidth = 0; uint16_t imageHeight = 0; +uint16_t imageX = 0; +uint16_t imageY = 0; + +uint16_t color_buf[256]; // Statistics for speed testing uint32_t rxCount = 0; @@ -47,9 +51,7 @@ uint32_t rxLastTime = 0; #define TFT_CS 31 #define STMPE_CS 30 #define SD_CS 27 -#endif - -#ifdef ARDUINO_NRF52840_FEATHER +#else #define TFT_DC 10 #define TFT_CS 9 #define STMPE_CS 6 @@ -86,10 +88,6 @@ void setup() // Set up and start advertising startAdv(); - // splash screen effect - delay(100); - - tft.println("Advertising ... "); } @@ -124,6 +122,30 @@ void startAdv(void) void loop() { + if ( !Bluefruit.connected() ) return; + if ( !bleuart.notifyEnabled() ) return; + + // extract pixel data and display on TFT + uint16_t pixelNum = 0; + while ( bleuart.available() >= 3 ) + { + uint8_t red = bleuart.read(); + uint8_t green = bleuart.read(); + uint8_t blue = bleuart.read(); + + color_buf[pixelNum++] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ( blue >> 3); + + rxCount += 3; + } + + if ( pixelNum ) + { + tft.drawRGBBitmap(imageX, imageY, color_buf, imageWidth, imageHeight); + + imageX += (imageX + pixelNum) % imageWidth; + imageY += pixelNum/imageHeight; + } + // 3 seconds has passed and there is no data received // then reset rx count if ( (rxCount > 0) && (rxLastTime + 1000 < millis()) ) @@ -170,21 +192,23 @@ void bleuart_rx_callback(uint16_t conn_hdl) rxLastTime = millis(); - // first packet + // first packet: reset time, extract Image Width & Height if ( rxCount == 0 ) { rxStartTime = millis(); // Incorrect format, possibly corrupted data if ( bleuart.read() != '!' ) bleuart.flush(); - rxCount++; + + imageWidth = bleuart.read16(); + imageHeight = bleuart.read16(); + + rxCount += 5; tft.fillScreen(ILI9341_BLACK); tft.setCursor(0, 0); + imageX = imageY = 0; } - - rxCount += bleuart.available(); - bleuart.flush(); // empty rx fifo } /** From 48d1b3c95c7eb5df84d301d578398730d3c0c378 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 19 Aug 2019 17:47:51 +0700 Subject: [PATCH 02/11] able draw 16x16 image, 32x32 has some tft noise --- .../Peripheral/image_upload/image_upload.ino | 67 +++++++++++++------ tools/build_all.py | 2 +- 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino index e6f072d4..10dc1adb 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -22,7 +22,10 @@ #include #include -BLEUart bleuart; // uart over ble +// Uart over BLE with large buffer to hold image, image size is +// size = width x height x 3 + 6 +// +BLEUart bleuart(1024*10); /* The Image Transfer module sends the image of your choice to Bluefruit LE over UART. * Each image sent begins with @@ -39,7 +42,7 @@ uint16_t imageHeight = 0; uint16_t imageX = 0; uint16_t imageY = 0; -uint16_t color_buf[256]; +uint16_t color_buf[512]; // Statistics for speed testing uint32_t rxCount = 0; @@ -128,35 +131,47 @@ void loop() { if ( !Bluefruit.connected() ) return; if ( !bleuart.notifyEnabled() ) return; + if ( !bleuart.available() ) return; + + PRINT_INT(bleuart.available()); + + // all pixel data is received + if ( rxCount == 1 + 5 + imageWidth*imageHeight*3 ) + { + uint8_t crc = bleuart.read(); + rxCount++; + // do checksum later + + print_speed(rxCount, rxLastTime-rxStartTime); + rxCount = 0; + } // extract pixel data and display on TFT - uint16_t pixelNum = 0; - while ( bleuart.available() >= 3 ) - { - uint8_t red = bleuart.read(); - uint8_t green = bleuart.read(); - uint8_t blue = bleuart.read(); + uint16_t pixelNum = bleuart.available() / 3; - color_buf[pixelNum++] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ( blue >> 3); - - rxCount += 3; - } + // draw up to 512 pixels since color_buf array size is 512 + // the rest will be drawn in the next invocation of loop() + pixelNum = min(pixelNum, sizeof(color_buf)/2); if ( pixelNum ) { + PRINT_INT(pixelNum); + for ( uint16_t i=0; i < pixelNum; i++) + { + uint8_t red = bleuart.read(); + uint8_t green = bleuart.read(); + uint8_t blue = bleuart.read(); + + color_buf[i] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ( blue >> 3); + + rxCount += 3; + } + tft.drawRGBBitmap(imageX, imageY, color_buf, imageWidth, imageHeight); imageX += (imageX + pixelNum) % imageWidth; imageY += pixelNum/imageHeight; } - - // 3 seconds has passed and there is no data received - // then reset rx count - if ( (rxCount > 0) && (rxLastTime + 1000 < millis()) ) - { - print_speed(rxCount, rxLastTime-rxStartTime); - rxCount = 0; - } } void connect_callback(uint16_t conn_handle) @@ -202,11 +217,21 @@ void bleuart_rx_callback(uint16_t conn_hdl) rxStartTime = millis(); // Incorrect format, possibly corrupted data - if ( bleuart.read() != '!' ) bleuart.flush(); + if ( bleuart.read() != '!' ) + { + PRINT_LOCATION(); + bleuart.flush(); + return; + } + bleuart.read(); rxCount++; // skip unicode extra byte following '!' + imageWidth = bleuart.read16(); imageHeight = bleuart.read16(); + PRINT_INT(imageWidth); + PRINT_INT(imageHeight); + rxCount += 5; tft.fillScreen(ILI9341_BLACK); diff --git a/tools/build_all.py b/tools/build_all.py index 010a2358..37aeab3e 100644 --- a/tools/build_all.py +++ b/tools/build_all.py @@ -27,7 +27,7 @@ def build_examples(variant): build_result = subprocess.run("arduino --verify {}".format(sketch), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) if build_result.returncode != 0: exit_status = build_result.returncode - success = "\033[31mfailed\033[0m" + success = "\033[31mfailed\033[0m " fail_count += 1 else: success = "\033[32msucceeded\033[0m" From fc8fa9926d362b98a5d4354f780195cad377dd1d Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 19 Aug 2019 18:03:30 +0700 Subject: [PATCH 03/11] debug info --- .../examples/Peripheral/image_upload/image_upload.ino | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino index 10dc1adb..7bd93079 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -133,7 +133,7 @@ void loop() if ( !bleuart.notifyEnabled() ) return; if ( !bleuart.available() ) return; - PRINT_INT(bleuart.available()); + //PRINT_INT(bleuart.available()); // all pixel data is received if ( rxCount == 1 + 5 + imageWidth*imageHeight*3 ) @@ -156,6 +156,9 @@ void loop() if ( pixelNum ) { PRINT_INT(pixelNum); + PRINT_INT(imageX); + PRINT_INT(imageY); + for ( uint16_t i=0; i < pixelNum; i++) { uint8_t red = bleuart.read(); From 9cb7937d9f57c34844d3c2cbefa551a29d8d1d41 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 19 Aug 2019 22:28:35 +0700 Subject: [PATCH 04/11] able to upload and display 128x128 image still has issue with 256x256 --- .../Peripheral/image_upload/image_upload.ino | 53 +++++++++---------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino index 7bd93079..093b883b 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -42,10 +42,11 @@ uint16_t imageHeight = 0; uint16_t imageX = 0; uint16_t imageY = 0; +uint32_t totalPixel = 0; // received pixel + uint16_t color_buf[512]; // Statistics for speed testing -uint32_t rxCount = 0; uint32_t rxStartTime = 0; uint32_t rxLastTime = 0; @@ -133,32 +134,30 @@ void loop() if ( !bleuart.notifyEnabled() ) return; if ( !bleuart.available() ) return; - //PRINT_INT(bleuart.available()); - // all pixel data is received - if ( rxCount == 1 + 5 + imageWidth*imageHeight*3 ) + if ( totalPixel == imageWidth*imageHeight ) { uint8_t crc = bleuart.read(); - rxCount++; // do checksum later - print_speed(rxCount, rxLastTime-rxStartTime); - rxCount = 0; + print_speed(totalPixel*3 + 7, rxLastTime-rxStartTime); + + // reset and waiting for new image + totalPixel = imageWidth = imageHeight = 0; } // extract pixel data and display on TFT uint16_t pixelNum = bleuart.available() / 3; - // draw up to 512 pixels since color_buf array size is 512 + // Draw up to color_buf size (512 pixel) // the rest will be drawn in the next invocation of loop() pixelNum = min(pixelNum, sizeof(color_buf)/2); + // Chop pixel number to multiple of image width + pixelNum = (pixelNum / imageWidth) * imageWidth; + if ( pixelNum ) { - PRINT_INT(pixelNum); - PRINT_INT(imageX); - PRINT_INT(imageY); - for ( uint16_t i=0; i < pixelNum; i++) { uint8_t red = bleuart.read(); @@ -166,14 +165,14 @@ void loop() uint8_t blue = bleuart.read(); color_buf[i] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ( blue >> 3); - - rxCount += 3; } - tft.drawRGBBitmap(imageX, imageY, color_buf, imageWidth, imageHeight); + tft.drawRGBBitmap(imageX, imageY, color_buf, imageWidth, imageHeight - imageY); - imageX += (imageX + pixelNum) % imageWidth; - imageY += pixelNum/imageHeight; + totalPixel += pixelNum; + + imageX = totalPixel % imageWidth; + imageY = totalPixel / imageWidth; } } @@ -214,20 +213,16 @@ void bleuart_rx_callback(uint16_t conn_hdl) rxLastTime = millis(); - // first packet: reset time, extract Image Width & Height - if ( rxCount == 0 ) + // Received new Image + if ( (imageWidth == 0) && (imageHeight == 0) ) { rxStartTime = millis(); - // Incorrect format, possibly corrupted data - if ( bleuart.read() != '!' ) - { - PRINT_LOCATION(); - bleuart.flush(); - return; - } + // Skip all data until '!' is found + while( bleuart.available() && bleuart.read() != '!' ) { } + if ( !bleuart.available() ) return; - bleuart.read(); rxCount++; // skip unicode extra byte following '!' + bleuart.read(); // skip unicode extra byte following '!' imageWidth = bleuart.read16(); imageHeight = bleuart.read16(); @@ -235,8 +230,6 @@ void bleuart_rx_callback(uint16_t conn_hdl) PRINT_INT(imageWidth); PRINT_INT(imageHeight); - rxCount += 5; - tft.fillScreen(ILI9341_BLACK); tft.setCursor(0, 0); imageX = imageY = 0; @@ -255,4 +248,6 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason) tft.fillScreen(ILI9341_BLACK); tft.setCursor(0, 0); tft.println("Advertising ..."); + + totalPixel = 0; } From dcca92309a4b60df5f6182eb029e34288cdf25a7 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 20 Aug 2019 13:07:38 +0700 Subject: [PATCH 05/11] print speed result to the tft --- .../Peripheral/image_upload/image_upload.ino | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino index 093b883b..81e4da8e 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -22,9 +22,7 @@ #include #include -// Uart over BLE with large buffer to hold image, image size is -// size = width x height x 3 + 6 -// +// Uart over BLE with large buffer to hold image data BLEUart bleuart(1024*10); /* The Image Transfer module sends the image of your choice to Bluefruit LE over UART. @@ -140,6 +138,7 @@ void loop() uint8_t crc = bleuart.read(); // do checksum later + tft.setCursor(0, imageHeight+1); print_speed(totalPixel*3 + 7, rxLastTime-rxStartTime); // reset and waiting for new image @@ -149,8 +148,9 @@ void loop() // extract pixel data and display on TFT uint16_t pixelNum = bleuart.available() / 3; - // Draw up to color_buf size (512 pixel) - // the rest will be drawn in the next invocation of loop() + // Draw multiple lines of image each time i.e pixelNum = n*imageWidth + // pixelNum is capped at color_buf size (512 pixel) + // the rest will be drawn in the next invocation of loop(). pixelNum = min(pixelNum, sizeof(color_buf)/2); // Chop pixel number to multiple of image width @@ -191,20 +191,36 @@ void connect_callback(uint16_t conn_handle) conn->requestMtuExchange(247); tft.println("Exchanging MTU"); - tft.println("Ready to receive Image"); + tft.setTextColor(ILI9341_GREEN); + tft.println("Ready to receive new image"); + tft.setTextColor(ILI9341_WHITE); } void print_speed(uint32_t count, uint32_t ms) { - Serial.print("Received "); - Serial.print(count); - Serial.print(" bytes in "); - Serial.print(ms / 1000.0F, 2); - Serial.println(" seconds."); + tft.print("Received "); - Serial.print("Speed : "); - Serial.print( (count / 1000.0F) / (ms / 1000.0F), 2); - Serial.println(" KB/s.\r\n"); + tft.setTextColor(ILI9341_YELLOW); + tft.print(count); + tft.setTextColor(ILI9341_WHITE); + + tft.print(" bytes in "); + + tft.setTextColor(ILI9341_YELLOW); + tft.print(ms / 1000.0F, 2); + tft.setTextColor(ILI9341_WHITE); + + tft.println(" seconds"); + + tft.print("Speed: "); + tft.setTextColor(ILI9341_YELLOW); + tft.print( (count / 1000.0F) / (ms / 1000.0F), 2); + tft.setTextColor(ILI9341_WHITE); + tft.println(" KB/s"); + + tft.setTextColor(ILI9341_GREEN); + tft.println("Ready to receive new image"); + tft.setTextColor(ILI9341_WHITE); } void bleuart_rx_callback(uint16_t conn_hdl) From 454db84ae5d5f28399de7c50ef8cb2bb4a7087df Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 21 Aug 2019 17:10:20 +0700 Subject: [PATCH 06/11] improve mutex lock/unlock for fifo --- cores/nRF5/utility/adafruit_fifo.cpp | 138 +++++++++--------- cores/nRF5/utility/adafruit_fifo.h | 11 +- .../Peripheral/image_upload/image_upload.ino | 3 +- 3 files changed, 76 insertions(+), 76 deletions(-) diff --git a/cores/nRF5/utility/adafruit_fifo.cpp b/cores/nRF5/utility/adafruit_fifo.cpp index e20ac1ec..6474167b 100644 --- a/cores/nRF5/utility/adafruit_fifo.cpp +++ b/cores/nRF5/utility/adafruit_fifo.cpp @@ -86,19 +86,6 @@ Adafruit_FIFO::~Adafruit_FIFO() if (_buffer) rtos_free(_buffer); } -bool Adafruit_FIFO::_mutex_lock(bool isr) -{ - (void) isr; - return xSemaphoreTake(_mutex, portMAX_DELAY); -} - -bool Adafruit_FIFO::_mutex_unlock(bool isr) -{ - (void) isr; - return xSemaphoreGive(_mutex); -} - - /******************************************************************************/ /*! @brief Clear the FIFO @@ -106,25 +93,14 @@ bool Adafruit_FIFO::_mutex_unlock(bool isr) /******************************************************************************/ void Adafruit_FIFO::clear(void) { - _mutex_lock(false); + _mutex_lock(); _rd_idx = _wr_idx = _count = 0; - _mutex_unlock(false); + _mutex_unlock(); } -/******************************************************************************/ -/*! - @brief Write an item to the FIFO - @param[in] item - Memory address of the item -*/ -/******************************************************************************/ -uint16_t Adafruit_FIFO::write(void const* item) +void Adafruit_FIFO::_push(void const* item) { - if ( full() && !_overwritable ) return 0; - - _mutex_lock(false); - memcpy( _buffer + (_wr_idx * _item_size), item, _item_size); @@ -139,8 +115,25 @@ uint16_t Adafruit_FIFO::write(void const* item) { _count++; } +} - _mutex_unlock(false); +/******************************************************************************/ +/*! + @brief Write an item to the FIFO + + @param[in] item + Memory address of the item +*/ +/******************************************************************************/ +uint16_t Adafruit_FIFO::write(void const* item) +{ + if ( full() && !_overwritable ) return 0; + + _mutex_lock(); + + _push(item); + + _mutex_unlock(); return 1; } @@ -157,20 +150,38 @@ uint16_t Adafruit_FIFO::write(void const* item) @return Number of written items */ /******************************************************************************/ -uint16_t Adafruit_FIFO::write(void const * data, uint16_t n) +uint16_t Adafruit_FIFO::write(void const * data, uint16_t count) { - if ( n == 0 ) return 0; + if ( count == 0 ) return 0; - uint8_t* buf = (uint8_t*) data; + _mutex_lock(); - uint16_t len = 0; - while( (len < n) && write(buf) ) + // Not overwritable limit up to full + if (!_overwritable) count = min(count, remaining()); + + uint8_t const* buf8 = (uint8_t const*) data; + uint16_t n = 0; + + while (n < count) { - len++; - buf += _item_size; + _push(buf8); + + n++; + buf8 += _item_size; } - return len; + _mutex_unlock(); + + return n; +} + +void Adafruit_FIFO::_pull(void * buffer) +{ + memcpy(buffer, + _buffer + (_rd_idx * _item_size), + _item_size); + _rd_idx = (_rd_idx + 1) % _depth; + _count--; } /******************************************************************************/ @@ -185,15 +196,11 @@ uint16_t Adafruit_FIFO::read(void* buffer) { if( empty() ) return 0; - _mutex_lock(false); + _mutex_lock(); - memcpy(buffer, - _buffer + (_rd_idx * _item_size), - _item_size); - _rd_idx = (_rd_idx + 1) % _depth; - _count--; + _pull(buffer); - _mutex_unlock(false); + _mutex_unlock(); return 1; } @@ -210,42 +217,31 @@ uint16_t Adafruit_FIFO::read(void* buffer) @return Number of read items */ /******************************************************************************/ -uint16_t Adafruit_FIFO::read(void * buffer, uint16_t n) +uint16_t Adafruit_FIFO::read(void * buffer, uint16_t count) { - if( n == 0 ) return 0; + if( count == 0 ) return 0; - uint8_t* buf = (uint8_t*) buffer; + _mutex_lock(); - uint16_t len = 0; - while( (len < n) && read(buf) ) + /* Limit up to fifo's count */ + if ( count > _count ) count = _count; + + uint8_t* buf8 = (uint8_t*) buffer; + uint16_t n = 0; + + while (n < count) { - len++; - buf += _item_size; + _pull(buf8); + + n++; + buf8 += _item_size; } - return len; + _mutex_unlock(); + + return n; } -/******************************************************************************/ -/*! - @brief Read an item without removing it from the FIFO - - @param[in] buffer - Memory address to store item -*/ -/******************************************************************************/ -bool Adafruit_FIFO::peek(void* buffer) -{ - if( empty() ) return false; - - memcpy(buffer, - _buffer + (_rd_idx * _item_size), - _item_size); - - return true; -} - - /******************************************************************************/ /*! @brief Read an item without removing it from the FIFO at the specific index diff --git a/cores/nRF5/utility/adafruit_fifo.h b/cores/nRF5/utility/adafruit_fifo.h index bfeb333e..31ce5b7e 100644 --- a/cores/nRF5/utility/adafruit_fifo.h +++ b/cores/nRF5/utility/adafruit_fifo.h @@ -54,8 +54,11 @@ class Adafruit_FIFO SemaphoreHandle_t _mutex; - bool _mutex_lock(bool isr); - bool _mutex_unlock(bool isr); + bool _mutex_lock (void) { return xSemaphoreTake(_mutex, portMAX_DELAY); } + bool _mutex_unlock(void) { return xSemaphoreGive(_mutex); } + + void _pull(void * buffer); + void _push(void const* item); public: // Constructor @@ -75,8 +78,8 @@ class Adafruit_FIFO uint16_t read(void* buffer); uint16_t read(void * buffer, uint16_t n); - bool peek(void* buffer); - bool peekAt(uint16_t position, void * p_buffer); + bool peekAt(uint16_t position, void * buffer); + bool peek(void* buffer) { return peekAt(0, buffer); } inline bool empty(void) { return _count == 0; } inline bool full(void) { return _count == _depth; } diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino index 81e4da8e..65642b45 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -138,7 +138,7 @@ void loop() uint8_t crc = bleuart.read(); // do checksum later - tft.setCursor(0, imageHeight+1); + // print speed summary print_speed(totalPixel*3 + 7, rxLastTime-rxStartTime); // reset and waiting for new image @@ -198,6 +198,7 @@ void connect_callback(uint16_t conn_handle) void print_speed(uint32_t count, uint32_t ms) { + tft.setCursor(0, imageHeight+5); tft.print("Received "); tft.setTextColor(ILI9341_YELLOW); From d8f91cda33ddc0dfafe670fa2bf5e574c1ebc743 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 21 Aug 2019 18:18:16 +0700 Subject: [PATCH 07/11] increase color_buf to prevent bleuart buffer overflow Able to draw 240x240 --- .../Peripheral/image_upload/image_upload.ino | 16 +++++++++------- .../Peripheral/throughput/throughput.ino | 5 +---- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino index 65642b45..da12fc65 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -27,12 +27,12 @@ BLEUart bleuart(1024*10); /* 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) + * - A single byte char '!' (0x21) followed by 'I' helper for image * - 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. * - * Format: [ '!' ] [ uint16 width ] [ uint16 height ] [ r g b ] [ r g b ] [ r g b ] … [ CRC ] + * Format: [ '!' ] [ 'I' ] [ uint16 width ] [ uint16 height ] [ r g b ] [ r g b ] [ r g b ] … [ CRC ] */ uint16_t imageWidth = 0; @@ -42,7 +42,9 @@ uint16_t imageY = 0; uint32_t totalPixel = 0; // received pixel -uint16_t color_buf[512]; +// color buf must be large enough to consume incoming data fast enough +// otherwise bleuart fifo could be overflow and start dropping data +uint16_t color_buf[2048]; // Statistics for speed testing uint32_t rxStartTime = 0; @@ -81,7 +83,7 @@ void setup() Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); Bluefruit.begin(); - Bluefruit.setTxPower(4); // Check bluefruit.h for supported values + Bluefruit.setTxPower(8); // Check bluefruit.h for supported values Bluefruit.setName("Bluefruit52"); Bluefruit.Periph.setConnectCallback(connect_callback); Bluefruit.Periph.setDisconnectCallback(disconnect_callback); @@ -235,11 +237,11 @@ void bleuart_rx_callback(uint16_t conn_hdl) { rxStartTime = millis(); - // Skip all data until '!' is found + // Skip all data until '!I' is found while( bleuart.available() && bleuart.read() != '!' ) { } - if ( !bleuart.available() ) return; + if (bleuart.read() != 'I') return; - bleuart.read(); // skip unicode extra byte following '!' + if ( !bleuart.available() ) return; imageWidth = bleuart.read16(); imageHeight = bleuart.read16(); diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/throughput/throughput.ino b/libraries/Bluefruit52Lib/examples/Peripheral/throughput/throughput.ino index 55a2f919..a5597a31 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/throughput/throughput.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/throughput/throughput.ino @@ -162,7 +162,7 @@ void bleuart_rx_callback(uint16_t conn_hdl) rxCount += count; bleuart.flush(); // empty rx fifo - Serial.printf("RX %d bytes\n", count); + // Serial.printf("RX %d bytes\n", count); } void bleuart_notify_callback(uint16_t conn_hdl, bool enabled) @@ -232,9 +232,6 @@ void loop(void) print_speed("Received ", rxCount, rxLastTime-rxStartTime); rxCount = 0; } - - // Bluefruit.disconnect(0); - // delay(2000); } } From 89eb4a1f83ad24b48abae2ff811efffcac9668bf Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 22 Aug 2019 10:09:08 +0700 Subject: [PATCH 08/11] clean up --- .../examples/Peripheral/image_upload/image_upload.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino index da12fc65..f9316c10 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -268,5 +268,5 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason) tft.setCursor(0, 0); tft.println("Advertising ..."); - totalPixel = 0; + totalPixel = imageWidth = imageHeight = 0; } From 7d2d4c7547eb41ee6e7c618e1271279bf34cdec3 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 22 Aug 2019 10:53:49 +0700 Subject: [PATCH 09/11] clean up and update travis build script --- tools/build_all.py | 47 ++++++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/tools/build_all.py b/tools/build_all.py index 37aeab3e..10f37e51 100644 --- a/tools/build_all.py +++ b/tools/build_all.py @@ -13,11 +13,27 @@ success_count = 0 fail_count = 0 build_format = '| {:20} | {:30} | {:9} ' -build_separator = '-' * 77 +build_separator = '-' * 78 + +variants_dict = { + 'feather52840': 'Feather nRF52840 Express', + 'cplaynrf52840': 'Circuit Playground Bluefruit Express', + 'feather52832': 'Feather nRF52832' +} def build_examples(variant): global exit_status, success_count, fail_count, build_format, build_separator + + print('\n') + print(build_separator) + print('| {:^74} |'.format(variants_dict[variant])) + print(build_separator) + print((build_format + '| {:6} |').format('Library', 'Example', 'Result', 'Time')) + print(build_separator) + subprocess.run("arduino --board adafruit:nrf52:{}:softdevice={},debug=l0 --save-prefs".format(variant, 's140v6' if variant != 'feather52832' else 's132v6'), shell=True, + stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + for sketch in glob.iglob('libraries/**/*.ino', recursive=True): start_time = time.monotonic() @@ -38,7 +54,7 @@ def build_examples(variant): if travis: print('travis_fold:start:build-{}\\r'.format(sketch)) - print((build_format + '| {:.2f}s |').format(sketch.split(os.path.sep)[1], os.path.basename(sketch), success, build_duration)) + print((build_format + '| {:5.2f}s |').format(sketch.split(os.path.sep)[1], os.path.basename(sketch), success, build_duration)) if build_result.returncode != 0: print(build_result.stdout.decode("utf-8")) @@ -49,31 +65,8 @@ def build_examples(variant): build_time = time.monotonic() -print(build_separator) -print('| {:^73} |'.format('Feather nRF52840 Express')) -print(build_separator) -print((build_format + '| {:5} |').format('Library', 'Example', 'Result', 'Time')) -print(build_separator) -subprocess.run("arduino --board adafruit:nrf52:feather52840:softdevice=s140v6,debug=l0 --save-prefs", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) -build_examples('feather52840') - -print('\r\n') -print(build_separator) -print('| {:^73} |'.format('Feather nRF52832')) -print((build_format + '| {:5} |').format('Library', 'Example', 'Result', 'Time')) -print(build_separator) -subprocess.run("arduino --board adafruit:nrf52:feather52832:softdevice=s132v6,debug=l0 --save-prefs", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) -build_examples('feather52832') - -print('\r\n') -print(build_separator) -print('| {:^73} |'.format('Circuit Playground Bluefruit Express')) -print(build_separator) -print((build_format + '| {:5} |').format('Library', 'Example', 'Result', 'Time')) -print(build_separator) -subprocess.run("arduino --board adafruit:nrf52:cplaynrf52840:softdevice=s140v6,debug=l0 --save-prefs", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) -build_examples('cplaynrf52840') - +for variant in variants_dict: + build_examples(variant) print(build_separator) build_time = time.monotonic() - build_time From 22ab53aa9925afed661d21e8ebf89742126d1532 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 22 Aug 2019 10:56:00 +0700 Subject: [PATCH 10/11] fix script typo --- tools/build_all.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/build_all.py b/tools/build_all.py index 10f37e51..2ce47a0e 100644 --- a/tools/build_all.py +++ b/tools/build_all.py @@ -65,12 +65,12 @@ def build_examples(variant): build_time = time.monotonic() -for variant in variants_dict: - build_examples(variant) +for var in variants_dict: + build_examples(var) print(build_separator) build_time = time.monotonic() - build_time -print("Build Sumamary: {} \033[32msucceeded\033[0m, {} \033[31mfailed\033[0m and took {:.2f}s".format(success_count, fail_count, build_time)) +print("Build Summary: {} \033[32msucceeded\033[0m, {} \033[31mfailed\033[0m and took {:.2f}s".format(success_count, fail_count, build_time)) print(build_separator) sys.exit(exit_status) From 6682a172af9bbf65a8e87e83acbc56c74fd164de Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 22 Aug 2019 12:35:07 +0700 Subject: [PATCH 11/11] change the arch to common macro for all nrf52 by adafruit --- libraries/PDM/src/PDM.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/PDM/src/PDM.h b/libraries/PDM/src/PDM.h index be965be0..d41f84fc 100644 --- a/libraries/PDM/src/PDM.h +++ b/libraries/PDM/src/PDM.h @@ -21,7 +21,7 @@ #include -#if !defined(ARDUINO_ARCH_ADAFRUIT_NRF52_ARDUINO) +#if !defined(ARDUINO_NRF52_ADAFRUIT) #error "This library targets only Adafruit NRF52840 boards" #endif