Ring Buffer reconciled with RTOS. If with using RTOS, this lib is enabled Mutex. Default RingBuffer size is 256 Bytes, Max size is 1024 Bytes.
Dependents: RN41 HC05 HC05 mySerial ... more
Diff: RingBuffer.cpp
- Revision:
- 0:5373472190f5
- Child:
- 1:8f2a3144902b
diff -r 000000000000 -r 5373472190f5 RingBuffer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RingBuffer.cpp Fri Oct 30 05:20:40 2015 +0000 @@ -0,0 +1,64 @@ +#include "RingBuffer.h" + +RingBuffer::RingBuffer(unsigned int size) +{ + empty= true; + idxF= idxR= 0; + buf= new char[size]; + bufSize= size; +} +RingBuffer::~RingBuffer() +{ + delete [] buf; +} + + + +bool RingBuffer::set(string &str) +{ + int size= str.size(); + for(int idx= 0; idx < size; idx++) + if(this->set( (char)str[idx] )) // True:FULL + return true; + return false; +} + +bool RingBuffer::set(char chr) +{ + if((idxR == idxF) && !empty) // R==F: empty or full + return true; // means FULL + +// mutex for RTOS +#ifdef RTOS_H + mutex.lock(); +#endif + buf[idxR] = chr; + idxR++; + idxR %= bufSize; + empty= false; +#ifdef RTOS_H + mutex.unlock(); +#endif + if(idxR == idxF) + return true; + return false; +} + +string RingBuffer::get() +{ + if(empty) + return ""; + + string str; +// int idx= idxF; + while(!empty) { + str += buf[idxF]; + idxF++; + idxF %= bufSize; + if(idxF == idxR) + empty= true; + } + return str; +} + +//eof \ No newline at end of file