-
Notifications
You must be signed in to change notification settings - Fork 516
Add support for Adafruit Feather M0 Bluefruit LE to StandardFirmataBLE #393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/* | ||
* Implementation is in BluefruitLE_SPI_Stream.h to avoid linker issues. | ||
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* | ||
BluefruitLE_SPI_Stream.h | ||
|
||
Documentation for the various AT commands used below is available at | ||
https://learn.adafruit.com/adafruit-feather-m0-bluefruit-le/at-commands | ||
*/ | ||
|
||
#ifndef _BLUEFRUIT_LE_SPI_STREAM_H_ | ||
#define _BLUEFRUIT_LE_SPI_STREAM_H_ | ||
|
||
#include <Adafruit_BluefruitLE_SPI.h> | ||
|
||
|
||
class BluefruitLE_SPI_Stream : public Stream | ||
{ | ||
public: | ||
BluefruitLE_SPI_Stream(int8_t csPin, int8_t irqPin, int8_t rstPin); | ||
|
||
void setLocalName(const char *localName); | ||
void setConnectionInterval(unsigned short minConnInterval, unsigned short maxConnInterval); | ||
void setFlushInterval(int flushInterval); | ||
|
||
void begin(); | ||
bool poll(); | ||
void end(); | ||
|
||
// Print overrides | ||
size_t write(uint8_t byte); | ||
using Print::write; // Expose other write variants | ||
|
||
// Stream overrides | ||
int available(); | ||
int read(); | ||
int peek(); | ||
void flush(); | ||
|
||
private: | ||
Adafruit_BluefruitLE_SPI ble; | ||
|
||
String localName; | ||
unsigned short minConnInterval; | ||
unsigned short maxConnInterval; | ||
|
||
uint8_t txBuffer[SDEP_MAX_PACKETSIZE]; | ||
size_t txCount; | ||
}; | ||
|
||
|
||
BluefruitLE_SPI_Stream::BluefruitLE_SPI_Stream(int8_t csPin, int8_t irqPin, int8_t rstPin) : | ||
ble(csPin, irqPin, rstPin), | ||
minConnInterval(0), | ||
maxConnInterval(0), | ||
txCount(0) | ||
{ } | ||
|
||
void BluefruitLE_SPI_Stream::setLocalName(const char *localName) | ||
{ | ||
this->localName = localName; | ||
} | ||
|
||
void BluefruitLE_SPI_Stream::setConnectionInterval(unsigned short minConnInterval, unsigned short maxConnInterval) | ||
{ | ||
this->minConnInterval = minConnInterval; | ||
this->maxConnInterval = maxConnInterval; | ||
} | ||
|
||
void BluefruitLE_SPI_Stream::setFlushInterval(int flushInterval) | ||
{ | ||
// Not used | ||
} | ||
|
||
void BluefruitLE_SPI_Stream::begin() | ||
{ | ||
// Initialize the SPI interface | ||
ble.begin(); | ||
|
||
// Perform a factory reset to make sure everything is in a known state | ||
ble.factoryReset(); | ||
|
||
// Disable command echo from Bluefruit | ||
ble.echo(false); | ||
|
||
// Change the MODE LED to indicate BLE UART activity | ||
ble.println("AT+HWMODELED=BLEUART"); | ||
|
||
// Set local name | ||
if (localName.length() > 0) { | ||
ble.print("AT+GAPDEVNAME="); | ||
ble.println(localName); | ||
} | ||
|
||
// Set connection interval | ||
if (minConnInterval > 0 && maxConnInterval > 0) { | ||
ble.print("AT+GAPINTERVALS="); | ||
ble.print(minConnInterval); | ||
ble.print(","); | ||
ble.print(maxConnInterval); | ||
ble.println(",,,"); | ||
} | ||
|
||
// Disable real and simulated mode switch (i.e. "+++") command | ||
ble.println("AT+MODESWITCHEN=local,0"); | ||
ble.enableModeSwitchCommand(false); | ||
|
||
// Switch to data mode | ||
ble.setMode(BLUEFRUIT_MODE_DATA); | ||
} | ||
|
||
bool BluefruitLE_SPI_Stream::poll() | ||
{ | ||
// If there's outgoing data in the buffer, just send it. The firmware on | ||
// the nRF51822 will decide when to transmit the data in its TX FIFO. | ||
if (txCount) flush(); | ||
|
||
// In order to check for a connection, we would need to switch from data to | ||
// command mode and back again. However, due to the internal workings of | ||
// Adafruit_BluefruitLE_SPI, this can lead to unread incoming data being | ||
// lost. Therefore, we always return true. | ||
return true; | ||
} | ||
|
||
void BluefruitLE_SPI_Stream::end() | ||
{ | ||
flush(); | ||
ble.end(); | ||
} | ||
|
||
size_t BluefruitLE_SPI_Stream::write(uint8_t byte) | ||
{ | ||
txBuffer[txCount++] = byte; | ||
if (txCount == sizeof(txBuffer)) flush(); | ||
return 1; | ||
} | ||
|
||
int BluefruitLE_SPI_Stream::available() | ||
{ | ||
return ble.available(); | ||
} | ||
|
||
int BluefruitLE_SPI_Stream::read() | ||
{ | ||
return ble.read(); | ||
} | ||
|
||
int BluefruitLE_SPI_Stream::peek() | ||
{ | ||
return ble.peek(); | ||
} | ||
|
||
void BluefruitLE_SPI_Stream::flush() | ||
{ | ||
ble.write(txBuffer, txCount); | ||
txCount = 0; | ||
} | ||
|
||
|
||
#endif // _BLUEFRUIT_LE_SPI_STREAM_H_ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove all the comments here since the user doesn't need to be concerned with the conversion anymore. I'd just update the comments on lines 34 and 35 to say
// 8ms
and// 30ms
like you did for the Bluefruit configuration.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say it's still useful to know that, under the hood, the values are converted to multiples of 1.25ms -- if only to explain why using the value 8 actually gets you an interval of 7.5ms.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok sgtm