forked from benlaurie/arduino--
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathringbuffer.h
100 lines (80 loc) · 1.68 KB
/
ringbuffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// ringbuffer.h
// Author: Markus Redeker
// small changes for arduino-- by Lars Immisch <[email protected]>
#ifndef ARDUINO_MINUS_MINUS_RINGBUFFER_H
#define ARDUINO_MINUS_MINUS_RINGBUFFER_H
// Usage:
// _Ringbuffer<...> r;
// r.push(new_element);
// oldest_element = r.front(); r.pop();
template<typename ET_, int S_, typename ST_=byte>
class _Ringbuffer
{
public:
typedef ET_ value_type;
typedef ST_ size_type;
_Ringbuffer()
{
clear();
}
~_Ringbuffer() {}
size_type size() const { return _size; }
size_type max_size() const { return S_; }
bool empty() const { return _size == 0; }
bool full() const { return _size == S_; }
value_type& front() { return _buffer[_front]; }
value_type& back() { return _buffer[_back]; }
void clear()
{
_size = 0;
_front = 0;
_back = S_ - 1;
}
void push()
{
_back = (_back + 1) % S_;
if( size() == S_ )
_front = (_front + 1) % S_;
else
_size++;
}
void push(const value_type& x)
{
push();
back() = x;
}
void pop()
{
if( _size > 0 )
{
_size--;
_front = (_front + 1) % S_;
}
}
void back_erase(const size_type n)
{
if( n >= _size )
clear();
else
{
_size -= n;
_back = (_front + _size - 1) % S_;
}
}
void front_erase(const size_type n)
{
if( n >= _size )
clear();
else
{
_size -= n;
_front = (_front + n) % S_;
}
}
protected:
value_type _buffer[S_];
volatile size_type _size;
volatile size_type _front;
volatile size_type _back;
};
#endif // ARDUINO_MINUS_MINUS_RINGBUFFER_H