robobuggy / arduino / InterruptRCRecieverReference / PinChangeInt / Examples / ByteBuffer / ByteBuffer.cpp @ c5d6b0e8
History | View | Annotate | Download (5.54 KB)
1 |
/*
|
---|---|
2 |
ByteBuffer.cpp - A circular buffer implementation for Arduino
|
3 |
Created by Sigurdur Orn, July 19, 2010.
|
4 |
siggi@mit.edu
|
5 |
Updated by GreyGnome (aka Mike Schwager) Thu Feb 23 17:25:14 CST 2012
|
6 |
added the putString() method and the fillError variable.
|
7 |
added the checkError() and resetError() methods. The checkError() method resets the fillError variable
|
8 |
to false as a side effect.
|
9 |
added the ByteBuffer(unsigned int buf_size) constructor.
|
10 |
added the init() method, and had the constructor call it automagically.
|
11 |
Also made the capacity, position, length, and fillError variables volatile, for safe use by interrupts.
|
12 |
*/
|
13 |
|
14 |
#include "ByteBuffer.h" |
15 |
|
16 |
void ByteBuffer::init(){
|
17 |
ByteBuffer::init(DEFAULTBUFSIZE); |
18 |
} |
19 |
|
20 |
void ByteBuffer::init(unsigned int buf_length){ |
21 |
data = (byte*)malloc(sizeof(byte)*buf_length);
|
22 |
capacity = buf_length; |
23 |
position = 0;
|
24 |
length = 0;
|
25 |
fillError=false;
|
26 |
} |
27 |
|
28 |
void ByteBuffer::deAllocate(){
|
29 |
free(data); |
30 |
} |
31 |
|
32 |
void ByteBuffer::clear(){
|
33 |
position = 0;
|
34 |
length = 0;
|
35 |
} |
36 |
|
37 |
void ByteBuffer::resetError(){
|
38 |
fillError=false;
|
39 |
} |
40 |
|
41 |
boolean ByteBuffer::checkError(){ |
42 |
/*
|
43 |
if (fillError) {
|
44 |
Serial.print("E: checkError: length ");
|
45 |
Serial.println(length, DEC);
|
46 |
}
|
47 |
*/
|
48 |
|
49 |
boolean result=fillError; |
50 |
fillError=false;
|
51 |
return(result);
|
52 |
} |
53 |
|
54 |
int ByteBuffer::getSize(){
|
55 |
return length;
|
56 |
} |
57 |
|
58 |
int ByteBuffer::getCapacity(){
|
59 |
return capacity;
|
60 |
} |
61 |
|
62 |
byte ByteBuffer::peek(unsigned int index){ |
63 |
byte b = data[(position+index)%capacity]; |
64 |
return b;
|
65 |
} |
66 |
|
67 |
uint8_t ByteBuffer::put(byte in){ |
68 |
if(length < capacity){
|
69 |
// save data byte at end of buffer
|
70 |
data[(position+length) % capacity] = in; |
71 |
// increment the length
|
72 |
length++; |
73 |
return 1; |
74 |
} |
75 |
// return failure
|
76 |
//Serial.print("E: put: ");
|
77 |
//Serial.println(length, DEC);
|
78 |
fillError=true;
|
79 |
return 0; |
80 |
} |
81 |
|
82 |
|
83 |
uint8_t ByteBuffer::putString(char *in){
|
84 |
uint8_t count=0;
|
85 |
char *inString;
|
86 |
|
87 |
inString=in; |
88 |
uint8_t oldSREG = SREG; cli(); |
89 |
while(length <= capacity){
|
90 |
if (length == capacity) {
|
91 |
fillError=true;
|
92 |
return count;
|
93 |
} |
94 |
// save data byte at end of buffer
|
95 |
data[(position+length) % capacity] = *inString; |
96 |
// increment the length
|
97 |
length++; |
98 |
inString++; |
99 |
count++; |
100 |
if (*inString == 0) { |
101 |
if (count==0) fillError=true; // Serial.println("E: putString"); }; |
102 |
SREG = oldSREG; // Restore register; reenables interrupts
|
103 |
return count;
|
104 |
} |
105 |
} |
106 |
SREG = oldSREG; // Restore register; reenables interrupts
|
107 |
return count;
|
108 |
} |
109 |
|
110 |
uint8_t ByteBuffer::putInFront(byte in){ |
111 |
uint8_t oldSREG = SREG; cli(); |
112 |
if(length < capacity){
|
113 |
// save data byte at end of buffer
|
114 |
if( position == 0 ) |
115 |
position = capacity-1;
|
116 |
else
|
117 |
position = (position-1)%capacity;
|
118 |
data[position] = in; |
119 |
// increment the length
|
120 |
length++; |
121 |
SREG = oldSREG; // Restore register; reenables interrupts
|
122 |
return 1; |
123 |
} |
124 |
// return failure
|
125 |
//Serial.println("E: putInFront");
|
126 |
fillError=true;
|
127 |
SREG = oldSREG; // Restore register; reenables interrupts
|
128 |
return 0; |
129 |
} |
130 |
|
131 |
byte ByteBuffer::get(){ |
132 |
uint8_t oldSREG = SREG; cli(); |
133 |
byte b = 0;
|
134 |
|
135 |
if(length > 0){ |
136 |
b = data[position]; |
137 |
// move index down and decrement length
|
138 |
position = (position+1)%capacity;
|
139 |
length--; |
140 |
} |
141 |
SREG = oldSREG; // Restore register; reenables interrupts
|
142 |
return b;
|
143 |
} |
144 |
|
145 |
byte ByteBuffer::getFromBack(){ |
146 |
byte b = 0;
|
147 |
if(length > 0){ |
148 |
uint8_t oldSREG = SREG; cli(); |
149 |
b = data[(position+length-1)%capacity];
|
150 |
length--; |
151 |
SREG = oldSREG; // Restore register; reenables interrupts
|
152 |
} |
153 |
|
154 |
return b;
|
155 |
} |
156 |
|
157 |
//
|
158 |
// Ints
|
159 |
//
|
160 |
|
161 |
void ByteBuffer::putIntInFront(int in){ |
162 |
byte *pointer = (byte *)∈ |
163 |
putInFront(pointer[0]);
|
164 |
putInFront(pointer[1]);
|
165 |
} |
166 |
|
167 |
void ByteBuffer::putInt(int in){ |
168 |
byte *pointer = (byte *)∈ |
169 |
put(pointer[1]);
|
170 |
put(pointer[0]);
|
171 |
} |
172 |
|
173 |
|
174 |
int ByteBuffer::getInt(){
|
175 |
int ret;
|
176 |
byte *pointer = (byte *)&ret; |
177 |
pointer[1] = get();
|
178 |
pointer[0] = get();
|
179 |
return ret;
|
180 |
} |
181 |
|
182 |
int ByteBuffer::getIntFromBack(){
|
183 |
int ret;
|
184 |
byte *pointer = (byte *)&ret; |
185 |
pointer[0] = getFromBack();
|
186 |
pointer[1] = getFromBack();
|
187 |
return ret;
|
188 |
} |
189 |
|
190 |
//
|
191 |
// Longs
|
192 |
//
|
193 |
|
194 |
void ByteBuffer::putLongInFront(long in){ |
195 |
byte *pointer = (byte *)∈ |
196 |
putInFront(pointer[0]);
|
197 |
putInFront(pointer[1]);
|
198 |
putInFront(pointer[2]);
|
199 |
putInFront(pointer[3]);
|
200 |
} |
201 |
|
202 |
void ByteBuffer::putLong(long in){ |
203 |
byte *pointer = (byte *)∈ |
204 |
put(pointer[3]);
|
205 |
put(pointer[2]);
|
206 |
put(pointer[1]);
|
207 |
put(pointer[0]);
|
208 |
} |
209 |
|
210 |
|
211 |
long ByteBuffer::getLong(){
|
212 |
long ret;
|
213 |
byte *pointer = (byte *)&ret; |
214 |
pointer[3] = get();
|
215 |
pointer[2] = get();
|
216 |
pointer[1] = get();
|
217 |
pointer[0] = get();
|
218 |
return ret;
|
219 |
} |
220 |
|
221 |
long ByteBuffer::getLongFromBack(){
|
222 |
long ret;
|
223 |
byte *pointer = (byte *)&ret; |
224 |
pointer[0] = getFromBack();
|
225 |
pointer[1] = getFromBack();
|
226 |
pointer[2] = getFromBack();
|
227 |
pointer[3] = getFromBack();
|
228 |
return ret;
|
229 |
} |
230 |
|
231 |
|
232 |
//
|
233 |
// Floats
|
234 |
//
|
235 |
|
236 |
void ByteBuffer::putFloatInFront(float in){ |
237 |
byte *pointer = (byte *)∈ |
238 |
putInFront(pointer[0]);
|
239 |
putInFront(pointer[1]);
|
240 |
putInFront(pointer[2]);
|
241 |
putInFront(pointer[3]);
|
242 |
} |
243 |
|
244 |
void ByteBuffer::putFloat(float in){ |
245 |
byte *pointer = (byte *)∈ |
246 |
put(pointer[3]);
|
247 |
put(pointer[2]);
|
248 |
put(pointer[1]);
|
249 |
put(pointer[0]);
|
250 |
} |
251 |
|
252 |
float ByteBuffer::getFloat(){
|
253 |
float ret;
|
254 |
byte *pointer = (byte *)&ret; |
255 |
pointer[3] = get();
|
256 |
pointer[2] = get();
|
257 |
pointer[1] = get();
|
258 |
pointer[0] = get();
|
259 |
return ret;
|
260 |
} |
261 |
|
262 |
float ByteBuffer::getFloatFromBack(){
|
263 |
float ret;
|
264 |
byte *pointer = (byte *)&ret; |
265 |
pointer[0] = getFromBack();
|
266 |
pointer[1] = getFromBack();
|
267 |
pointer[2] = getFromBack();
|
268 |
pointer[3] = getFromBack();
|
269 |
return ret;
|
270 |
} |
271 |
|
272 |
|