diff --git a/Sts1CobcSw/Periphery/FramRingBuffer.hpp b/Sts1CobcSw/Periphery/FramRingBuffer.hpp index 735b3d96..27a60fab 100644 --- a/Sts1CobcSw/Periphery/FramRingBuffer.hpp +++ b/Sts1CobcSw/Periphery/FramRingBuffer.hpp @@ -7,6 +7,8 @@ #include +#include + #include #include #include @@ -18,10 +20,11 @@ template class RingBuffer { public: - RingBuffer() - : bufferSize_(size + 1U){ - - }; + RingBuffer() : bufferSize_(size + 1U) + { + nextWriteIndex_.set(0); + nextReadIndex_.set(0); + }; auto Push(T const & newData) -> void; @@ -38,9 +41,9 @@ class RingBuffer private: - std::uint32_t nextWriteIndex_ = 0; - std::uint32_t nextReadIndex_ = 0; std::size_t bufferSize_; + etl::cyclic_value nextWriteIndex_; + etl::cyclic_value nextReadIndex_; }; } diff --git a/Sts1CobcSw/Periphery/FramRingBuffer.ipp b/Sts1CobcSw/Periphery/FramRingBuffer.ipp index 55de4e0c..2c5c367b 100644 --- a/Sts1CobcSw/Periphery/FramRingBuffer.ipp +++ b/Sts1CobcSw/Periphery/FramRingBuffer.ipp @@ -9,22 +9,14 @@ namespace sts1cobcsw::fram template void RingBuffer::Push(T const & newData) { + // auto const rawaddress = value_of(startAddress) + (nextWriteIndex_ * serialSize); auto const rawaddress = value_of(startAddress) + (nextWriteIndex_ * serialSize); fram::WriteTo(fram::Address(rawaddress), Span(Serialize(newData)), 0); ++nextWriteIndex_; - if(nextWriteIndex_ == bufferSize_) - { - nextWriteIndex_ = 0; - } - if(nextWriteIndex_ == nextReadIndex_) { - ++nextReadIndex_; - if(nextReadIndex_ == bufferSize_) - { - nextReadIndex_ = 0; - } + nextReadIndex_++; } } @@ -50,11 +42,11 @@ auto RingBuffer::Back() -> T } else { - readIndex = nextWriteIndex_ - 1; + readIndex = nextWriteIndex_.get() - 1; } - auto const rawaddress = value_of(startAddress) + readIndex * serialSize; - auto readData = fram::ReadFrom>(fram::Address(rawaddress), 0); + auto const rawAddress = value_of(startAddress) + readIndex * serialSize; + auto readData = fram::ReadFrom>(fram::Address(rawAddress), 0); auto fromRing = Deserialize(std::span(readData)); return fromRing; @@ -64,9 +56,9 @@ auto RingBuffer::Back() -> T template auto RingBuffer::operator[](std::size_t index) -> T { - auto const rawaddress = + auto const rawAddress = value_of(startAddress) + ((nextReadIndex_ + index) % bufferSize_) * serialSize; - auto readData = fram::ReadFrom>(fram::Address(rawaddress), 0); + auto readData = fram::ReadFrom>(fram::Address(rawAddress), 0); return Deserialize(std::span(readData)); }