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:
- 1:8f2a3144902b
- Parent:
- 0:5373472190f5
- Child:
- 2:db4675083c8c
--- a/RingBuffer.cpp Fri Oct 30 05:20:40 2015 +0000 +++ b/RingBuffer.cpp Fri Oct 30 06:34:07 2015 +0000 @@ -2,17 +2,26 @@ RingBuffer::RingBuffer(unsigned int size) { - empty= true; + _empty= true; idxF= idxR= 0; + // BufSizeの制限は… + if(size > MaxBufSize) + size= MaxBufSize; buf= new char[size]; bufSize= size; + isPowers2= false; + if( (size & (size- 1)) == 0) + isPowers2= true; } RingBuffer::~RingBuffer() { delete [] buf; } - +bool RingBuffer::empty() +{ + return this->_empty; +} bool RingBuffer::set(string &str) { @@ -25,7 +34,7 @@ bool RingBuffer::set(char chr) { - if((idxR == idxF) && !empty) // R==F: empty or full + if((idxR == idxF) && !_empty) // R==F: empty or full return true; // means FULL // mutex for RTOS @@ -34,8 +43,9 @@ #endif buf[idxR] = chr; idxR++; - idxR %= bufSize; - empty= false; +// idxR %= bufSize; + modulo(idxR); + _empty= false; #ifdef RTOS_H mutex.unlock(); #endif @@ -46,19 +56,29 @@ string RingBuffer::get() { - if(empty) + if(_empty) return ""; string str; // int idx= idxF; - while(!empty) { + while(!_empty) { str += buf[idxF]; idxF++; - idxF %= bufSize; +// idxF %= bufSize; + modulo(idxF); if(idxF == idxR) - empty= true; + _empty= true; } return str; } +void RingBuffer::modulo(unsigned short &idx) +{ + if(isPowers2) + idx= idx & (bufSize-1); + else + idx %= bufSize; + return; +} + //eof \ No newline at end of file