-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(flex-stacker): implement home motor sequence (#475)
- Loading branch information
1 parent
bc59475
commit 89f15dd
Showing
9 changed files
with
369 additions
and
88 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
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,69 @@ | ||
#pragma once | ||
|
||
namespace circular_buffer { | ||
|
||
template <typename T, std::size_t MaxSize> | ||
class CircularBuffer { | ||
public: | ||
using BackingStore = std::array<T, MaxSize>; | ||
|
||
explicit CircularBuffer(bool allow_overwrite = false) | ||
: _buffer(BackingStore()), _overwrite(allow_overwrite) {} | ||
|
||
CircularBuffer(CircularBuffer& other) = delete; | ||
auto operator=(CircularBuffer& other) -> CircularBuffer& = delete; | ||
CircularBuffer(CircularBuffer&& other) noexcept = delete; | ||
auto operator=(CircularBuffer&& other) noexcept -> CircularBuffer& = delete; | ||
~CircularBuffer() = default; | ||
|
||
[[nodiscard]] auto empty() const -> bool { return _count == 0; } | ||
|
||
[[nodiscard]] auto full() const -> bool { return _count == MaxSize; } | ||
|
||
[[nodiscard]] auto capacity() const -> std::size_t { return MaxSize; } | ||
|
||
[[nodiscard]] auto size() const -> std::size_t { return _count; } | ||
|
||
auto enqueue(T item) -> bool { | ||
if (!_overwrite && full()) { | ||
return false; | ||
} | ||
|
||
_buffer.at(_tail) = item; | ||
_tail = (_tail + 1) % MaxSize; | ||
|
||
if (full() && _overwrite) { | ||
// overwrite the oldest item | ||
_head = (_head + 1) % MaxSize; | ||
} else if (!full()) { | ||
_count++; | ||
} | ||
return true; | ||
} | ||
|
||
auto dequeue(T& item) -> bool { | ||
if (empty()) { | ||
return false; | ||
} | ||
item = _buffer.at(_head); | ||
_head = (_head + 1) % MaxSize; | ||
_count--; | ||
|
||
return true; | ||
} | ||
|
||
auto reset() -> void { | ||
_head = 0; | ||
_tail = 0; | ||
_count = 0; | ||
} | ||
|
||
private: | ||
BackingStore _buffer; | ||
bool _overwrite; | ||
std::size_t _head = 0; | ||
std::size_t _tail = 0; | ||
std::size_t _count = 0; | ||
}; | ||
|
||
} // namespace circular_buffer |
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
Oops, something went wrong.