AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
RingBuffer.h
Go to the documentation of this file.
1 /*
2  RingBuffer.h - A ring buffer class for AVR processors.
3  For AVR ATMega328p (Arduino Uno) and ATMega2560 (Arduino Mega).
4  This is part of the AVRTools library.
5  Copyright (c) 2014 Igor Mikolic-Torreira. All right reserved.
6 
7  This program is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 
34 #ifndef RingBuffer_h
35 #define RingBuffer_h
36 
37 
38 #include <util/atomic.h>
39 
40 
41 
61 {
62 public:
63 
70  RingBuffer( unsigned char *buffer, unsigned short size );
71 
77  int pull();
78 
87  int peek( unsigned short index = 0 );
88 
97  bool push( unsigned char element );
98 
99 
105  bool isFull();
106 
112  bool isNotFull();
113 
119  bool isEmpty()
120  { return !static_cast<bool>( mLength ); }
121 
127  bool isNotEmpty()
128  { return static_cast<bool>( mLength ); }
129 
133  void clear();
134 
135 
136 private:
137 
138  unsigned char *mBuffer;
139  volatile unsigned short mSize;
140  volatile unsigned short mLength;
141  volatile unsigned short mIndex;
142 
143 };
144 
145 
146 #endif
147 
148 
void clear()
Clear the ring buffer, leaving it empty.
Definition: RingBuffer.cpp:109
RingBuffer(unsigned char *buffer, unsigned short size)
Construct a ring buffer by providing the storage area for the ring buffer.
Definition: RingBuffer.cpp:30
int pull()
Extract the next (first) byte from the ring buffer.
Definition: RingBuffer.cpp:36
bool isEmpty()
Determine if the buffer is empty .
Definition: RingBuffer.h:119
bool isNotEmpty()
Determine if the buffer is not empty.
Definition: RingBuffer.h:127
bool isNotFull()
Determine if the buffer is not full and can accept more bytes.
Definition: RingBuffer.cpp:98
This class provides an efficient ring buffer implementation for storing bytes. Ring buffers are parti...
Definition: RingBuffer.h:60
bool isFull()
Determine if the buffer is full and cannot accept more bytes.
Definition: RingBuffer.cpp:89
int peek(unsigned short index=0)
Examine an element in the ring buffer.
Definition: RingBuffer.cpp:56
bool push(unsigned char element)
Push a byte into the ring buffer. The element is appended to the back of the buffer.
Definition: RingBuffer.cpp:70