APM:Libraries
AP_Buffer.h
Go to the documentation of this file.
1 #pragma once
4 
5 #include <stdint.h>
6 
8 template <class T, uint8_t SIZE>
9 class AP_Buffer {
10 public:
11 
14  AP_Buffer();
15 
18  void clear();
19 
23  void push_back( const T &item );
24 
29  bool pop_front(T &ret);
30 
36  const T& peek(uint8_t position) const;
37 
38  T& peek_mutable(uint8_t position);
39 
43  const T& front() const { return this->peek(0); }
44 
47  uint8_t size() const { return _num_items; }
48 
51  bool is_full() const { return _num_items >= SIZE; }
52 
55  bool is_empty() const { return _num_items == 0; }
56 
57 private:
58  uint8_t _num_items; // number of items in the buffer
59  uint8_t _head; // first item in the buffer (will be returned with the next pop_front call)
60  T _buff[SIZE]; // x values of each point on the curve
61 };
62 
63 // Typedef for convenience - add more as needed
66 
67 template <class T, uint8_t SIZE>
69  _num_items(0), _head(0)
70 {
71 }
72 
73 template <class T, uint8_t SIZE>
75  // clear the curve
76  _num_items = 0;
77  _head = 0;
78 }
79 
80 template <class T, uint8_t SIZE>
81 void AP_Buffer<T,SIZE>::push_back( const T &item )
82 {
83  // determine position of new item
84  uint8_t tail = _head + _num_items;
85  if( tail >= SIZE ) {
86  tail -= SIZE;
87  }
88 
89  // add item to buffer
90  _buff[tail] = item;
91 
92  // increment number of items
93  if( _num_items < SIZE ) {
94  _num_items++;
95  }else{
96  // no room for new items so drop oldest item
97  _head++;
98  if( _head >= SIZE ) {
99  _head = 0;
100  }
101  }
102 }
103 
104 template <class T, uint8_t SIZE>
106 {
107  if(_num_items == 0) {
108  // buffer is empty
109  return false;
110  }
111 
112  // get next value in buffer
113  ret = _buff[_head];
114 
115  // increment to next point
116  _head++;
117  if( _head >= SIZE )
118  _head = 0;
119 
120  // reduce number of items
121  _num_items--;
122 
123  // success
124  return true;
125 }
126 
127 template <class T, uint8_t SIZE>
128 const T& AP_Buffer<T,SIZE>::peek(uint8_t position) const
129 {
130  uint8_t j = _head + position;
131 
132  // wrap around if necessary
133  if( j >= SIZE )
134  j -= SIZE;
135 
136  // return desired value
137  return _buff[j];
138 }
139 
140 template <class T, uint8_t SIZE>
142 {
143  uint8_t j = _head + position;
144 
145  // wrap around if necessary
146  if( j >= SIZE )
147  j -= SIZE;
148 
149  // return desired value
150  return _buff[j];
151 }
uint8_t _head
Definition: AP_Buffer.h:59
bool pop_front(T &ret)
Definition: AP_Buffer.h:105
AP_Buffer< float, 15 > AP_BufferFloat_Size15
Definition: AP_Buffer.h:65
bool is_full() const
Definition: AP_Buffer.h:51
T _buff[SIZE]
Definition: AP_Buffer.h:60
AP_Buffer()
Definition: AP_Buffer.h:68
const T & peek(uint8_t position) const
Definition: AP_Buffer.h:128
const T & front() const
Definition: AP_Buffer.h:43
bool is_empty() const
Definition: AP_Buffer.h:55
uint8_t size() const
Definition: AP_Buffer.h:47
uint8_t _num_items
Definition: AP_Buffer.h:58
AP_Buffer< float, 5 > AP_BufferFloat_Size5
Definition: AP_Buffer.h:64
T & peek_mutable(uint8_t position)
Definition: AP_Buffer.h:141
void clear()
Definition: AP_Buffer.h:74
void push_back(const T &item)
Definition: AP_Buffer.h:81