circular_buffer: implement reset()

reset() discards any data in the buffer (head == tail)

Change in memory:
Flash: -116 bytes
SRAM: 0 bytes
pull/231/head
Guðni Már Gilbert 2022-10-22 19:53:40 +00:00
parent 8a0ede668c
commit 0bf7ff1093
4 changed files with 37 additions and 6 deletions

View File

@ -31,6 +31,11 @@ public:
return ((index_t)(head - tail) % (size * 2)) == size;
}
/// Reset the indexes to empty
inline void reset() {
head = tail;
}
/// Advance the head index of the buffer.
/// No checks are performed. full() needs to be queried beforehand.
inline void push() {
@ -101,6 +106,11 @@ public:
return index.full();
}
/// Reset the circular buffer to empty
inline void reset() {
index.reset();
}
/// Insert an element into the buffer.
/// Checks for empty spot for the element and does not change the buffer content
/// in case the buffer is full.

View File

@ -163,8 +163,7 @@ void PulseGen::AbortPlannedMoves(bool halt) {
}
// drop all remaining blocks
while (!block_index.empty())
block_index.pop();
block_index.reset();
// truncate the last rate if halting
if (halt)

View File

@ -55,10 +55,7 @@ Event UserInput::ConsumeEventForPrinter() {
}
void UserInput::Clear() {
while (!eventQueue.empty()) {
Event x;
eventQueue.pop(x);
}
eventQueue.reset();
}
} // namespace user_input

View File

@ -63,6 +63,31 @@ TEST_CASE("circular_buffer::fill", "[circular_buffer]") {
REQUIRE(cb.count() == 0);
}
TEST_CASE("circular_buffer::reset", "[circular_buffer]") {
static constexpr auto size = 2;
using CB = CircularBuffer<uint8_t, uint8_t, size>;
// start with an empty buffer
CB cb;
REQUIRE(cb.empty());
// push four elements
REQUIRE(cb.push(1));
REQUIRE(cb.push(2));
// Check there are elements in the buffer
REQUIRE(cb.full());
REQUIRE(!cb.empty());
// Reset the buffer
cb.reset();
// Buffer should be empty now
REQUIRE(cb.empty());
REQUIRE(cb.count() == 0);
}
TEST_CASE("circular_buffer::wrap_around", "[circular_buffer]") {
static constexpr auto size = 4;