-
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1345 from bdring/dymk/uart-flow-control
Allow rewinding of commands sent via UART buffer
- Loading branch information
Showing
12 changed files
with
261 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) 2024 - Dylan Knutson <[email protected]> | ||
// Use of this source code is governed by a GPLv3 license that can be found in the LICENSE file. | ||
|
||
#pragma once | ||
|
||
#include <vector> | ||
#include <optional> | ||
|
||
/** | ||
* A fixed-size circular buffer that stores elements of type T. | ||
* Keeps track of how many elements have been pushed onto it, and allows | ||
* for indexing as if it was an infinite sized array. If indexing into | ||
* the buffer would result in an out-of-bounds access, returns std::nullopt. | ||
* | ||
* This is useful for implementing "scrollback" of a buffer of e.g. user | ||
* provided commands, without using an unbounded amount of memory. | ||
*/ | ||
template <typename T> | ||
class FixedCircularBuffer { | ||
public: | ||
std::vector<T> storage; | ||
std::size_t head_idx, tail_idx; | ||
|
||
public: | ||
FixedCircularBuffer() : FixedCircularBuffer(0) {} | ||
FixedCircularBuffer(size_t size) : storage(size), head_idx(0), tail_idx(0) {} | ||
|
||
/** | ||
* Push an element onto the end of the buffer. | ||
*/ | ||
void push(T&& elem) { | ||
storage[tail_idx % storage.size()] = std::move(elem); | ||
tail_idx += 1; | ||
if (tail_idx - head_idx > storage.size()) { | ||
head_idx += 1; | ||
} | ||
} | ||
|
||
/** | ||
* Get the element at the given index, or std::nullopt if the index is out of bounds. | ||
*/ | ||
std::optional<T> at(std::size_t idx) const { | ||
if (idx >= tail_idx) { | ||
return std::nullopt; | ||
} | ||
if (idx < head_idx) { | ||
return std::nullopt; | ||
} | ||
return storage[idx % storage.size()]; | ||
} | ||
|
||
/** | ||
* Is the buffer empty? | ||
*/ | ||
bool is_empty() const { return head_idx == tail_idx; } | ||
|
||
/** | ||
* Get the index of the last element pushed onto the buffer. | ||
*/ | ||
std::size_t position() const { return tail_idx; } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2024 - Dylan Knutson <[email protected]> | ||
// Use of this source code is governed by a GPLv3 license that can be found in the LICENSE file. | ||
|
||
#include "gtest/gtest.h" | ||
#include "src/FixedCircularBuffer.h" | ||
|
||
TEST(FixedCircularBuffer, Empty) { | ||
FixedCircularBuffer<int> buffer(0); | ||
|
||
ASSERT_TRUE(buffer.is_empty()); | ||
ASSERT_EQ(buffer.position(), 0); | ||
ASSERT_EQ(buffer.at(0), std::nullopt); | ||
ASSERT_EQ(buffer.at(1), std::nullopt); | ||
ASSERT_EQ(buffer.at(2), std::nullopt); | ||
} | ||
|
||
TEST(FixedCircularBuffer, OneElement) { | ||
FixedCircularBuffer<int> buffer(1); | ||
|
||
buffer.push(42); | ||
|
||
ASSERT_FALSE(buffer.is_empty()); | ||
ASSERT_EQ(buffer.position(), 1); | ||
ASSERT_EQ(buffer.at(0), 42); | ||
ASSERT_EQ(buffer.at(1), std::nullopt); | ||
ASSERT_EQ(buffer.at(2), std::nullopt); | ||
} | ||
|
||
TEST(FixedCircularBuffer, FrontElementsPopped) { | ||
FixedCircularBuffer<int> buffer(2); | ||
|
||
buffer.push(1); | ||
buffer.push(2); | ||
buffer.push(3); | ||
|
||
ASSERT_FALSE(buffer.is_empty()); | ||
ASSERT_EQ(buffer.position(), 3); | ||
ASSERT_EQ(buffer.at(0), std::nullopt); | ||
ASSERT_EQ(buffer.at(1), 2); | ||
ASSERT_EQ(buffer.at(2), 3); | ||
ASSERT_EQ(buffer.at(3), std::nullopt); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
ignore ok | ||
|
||
# test repeat zero times (should not print anything) | ||
-> o100 repeat [0] | ||
-> (print, fail lit 0) | ||
-> o100 endrepeat | ||
-> (print, pass lit 0) | ||
<- [MSG:INFO: PRINT, pass lit 0] | ||
|
||
# test when using a variable | ||
-> #<count> = 0 | ||
-> o100 repeat [#<count>] | ||
-> (print, fail var 0) | ||
-> o100 endrepeat | ||
-> (print, pass var 0) | ||
<- [MSG:INFO: PRINT, pass var 0] | ||
|
||
# test negative repeat (should not print anything) | ||
# todo - negative repeat should probably set an error | ||
-> o100 repeat [-1] | ||
-> (print, fail lit -1) | ||
-> o100 endrepeat | ||
-> (print, pass lit -1) | ||
<- [MSG:INFO: PRINT, pass lit -1] | ||
|
||
# test repeat a fixed number of times | ||
-> #<count> = 0 | ||
-> o100 repeat [3] | ||
-> #<count> = [#<count> + 1] | ||
-> (print, count=%d#<count>) | ||
<- [MSG:INFO: PRINT, count=1] | ||
-> o100 endrepeat | ||
<- [MSG:INFO: PRINT, count=2] | ||
<- [MSG:INFO: PRINT, count=3] | ||
|
||
# test repeating a variable number of times | ||
-> #<count> = 0 | ||
-> #<i> = 3 | ||
-> o100 repeat [#<i>] | ||
-> #<count> = [#<count> + 1] | ||
-> (print, count=%d#<count>) | ||
<- [MSG:INFO: PRINT, count=1] | ||
-> o100 endrepeat | ||
<- [MSG:INFO: PRINT, count=2] | ||
<- [MSG:INFO: PRINT, count=3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.