MySensors Library & Examples  2.3.2-62-ge298769
CircularBuffer.h
Go to the documentation of this file.
1 /*
2  CircularBuffer - An Arduino circular buffering library for arbitrary types.
3 
4  Created by Ivo Pullens, Emmission, 2014-2016 -- www.emmission.nl
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
27 #ifndef CircularBuffer_h
28 #define CircularBuffer_h
29 
34 template <class T> class CircularBuffer
35 {
36 public:
42  CircularBuffer(T* buffer, const uint8_t size )
43  : m_size(size), m_buff(buffer)
44  {
45  clear();
46  }
47 
51  void clear(void)
52  {
54  m_front = 0;
55  m_fill = 0;
56  }
57  }
58 
63  inline bool empty(void) const
64  {
65  bool empty;
67  empty = !m_fill;
68  }
69  return empty;
70  }
71 
76  inline bool full(void) const
77  {
78  bool full;
80  full = m_fill == m_size;
81  }
82  return full;
83  }
84 
89  inline uint8_t available(void) const
90  {
91  uint8_t ret_value;
93  ret_value = m_fill;
94  }
95  return ret_value;
96  }
97 
104  T* getFront(void) const
105  {
107  if (!full())
108  {
109  return get(m_front);
110  }
111  }
112  return static_cast<T*>(NULL);
113  }
114 
121  bool pushFront(T* record)
122  {
124  if (!full())
125  {
126  T* f = get(m_front);
127  if (f != record) {
128  *f = *record;
129  }
130  m_front = (m_front+1) % m_size;
131  m_fill++;
132  return true;
133  }
134  }
135  return false;
136  }
137 
144  T* getBack(void) const
145  {
147  if (!empty())
148  {
149  return get(back());
150  }
151  }
152  return static_cast<T*>(NULL);
153  }
154 
159  bool popBack(void)
160  {
162  if (!empty())
163  {
164  m_fill--;
165  return true;
166  }
167  }
168  return false;
169  }
170 
171 protected:
177  inline T * get(const uint8_t idx) const
178  {
179  return &(m_buff[idx]);
180  }
181 
186  inline uint8_t back(void) const
187  {
188  return (m_front - m_fill + m_size) % m_size;
189  }
190 
191  const uint8_t m_size;
192  T* const m_buff;
193  volatile uint8_t m_front;
194  volatile uint8_t m_fill;
195 };
196 
197 #endif // CircularBuffer_h
MY_CRITICAL_SECTION
#define MY_CRITICAL_SECTION
Creates a block of code that is guaranteed to be executed atomically. Upon entering the block all int...
Definition: MyHwHAL.h:187
CircularBuffer::pushFront
bool pushFront(T *record)
Definition: CircularBuffer.h:121
CircularBuffer::m_front
volatile uint8_t m_front
Index of front element (not pushed yet).
Definition: CircularBuffer.h:193
CircularBuffer::m_size
const uint8_t m_size
Total number of records that can be stored in the buffer.
Definition: CircularBuffer.h:191
CircularBuffer::m_buff
T *const m_buff
Ptr to buffer holding all records.
Definition: CircularBuffer.h:192
CircularBuffer::getBack
T * getBack(void) const
Definition: CircularBuffer.h:144
CircularBuffer
Definition: CircularBuffer.h:34
CircularBuffer::empty
bool empty(void) const
Definition: CircularBuffer.h:63
CircularBuffer::getFront
T * getFront(void) const
Definition: CircularBuffer.h:104
CircularBuffer::back
uint8_t back(void) const
Definition: CircularBuffer.h:186
CircularBuffer::clear
void clear(void)
Definition: CircularBuffer.h:51
CircularBuffer::full
bool full(void) const
Definition: CircularBuffer.h:76
CircularBuffer::m_fill
volatile uint8_t m_fill
Amount of records currently pushed.
Definition: CircularBuffer.h:194
CircularBuffer::available
uint8_t available(void) const
Definition: CircularBuffer.h:89
CircularBuffer::get
T * get(const uint8_t idx) const
Definition: CircularBuffer.h:177
CircularBuffer::popBack
bool popBack(void)
Definition: CircularBuffer.h:159
CircularBuffer::CircularBuffer
CircularBuffer(T *buffer, const uint8_t size)
Definition: CircularBuffer.h:42