Skip to content

Commit

Permalink
Test the use of an etl::cyclic_value in FramRingBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromehue committed Aug 10, 2024
1 parent 049ac75 commit 384d7e3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
7 changes: 5 additions & 2 deletions Sts1CobcSw/Periphery/FramRingBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <Sts1CobcSw/Serial/Serial.hpp>
#include <Sts1CobcSw/Utility/Span.hpp>

#include <etl/cyclic_value.h>

#include <rodos-debug.h>

#include <cstddef>
Expand All @@ -19,8 +21,8 @@ class RingBuffer
{
public:
RingBuffer()
: bufferSize_(size + 1U){

: bufferSize_(size + 1U) {
cyclicNextWriteIndex_.set(0);
};

auto Push(T const & newData) -> void;
Expand All @@ -41,6 +43,7 @@ class RingBuffer
std::uint32_t nextWriteIndex_ = 0;
std::uint32_t nextReadIndex_ = 0;
std::size_t bufferSize_;
etl::cyclic_value<std::size_t, 0, size> cyclicNextWriteIndex_;
};
}

Expand Down
32 changes: 20 additions & 12 deletions Sts1CobcSw/Periphery/FramRingBuffer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ namespace sts1cobcsw::fram
template<typename T, std::size_t size, Address startAddress>
void RingBuffer<T, size, startAddress>::Push(T const & newData)
{
auto const rawaddress = value_of(startAddress) + (nextWriteIndex_ * serialSize<T>);
//auto const rawaddress = value_of(startAddress) + (nextWriteIndex_ * serialSize<T>);
auto const rawaddress = value_of(startAddress) + (cyclicNextWriteIndex_ * serialSize<T>);
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_)
Expand All @@ -44,13 +47,15 @@ template<typename T, std::size_t size, Address startAddress>
auto RingBuffer<T, size, startAddress>::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<T>;
Expand All @@ -74,11 +79,14 @@ auto RingBuffer<T, size, startAddress>::operator[](std::size_t index) -> T
template<typename T, std::size_t size, Address startAddress>
auto RingBuffer<T, size, startAddress>::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<typename T, std::size_t size, Address startAddress>
Expand Down

0 comments on commit 384d7e3

Please sign in to comment.