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:
- 3:dced590a2d1b
- Parent:
- 2:db4675083c8c
- Child:
- 4:29917182b5c8
diff -r db4675083c8c -r dced590a2d1b RingBuffer.cpp --- a/RingBuffer.cpp Mon Nov 02 06:59:10 2015 +0000 +++ b/RingBuffer.cpp Thu Jan 21 06:21:06 2016 +0000 @@ -23,6 +23,20 @@ return this->_empty; } +bool RingBuffer::chkCR() +{ + unsigned short idx= idxF; + while(true) { + if(buf[idx] == '\r') + return true; + idx++; + modulo(idx); + if(idx == idxR) + return false; + } + +} + bool RingBuffer::set(string &str) { int size= str.size(); @@ -41,22 +55,44 @@ return false; // mutex for RTOS -#ifdef RTOS_H - mutex.lock(); -#endif + /* + #ifdef RTOS_H + mutex.lock(); + #endif + */ buf[idxR] = chr; idxR++; // idxR %= bufSize; modulo(idxR); _empty= false; -#ifdef RTOS_H - mutex.unlock(); -#endif + /* + #ifdef RTOS_H + mutex.unlock(); + #endif + */ if(idxR == idxF) return true; return false; } +char RingBuffer::getc() +{ + if(_empty) + return NULL; + +// string str; +// int idx= idxF; +// while(!_empty) { + char tmp= buf[idxF]; +// idxF++; +// idxF %= bufSize; + modulo(++idxF); + if(idxF == idxR) + _empty= true; + + return tmp; +} + string RingBuffer::get() { if(_empty) @@ -75,6 +111,38 @@ return str; } +string RingBuffer::getLine() +{ + if(_empty || !this->chkCR()) + return ""; + + string str; +// int idx= idxF; + bool breakFlag= false; + char chr; + while(!breakFlag) { + chr= buf[idxF++]; + str += chr; + + modulo(idxF); + if(idxF == idxR) { + _empty= true; + breakFalg= true; + } + + if(chr == '\r') { + breakFlag= true; + if(!_empty && buf[idxF]=='\n') { + str += buf[idxF++]; + modulo(idxF); + if(idxF == idxR) + _empty= true; + } + } + } + return str; +} + void RingBuffer::modulo(unsigned short &idx) { if(isPowers2)