circular_buffer: implement reset()

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

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

View File

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

View File

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

View File

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

View File

@ -63,6 +63,31 @@ TEST_CASE("circular_buffer::fill", "[circular_buffer]") {
REQUIRE(cb.count() == 0); 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]") { TEST_CASE("circular_buffer::wrap_around", "[circular_buffer]") {
static constexpr auto size = 4; static constexpr auto size = 4;