-
-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Allow looping uart streamed gcode
- Loading branch information
Showing
8 changed files
with
214 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "FixedCircularBuffer.h" | ||
|
||
#include <iostream> | ||
#include <iterator> | ||
|
||
template <typename T> | ||
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) { | ||
os << "{ "; | ||
std::copy(vec.begin(), vec.end(), std::ostream_iterator<T>(os, " ")); | ||
os << "}"; | ||
return os; | ||
} | ||
|
||
template <typename T> | ||
std::ostream& operator<<(std::ostream& os, FixedCircularBuffer<T> buff) { | ||
os << "[head_idx: " << buff.head_idx << "][tail_idx: " << buff.tail_idx << "][storage: " << buff.storage << "]"; | ||
return os; | ||
} | ||
|
||
template <typename T> | ||
std::ostream& operator<<(std::ostream& os, std::optional<T> opt) { | ||
if (opt.has_value()) { | ||
os << opt.value(); | ||
} else { | ||
os << "nullopt"; | ||
} | ||
return os; | ||
} | ||
|
||
int main() { | ||
FixedCircularBuffer<char> buffer(4); | ||
buffer.push('a'); | ||
buffer.push('b'); | ||
std::cout << "buffer is " << buffer << std::endl; | ||
std::cout << "buffer.at(0) is " << buffer.at(0) << std::endl; | ||
std::cout << "buffer.at(1) is " << buffer.at(1) << std::endl; | ||
std::cout << "buffer.at(2) is " << buffer.at(2) << std::endl; | ||
std::cout << "buffer.at(3) is " << buffer.at(3) << std::endl; | ||
std::cout << std::endl; | ||
|
||
buffer.push('c'); | ||
buffer.push('d'); | ||
std::cout << "buffer is " << buffer << std::endl; | ||
std::cout << "buffer.at(0) is " << buffer.at(0) << std::endl; | ||
std::cout << "buffer.at(1) is " << buffer.at(1) << std::endl; | ||
std::cout << "buffer.at(2) is " << buffer.at(2) << std::endl; | ||
std::cout << "buffer.at(3) is " << buffer.at(3) << std::endl; | ||
std::cout << std::endl; | ||
|
||
buffer.push('e'); | ||
std::cout << "buffer is " << buffer << std::endl; | ||
std::cout << "buffer.at(0) is " << buffer.at(0) << std::endl; | ||
std::cout << "buffer.at(1) is " << buffer.at(1) << std::endl; | ||
std::cout << "buffer.at(2) is " << buffer.at(2) << std::endl; | ||
std::cout << "buffer.at(3) is " << buffer.at(3) << std::endl; | ||
std::cout << "buffer.at(4) is " << buffer.at(4) << std::endl; | ||
std::cout << "buffer.at(5) is " << buffer.at(5) << std::endl; | ||
std::cout << std::endl; | ||
} |
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); | ||
} |