EE445M RTOS
Taken at the University of Texas Spring 2015
circularbuffer.hpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset: 4; -*- */
2 #ifndef __circularbuffer__
3 #define __circularbuffer__
4 
5 #include <stdint.h>
6 #include "bufferpp.hpp"
7 
12 template <typename T, uint32_t N>
13 class circularbuffer : public buffer<T, N> {
14 private:
15 
16 public:
17  circularbuffer() : buffer<T, N>() { }
18 
19  void increment_ptr(uint32_t* ptr, uint32_t increment,
20  uint32_t wrap_len) {
21  *ptr = (*ptr + increment) % wrap_len;
22  }
23 
24  void add(const T ch) {
25  this->buf[this->pos] = ch;
26  increment_ptr(&(this->pos), 1, this->len);
27  }
28 
29  T peek() {
30  return this->buf(this->pos);
31  }
32 
33  T get(int32_t offset) {
34 
35  int32_t position_to_get = ((this->pos+offset) % this->len);
36 
37  if (position_to_get < 0) {
38  return this->buf[position_to_get + this->len];
39  }
40  return this->buf[position_to_get];
41  }
42 };
43 
44 #endif /* __circularbuffer__ */
45 
void increment_ptr(uint32_t *ptr, uint32_t increment, uint32_t wrap_len)
T buf[N]
Definition: bufferpp.hpp:115
uint32_t len
Definition: bufferpp.hpp:113
void add(const T ch)
uint32_t pos
Definition: bufferpp.hpp:112