Skip to content

Commit e3a2932

Browse files
committed
Moved configuration of BLE connection and flush intervals to bleConfig.h
These are hardware-specific settings, so they don't belong with the hardware-agnostic code in StandardFirmataBLE.ino. For Arduino 101, specify connection intervals in milliseconds, as expected by BLEPeripheral::setConnectionInterval in Intel Curie Boards package v2.0.2.
1 parent a1a3067 commit e3a2932

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

examples/StandardFirmataBLE/StandardFirmataBLE.ino

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@
5656
// the minimum interval for sampling analog input
5757
#define MINIMUM_SAMPLING_INTERVAL 1
5858

59-
// min cannot be < 0x0006. Adjust max if necessary
60-
#define FIRMATA_BLE_MIN_INTERVAL 0x0006 // 7.5ms (7.5 / 1.25)
61-
#define FIRMATA_BLE_MAX_INTERVAL 0x0018 // 30ms (30 / 1.25)
62-
6359
/*==============================================================================
6460
* GLOBAL VARIABLES
6561
*============================================================================*/
@@ -772,13 +768,6 @@ void setup()
772768
Firmata.attach(START_SYSEX, sysexCallback);
773769
Firmata.attach(SYSTEM_RESET, systemResetCallback);
774770

775-
stream.setLocalName(FIRMATA_BLE_LOCAL_NAME);
776-
777-
// set the BLE connection interval - this is the fastest interval you can read inputs
778-
stream.setConnectionInterval(FIRMATA_BLE_MIN_INTERVAL, FIRMATA_BLE_MAX_INTERVAL);
779-
// set how often the BLE TX buffer is flushed (if not full)
780-
stream.setFlushInterval(FIRMATA_BLE_MAX_INTERVAL);
781-
782771
#ifdef BLE_REQ
783772
for (byte i = 0; i < TOTAL_PINS; i++) {
784773
if (IS_IGNORE_BLE_PINS(i)) {

examples/StandardFirmataBLE/bleConfig.h

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* need a unique ble local name (see below). If you are using another supported BLE board or shield,
66
* follow the instructions for the specific board or shield below.
77
*
8-
* Make sure you have the Intel Curie Boards package v1.0.6 or higher installed via the Arduino
8+
* Make sure you have the Intel Curie Boards package v2.0.2 or higher installed via the Arduino
99
* Boards Manager.
1010
*
1111
* Supported boards and shields:
@@ -19,6 +19,22 @@
1919
// within the same physical space
2020
#define FIRMATA_BLE_LOCAL_NAME "FIRMATA"
2121

22+
/*
23+
* Arduino 101
24+
*
25+
* Make sure you have the Intel Curie Boards package v2.0.2 or higher installed via the Arduino
26+
* Boards Manager.
27+
*
28+
* Test script: https://gist.github.com/soundanalogous/927360b797574ed50e27
29+
*/
30+
#ifdef _VARIANT_ARDUINO_101_X_
31+
// After conversion to units of 1.25ms, both values must be between
32+
// 0x0006 (7.5ms) and 0x0c80 (4s)
33+
#define FIRMATA_BLE_MIN_INTERVAL 8 // ( 8 * 1000) / 1250 == 0x06 -> 7.5ms
34+
#define FIRMATA_BLE_MAX_INTERVAL 30 // (30 * 1000) / 1250 == 0x18 -> 30ms
35+
#endif
36+
37+
2238
/*
2339
* RedBearLab BLE Shield
2440
*
@@ -36,37 +52,47 @@
3652
//#define REDBEAR_BLE_SHIELD
3753

3854
#ifdef REDBEAR_BLE_SHIELD
39-
#include <SPI.h>
40-
#include <BLEPeripheral.h>
41-
#include "utility/BLEStream.h"
42-
4355
#define BLE_REQ 9
4456
#define BLE_RDY 8
4557
#define BLE_RST 4 // 4 or 7 via jumper on shield
58+
#endif
4659

47-
BLEStream stream(BLE_REQ, BLE_RDY, BLE_RST);
60+
61+
/*
62+
* Generic settings
63+
*/
64+
#if !defined(FIRMATA_BLE_MIN_INTERVAL) && !defined(FIRMATA_BLE_MAX_INTERVAL)
65+
// BLE connection interval - this is the fastest interval you can read inputs.
66+
// These values apply to all devices using the Arduino BLEPeripheral library
67+
// with a Nordic nRF8001 or nRF51822. Both values must be between
68+
// 0x0006 (7.5ms) and 0x0c80 (4s).
69+
#define FIRMATA_BLE_MIN_INTERVAL 0x0006 // 7.5ms (7.5 / 1.25)
70+
#define FIRMATA_BLE_MAX_INTERVAL 0x0018 // 30ms (30 / 1.25)
71+
#endif
72+
73+
#if !defined(FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL)
74+
// How often the BLE TX buffer is flushed (if not full)
75+
#define FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL 30 // 30ms
4876
#endif
4977

5078

5179
/*==================================================================================================
5280
* END BLE CONFIGURATION - you should not need to change anything below this line
5381
*================================================================================================*/
5482

55-
/*
56-
* Arduino 101
57-
*
58-
* Make sure you have the Intel Curie Boards package v1.0.6 or higher installed via the Arduino
59-
* Boards Manager.
60-
*
61-
* Test script: https://gist.github.com/soundanalogous/927360b797574ed50e27
62-
*/
6383
#ifdef _VARIANT_ARDUINO_101_X_
64-
#include <CurieBLE.h>
6584
#include "utility/BLEStream.h"
6685
BLEStream stream;
6786
#endif
6887

6988

89+
#ifdef REDBEAR_BLE_SHIELD
90+
#include <SPI.h>
91+
#include "utility/BLEStream.h"
92+
BLEStream stream(BLE_REQ, BLE_RDY, BLE_RST);
93+
#endif
94+
95+
7096
/*
7197
* RedBearLab BLE Nano (with default switch settings)
7298
*
@@ -81,7 +107,6 @@ BLEStream stream;
81107
* the pins are currently mapped in Firmata only for the default (factory) jumper settings.
82108
*/
83109
// #ifdef BLE_NANO
84-
// #include <BLEPeripheral.h>
85110
// #include "utility/BLEStream.h"
86111
// BLEStream stream;
87112
// #endif
@@ -96,7 +121,6 @@ BLEStream stream;
96121
*/
97122
// #if defined(BLEND_MICRO) || defined(BLEND)
98123
// #include <SPI.h>
99-
// #include <BLEPeripheral.h>
100124
// #include "utility/BLEStream.h"
101125

102126
// #define BLE_REQ 6

utility/BLEStream.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
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-
2522
// #define BLE_SERIAL_DEBUG
2623

2724
class BLEStream : public BLEPeripheral, public Stream
@@ -32,7 +29,6 @@ class BLEStream : public BLEPeripheral, public Stream
3229
void begin(...);
3330
bool poll();
3431
void end();
35-
void setFlushInterval(int);
3632

3733
virtual int available(void);
3834
virtual int peek(void);
@@ -45,7 +41,6 @@ class BLEStream : public BLEPeripheral, public Stream
4541
private:
4642
bool _connected;
4743
unsigned long _flushed;
48-
int _flushInterval;
4944
static BLEStream* _instance;
5045

5146
size_t _rxHead;
@@ -85,7 +80,6 @@ BLEStream::BLEStream(unsigned char req, unsigned char rdy, unsigned char rst) :
8580
this->_txCount = 0;
8681
this->_rxHead = this->_rxTail = 0;
8782
this->_flushed = 0;
88-
this->_flushInterval = BLESTREAM_TXBUFFER_FLUSH_INTERVAL;
8983
BLEStream::_instance = this;
9084

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

10195
void BLEStream::begin(...)
10296
{
97+
BLEPeripheral::setLocalName(FIRMATA_BLE_LOCAL_NAME);
98+
BLEPeripheral::setConnectionInterval(FIRMATA_BLE_MIN_INTERVAL, FIRMATA_BLE_MAX_INTERVAL);
10399
BLEPeripheral::begin();
104100
#ifdef BLE_SERIAL_DEBUG
105101
Serial.println(F("BLEStream::begin()"));
@@ -110,7 +106,7 @@ bool BLEStream::poll()
110106
{
111107
// BLEPeripheral::poll is called each time connected() is called
112108
this->_connected = BLEPeripheral::connected();
113-
if (millis() > this->_flushed + this->_flushInterval) {
109+
if (millis() > this->_flushed + FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL) {
114110
flush();
115111
}
116112
return this->_connected;
@@ -214,13 +210,6 @@ BLEStream::operator bool()
214210
return retval;
215211
}
216212

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

0 commit comments

Comments
 (0)