Skip to content

Commit d72508e

Browse files
committed
add BluefruitStream wrapper for firmata
1 parent f13e373 commit d72508e

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**************************************************************************/
2+
/*!
3+
@file BluefruitStream.h
4+
5+
Wrapper for Adafruit Bluefruit nRF52
6+
*/
7+
/**************************************************************************/
8+
#ifndef BLUEFRUITSTREAM_H_
9+
#define BLUEFRUITSTREAM_H_
10+
11+
#ifdef ARDUINO_NRF52_ADAFRUIT
12+
13+
#include <bluefruit.h>
14+
15+
class BLEStream : public Stream
16+
{
17+
private:
18+
BLEUart _bleuart;
19+
const char* _name;
20+
uint16_t _conn_min, _conn_max;
21+
22+
public:
23+
BLEStream(void);
24+
25+
void begin(void);
26+
bool poll();
27+
void setLocalName(const char* str) { _name = str; }
28+
void setConnectionInterval(uint16_t min, uint16_t max) { _conn_min = min; _conn_max = max; }
29+
void setFlushInterval(int interval) {} // flush interval is handled internally by BLEUart
30+
31+
// Forward all stream API to bleuart
32+
virtual int available (void) { return _bleuart.available(); }
33+
virtual int peek (void) { return _bleuart.peek(); }
34+
virtual int read (void) { return _bleuart.read(); }
35+
virtual void flush (void) { return _bleuart.flush(); }
36+
virtual size_t write (uint8_t byte) { return _bleuart.write(byte); }
37+
38+
virtual size_t write ( const uint8_t *content, size_t len )
39+
{
40+
return _bleuart.write(content, len);
41+
}
42+
using Print::write;
43+
44+
// virtual operator bool();
45+
};
46+
47+
BLEStream::BLEStream(void)
48+
{
49+
_name = NULL;
50+
_conn_min = 9; // 9*1.25=11.25 ms
51+
_conn_max = 16; // 16*1.25=20ms
52+
}
53+
54+
void BLEStream::begin(void)
55+
{
56+
Bluefruit.begin();
57+
58+
// set name
59+
if (_name) Bluefruit.setName(_name);
60+
61+
// set connection interval
62+
Bluefruit.setConnInterval(_conn_min, _conn_max);
63+
64+
// Configure and Start BLE Uart Service
65+
// Firmata use several small write(1) --> buffering TXD is required to run smoothly
66+
// Enable buffering TXD
67+
_bleuart.begin();
68+
_bleuart.bufferTXD(true);
69+
70+
/*------------- Advertising -------------*/
71+
// Advertising packet
72+
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
73+
Bluefruit.Advertising.addTxPower();
74+
75+
// Include bleuart 128-bit uuid
76+
Bluefruit.Advertising.addService(_bleuart);
77+
78+
// Secondary Scan Response packet (optional)
79+
// Since there is no room for 'Name' in Advertising packet
80+
Bluefruit.ScanResponse.addName();
81+
82+
/* Start Advertising
83+
* - Enable auto advertising if disconnected
84+
* - Interval: fast mode = 20 ms, slow mode = 152.5 ms
85+
* - Timeout for fast mode is 30 seconds
86+
* - Start(timeout) with timeout = 0 will advertise forever (until connected)
87+
*
88+
* For recommended advertising interval
89+
* https://developer.apple.com/library/content/qa/qa1931/_index.html
90+
*/
91+
Bluefruit.Advertising.restartOnDisconnect(true);
92+
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
93+
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
94+
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
95+
}
96+
97+
/**
98+
* Flush is handled by BLEUart internally.
99+
* @return true if connected and bleuart notify is enabled
100+
*/
101+
bool BLEStream::poll(void)
102+
{
103+
return Bluefruit.connected() && _bleuart.notifyEnabled();
104+
}
105+
106+
#endif
107+
108+
#endif /* BLUEFRUITSTREAM_H_ */

0 commit comments

Comments
 (0)