Simple ring buffer

Committer:
jhestolano
Date:
Thu May 30 03:27:50 2013 +0000
Revision:
0:831ea68a2334
Simple ringbuffer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhestolano 0:831ea68a2334 1 #include "RingBuffer.h"
jhestolano 0:831ea68a2334 2
jhestolano 0:831ea68a2334 3 Buffer::Buffer(int size) : bufSize(size), full(false), empty(true), windex(0), rindex(0)
jhestolano 0:831ea68a2334 4 {
jhestolano 0:831ea68a2334 5 data = std::vector<float>(bufSize);
jhestolano 0:831ea68a2334 6 itBeg = data.begin();
jhestolano 0:831ea68a2334 7 itEnd = data.end();
jhestolano 0:831ea68a2334 8 head = itBeg;
jhestolano 0:831ea68a2334 9 tail = itBeg;
jhestolano 0:831ea68a2334 10 }
jhestolano 0:831ea68a2334 11
jhestolano 0:831ea68a2334 12 void Buffer::put(float val)
jhestolano 0:831ea68a2334 13 {
jhestolano 0:831ea68a2334 14 if(!full)
jhestolano 0:831ea68a2334 15 {
jhestolano 0:831ea68a2334 16 //std::cout << "Escribiendo " << val << " en " << windex << std::endl;
jhestolano 0:831ea68a2334 17 data[windex] = val;
jhestolano 0:831ea68a2334 18 windex++;
jhestolano 0:831ea68a2334 19 empty = false;
jhestolano 0:831ea68a2334 20 if(windex >= bufSize)
jhestolano 0:831ea68a2334 21 {
jhestolano 0:831ea68a2334 22 windex = 0;
jhestolano 0:831ea68a2334 23 }
jhestolano 0:831ea68a2334 24 if(windex == rindex)
jhestolano 0:831ea68a2334 25 {
jhestolano 0:831ea68a2334 26 full = true;
jhestolano 0:831ea68a2334 27 //std::cout << "Buffer lleno..." << std::endl;
jhestolano 0:831ea68a2334 28 }
jhestolano 0:831ea68a2334 29 }
jhestolano 0:831ea68a2334 30 }
jhestolano 0:831ea68a2334 31
jhestolano 0:831ea68a2334 32 const float Buffer::get()
jhestolano 0:831ea68a2334 33 {
jhestolano 0:831ea68a2334 34 float temp;
jhestolano 0:831ea68a2334 35 if(!empty)
jhestolano 0:831ea68a2334 36 {
jhestolano 0:831ea68a2334 37 temp = data[rindex];
jhestolano 0:831ea68a2334 38 //std::cout << "Leyendo " << temp << " de " << rindex << std::endl;
jhestolano 0:831ea68a2334 39 data[rindex] = 0;
jhestolano 0:831ea68a2334 40 full = false;
jhestolano 0:831ea68a2334 41 rindex++;
jhestolano 0:831ea68a2334 42 if(rindex >= bufSize)
jhestolano 0:831ea68a2334 43 {
jhestolano 0:831ea68a2334 44 rindex = 0;
jhestolano 0:831ea68a2334 45 }
jhestolano 0:831ea68a2334 46 if(rindex == windex)
jhestolano 0:831ea68a2334 47 {
jhestolano 0:831ea68a2334 48 empty = true;
jhestolano 0:831ea68a2334 49 //std::cout << "Buffer vacío. R-Index: " << rindex << std::endl;
jhestolano 0:831ea68a2334 50 }
jhestolano 0:831ea68a2334 51 }
jhestolano 0:831ea68a2334 52 return temp;
jhestolano 0:831ea68a2334 53 }
jhestolano 0:831ea68a2334 54
jhestolano 0:831ea68a2334 55 const bool Buffer::isFull()
jhestolano 0:831ea68a2334 56 {
jhestolano 0:831ea68a2334 57 return full;
jhestolano 0:831ea68a2334 58 }
jhestolano 0:831ea68a2334 59
jhestolano 0:831ea68a2334 60 const bool Buffer::isEmpty()
jhestolano 0:831ea68a2334 61 {
jhestolano 0:831ea68a2334 62 return empty;
jhestolano 0:831ea68a2334 63 }
jhestolano 0:831ea68a2334 64
jhestolano 0:831ea68a2334 65 const int Buffer::getSize()
jhestolano 0:831ea68a2334 66 {
jhestolano 0:831ea68a2334 67 return bufSize;
jhestolano 0:831ea68a2334 68 }
jhestolano 0:831ea68a2334 69
jhestolano 0:831ea68a2334 70 const int Buffer::getWritingIndex()
jhestolano 0:831ea68a2334 71 {
jhestolano 0:831ea68a2334 72 return windex;
jhestolano 0:831ea68a2334 73 }
jhestolano 0:831ea68a2334 74
jhestolano 0:831ea68a2334 75 /*
jhestolano 0:831ea68a2334 76 void Buffer::printBuffer()
jhestolano 0:831ea68a2334 77 {
jhestolano 0:831ea68a2334 78 std::cout << "Imprimiendo buffer..." << std::endl;
jhestolano 0:831ea68a2334 79 int k = 0;
jhestolano 0:831ea68a2334 80 for(std::vector<float>::iterator it = data.begin(); it != data.end(); ++it)
jhestolano 0:831ea68a2334 81 {
jhestolano 0:831ea68a2334 82 std::cout << "Elemento " << k++ << " es: " << *it << std::endl;
jhestolano 0:831ea68a2334 83 }
jhestolano 0:831ea68a2334 84 }
jhestolano 0:831ea68a2334 85 */