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
Next Next commit
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.
  • Loading branch information
cstawarz committed Mar 27, 2018
commit e3a29322d8c00220970a84074c683ea6ba479e61
11 changes: 0 additions & 11 deletions examples/StandardFirmataBLE/StandardFirmataBLE.ino
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@
// the minimum interval for sampling analog input
#define MINIMUM_SAMPLING_INTERVAL 1

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

/*==============================================================================
* GLOBAL VARIABLES
*============================================================================*/
Expand Down Expand Up @@ -772,13 +768,6 @@ void setup()
Firmata.attach(START_SYSEX, sysexCallback);
Firmata.attach(SYSTEM_RESET, systemResetCallback);

stream.setLocalName(FIRMATA_BLE_LOCAL_NAME);
Copy link
Member

@soundanalogous soundanalogous Mar 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can live with this change, but the reason I used these setters initially was to avoid tightly coupling defines in bleConfig.h with the BLE stream classes. The only reason your change here works is all the code in BLEStream is in the header file, otherwise BLEStream would have been precompiled and the defines in bleConfig.h would not be available due to the weird way the Arduino linker works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I was trying to simplify things, but I didn't consider this issue. I'll add the setters back.


// 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_MAX_INTERVAL);

#ifdef BLE_REQ
for (byte i = 0; i < TOTAL_PINS; i++) {
if (IS_IGNORE_BLE_PINS(i)) {
Expand Down
58 changes: 41 additions & 17 deletions examples/StandardFirmataBLE/bleConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* need a unique ble local name (see below). If you are using another supported BLE board or shield,
* follow the instructions for the specific board or shield below.
*
* Make sure you have the Intel Curie Boards package v1.0.6 or higher installed via the Arduino
* Make sure you have the Intel Curie Boards package v2.0.2 or higher installed via the Arduino
* Boards Manager.
*
* Supported boards and shields:
Expand All @@ -19,6 +19,22 @@
// within the same physical space
#define FIRMATA_BLE_LOCAL_NAME "FIRMATA"

/*
* Arduino 101
*
* Make sure you have the Intel Curie Boards package v2.0.2 or higher installed via the Arduino
* Boards Manager.
*
* Test script: https://gist.github.com/soundanalogous/927360b797574ed50e27
*/
#ifdef _VARIANT_ARDUINO_101_X_
// After conversion to units of 1.25ms, both values must be between
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok sgtm

// 0x0006 (7.5ms) and 0x0c80 (4s)
#define FIRMATA_BLE_MIN_INTERVAL 8 // ( 8 * 1000) / 1250 == 0x06 -> 7.5ms
#define FIRMATA_BLE_MAX_INTERVAL 30 // (30 * 1000) / 1250 == 0x18 -> 30ms
#endif


/*
* RedBearLab BLE Shield
*
Expand All @@ -36,37 +52,47 @@
//#define REDBEAR_BLE_SHIELD

#ifdef REDBEAR_BLE_SHIELD
#include <SPI.h>
#include <BLEPeripheral.h>
#include "utility/BLEStream.h"

#define BLE_REQ 9
#define BLE_RDY 8
#define BLE_RST 4 // 4 or 7 via jumper on shield
#endif

BLEStream stream(BLE_REQ, BLE_RDY, BLE_RST);

/*
* 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).
#define FIRMATA_BLE_MIN_INTERVAL 0x0006 // 7.5ms (7.5 / 1.25)
#define FIRMATA_BLE_MAX_INTERVAL 0x0018 // 30ms (30 / 1.25)
#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


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

/*
* Arduino 101
*
* Make sure you have the Intel Curie Boards package v1.0.6 or higher installed via the Arduino
* Boards Manager.
*
* Test script: https://gist.github.com/soundanalogous/927360b797574ed50e27
*/
#ifdef _VARIANT_ARDUINO_101_X_
#include <CurieBLE.h>
#include "utility/BLEStream.h"
BLEStream stream;
#endif


#ifdef REDBEAR_BLE_SHIELD
#include <SPI.h>
#include "utility/BLEStream.h"
BLEStream stream(BLE_REQ, BLE_RDY, BLE_RST);
#endif


/*
* RedBearLab BLE Nano (with default switch settings)
*
Expand All @@ -81,7 +107,6 @@ BLEStream stream;
* the pins are currently mapped in Firmata only for the default (factory) jumper settings.
*/
// #ifdef BLE_NANO
// #include <BLEPeripheral.h>
// #include "utility/BLEStream.h"
// BLEStream stream;
// #endif
Expand All @@ -96,7 +121,6 @@ BLEStream stream;
*/
// #if defined(BLEND_MICRO) || defined(BLEND)
// #include <SPI.h>
// #include <BLEPeripheral.h>
// #include "utility/BLEStream.h"

// #define BLE_REQ 6
Expand Down
17 changes: 3 additions & 14 deletions utility/BLEStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
#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 @@ -32,7 +29,6 @@ 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 @@ -45,7 +41,6 @@ class BLEStream : public BLEPeripheral, public Stream
private:
bool _connected;
unsigned long _flushed;
int _flushInterval;
static BLEStream* _instance;

size_t _rxHead;
Expand Down Expand Up @@ -85,7 +80,6 @@ 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 @@ -100,6 +94,8 @@ 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 @@ -110,7 +106,7 @@ bool BLEStream::poll()
{
// BLEPeripheral::poll is called each time connected() is called
this->_connected = BLEPeripheral::connected();
if (millis() > this->_flushed + this->_flushInterval) {
if (millis() > this->_flushed + FIRMATA_BLE_TXBUFFER_FLUSH_INTERVAL) {
flush();
}
return this->_connected;
Expand Down Expand Up @@ -214,13 +210,6 @@ 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