summaryrefslogtreecommitdiff
path: root/tmk_core/protocol/lufa/ringbuffer.hpp
diff options
context:
space:
mode:
authorJack Humbert <jack.humb@gmail.com>2016-12-02 13:14:04 -0500
committerGitHub <noreply@github.com>2016-12-02 13:14:04 -0500
commit78f8fe361f9f6d5c32c785e90da3c868a0b255aa (patch)
tree28fe3de019ffd4c4c116c0cc9444cd391a59c85c /tmk_core/protocol/lufa/ringbuffer.hpp
parent8f0a8cb7a6c001dd07bdf0c701e40a80b16f5c15 (diff)
parent712476cd288505cabb2ad6163d1c1ba13a7a1cca (diff)
Merge pull request #915 from wez/ble_3
Add support for Adafruit BLE modules
Diffstat (limited to 'tmk_core/protocol/lufa/ringbuffer.hpp')
-rw-r--r--tmk_core/protocol/lufa/ringbuffer.hpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/tmk_core/protocol/lufa/ringbuffer.hpp b/tmk_core/protocol/lufa/ringbuffer.hpp
new file mode 100644
index 0000000000..70a3c4881d
--- /dev/null
+++ b/tmk_core/protocol/lufa/ringbuffer.hpp
@@ -0,0 +1,66 @@
+#pragma once
+// A simple ringbuffer holding Size elements of type T
+template <typename T, uint8_t Size>
+class RingBuffer {
+ protected:
+ T buf_[Size];
+ uint8_t head_{0}, tail_{0};
+ public:
+ inline uint8_t nextPosition(uint8_t position) {
+ return (position + 1) % Size;
+ }
+
+ inline uint8_t prevPosition(uint8_t position) {
+ if (position == 0) {
+ return Size - 1;
+ }
+ return position - 1;
+ }
+
+ inline bool enqueue(const T &item) {
+ static_assert(Size > 1, "RingBuffer size must be > 1");
+ uint8_t next = nextPosition(head_);
+ if (next == tail_) {
+ // Full
+ return false;
+ }
+
+ buf_[head_] = item;
+ head_ = next;
+ return true;
+ }
+
+ inline bool get(T &dest, bool commit = true) {
+ auto tail = tail_;
+ if (tail == head_) {
+ // No more data
+ return false;
+ }
+
+ dest = buf_[tail];
+ tail = nextPosition(tail);
+
+ if (commit) {
+ tail_ = tail;
+ }
+ return true;
+ }
+
+ inline bool empty() const { return head_ == tail_; }
+
+ inline uint8_t size() const {
+ int diff = head_ - tail_;
+ if (diff >= 0) {
+ return diff;
+ }
+ return Size + diff;
+ }
+
+ inline T& front() {
+ return buf_[tail_];
+ }
+
+ inline bool peek(T &item) {
+ return get(item, false);
+ }
+};