Skip to content

Commit

Permalink
Use cyclic_value for framRingBuffer indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromehue committed Aug 10, 2024
1 parent 049ac75 commit 21cb751
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
15 changes: 9 additions & 6 deletions Sts1CobcSw/Periphery/FramRingBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include <rodos-debug.h>

#include <etl/cyclic_value.h>

#include <cstddef>
#include <cstdint>
#include <span>
Expand All @@ -18,10 +20,11 @@ template<typename T, std::size_t size, Address startAddress>
class RingBuffer
{
public:
RingBuffer()
: bufferSize_(size + 1U){

};
RingBuffer() : bufferSize_(size + 1U)
{
nextWriteIndex_.set(0);
nextReadIndex_.set(0);
};

auto Push(T const & newData) -> void;

Expand All @@ -38,9 +41,9 @@ class RingBuffer


private:
std::uint32_t nextWriteIndex_ = 0;
std::uint32_t nextReadIndex_ = 0;
std::size_t bufferSize_;
etl::cyclic_value<std::size_t, 0, size> nextWriteIndex_;
etl::cyclic_value<std::size_t, 0, size> nextReadIndex_;
};
}

Expand Down
22 changes: 7 additions & 15 deletions Sts1CobcSw/Periphery/FramRingBuffer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,14 @@ 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>);
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_++;
}
}

Expand All @@ -50,11 +42,11 @@ auto RingBuffer<T, size, startAddress>::Back() -> T
}
else
{
readIndex = nextWriteIndex_ - 1;
readIndex = nextWriteIndex_.get() - 1;
}

auto const rawaddress = value_of(startAddress) + readIndex * serialSize<T>;
auto readData = fram::ReadFrom<serialSize<T>>(fram::Address(rawaddress), 0);
auto const rawAddress = value_of(startAddress) + readIndex * serialSize<T>;
auto readData = fram::ReadFrom<serialSize<T>>(fram::Address(rawAddress), 0);
auto fromRing = Deserialize<T>(std::span(readData));

return fromRing;
Expand All @@ -64,9 +56,9 @@ auto RingBuffer<T, size, startAddress>::Back() -> T
template<typename T, std::size_t size, Address startAddress>
auto RingBuffer<T, size, startAddress>::operator[](std::size_t index) -> T
{
auto const rawaddress =
auto const rawAddress =
value_of(startAddress) + ((nextReadIndex_ + index) % bufferSize_) * serialSize<T>;
auto readData = fram::ReadFrom<serialSize<T>>(fram::Address(rawaddress), 0);
auto readData = fram::ReadFrom<serialSize<T>>(fram::Address(rawAddress), 0);
return Deserialize<T>(std::span(readData));
}

Expand Down

0 comments on commit 21cb751

Please sign in to comment.