Simple ring buffer
Revision 0:831ea68a2334, committed 2013-05-30
- Comitter:
- jhestolano
- Date:
- Thu May 30 03:27:50 2013 +0000
- Commit message:
- Simple ringbuffer
Changed in this revision
RingBuffer.cpp | Show annotated file Show diff for this revision Revisions of this file |
RingBuffer.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 831ea68a2334 RingBuffer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RingBuffer.cpp Thu May 30 03:27:50 2013 +0000 @@ -0,0 +1,85 @@ +#include "RingBuffer.h" + +Buffer::Buffer(int size) : bufSize(size), full(false), empty(true), windex(0), rindex(0) +{ + data = std::vector<float>(bufSize); + itBeg = data.begin(); + itEnd = data.end(); + head = itBeg; + tail = itBeg; +} + +void Buffer::put(float val) +{ + if(!full) + { + //std::cout << "Escribiendo " << val << " en " << windex << std::endl; + data[windex] = val; + windex++; + empty = false; + if(windex >= bufSize) + { + windex = 0; + } + if(windex == rindex) + { + full = true; + //std::cout << "Buffer lleno..." << std::endl; + } + } +} + +const float Buffer::get() +{ + float temp; + if(!empty) + { + temp = data[rindex]; + //std::cout << "Leyendo " << temp << " de " << rindex << std::endl; + data[rindex] = 0; + full = false; + rindex++; + if(rindex >= bufSize) + { + rindex = 0; + } + if(rindex == windex) + { + empty = true; + //std::cout << "Buffer vacío. R-Index: " << rindex << std::endl; + } + } + return temp; +} + +const bool Buffer::isFull() +{ + return full; +} + +const bool Buffer::isEmpty() +{ + return empty; +} + +const int Buffer::getSize() +{ + return bufSize; +} + +const int Buffer::getWritingIndex() +{ + return windex; +} + +/* +void Buffer::printBuffer() +{ + std::cout << "Imprimiendo buffer..." << std::endl; + int k = 0; + for(std::vector<float>::iterator it = data.begin(); it != data.end(); ++it) + { + std::cout << "Elemento " << k++ << " es: " << *it << std::endl; + } +} +*/
diff -r 000000000000 -r 831ea68a2334 RingBuffer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RingBuffer.h Thu May 30 03:27:50 2013 +0000 @@ -0,0 +1,32 @@ +#ifndef BUFFER_H +#define BUFFER_H + +#include <vector> +#include "mbed.h" + +class Buffer +{ +private: + std::vector<float> data; + std::vector<float>::iterator itBeg; + std::vector<float>::iterator itEnd; + std::vector<float>::iterator head; + std::vector<float>::iterator tail; + int windex; + int rindex; + bool full; + bool empty; + int bufSize; +public: + Buffer(int); + void put(float); + const float get(); + const int getSize(); + const bool isFull(); + const bool isEmpty(); + const int getWritingIndex(); + //void printBuffer(); + //const int getIndex(); +}; + +#endif // BUFFER_H \ No newline at end of file