From 5b6ee693ac23f7ad3c3042983e3c0e2835d76126 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 26 Jul 2017 15:17:54 +0700 Subject: [PATCH] more fifo refinement --- changelog.md | 1 + cores/nRF5/utility/adafruit_fifo.cpp | 17 +++++++++++++---- cores/nRF5/utility/adafruit_fifo.h | 6 ++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/changelog.md b/changelog.md index ae671e2d..5731d282 100644 --- a/changelog.md +++ b/changelog.md @@ -6,6 +6,7 @@ - Add printf with float format aka %f - Add Servo library port +- Enhance Adafruit_FIFO, add overwriteIfFull(), begin(). change constructor's signature ### Bluefruit diff --git a/cores/nRF5/utility/adafruit_fifo.cpp b/cores/nRF5/utility/adafruit_fifo.cpp index 36b16ec8..8a362891 100644 --- a/cores/nRF5/utility/adafruit_fifo.cpp +++ b/cores/nRF5/utility/adafruit_fifo.cpp @@ -48,12 +48,14 @@ */ /******************************************************************************/ Adafruit_FIFO::Adafruit_FIFO(uint8_t item_size, uint16_t depth) - : _item_size(item_size), _depth(depth) + : _item_size(item_size) { - _buffer= NULL; - _mutex = NULL; - _count = _wr_idx = _rd_idx = 0; + _buffer = NULL; + _mutex = NULL; + _depth = depth; _overwritable = false; + + _count = _wr_idx = _rd_idx = 0; } void Adafruit_FIFO::begin(void) @@ -62,6 +64,13 @@ void Adafruit_FIFO::begin(void) _mutex = xSemaphoreCreateMutex(); } +void Adafruit_FIFO::begin(uint16_t depth) +{ + _depth = depth; + _buffer = (uint8_t*) malloc(_item_size*_depth); + _mutex = xSemaphoreCreateMutex(); +} + void Adafruit_FIFO::overwriteIfFull(bool enable) { _overwritable = enable; diff --git a/cores/nRF5/utility/adafruit_fifo.h b/cores/nRF5/utility/adafruit_fifo.h index 2a6ad48c..30e54913 100644 --- a/cores/nRF5/utility/adafruit_fifo.h +++ b/cores/nRF5/utility/adafruit_fifo.h @@ -45,7 +45,7 @@ class Adafruit_FIFO { private: uint8_t* _buffer ; ///< buffer pointer - const uint16_t _depth ; ///< max items + uint16_t _depth ; ///< max items const uint8_t _item_size ; ///< size of each item bool _overwritable ; ///< Overwrite when full volatile uint16_t _count ; ///< number of items in queue @@ -59,11 +59,13 @@ class Adafruit_FIFO public: // Constructor - Adafruit_FIFO(uint8_t item_size, uint16_t depth); + Adafruit_FIFO(uint8_t item_size, uint16_t depth = 0); virtual ~Adafruit_FIFO(); void begin(void); + void begin(uint16_t depth); + void clear(void); void overwriteIfFull(bool enable);