Skip to content

Commit fc34c02

Browse files
committed
BLEUart add setNotifyCallback(), add flushTXD() for application, remove auto flush_txd() by timer.
1 parent a736419 commit fc34c02

File tree

3 files changed

+26
-76
lines changed

3 files changed

+26
-76
lines changed

libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/StandardFirmataBLE.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,4 +913,7 @@ void loop()
913913
#ifdef FIRMATA_SERIAL_FEATURE
914914
serialFeature.update();
915915
#endif
916+
917+
// flush TXD since we use bufferTXD()
918+
bleuart.flushTXD();
916919
}

libraries/Bluefruit52Lib/src/services/BLEUart.cpp

Lines changed: 15 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,21 @@ const uint8_t BLEUART_UUID_CHR_TXD[] =
6060
0x93, 0xF3, 0xA3, 0xB5, 0x03, 0x00, 0x40, 0x6E
6161
};
6262

63-
/**
64-
* Constructor
65-
*/
63+
// Constructor
6664
BLEUart::BLEUart(uint16_t fifo_depth)
6765
: BLEService(BLEUART_UUID_SERVICE), _txd(BLEUART_UUID_CHR_TXD), _rxd(BLEUART_UUID_CHR_RXD)
6866
{
6967
_rx_fifo = NULL;
70-
_rx_cb = NULL;
7168
_rx_fifo_depth = fifo_depth;
7269

70+
_rx_cb = NULL;
71+
_notify_cb = NULL;
72+
7373
_tx_fifo = NULL;
7474
_tx_buffered = false;
75-
_buffered_th = NULL;
7675
}
7776

78-
/**
79-
* Destructor
80-
*/
77+
// Destructor
8178
BLEUart::~BLEUart()
8279
{
8380
if ( _tx_fifo ) delete _tx_fifo;
@@ -99,42 +96,24 @@ void BLEUart::bleuart_rxd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t*
9996
if ( svc._rx_cb ) svc._rx_cb(conn_hdl);
10097
}
10198

102-
/**
103-
* Timer callback periodically to send TX packet (if enabled).
104-
* @param timer
105-
*/
106-
void bleuart_txd_buffered_hdlr(TimerHandle_t timer)
107-
{
108-
BLEUart* svc = (BLEUart*) pvTimerGetTimerID(timer);
109-
110-
// skip if null (unlikely)
111-
if ( !svc->_tx_fifo ) return;
112-
113-
// flush tx data
114-
(void) svc->flush_txd();
115-
}
116-
11799
void BLEUart::bleuart_txd_cccd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint16_t value)
118100
{
119101
BLEUart& svc = (BLEUart&) chr->parentService();
120102

121-
if ( svc._buffered_th == NULL) return;
122-
123-
// Enable TXD timer if configured
124-
if (value & BLE_GATT_HVX_NOTIFICATION)
125-
{
126-
xTimerStart(svc._buffered_th, 0); // if started --> timer got reset
127-
}else
128-
{
129-
xTimerStop(svc._buffered_th, 0);
130-
}
103+
if ( svc._notify_cb ) svc._notify_cb(conn_hdl, value & BLE_GATT_HVX_NOTIFICATION);
131104
}
132105

133106
void BLEUart::setRxCallback( rx_callback_t fp)
134107
{
135108
_rx_cb = fp;
136109
}
137110

111+
void BLEUart::setNotifyCallback(notify_callback_t fp)
112+
{
113+
_notify_cb = fp;
114+
_txd.setCccdWriteCallback( fp ? BLEUart::bleuart_txd_cccd_cb : NULL );
115+
}
116+
138117
/**
139118
* Enable packet buffered for TXD
140119
* Note: packet is sent right away if it reach MTU bytes
@@ -146,19 +125,14 @@ void BLEUart::bufferTXD(bool enable)
146125

147126
if ( enable )
148127
{
149-
// enable cccd callback to start timer when enabled
150-
_txd.setCccdWriteCallback(BLEUart::bleuart_txd_cccd_cb);
151-
152-
// Create FIFO for TX
128+
// Create FIFO for TXD
153129
if ( _tx_fifo == NULL )
154130
{
155131
_tx_fifo = new Adafruit_FIFO(1);
156132
_tx_fifo->begin( Bluefruit.getMaxMtu(BLE_GAP_ROLE_PERIPH) );
157133
}
158134
}else
159135
{
160-
_txd.setCccdWriteCallback(NULL);
161-
162136
if ( _tx_fifo ) delete _tx_fifo;
163137
}
164138
}
@@ -204,30 +178,6 @@ bool BLEUart::notifyEnabled(uint16_t conn_hdl)
204178
return _txd.notifyEnabled(conn_hdl);
205179
}
206180

207-
void BLEUart::svc_disconnect_hdl(uint16_t conn_hdl)
208-
{
209-
if (_buffered_th)
210-
{
211-
xTimerDelete(_buffered_th, 0);
212-
_buffered_th = NULL;
213-
214-
if (_tx_fifo) _tx_fifo->clear();
215-
}
216-
}
217-
218-
void BLEUart::svc_connect_hdl (uint16_t conn_hdl)
219-
{
220-
if ( _tx_buffered )
221-
{
222-
// create TXD timer TODO take connInterval into account
223-
// ((5*ms2tick(Bluefruit.connInterval())) / 4) / 2
224-
_buffered_th = xTimerCreate(NULL, ms2tick(10), true, this, bleuart_txd_buffered_hdlr);
225-
226-
// Start the timer
227-
xTimerStart(_buffered_th, 0);
228-
}
229-
}
230-
231181
/*------------------------------------------------------------------*/
232182
/* STREAM API
233183
*------------------------------------------------------------------*/
@@ -281,7 +231,7 @@ size_t BLEUart::write (const uint8_t *content, size_t len, uint16_t conn_hdl)
281231
else
282232
{
283233
// TX fifo has enough data, send notify right away
284-
VERIFY( flush_txd(conn_hdl), 0);
234+
VERIFY( flushTXD(conn_hdl), 0);
285235

286236
// still more data left, send them all
287237
if ( written < len )
@@ -310,7 +260,7 @@ void BLEUart::flush (void)
310260
_rx_fifo->clear();
311261
}
312262

313-
bool BLEUart::flush_txd(uint16_t conn_hdl)
263+
bool BLEUart::flushTXD(uint16_t conn_hdl)
314264
{
315265
// use default conn handle if not passed
316266
if ( conn_hdl == BLE_CONN_HANDLE_INVALID ) conn_hdl = Bluefruit.connHandle();

libraries/Bluefruit52Lib/src/services/BLEUart.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class BLEUart : public BLEService, public Stream
5252
{
5353
public:
5454
typedef void (*rx_callback_t) (uint16_t conn_hdl);
55+
typedef void (*notify_callback_t)(uint16_t conn_hdl, bool enabled);
5556

5657
BLEUart(uint16_t fifo_depth = BLE_UART_DEFAULT_FIFO_DEPTH);
5758
virtual ~BLEUart();
@@ -62,7 +63,10 @@ class BLEUart : public BLEService, public Stream
6263
bool notifyEnabled(uint16_t conn_hdl);
6364

6465
void setRxCallback (rx_callback_t fp);
65-
void bufferTXD (bool enable);
66+
void setNotifyCallback(notify_callback_t fp);
67+
68+
void bufferTXD(bool enable);
69+
bool flushTXD (uint16_t conn_hdl = BLE_CONN_HANDLE_INVALID);
6670

6771
// Stream API
6872
virtual int read ( void );
@@ -89,25 +93,18 @@ class BLEUart : public BLEService, public Stream
8993
// RXD
9094
Adafruit_FIFO* _rx_fifo;
9195
uint16_t _rx_fifo_depth;
92-
rx_callback_t _rx_cb;
9396

9497
// TXD
9598
Adafruit_FIFO* _tx_fifo;
9699
bool _tx_buffered; // default is false
97-
TimerHandle_t _buffered_th;
98-
99-
bool flush_txd(uint16_t conn_hdl = BLE_CONN_HANDLE_INVALID);
100100

101-
// from BLEService
102-
virtual void svc_disconnect_hdl(uint16_t conn_hdl);
103-
virtual void svc_connect_hdl(uint16_t conn_hdl);
101+
// Callbacks
102+
rx_callback_t _rx_cb;
103+
notify_callback_t _notify_cb;
104104

105105
// Static Method for callbacks
106106
static void bleuart_rxd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len);
107107
static void bleuart_txd_cccd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint16_t value);
108-
friend void bleuart_txd_buffered_hdlr(TimerHandle_t timer);
109108
};
110109

111-
112-
113110
#endif /* BLEUART_H_ */

0 commit comments

Comments
 (0)