From 384d7e3a24773a4efe9c8dddb8d1f20daa558659 Mon Sep 17 00:00:00 2001 From: Jerome Hue Date: Sat, 10 Aug 2024 22:15:28 +0200 Subject: [PATCH] Test the use of an etl::cyclic_value in FramRingBuffer --- Sts1CobcSw/Periphery/FramRingBuffer.hpp | 7 ++++-- Sts1CobcSw/Periphery/FramRingBuffer.ipp | 32 +++++++++++++++---------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Sts1CobcSw/Periphery/FramRingBuffer.hpp b/Sts1CobcSw/Periphery/FramRingBuffer.hpp index 735b3d96..c95d3e9b 100644 --- a/Sts1CobcSw/Periphery/FramRingBuffer.hpp +++ b/Sts1CobcSw/Periphery/FramRingBuffer.hpp @@ -5,6 +5,8 @@ #include #include +#include + #include #include @@ -19,8 +21,8 @@ class RingBuffer { public: RingBuffer() - : bufferSize_(size + 1U){ - + : bufferSize_(size + 1U) { + cyclicNextWriteIndex_.set(0); }; auto Push(T const & newData) -> void; @@ -41,6 +43,7 @@ class RingBuffer std::uint32_t nextWriteIndex_ = 0; std::uint32_t nextReadIndex_ = 0; std::size_t bufferSize_; + etl::cyclic_value cyclicNextWriteIndex_; }; } diff --git a/Sts1CobcSw/Periphery/FramRingBuffer.ipp b/Sts1CobcSw/Periphery/FramRingBuffer.ipp index 55de4e0c..f59f548f 100644 --- a/Sts1CobcSw/Periphery/FramRingBuffer.ipp +++ b/Sts1CobcSw/Periphery/FramRingBuffer.ipp @@ -9,16 +9,19 @@ 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); + auto const rawaddress = value_of(startAddress) + (cyclicNextWriteIndex_ * serialSize); fram::WriteTo(fram::Address(rawaddress), Span(Serialize(newData)), 0); - ++nextWriteIndex_; - if(nextWriteIndex_ == bufferSize_) - { - nextWriteIndex_ = 0; - } + ++cyclicNextWriteIndex_; + //++nextWriteIndex_; + //if(nextWriteIndex_ == bufferSize_) + //{ + // nextWriteIndex_ = 0; + //} - if(nextWriteIndex_ == nextReadIndex_) + //if(nextWriteIndex_ == nextReadIndex_) + if(cyclicNextWriteIndex_ == nextReadIndex_) { ++nextReadIndex_; if(nextReadIndex_ == bufferSize_) @@ -44,13 +47,15 @@ template auto RingBuffer::Back() -> T { std::uint32_t readIndex = 0; - if(nextWriteIndex_ == 0) + //if(nextWriteIndex_ == 0) + if(cyclicNextWriteIndex_ == 0) { readIndex = bufferSize_ - 1; } else { - readIndex = nextWriteIndex_ - 1; + //readIndex = nextWriteIndex_ - 1; + readIndex = cyclicNextWriteIndex_ - 1; } auto const rawaddress = value_of(startAddress) + readIndex * serialSize; @@ -74,11 +79,14 @@ auto RingBuffer::operator[](std::size_t index) -> T template auto RingBuffer::Size() -> std::size_t { - if(nextWriteIndex_ >= nextReadIndex_) + //if(nextWriteIndex_ >= nextReadIndex_) + if(cyclicNextWriteIndex_ >= nextReadIndex_) { - return (nextWriteIndex_ - nextReadIndex_); + //return (nextWriteIndex_ - nextReadIndex_); + return (cyclicNextWriteIndex_ - nextReadIndex_); } - return (bufferSize_ - (nextReadIndex_ - nextWriteIndex_)); + //return (bufferSize_ - (nextReadIndex_ - nextWriteIndex_)); + return (bufferSize_ - (nextReadIndex_ - cyclicNextWriteIndex_)); } template