From 0bf7ff1093029739ce7000ab03057add6ca48eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sat, 22 Oct 2022 19:53:40 +0000 Subject: [PATCH] circular_buffer: implement reset() reset() discards any data in the buffer (head == tail) Change in memory: Flash: -116 bytes SRAM: 0 bytes --- src/hal/circular_buffer.h | 10 ++++++++ src/modules/pulse_gen.cpp | 3 +-- src/modules/user_input.cpp | 5 +--- .../circular_buffer/test_circular_buffer.cpp | 25 +++++++++++++++++++ 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/hal/circular_buffer.h b/src/hal/circular_buffer.h index a76b1de..55d9bf0 100644 --- a/src/hal/circular_buffer.h +++ b/src/hal/circular_buffer.h @@ -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. diff --git a/src/modules/pulse_gen.cpp b/src/modules/pulse_gen.cpp index 4d85b21..827c4af 100644 --- a/src/modules/pulse_gen.cpp +++ b/src/modules/pulse_gen.cpp @@ -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) diff --git a/src/modules/user_input.cpp b/src/modules/user_input.cpp index 8bc241a..a2ed731 100644 --- a/src/modules/user_input.cpp +++ b/src/modules/user_input.cpp @@ -55,10 +55,7 @@ Event UserInput::ConsumeEventForPrinter() { } void UserInput::Clear() { - while (!eventQueue.empty()) { - Event x; - eventQueue.pop(x); - } + eventQueue.reset(); } } // namespace user_input diff --git a/tests/unit/hal/circular_buffer/test_circular_buffer.cpp b/tests/unit/hal/circular_buffer/test_circular_buffer.cpp index 0ad2803..ae3a73b 100644 --- a/tests/unit/hal/circular_buffer/test_circular_buffer.cpp +++ b/tests/unit/hal/circular_buffer/test_circular_buffer.cpp @@ -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; + + // 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;