adafruit / Adafruit_BluefruitLE_nRF51

Arduino library for nRF51822-based Adafruit Bluefruit LE modules
197 stars 122 forks source link

Fix some issues revealed by integration with StandardFirmataBLE #43

Closed cstawarz closed 6 years ago

cstawarz commented 6 years ago

I've been working on adding support for the Feather M0 Bluefruit LE to StandardFirmataBLE. While doing so, I discovered a few issues, which are addressed by this pull request. The changes here affect only the Adafruit_BluefruitLE_SPI class.

First, when alternating between reads and writes in DATA mode, it was possible to lose received data that hadn't yet been read. For example, I was testing a scenario where the Firmata client (a Mac desktop) sent 6 bytes to the Feather. After reading the first 3 bytes, the Firmata library wrote a response. However, because Adafruit_BluefruitLE_SPI::sendPacket always empties the RX FIFO when more_data is 0, the last 3 bytes were lost. I've changed it so that the FIFO is emptied only when not in DATA mode. Also, I modified Adafruit_BluefruitLE_SPI::setMode to empty the RX FIFO when entering DATA mode, so that a previous command response isn't interpreted as incoming UART data.

Second, I was nervous about Adafruit_BluefruitLE_SPI's simulated handling of the "+++" command. Since Firmata is a binary protocol, it's possible that that sequence of bytes could be sent to the client, which would then trigger an unexpected switch to command mode and break everything. To eliminate this possibility, I added a method to disable the simulated "+++" handling. It's still enabled by default, so the behavior of existing code won't change.

microbuilder commented 6 years ago

Thanks for the PR, and sorry for the delay merging this in!

cstawarz commented 6 years ago

Great, thanks!