refactor adafruit fifo, move malloc from constructor to begin()
This commit is contained in:
1 parent
c078c3565f
commit
ce38d3ce71
8 files changed
+22
-12
No files matched your search
@@ -52,9 +52,14 @@
|
||||
Adafruit_FIFO::Adafruit_FIFO(uint16_t depth, uint8_t item_size, bool overwrite)
|
||||
: _depth(depth), _item_size(item_size), _overwritable(overwrite)
|
||||
{
|
||||
_buffer = (uint8_t*) malloc(item_size*depth);
|
||||
_buffer= NULL;
|
||||
_mutex = NULL;
|
||||
_count = _wr_idx = _rd_idx = 0;
|
||||
}
|
||||
|
||||
void Adafruit_FIFO::begin(void)
|
||||
{
|
||||
_buffer = (uint8_t*) malloc(_item_size*_depth);
|
||||
_mutex = xSemaphoreCreateMutex();
|
||||
}
|
||||
|
||||
@@ -65,8 +70,8 @@ Adafruit_FIFO::Adafruit_FIFO(uint16_t depth, uint8_t item_size, bool overwrite)
|
||||
*/
|
||||
Adafruit_FIFO::~Adafruit_FIFO()
|
||||
{
|
||||
vSemaphoreDelete(_mutex);
|
||||
rtos_free(_buffer); // use free_name for debug
|
||||
if (_mutex) vSemaphoreDelete(_mutex);
|
||||
if (_buffer) rtos_free(_buffer);
|
||||
}
|
||||
|
||||
bool Adafruit_FIFO::_mutex_lock(bool isr)
|
||||
|
||||
@@ -64,6 +64,7 @@ class Adafruit_FIFO
|
||||
virtual ~Adafruit_FIFO();
|
||||
|
||||
|
||||
void begin(void);
|
||||
void clear(void);
|
||||
bool peek(void* buffer);
|
||||
bool peekAt(uint16_t position, void * p_buffer);
|
||||
|
||||
@@ -40,13 +40,15 @@ void bleuart_central_notify_cb(BLEClientCharacteristic& chr, uint8_t* data, uint
|
||||
|
||||
BLEClientUart::BLEClientUart(uint16_t fifo_depth)
|
||||
: BLEClientService(BLEUART_UUID_SERVICE), _txd(BLEUART_UUID_CHR_TXD), _rxd(BLEUART_UUID_CHR_RXD),
|
||||
_fifo(fifo_depth, 1)
|
||||
_rx_fifo(fifo_depth, 1)
|
||||
{
|
||||
_rx_cb = NULL;
|
||||
}
|
||||
|
||||
bool BLEClientUart::begin(void)
|
||||
{
|
||||
_rx_fifo.begin();
|
||||
|
||||
// Invoke base class begin()
|
||||
BLEClientService::begin();
|
||||
|
||||
@@ -97,7 +99,7 @@ void BLEClientUart::disconnect(void)
|
||||
void bleuart_central_notify_cb(BLEClientCharacteristic& chr, uint8_t* data, uint16_t len)
|
||||
{
|
||||
BLEClientUart& uart_svc = (BLEClientUart&) chr.parentService();
|
||||
uart_svc._fifo.write(data, len);
|
||||
uart_svc._rx_fifo.write(data, len);
|
||||
|
||||
// invoke callback
|
||||
if ( uart_svc._rx_cb ) uart_svc._rx_cb(uart_svc);
|
||||
@@ -114,7 +116,7 @@ int BLEClientUart::read (void)
|
||||
|
||||
int BLEClientUart::read (uint8_t * buf, size_t size)
|
||||
{
|
||||
return _fifo.read(buf, size);
|
||||
return _rx_fifo.read(buf, size);
|
||||
}
|
||||
|
||||
size_t BLEClientUart::write (uint8_t b)
|
||||
@@ -130,16 +132,16 @@ size_t BLEClientUart::write (const uint8_t *content, size_t len)
|
||||
|
||||
int BLEClientUart::available (void)
|
||||
{
|
||||
return _fifo.count();
|
||||
return _rx_fifo.count();
|
||||
}
|
||||
|
||||
int BLEClientUart::peek (void)
|
||||
{
|
||||
uint8_t ch;
|
||||
return _fifo.peek(&ch) ? (int) ch : EOF;
|
||||
return _rx_fifo.peek(&ch) ? (int) ch : EOF;
|
||||
}
|
||||
|
||||
void BLEClientUart::flush (void)
|
||||
{
|
||||
_fifo.clear();
|
||||
_rx_fifo.clear();
|
||||
}
|
||||
@@ -79,7 +79,7 @@ class BLEClientUart : public BLEClientService, public Stream
|
||||
BLEClientCharacteristic _txd;
|
||||
BLEClientCharacteristic _rxd;
|
||||
|
||||
Adafruit_FIFO _fifo;
|
||||
Adafruit_FIFO _rx_fifo;
|
||||
rx_callback_t _rx_cb;
|
||||
|
||||
friend void bleuart_central_notify_cb(BLEClientCharacteristic& chr, uint8_t* data, uint16_t len);
|
||||
|
||||
@@ -37,7 +37,6 @@
|
||||
#define BLEHIDADAFRUIT_H_
|
||||
|
||||
#include "bluefruit_common.h"
|
||||
#include "utility/adafruit_fifo.h"
|
||||
|
||||
#include "BLECharacteristic.h"
|
||||
#include "BLEHidGeneric.h"
|
||||
|
||||
@@ -37,7 +37,6 @@
|
||||
#define BLEHIDGENERIC_H_
|
||||
|
||||
#include "bluefruit_common.h"
|
||||
#include "utility/adafruit_fifo.h"
|
||||
|
||||
#include "BLECharacteristic.h"
|
||||
#include "BLEService.h"
|
||||
|
||||
@@ -140,6 +140,8 @@ void BLEMidi::begin(int baudrate)
|
||||
|
||||
err_t BLEMidi::begin(void)
|
||||
{
|
||||
_rxd_fifo.begin();
|
||||
|
||||
// Invoke base class begin()
|
||||
VERIFY_STATUS( BLEService::begin() );
|
||||
|
||||
|
||||
@@ -136,6 +136,8 @@ void BLEUart::bufferTXD(bool enable)
|
||||
|
||||
err_t BLEUart::begin(void)
|
||||
{
|
||||
_rxd_fifo.begin();
|
||||
|
||||
// Invoke base class begin()
|
||||
VERIFY_STATUS( BLEService::begin() );
|
||||
|
||||
|
||||
Reference in new issue
Block a user