Skip to content

Commit 9788f7f

Browse files
add ble connection interval and ble TX flush interval
1 parent 733746f commit 9788f7f

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

examples/StandardFirmataBLE/StandardFirmataBLE.ino

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
See file LICENSE.txt for further informations on licensing terms.
2222
23-
Last updated by Jeff Hoefs: February 27th, 2016
23+
Last updated by Jeff Hoefs: March 5th, 2016
2424
*/
2525

2626
#include <Servo.h>
@@ -47,6 +47,9 @@
4747
// the minimum interval for sampling analog input
4848
#define MINIMUM_SAMPLING_INTERVAL 1
4949

50+
// min cannot be < 0x0006. Adjust max if necessary
51+
#define FIRMATA_BLE_MIN_INTERVAL 0x0006 // 7.5ms (7.5 / 1.25)
52+
#define FIRMATA_BLE_MAX_INTERVAL 0x0018 // 30ms (30 / 1.25)
5053

5154
/*==============================================================================
5255
* GLOBAL VARIABLES
@@ -751,7 +754,12 @@ void setup()
751754
Firmata.attach(START_SYSEX, sysexCallback);
752755
Firmata.attach(SYSTEM_RESET, systemResetCallback);
753756

754-
stream.setLocalName("FIRMATA");
757+
stream.setLocalName(FIRMATA_BLE_LOCAL_NAME);
758+
759+
// set the BLE connection interval - this is the fastest interval you can read inputs
760+
stream.setConnectionInterval(FIRMATA_BLE_MIN_INTERVAL, FIRMATA_BLE_MAX_INTERVAL);
761+
// set how often the BLE TX buffer is flushed (if not full)
762+
stream.setFlushInterval(FIRMATA_BLE_MAX_INTERVAL);
755763

756764
#ifdef BLE_REQ
757765
for (byte i = 0; i < TOTAL_PINS; i++) {
@@ -775,7 +783,7 @@ void loop()
775783
byte pin, analogPin;
776784

777785
// do not process data if no BLE connection is established
778-
// poll will send the TX buffer every 30ms while it contains data
786+
// poll will send the TX buffer at the specified flush interval or when the buffer is full
779787
if (!stream.poll()) return;
780788

781789
/* DIGITALREAD - as fast as possible, check for changes and output them to the

examples/StandardFirmataBLE/bleConfig.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
/*===================================================================================
1+
/*==================================================================================================
22
* BLE CONFIGURATION
33
*
4-
* If you are using an Arduino 101, you do not need to make any changes to this
5-
* file. If you are using another supported BLE board or shield, follow the
6-
* instructions for the specific board or shield below.
4+
* If you are using an Arduino 101, you do not need to make any changes to this file (unless you
5+
* need a unique ble local name (see below). If you are using another supported BLE board or shield,
6+
* follow the instructions for the specific board or shield below.
77
*
88
* Supported boards and shields:
99
* - Arduino 101 (recommended)
1010
* - RedBearLab BLE Shield (v2) ** to be verified **
1111
* - RedBearLab BLE Nano ** works with modifications **
1212
*
13-
*==================================================================================*/
13+
*================================================================================================*/
14+
15+
// change this to a unique name per board if running StandardFirmataBLE on multiple boards
16+
// within the same physical space
17+
#define FIRMATA_BLE_LOCAL_NAME "FIRMATA"
1418

1519
/*
1620
* RedBearLab BLE Shield
@@ -42,18 +46,17 @@ BLEStream stream(BLE_REQ, BLE_RDY, BLE_RST);
4246
#endif
4347

4448

45-
/*===================================================================================
49+
/*==================================================================================================
4650
* END BLE CONFIGURATION - you should not need to change anything below this line
47-
*==================================================================================*/
51+
*================================================================================================*/
4852

4953
/*
5054
* Arduino 101
5155
*
5256
* Test script: https://gist.github.com/soundanalogous/927360b797574ed50e27
5357
*/
5458
#ifdef _VARIANT_ARDUINO_101_X_
55-
#include <CurieBle.h>
56-
//#include <CurieBLE.h> // switch to this once new Arduino 101 board package is available
59+
#include <CurieBLE.h>
5760
#include "utility/BLEStream.h"
5861
BLEStream stream;
5962
#endif

utility/BLEStream.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Based on BLESerial.cpp by Voita Molda
55
https://github.com/sandeepmistry/arduino-BLEPeripheral/blob/master/examples/serial/BLESerial.cpp
66
7-
Last updated by Jeff Hoefs: February 28th, 2016
7+
Last updated by Jeff Hoefs: March 5th, 2016
88
*/
99

1010
#include "BLEStream.h"
@@ -23,6 +23,7 @@ BLEStream::BLEStream(unsigned char req, unsigned char rdy, unsigned char rst) :
2323
this->_txCount = 0;
2424
this->_rxHead = this->_rxTail = 0;
2525
this->_flushed = 0;
26+
this->_flushInterval = BLESTREAM_TXBUFFER_FLUSH_INTERVAL;
2627
BLEStream::_instance = this;
2728

2829
addAttribute(this->_uartService);
@@ -47,7 +48,7 @@ bool BLEStream::poll()
4748
{
4849
// BLEPeripheral::poll is called each time connected() is called
4950
this->_connected = BLEPeripheral::connected();
50-
if (millis() > this->_flushed + BLESTREAM_TXBUFFER_FLUSH_INTERVAL) {
51+
if (millis() > this->_flushed + this->_flushInterval) {
5152
flush();
5253
}
5354
return this->_connected;
@@ -151,6 +152,13 @@ BLEStream::operator bool()
151152
return retval;
152153
}
153154

155+
void BLEStream::setFlushInterval(int interval)
156+
{
157+
if (interval > BLESTREAM_MIN_FLUSH_INTERVAL) {
158+
this->_flushInterval = interval;
159+
}
160+
}
161+
154162
void BLEStream::_received(const unsigned char* data, size_t size)
155163
{
156164
for (size_t i = 0; i < size; i++) {

utility/BLEStream.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
Based on BLESerial.cpp by Voita Molda
55
https://github.com/sandeepmistry/arduino-BLEPeripheral/blob/master/examples/serial/BLESerial.h
66
7-
Last updated by Jeff Hoefs: February 28th, 2016
7+
Last updated by Jeff Hoefs: March 5th, 2016
88
*/
99

1010
#ifndef _BLE_STREAM_H_
1111
#define _BLE_STREAM_H_
1212

1313
#include <Arduino.h>
1414
#if defined(_VARIANT_ARDUINO_101_X_)
15-
#include <CurieBle.h>
16-
//#include <CurieBLE.h> // switch to this once new Arduino 101 board package is available
15+
#include <CurieBLE.h>
1716
#define _MAX_ATTR_DATA_LEN_ BLE_MAX_ATTR_DATA_LEN
1817
#else
1918
#include <BLEPeripheral.h>
@@ -25,6 +24,7 @@
2524
#else
2625
#define BLESTREAM_TXBUFFER_FLUSH_INTERVAL 80
2726
#endif
27+
#define BLESTREAM_MIN_FLUSH_INTERVAL 8 // minimum interval for flushing the TX buffer
2828

2929
class BLEStream : public BLEPeripheral, public Stream
3030
{
@@ -34,6 +34,7 @@ class BLEStream : public BLEPeripheral, public Stream
3434
void begin(...);
3535
bool poll();
3636
void end();
37+
void setFlushInterval(int);
3738

3839
virtual int available(void);
3940
virtual int peek(void);
@@ -46,6 +47,7 @@ class BLEStream : public BLEPeripheral, public Stream
4647
private:
4748
bool _connected;
4849
unsigned long _flushed;
50+
int _flushInterval;
4951
static BLEStream* _instance;
5052

5153
size_t _rxHead;

0 commit comments

Comments
 (0)