Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Restored use of setter methods for BLE configuration
  • Loading branch information
cstawarz committed Apr 4, 2018
commit 5b8b9f0060f645cda5c28b41a2ed7d89f19b48d9
7 changes: 7 additions & 0 deletions examples/StandardFirmataBLE/StandardFirmataBLE.ino
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,13 @@ void setup()
Firmata.attach(START_SYSEX, sysexCallback);
Firmata.attach(SYSTEM_RESET, systemResetCallback);

stream.setLocalName(FIRMATA_BLE_LOCAL_NAME);

// set the BLE connection interval - this is the fastest interval you can read inputs
stream.setConnectionInterval(FIRMATA_BLE_MIN_INTERVAL, FIRMATA_BLE_MAX_INTERVAL);
// set how often the BLE TX buffer is flushed (if not full)
stream.setFlushInterval(FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL);

#ifdef IS_IGNORE_BLE_PINS
for (byte i = 0; i < TOTAL_PINS; i++) {
if (IS_IGNORE_BLE_PINS(i)) {
Expand Down
2 changes: 0 additions & 2 deletions examples/StandardFirmataBLE/bleConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
* Generic settings
*/
#if !defined(FIRMATA_BLE_MIN_INTERVAL) && !defined(FIRMATA_BLE_MAX_INTERVAL)
// BLE connection interval - this is the fastest interval you can read inputs.
// These values apply to all devices using the Arduino BLEPeripheral library
// with a Nordic nRF8001 or nRF51822. Both values must be between
// 0x0006 (7.5ms) and 0x0c80 (4s).
Expand All @@ -97,7 +96,6 @@
#endif

#if !defined(FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL)
// How often the BLE TX buffer is flushed (if not full)
#define FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL 30 // 30ms
#endif

Expand Down
17 changes: 14 additions & 3 deletions utility/BLEStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#define _MAX_ATTR_DATA_LEN_ BLE_ATTRIBUTE_MAX_VALUE_LENGTH
#endif

#define BLESTREAM_TXBUFFER_FLUSH_INTERVAL 80
#define BLESTREAM_MIN_FLUSH_INTERVAL 8 // minimum interval for flushing the TX buffer

// #define BLE_SERIAL_DEBUG

class BLEStream : public BLEPeripheral, public Stream
Expand All @@ -29,6 +32,7 @@ class BLEStream : public BLEPeripheral, public Stream
void begin(...);
bool poll();
void end();
void setFlushInterval(int);

virtual int available(void);
virtual int peek(void);
Expand All @@ -41,6 +45,7 @@ class BLEStream : public BLEPeripheral, public Stream
private:
bool _connected;
unsigned long _flushed;
int _flushInterval;
static BLEStream* _instance;

size_t _rxHead;
Expand Down Expand Up @@ -80,6 +85,7 @@ BLEStream::BLEStream(unsigned char req, unsigned char rdy, unsigned char rst) :
this->_txCount = 0;
this->_rxHead = this->_rxTail = 0;
this->_flushed = 0;
this->_flushInterval = BLESTREAM_TXBUFFER_FLUSH_INTERVAL;
BLEStream::_instance = this;

addAttribute(this->_uartService);
Expand All @@ -94,8 +100,6 @@ BLEStream::BLEStream(unsigned char req, unsigned char rdy, unsigned char rst) :

void BLEStream::begin(...)
{
BLEPeripheral::setLocalName(FIRMATA_BLE_LOCAL_NAME);
BLEPeripheral::setConnectionInterval(FIRMATA_BLE_MIN_INTERVAL, FIRMATA_BLE_MAX_INTERVAL);
BLEPeripheral::begin();
#ifdef BLE_SERIAL_DEBUG
Serial.println(F("BLEStream::begin()"));
Expand All @@ -106,7 +110,7 @@ bool BLEStream::poll()
{
// BLEPeripheral::poll is called each time connected() is called
this->_connected = BLEPeripheral::connected();
if (millis() > this->_flushed + FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL) {
if (millis() > this->_flushed + this->_flushInterval) {
flush();
}
return this->_connected;
Expand Down Expand Up @@ -210,6 +214,13 @@ BLEStream::operator bool()
return retval;
}

void BLEStream::setFlushInterval(int interval)
{
if (interval > BLESTREAM_MIN_FLUSH_INTERVAL) {
this->_flushInterval = interval;
}
}

void BLEStream::_received(const unsigned char* data, size_t size)
{
for (size_t i = 0; i < size; i++) {
Expand Down
44 changes: 37 additions & 7 deletions utility/BluefruitLE_SPI_Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ 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();
Expand All @@ -33,16 +37,38 @@ class BluefruitLE_SPI_Stream : public Stream
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
Expand All @@ -58,15 +84,19 @@ void BluefruitLE_SPI_Stream::begin()
ble.println("AT+HWMODELED=BLEUART");

// Set local name
ble.print("AT+GAPDEVNAME=");
ble.println(FIRMATA_BLE_LOCAL_NAME);
if (localName.length() > 0) {
ble.print("AT+GAPDEVNAME=");
ble.println(localName);
}

// Set connection interval
ble.print("AT+GAPINTERVALS=");
ble.print(FIRMATA_BLE_MIN_INTERVAL);
ble.print(",");
ble.print(FIRMATA_BLE_MAX_INTERVAL);
ble.println(",,,");
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");
Expand Down