2020-09-04 15:09:24 +02:00
|
|
|
#ifndef FSFW_CONTAINER_RINGBUFFERBASE_H_
|
|
|
|
#define FSFW_CONTAINER_RINGBUFFERBASE_H_
|
2016-06-15 23:48:41 +02:00
|
|
|
|
2020-08-13 20:53:35 +02:00
|
|
|
#include "../returnvalues/HasReturnvaluesIF.h"
|
2020-09-04 15:09:24 +02:00
|
|
|
#include <cstddef>
|
2016-06-15 23:48:41 +02:00
|
|
|
|
|
|
|
template<uint8_t N_READ_PTRS = 1>
|
|
|
|
class RingBufferBase {
|
|
|
|
public:
|
2020-09-04 15:09:24 +02:00
|
|
|
RingBufferBase(size_t startAddress, const size_t size, bool overwriteOld) :
|
|
|
|
start(startAddress), write(startAddress), size(size),
|
|
|
|
overwriteOld(overwriteOld) {
|
2016-06-15 23:48:41 +02:00
|
|
|
for (uint8_t count = 0; count < N_READ_PTRS; count++) {
|
|
|
|
read[count] = startAddress;
|
|
|
|
}
|
|
|
|
}
|
2020-09-04 15:09:24 +02:00
|
|
|
|
|
|
|
virtual ~RingBufferBase() {}
|
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
bool isFull(uint8_t n = 0) {
|
|
|
|
return (availableWriteSpace(n) == 0);
|
|
|
|
}
|
|
|
|
bool isEmpty(uint8_t n = 0) {
|
2020-09-04 15:09:24 +02:00
|
|
|
return (getAvailableReadData(n) == 0);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 15:09:24 +02:00
|
|
|
size_t getAvailableReadData(uint8_t n = 0) const {
|
|
|
|
return ((write + size) - read[n]) % size;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2020-09-04 15:09:24 +02:00
|
|
|
size_t availableWriteSpace(uint8_t n = 0) const {
|
|
|
|
//One less to avoid ambiguous full/empty problem.
|
|
|
|
return (((read[n] + size) - write - 1) % size);
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2020-09-04 15:09:24 +02:00
|
|
|
|
|
|
|
bool overwritesOld() const {
|
|
|
|
return overwriteOld;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2020-09-04 15:09:24 +02:00
|
|
|
|
|
|
|
size_t getMaxSize() const {
|
|
|
|
return size - 1;
|
2016-06-15 23:48:41 +02:00
|
|
|
}
|
2020-09-04 15:09:24 +02:00
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
void clear() {
|
|
|
|
write = start;
|
|
|
|
for (uint8_t count = 0; count < N_READ_PTRS; count++) {
|
|
|
|
read[count] = start;
|
|
|
|
}
|
|
|
|
}
|
2020-09-04 15:09:24 +02:00
|
|
|
|
|
|
|
size_t writeTillWrap() {
|
2016-06-15 23:48:41 +02:00
|
|
|
return (start + size) - write;
|
|
|
|
}
|
2020-09-04 15:09:24 +02:00
|
|
|
|
|
|
|
size_t readTillWrap(uint8_t n = 0) {
|
2016-06-15 23:48:41 +02:00
|
|
|
return (start + size) - read[n];
|
|
|
|
}
|
2020-09-04 15:09:24 +02:00
|
|
|
|
|
|
|
size_t getStart() const {
|
2016-06-15 23:48:41 +02:00
|
|
|
return start;
|
|
|
|
}
|
2020-09-04 15:09:24 +02:00
|
|
|
|
2018-07-12 16:29:32 +02:00
|
|
|
protected:
|
2020-09-04 15:09:24 +02:00
|
|
|
const size_t start;
|
|
|
|
size_t write;
|
|
|
|
size_t read[N_READ_PTRS];
|
|
|
|
const size_t size;
|
2016-06-15 23:48:41 +02:00
|
|
|
const bool overwriteOld;
|
2020-09-04 15:09:24 +02:00
|
|
|
|
2016-06-15 23:48:41 +02:00
|
|
|
void incrementWrite(uint32_t amount) {
|
|
|
|
write = ((write + amount - start) % size) + start;
|
|
|
|
}
|
|
|
|
void incrementRead(uint32_t amount, uint8_t n = 0) {
|
|
|
|
read[n] = ((read[n] + amount - start) % size) + start;
|
|
|
|
}
|
2020-09-04 15:09:24 +02:00
|
|
|
|
|
|
|
ReturnValue_t readData(uint32_t amount, uint8_t n = 0) {
|
|
|
|
if (getAvailableReadData(n) >= amount) {
|
|
|
|
incrementRead(amount, n);
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t writeData(uint32_t amount) {
|
|
|
|
if (availableWriteSpace() >= amount or overwriteOld) {
|
|
|
|
incrementWrite(amount);
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
} else {
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getRead(uint8_t n = 0) const {
|
|
|
|
return read[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
void setRead(uint32_t read, uint8_t n = 0) {
|
|
|
|
if (read >= start && read < (start+size)) {
|
|
|
|
this->read[n] = read;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getWrite() const {
|
|
|
|
return write;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setWrite(uint32_t write) {
|
|
|
|
this->write = write;
|
|
|
|
}
|
2016-06-15 23:48:41 +02:00
|
|
|
};
|
|
|
|
|
2020-09-04 15:09:24 +02:00
|
|
|
#endif /* FSFW_CONTAINER_RINGBUFFERBASE_H_ */
|