libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
ringbuffer.h
Go to the documentation of this file.
1#pragma once
2#ifndef DSY_RINGBUFFER_H
3#define DSY_RINGBUFFER_H
4
5#include <algorithm>
6
7namespace daisy
8{
17template <typename T, size_t size>
19{
20 public:
22
24 inline void Init() { read_ptr_ = write_ptr_ = 0; }
25
27 inline size_t capacity() const { return size; }
28
30 inline size_t writable() const
31 {
32 return (read_ptr_ - write_ptr_ - 1) % size;
33 }
34
36 inline size_t readable() const { return (write_ptr_ - read_ptr_) % size; }
37
39 inline bool isEmpty() const { return write_ptr_ == read_ptr_; }
40
44 inline void Write(T v)
45 {
46 while(!writable())
47 ;
48 Overwrite(v);
49 }
50
54 inline void Overwrite(T v)
55 {
56 size_t w = write_ptr_;
57 buffer_[w] = v;
58 write_ptr_ = (w + 1) % size;
59 }
60
64 inline T Read()
65 {
66 while(!readable())
67 ;
68 return ImmediateRead();
69 }
70
74 inline T ImmediateRead()
75 {
76 size_t r = read_ptr_;
77 T result = buffer_[r];
78 read_ptr_ = (r + 1) % size;
79 return result;
80 }
81
83 inline void Flush() { write_ptr_ = read_ptr_; }
84
88 inline void Swallow(size_t n)
89 {
90 if(writable() >= n)
91 {
92 return;
93 }
94 read_ptr_ = (write_ptr_ + 1 + n) % size;
95 }
96
101 inline void ImmediateRead(T* destination, size_t num_elements)
102 {
103 size_t r = read_ptr_;
104 size_t read = num_elements;
105
106 if(r + read > size)
107 {
108 read = size - r;
109 }
110 std::copy(&buffer_[r], &buffer_[r + read], destination);
111 if(read != num_elements)
112 {
113 std::copy(
114 &buffer_[0], &buffer_[num_elements - read], destination + read);
115 }
116 read_ptr_ = (r + num_elements) % size;
117 }
118
123 inline void Overwrite(const T* source, size_t num_elements)
124 {
125 size_t w = write_ptr_;
126 size_t written = num_elements;
127
128 if(w + written > size)
129 {
130 written = size - w;
131 }
132 std::copy(source, source + written, &buffer_[w]);
133 if(written != num_elements)
134 {
135 std::copy(source + written, source + num_elements, &buffer_[0]);
136 }
137
138 write_ptr_ = (w + num_elements) % size;
139 }
140
142 inline void Advance(size_t num_elements)
143 {
144 size_t free;
145 free = this->writable();
146 num_elements = num_elements < free ? num_elements : free;
147 write_ptr_ = write_ptr_ + num_elements;
148 if(write_ptr_ > size)
149 write_ptr_ = write_ptr_ - size;
150 }
151
154 inline T* GetMutableBuffer() { return buffer_; }
155
156 private:
157 T buffer_[size];
158 volatile size_t read_ptr_;
159 volatile size_t write_ptr_;
160};
161
165template <typename T>
166class RingBuffer<T, 0>
167{
168 public:
170
171 inline void Init() {}
172 inline size_t capacity() const { return 0; }
173 inline size_t writable() const { return 0; }
174 inline size_t readable() const { return 0; }
175 inline void Write(T v) { (void)(v); }
176 inline void Overwrite(T v)
177 {
178 (void)(v);
179 }
180 inline T Read() { return T(0); }
181 inline T ImmediateRead() { return T(0); }
182 inline void Flush() {}
183 inline void ImmediateRead(T* destination, size_t num_elements)
184 {
185 (void)(destination);
187 }
188 inline void Overwrite(const T* source, size_t num_elements)
189 {
190 (void)(source);
192 }
194 private:
195};
196
198} // namespace daisy
199
200#endif
Definition leddriver.h:33
void Init()
Definition ringbuffer.h:171
T ImmediateRead()
Definition ringbuffer.h:181
size_t capacity() const
Definition ringbuffer.h:172
T Read()
Definition ringbuffer.h:180
RingBuffer()
Definition ringbuffer.h:169
void Overwrite(const T *source, size_t num_elements)
Definition ringbuffer.h:188
void Write(T v)
Definition ringbuffer.h:175
size_t writable() const
Definition ringbuffer.h:173
void Flush()
Definition ringbuffer.h:182
void ImmediateRead(T *destination, size_t num_elements)
Definition ringbuffer.h:183
size_t readable() const
Definition ringbuffer.h:174
void Overwrite(T v)
Definition ringbuffer.h:176
Definition ringbuffer.h:19
T ImmediateRead()
Definition ringbuffer.h:74
T Read()
Definition ringbuffer.h:64
size_t writable() const
Definition ringbuffer.h:30
void Overwrite(const T *source, size_t num_elements)
Definition ringbuffer.h:123
bool isEmpty() const
Definition ringbuffer.h:39
void Init()
Definition ringbuffer.h:24
void Overwrite(T v)
Definition ringbuffer.h:54
T * GetMutableBuffer()
Definition ringbuffer.h:154
void Flush()
Definition ringbuffer.h:83
size_t readable() const
Definition ringbuffer.h:36
void Write(T v)
Definition ringbuffer.h:44
RingBuffer()
Definition ringbuffer.h:21
void Swallow(size_t n)
Definition ringbuffer.h:88
void ImmediateRead(T *destination, size_t num_elements)
Definition ringbuffer.h:101
void Advance(size_t num_elements)
Definition ringbuffer.h:142
size_t capacity() const
Definition ringbuffer.h:27
Hardware defines and helpers for daisy field platform.
Definition index.h:2