2018-07-12 16:29:32 +02:00
|
|
|
#ifndef FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_
|
|
|
|
#define FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_
|
|
|
|
|
|
|
|
#include <framework/container/RingBufferBase.h>
|
2020-07-07 17:42:37 +02:00
|
|
|
#include <cstddef>
|
2018-07-12 16:29:32 +02:00
|
|
|
|
2019-10-29 19:31:18 +01:00
|
|
|
/**
|
2020-06-17 19:50:26 +02:00
|
|
|
* @brief Circular buffer implementation, useful for buffering
|
|
|
|
* into data streams.
|
|
|
|
* @details
|
|
|
|
* Note that the deleteData() has to be called to increment the read pointer.
|
|
|
|
* This class allocated dynamically, so
|
2020-01-12 14:18:12 +01:00
|
|
|
* @ingroup containers
|
2019-10-29 19:31:18 +01:00
|
|
|
*/
|
2018-07-12 16:29:32 +02:00
|
|
|
class SimpleRingBuffer: public RingBufferBase<> {
|
|
|
|
public:
|
2020-06-17 19:50:26 +02:00
|
|
|
/**
|
|
|
|
* This constructor allocates a new internal buffer with the supplied size.
|
|
|
|
* @param size
|
|
|
|
* @param overwriteOld
|
2020-07-09 14:26:15 +02:00
|
|
|
* If the ring buffer is overflowing at a write operartion, the oldest data
|
|
|
|
* will be overwritten.
|
2020-06-17 19:50:26 +02:00
|
|
|
*/
|
|
|
|
SimpleRingBuffer(const size_t size, bool overwriteOld);
|
|
|
|
/**
|
|
|
|
* This constructor takes an external buffer with the specified size.
|
|
|
|
* @param buffer
|
|
|
|
* @param size
|
|
|
|
* @param overwriteOld
|
2020-07-09 14:26:15 +02:00
|
|
|
* If the ring buffer is overflowing at a write operartion, the oldest data
|
|
|
|
* will be overwritten.
|
2020-06-17 19:50:26 +02:00
|
|
|
*/
|
|
|
|
SimpleRingBuffer(uint8_t* buffer, const size_t size, bool overwriteOld);
|
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
virtual ~SimpleRingBuffer();
|
2019-10-29 19:31:18 +01:00
|
|
|
|
|
|
|
/**
|
2020-07-09 14:26:15 +02:00
|
|
|
* Write to circular buffer and increment write pointer by amount.
|
2019-10-29 19:31:18 +01:00
|
|
|
* @param data
|
|
|
|
* @param amount
|
2020-07-09 14:26:15 +02:00
|
|
|
* @return -@c RETURN_OK if write operation was successfull
|
|
|
|
* -@c RETURN_FAILED if
|
2019-10-29 19:31:18 +01:00
|
|
|
*/
|
2020-07-09 14:26:15 +02:00
|
|
|
ReturnValue_t writeData(const uint8_t* data, size_t amount);
|
2019-10-29 19:31:18 +01:00
|
|
|
|
|
|
|
/**
|
2020-07-09 14:26:15 +02:00
|
|
|
* Read from circular buffer at read pointer.
|
2019-10-29 19:31:18 +01:00
|
|
|
* @param data
|
|
|
|
* @param amount
|
2020-07-09 14:26:15 +02:00
|
|
|
* @param incrementReadPtr
|
|
|
|
* If this is set to true, the read pointer will be incremented.
|
|
|
|
* If readRemaining is set to true, the read pointer will be incremented
|
|
|
|
* accordingly.
|
2019-10-29 19:31:18 +01:00
|
|
|
* @param readRemaining
|
2020-07-09 14:26:15 +02:00
|
|
|
* If this is set to true, the data will be read even if the amount
|
|
|
|
* specified exceeds the read data available.
|
|
|
|
* @param trueAmount [out]
|
|
|
|
* If readRemaining was set to true, the true amount read will be assigned
|
|
|
|
* to the passed value.
|
2019-10-29 19:31:18 +01:00
|
|
|
* @return
|
2020-07-09 14:26:15 +02:00
|
|
|
* - @c RETURN_OK if data was read successfully
|
|
|
|
* - @c RETURN_FAILED if not enough data was available and readRemaining
|
|
|
|
* was set to false.
|
2019-10-29 19:31:18 +01:00
|
|
|
*/
|
2020-07-09 14:26:15 +02:00
|
|
|
ReturnValue_t readData(uint8_t* data, size_t amount,
|
|
|
|
bool incrementReadPtr = false, bool readRemaining = false,
|
|
|
|
size_t* trueAmount = nullptr);
|
2019-10-29 19:31:18 +01:00
|
|
|
|
|
|
|
/**
|
2020-07-09 14:26:15 +02:00
|
|
|
* Delete data by incrementing read pointer.
|
2019-10-29 19:31:18 +01:00
|
|
|
* @param amount
|
|
|
|
* @param deleteRemaining
|
2020-07-09 14:26:15 +02:00
|
|
|
* If the amount specified is larger than the remaing size to read and this
|
|
|
|
* is set to true, the remaining amount will be deleted as well
|
|
|
|
* @param trueAmount [out]
|
|
|
|
* If deleteRemaining was set to true, the amount deleted will be assigned
|
|
|
|
* to the passed value.
|
2019-10-29 19:31:18 +01:00
|
|
|
* @return
|
|
|
|
*/
|
2020-07-09 14:26:15 +02:00
|
|
|
ReturnValue_t deleteData(size_t amount, bool deleteRemaining = false,
|
|
|
|
size_t* trueAmount = nullptr);
|
2018-07-12 16:29:32 +02:00
|
|
|
private:
|
|
|
|
static const uint8_t READ_PTR = 0;
|
2020-06-17 19:50:26 +02:00
|
|
|
uint8_t* buffer = nullptr;
|
2018-07-12 16:29:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* FRAMEWORK_CONTAINER_SIMPLERINGBUFFER_H_ */
|
|
|
|
|