From fc34c0223ce56b6d9eb9750c41028392b558c50d Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 27 Feb 2019 13:18:20 +0700 Subject: [PATCH] BLEUart add setNotifyCallback(), add flushTXD() for application, remove auto flush_txd() by timer. --- .../StandardFirmataBLE/StandardFirmataBLE.ino | 3 + .../Bluefruit52Lib/src/services/BLEUart.cpp | 80 ++++--------------- .../Bluefruit52Lib/src/services/BLEUart.h | 19 ++--- 3 files changed, 26 insertions(+), 76 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/StandardFirmataBLE.ino b/libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/StandardFirmataBLE.ino index e86f953d..92fd90c6 100644 --- a/libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/StandardFirmataBLE.ino +++ b/libraries/Bluefruit52Lib/examples/Peripheral/StandardFirmataBLE/StandardFirmataBLE.ino @@ -913,4 +913,7 @@ void loop() #ifdef FIRMATA_SERIAL_FEATURE serialFeature.update(); #endif + + // flush TXD since we use bufferTXD() + bleuart.flushTXD(); } diff --git a/libraries/Bluefruit52Lib/src/services/BLEUart.cpp b/libraries/Bluefruit52Lib/src/services/BLEUart.cpp index eafce20a..1244de8e 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEUart.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEUart.cpp @@ -60,24 +60,21 @@ const uint8_t BLEUART_UUID_CHR_TXD[] = 0x93, 0xF3, 0xA3, 0xB5, 0x03, 0x00, 0x40, 0x6E }; -/** - * Constructor - */ +// Constructor BLEUart::BLEUart(uint16_t fifo_depth) : BLEService(BLEUART_UUID_SERVICE), _txd(BLEUART_UUID_CHR_TXD), _rxd(BLEUART_UUID_CHR_RXD) { _rx_fifo = NULL; - _rx_cb = NULL; _rx_fifo_depth = fifo_depth; + _rx_cb = NULL; + _notify_cb = NULL; + _tx_fifo = NULL; _tx_buffered = false; - _buffered_th = NULL; } -/** - * Destructor - */ +// Destructor BLEUart::~BLEUart() { if ( _tx_fifo ) delete _tx_fifo; @@ -99,35 +96,11 @@ void BLEUart::bleuart_rxd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* if ( svc._rx_cb ) svc._rx_cb(conn_hdl); } -/** - * Timer callback periodically to send TX packet (if enabled). - * @param timer - */ -void bleuart_txd_buffered_hdlr(TimerHandle_t timer) -{ - BLEUart* svc = (BLEUart*) pvTimerGetTimerID(timer); - - // skip if null (unlikely) - if ( !svc->_tx_fifo ) return; - - // flush tx data - (void) svc->flush_txd(); -} - void BLEUart::bleuart_txd_cccd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint16_t value) { BLEUart& svc = (BLEUart&) chr->parentService(); - if ( svc._buffered_th == NULL) return; - - // Enable TXD timer if configured - if (value & BLE_GATT_HVX_NOTIFICATION) - { - xTimerStart(svc._buffered_th, 0); // if started --> timer got reset - }else - { - xTimerStop(svc._buffered_th, 0); - } + if ( svc._notify_cb ) svc._notify_cb(conn_hdl, value & BLE_GATT_HVX_NOTIFICATION); } void BLEUart::setRxCallback( rx_callback_t fp) @@ -135,6 +108,12 @@ void BLEUart::setRxCallback( rx_callback_t fp) _rx_cb = fp; } +void BLEUart::setNotifyCallback(notify_callback_t fp) +{ + _notify_cb = fp; + _txd.setCccdWriteCallback( fp ? BLEUart::bleuart_txd_cccd_cb : NULL ); +} + /** * Enable packet buffered for TXD * Note: packet is sent right away if it reach MTU bytes @@ -146,10 +125,7 @@ void BLEUart::bufferTXD(bool enable) if ( enable ) { - // enable cccd callback to start timer when enabled - _txd.setCccdWriteCallback(BLEUart::bleuart_txd_cccd_cb); - - // Create FIFO for TX + // Create FIFO for TXD if ( _tx_fifo == NULL ) { _tx_fifo = new Adafruit_FIFO(1); @@ -157,8 +133,6 @@ void BLEUart::bufferTXD(bool enable) } }else { - _txd.setCccdWriteCallback(NULL); - if ( _tx_fifo ) delete _tx_fifo; } } @@ -204,30 +178,6 @@ bool BLEUart::notifyEnabled(uint16_t conn_hdl) return _txd.notifyEnabled(conn_hdl); } -void BLEUart::svc_disconnect_hdl(uint16_t conn_hdl) -{ - if (_buffered_th) - { - xTimerDelete(_buffered_th, 0); - _buffered_th = NULL; - - if (_tx_fifo) _tx_fifo->clear(); - } -} - -void BLEUart::svc_connect_hdl (uint16_t conn_hdl) -{ - if ( _tx_buffered ) - { - // create TXD timer TODO take connInterval into account - // ((5*ms2tick(Bluefruit.connInterval())) / 4) / 2 - _buffered_th = xTimerCreate(NULL, ms2tick(10), true, this, bleuart_txd_buffered_hdlr); - - // Start the timer - xTimerStart(_buffered_th, 0); - } -} - /*------------------------------------------------------------------*/ /* STREAM API *------------------------------------------------------------------*/ @@ -281,7 +231,7 @@ size_t BLEUart::write (const uint8_t *content, size_t len, uint16_t conn_hdl) else { // TX fifo has enough data, send notify right away - VERIFY( flush_txd(conn_hdl), 0); + VERIFY( flushTXD(conn_hdl), 0); // still more data left, send them all if ( written < len ) @@ -310,7 +260,7 @@ void BLEUart::flush (void) _rx_fifo->clear(); } -bool BLEUart::flush_txd(uint16_t conn_hdl) +bool BLEUart::flushTXD(uint16_t conn_hdl) { // use default conn handle if not passed if ( conn_hdl == BLE_CONN_HANDLE_INVALID ) conn_hdl = Bluefruit.connHandle(); diff --git a/libraries/Bluefruit52Lib/src/services/BLEUart.h b/libraries/Bluefruit52Lib/src/services/BLEUart.h index 0763bda7..ffaaf4cf 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEUart.h +++ b/libraries/Bluefruit52Lib/src/services/BLEUart.h @@ -52,6 +52,7 @@ class BLEUart : public BLEService, public Stream { public: typedef void (*rx_callback_t) (uint16_t conn_hdl); + typedef void (*notify_callback_t)(uint16_t conn_hdl, bool enabled); BLEUart(uint16_t fifo_depth = BLE_UART_DEFAULT_FIFO_DEPTH); virtual ~BLEUart(); @@ -62,7 +63,10 @@ class BLEUart : public BLEService, public Stream bool notifyEnabled(uint16_t conn_hdl); void setRxCallback (rx_callback_t fp); - void bufferTXD (bool enable); + void setNotifyCallback(notify_callback_t fp); + + void bufferTXD(bool enable); + bool flushTXD (uint16_t conn_hdl = BLE_CONN_HANDLE_INVALID); // Stream API virtual int read ( void ); @@ -89,25 +93,18 @@ class BLEUart : public BLEService, public Stream // RXD Adafruit_FIFO* _rx_fifo; uint16_t _rx_fifo_depth; - rx_callback_t _rx_cb; // TXD Adafruit_FIFO* _tx_fifo; bool _tx_buffered; // default is false - TimerHandle_t _buffered_th; - bool flush_txd(uint16_t conn_hdl = BLE_CONN_HANDLE_INVALID); - - // from BLEService - virtual void svc_disconnect_hdl(uint16_t conn_hdl); - virtual void svc_connect_hdl(uint16_t conn_hdl); + // Callbacks + rx_callback_t _rx_cb; + notify_callback_t _notify_cb; // Static Method for callbacks static void bleuart_rxd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint8_t* data, uint16_t len); static void bleuart_txd_cccd_cb(uint16_t conn_hdl, BLECharacteristic* chr, uint16_t value); - friend void bleuart_txd_buffered_hdlr(TimerHandle_t timer); }; - - #endif /* BLEUART_H_ */