Main Page
Namespaces
Classes
Files
File List
File Members
src
ros_lib
rosserial_vex_v5
utils
RingBuf.h
Go to the documentation of this file.
1
/*
2
* The MIT License (MIT)
3
*
4
* Copyright (c) 2015 D. Aaron Wisner
5
*
6
* Permission is hereby granted, free of charge, to any person obtaining a copy
7
* of this software and associated documentation files (the "Software"), to deal
8
* in the Software without restriction, including without limitation the rights
9
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
* copies of the Software, and to permit persons to whom the Software is
11
* furnished to do so, subject to the following conditions:
12
*
13
* The above copyright notice and this permission notice shall be included in all
14
* copies or substantial portions of the Software.
15
*
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
* SOFTWARE.
23
*/
24
25
#ifndef EM_RINGBUF_CPP_H
26
#define EM_RINGBUF_CPP_H
27
28
#include "
ros_lib/rosserial_vex_v5/utils/RingBufHelpers.h
"
29
#include "main.h"
30
31
template
<
typename
Type, u
int
16_t MaxElements>
32
class
RingBufCPP
33
{
34
public
:
35
36
RingBufCPP
(pros::Mutex& mut):
rosvexMutex
(mut)
37
{
38
RB_ATOMIC_START
39
{
40
_numElements
= 0;
41
42
_head
= 0;
43
}
44
RB_ATOMIC_END
45
}
46
51
bool
add
(Type &obj)
52
{
53
bool
ret =
false
;
54
RB_ATOMIC_START
55
{
56
if
(!
isFull
()) {
57
_buf
[
_head
] = obj;
58
_head
= (
_head
+ 1)%MaxElements;
59
_numElements
++;
60
61
ret =
true
;
62
}
63
}
64
RB_ATOMIC_END
65
66
return
ret;
67
}
68
69
74
bool
pull
(Type *dest)
75
{
76
bool
ret =
false
;
77
uint16_t tail;
78
79
RB_ATOMIC_START
80
{
81
if
(!
isEmpty
()) {
82
tail =
getTail
();
83
*dest =
_buf
[tail];
84
_numElements
--;
85
86
ret =
true
;
87
}
88
}
89
RB_ATOMIC_END
90
91
return
ret;
92
}
93
94
99
Type
peek
(uint16_t num)
100
{
101
Type *ret =
NULL
;
102
103
RB_ATOMIC_START
104
{
105
if
(num <
_numElements
)
//make sure not out of bounds
106
ret = &
_buf
[(
getTail
() + num)%MaxElements];
107
}
108
RB_ATOMIC_END
109
110
return
ret;
111
}
112
113
117
bool
isFull
()
118
{
119
bool
ret;
120
121
RB_ATOMIC_START
122
{
123
ret =
_numElements
>= MaxElements;
124
}
125
RB_ATOMIC_END
126
127
return
ret;
128
}
129
130
134
uint16_t
numElements
()
135
{
136
uint16_t ret;
137
138
RB_ATOMIC_START
139
{
140
ret =
_numElements
;
141
}
142
RB_ATOMIC_END
143
144
return
ret;
145
}
146
147
151
bool
isEmpty
()
152
{
153
bool
ret;
154
155
RB_ATOMIC_START
156
{
157
ret = !
_numElements
;
158
}
159
RB_ATOMIC_END
160
161
return
ret;
162
}
163
164
protected
:
169
uint16_t
getTail
()
170
{
171
return
(
_head
+ (MaxElements -
_numElements
))%MaxElements;
172
}
173
174
175
// underlying array
176
Type
_buf
[MaxElements];
177
178
uint16_t
_head
;
179
uint16_t
_numElements
;
180
private
:
181
pros::Mutex&
rosvexMutex
;
182
};
183
184
#endif
NULL
#define NULL
Definition:
RingBufHelpers.h:30
RingBufCPP::_buf
Type _buf[MaxElements]
Definition:
RingBuf.h:176
RingBufCPP::RingBufCPP
RingBufCPP(pros::Mutex &mut)
Definition:
RingBuf.h:36
RB_ATOMIC_END
#define RB_ATOMIC_END
Definition:
RingBufHelpers.h:34
RingBufCPP::isEmpty
bool isEmpty()
Definition:
RingBuf.h:151
RingBufCPP::isFull
bool isFull()
Definition:
RingBuf.h:117
RingBufCPP::pull
bool pull(Type *dest)
Definition:
RingBuf.h:74
RingBufHelpers.h
RingBufCPP::rosvexMutex
pros::Mutex & rosvexMutex
Definition:
RingBuf.h:181
RingBufCPP
Definition:
RingBuf.h:32
RingBufCPP::_numElements
uint16_t _numElements
Definition:
RingBuf.h:179
RingBufCPP::add
bool add(Type &obj)
Definition:
RingBuf.h:51
RingBufCPP::getTail
uint16_t getTail()
Definition:
RingBuf.h:169
RingBufCPP::numElements
uint16_t numElements()
Definition:
RingBuf.h:134
RingBufCPP::_head
uint16_t _head
Definition:
RingBuf.h:178
RB_ATOMIC_START
#define RB_ATOMIC_START
Definition:
RingBufHelpers.h:33
RingBufCPP::peek
Type peek(uint16_t num)
Definition:
RingBuf.h:99
rosserial_vex_v5
Author(s): Cannon
autogenerated on Fri Jun 7 2019 22:03:08