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 db26cafa..f9316c10 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/image_upload/image_upload.ino @@ -22,23 +22,31 @@ #include #include -BLEUart bleuart; // uart over ble +// 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. * 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; uint16_t imageHeight = 0; +uint16_t imageX = 0; +uint16_t imageY = 0; + +uint32_t totalPixel = 0; // received pixel + +// 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 rxCount = 0; uint32_t rxStartTime = 0; uint32_t rxLastTime = 0; @@ -75,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); @@ -88,10 +96,6 @@ void setup() // Set up and start advertising startAdv(); - // splash screen effect - delay(100); - - tft.println("Advertising ... "); } @@ -126,12 +130,51 @@ void startAdv(void) void loop() { - // 3 seconds has passed and there is no data received - // then reset rx count - if ( (rxCount > 0) && (rxLastTime + 1000 < millis()) ) + if ( !Bluefruit.connected() ) return; + if ( !bleuart.notifyEnabled() ) return; + if ( !bleuart.available() ) return; + + // all pixel data is received + if ( totalPixel == imageWidth*imageHeight ) { - print_speed(rxCount, rxLastTime-rxStartTime); - rxCount = 0; + uint8_t crc = bleuart.read(); + // do checksum later + + // print speed summary + 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 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 + pixelNum = (pixelNum / imageWidth) * imageWidth; + + if ( 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); + } + + tft.drawRGBBitmap(imageX, imageY, color_buf, imageWidth, imageHeight - imageY); + + totalPixel += pixelNum; + + imageX = totalPixel % imageWidth; + imageY = totalPixel / imageWidth; } } @@ -150,20 +193,37 @@ 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.setCursor(0, imageHeight+5); + 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) @@ -172,21 +232,27 @@ void bleuart_rx_callback(uint16_t conn_hdl) rxLastTime = millis(); - // first packet - if ( rxCount == 0 ) + // Received new Image + if ( (imageWidth == 0) && (imageHeight == 0) ) { rxStartTime = millis(); - // Incorrect format, possibly corrupted data - if ( bleuart.read() != '!' ) bleuart.flush(); - rxCount++; + // Skip all data until '!I' is found + while( bleuart.available() && bleuart.read() != '!' ) { } + if (bleuart.read() != 'I') return; + + if ( !bleuart.available() ) return; + + imageWidth = bleuart.read16(); + imageHeight = bleuart.read16(); + + PRINT_INT(imageWidth); + PRINT_INT(imageHeight); tft.fillScreen(ILI9341_BLACK); tft.setCursor(0, 0); + imageX = imageY = 0; } - - rxCount += bleuart.available(); - bleuart.flush(); // empty rx fifo } /** @@ -201,4 +267,6 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason) tft.fillScreen(ILI9341_BLACK); tft.setCursor(0, 0); tft.println("Advertising ..."); -} \ No newline at end of file + + totalPixel = imageWidth = imageHeight = 0; +} 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); } } 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 diff --git a/tools/build_all.py b/tools/build_all.py index 010a2358..2ce47a0e 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() @@ -27,7 +43,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" @@ -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,35 +65,12 @@ 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 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)