Skip to content

Commit 5b8b9f0

Browse files
committed
Restored use of setter methods for BLE configuration
1 parent c2e8da3 commit 5b8b9f0

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

examples/StandardFirmataBLE/StandardFirmataBLE.ino

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,13 @@ void setup()
768768
Firmata.attach(START_SYSEX, sysexCallback);
769769
Firmata.attach(SYSTEM_RESET, systemResetCallback);
770770

771+
stream.setLocalName(FIRMATA_BLE_LOCAL_NAME);
772+
773+
// set the BLE connection interval - this is the fastest interval you can read inputs
774+
stream.setConnectionInterval(FIRMATA_BLE_MIN_INTERVAL, FIRMATA_BLE_MAX_INTERVAL);
775+
// set how often the BLE TX buffer is flushed (if not full)
776+
stream.setFlushInterval(FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL);
777+
771778
#ifdef IS_IGNORE_BLE_PINS
772779
for (byte i = 0; i < TOTAL_PINS; i++) {
773780
if (IS_IGNORE_BLE_PINS(i)) {

examples/StandardFirmataBLE/bleConfig.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
* Generic settings
8989
*/
9090
#if !defined(FIRMATA_BLE_MIN_INTERVAL) && !defined(FIRMATA_BLE_MAX_INTERVAL)
91-
// BLE connection interval - this is the fastest interval you can read inputs.
9291
// These values apply to all devices using the Arduino BLEPeripheral library
9392
// with a Nordic nRF8001 or nRF51822. Both values must be between
9493
// 0x0006 (7.5ms) and 0x0c80 (4s).
@@ -97,7 +96,6 @@
9796
#endif
9897

9998
#if !defined(FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL)
100-
// How often the BLE TX buffer is flushed (if not full)
10199
#define FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL 30 // 30ms
102100
#endif
103101

utility/BLEStream.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#define _MAX_ATTR_DATA_LEN_ BLE_ATTRIBUTE_MAX_VALUE_LENGTH
2020
#endif
2121

22+
#define BLESTREAM_TXBUFFER_FLUSH_INTERVAL 80
23+
#define BLESTREAM_MIN_FLUSH_INTERVAL 8 // minimum interval for flushing the TX buffer
24+
2225
// #define BLE_SERIAL_DEBUG
2326

2427
class BLEStream : public BLEPeripheral, public Stream
@@ -29,6 +32,7 @@ class BLEStream : public BLEPeripheral, public Stream
2932
void begin(...);
3033
bool poll();
3134
void end();
35+
void setFlushInterval(int);
3236

3337
virtual int available(void);
3438
virtual int peek(void);
@@ -41,6 +45,7 @@ class BLEStream : public BLEPeripheral, public Stream
4145
private:
4246
bool _connected;
4347
unsigned long _flushed;
48+
int _flushInterval;
4449
static BLEStream* _instance;
4550

4651
size_t _rxHead;
@@ -80,6 +85,7 @@ BLEStream::BLEStream(unsigned char req, unsigned char rdy, unsigned char rst) :
8085
this->_txCount = 0;
8186
this->_rxHead = this->_rxTail = 0;
8287
this->_flushed = 0;
88+
this->_flushInterval = BLESTREAM_TXBUFFER_FLUSH_INTERVAL;
8389
BLEStream::_instance = this;
8490

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

95101
void BLEStream::begin(...)
96102
{
97-
BLEPeripheral::setLocalName(FIRMATA_BLE_LOCAL_NAME);
98-
BLEPeripheral::setConnectionInterval(FIRMATA_BLE_MIN_INTERVAL, FIRMATA_BLE_MAX_INTERVAL);
99103
BLEPeripheral::begin();
100104
#ifdef BLE_SERIAL_DEBUG
101105
Serial.println(F("BLEStream::begin()"));
@@ -106,7 +110,7 @@ bool BLEStream::poll()
106110
{
107111
// BLEPeripheral::poll is called each time connected() is called
108112
this->_connected = BLEPeripheral::connected();
109-
if (millis() > this->_flushed + FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL) {
113+
if (millis() > this->_flushed + this->_flushInterval) {
110114
flush();
111115
}
112116
return this->_connected;
@@ -210,6 +214,13 @@ BLEStream::operator bool()
210214
return retval;
211215
}
212216

217+
void BLEStream::setFlushInterval(int interval)
218+
{
219+
if (interval > BLESTREAM_MIN_FLUSH_INTERVAL) {
220+
this->_flushInterval = interval;
221+
}
222+
}
223+
213224
void BLEStream::_received(const unsigned char* data, size_t size)
214225
{
215226
for (size_t i = 0; i < size; i++) {

utility/BluefruitLE_SPI_Stream.h

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class BluefruitLE_SPI_Stream : public Stream
1616
public:
1717
BluefruitLE_SPI_Stream(int8_t csPin, int8_t irqPin, int8_t rstPin);
1818

19+
void setLocalName(const char *localName);
20+
void setConnectionInterval(unsigned short minConnInterval, unsigned short maxConnInterval);
21+
void setFlushInterval(int flushInterval);
22+
1923
void begin();
2024
bool poll();
2125
void end();
@@ -33,16 +37,38 @@ class BluefruitLE_SPI_Stream : public Stream
3337
private:
3438
Adafruit_BluefruitLE_SPI ble;
3539

40+
String localName;
41+
unsigned short minConnInterval;
42+
unsigned short maxConnInterval;
43+
3644
uint8_t txBuffer[SDEP_MAX_PACKETSIZE];
3745
size_t txCount;
3846
};
3947

4048

4149
BluefruitLE_SPI_Stream::BluefruitLE_SPI_Stream(int8_t csPin, int8_t irqPin, int8_t rstPin) :
4250
ble(csPin, irqPin, rstPin),
51+
minConnInterval(0),
52+
maxConnInterval(0),
4353
txCount(0)
4454
{ }
4555

56+
void BluefruitLE_SPI_Stream::setLocalName(const char *localName)
57+
{
58+
this->localName = localName;
59+
}
60+
61+
void BluefruitLE_SPI_Stream::setConnectionInterval(unsigned short minConnInterval, unsigned short maxConnInterval)
62+
{
63+
this->minConnInterval = minConnInterval;
64+
this->maxConnInterval = maxConnInterval;
65+
}
66+
67+
void BluefruitLE_SPI_Stream::setFlushInterval(int flushInterval)
68+
{
69+
// Not used
70+
}
71+
4672
void BluefruitLE_SPI_Stream::begin()
4773
{
4874
// Initialize the SPI interface
@@ -58,15 +84,19 @@ void BluefruitLE_SPI_Stream::begin()
5884
ble.println("AT+HWMODELED=BLEUART");
5985

6086
// Set local name
61-
ble.print("AT+GAPDEVNAME=");
62-
ble.println(FIRMATA_BLE_LOCAL_NAME);
87+
if (localName.length() > 0) {
88+
ble.print("AT+GAPDEVNAME=");
89+
ble.println(localName);
90+
}
6391

6492
// Set connection interval
65-
ble.print("AT+GAPINTERVALS=");
66-
ble.print(FIRMATA_BLE_MIN_INTERVAL);
67-
ble.print(",");
68-
ble.print(FIRMATA_BLE_MAX_INTERVAL);
69-
ble.println(",,,");
93+
if (minConnInterval > 0 && maxConnInterval > 0) {
94+
ble.print("AT+GAPINTERVALS=");
95+
ble.print(minConnInterval);
96+
ble.print(",");
97+
ble.print(maxConnInterval);
98+
ble.println(",,,");
99+
}
70100

71101
// Disable real and simulated mode switch (i.e. "+++") command
72102
ble.println("AT+MODESWITCHEN=local,0");

0 commit comments

Comments
 (0)