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
RingBuffer.cpp
00001 #include "RingBuffer.h" 00002 00003 RingBuffer::RingBuffer(unsigned int size) 00004 { 00005 _empty= true; 00006 idxF= idxR= 0; 00007 // BufSizeの制限は… 00008 if(size > MaxBufSize) 00009 size= MaxBufSize; 00010 buf= new char[size]; 00011 bufSize= size; 00012 isPowers2= false; 00013 if( (size & (size- 1)) == 0) 00014 isPowers2= true; 00015 } 00016 RingBuffer::~RingBuffer() 00017 { 00018 delete [] buf; 00019 } 00020 00021 bool RingBuffer::empty() 00022 { 00023 return this->_empty; 00024 } 00025 00026 bool RingBuffer::chkCR() 00027 { 00028 unsigned short idx= idxF; 00029 while(true) { 00030 if(buf[idx] == '\r') 00031 return true; 00032 idx++; 00033 modulo(idx); 00034 if(idx == idxR) 00035 return false; 00036 } 00037 00038 } 00039 00040 bool RingBuffer::set(string &str) 00041 { 00042 int size= str.size(); 00043 for(int idx= 0; idx < size; idx++) 00044 if(this->set( (char)str[idx] )) // True:FULL 00045 return true; 00046 return false; 00047 } 00048 00049 bool RingBuffer::set(char chr) 00050 { 00051 if((idxR == idxF) && !_empty) // R==F: empty or full 00052 return true; // means FULL 00053 00054 if(chr == NULL) 00055 return false; 00056 00057 buf[idxR] = chr; 00058 // idxR++; 00059 modulo(++idxR); 00060 _empty= false; 00061 00062 if(idxR == idxF) 00063 return true; 00064 return false; 00065 } 00066 00067 char RingBuffer::getc() 00068 { 00069 if(_empty) 00070 return NULL; 00071 00072 char tmp= buf[idxF]; 00073 modulo(++idxF); 00074 if(idxF == idxR) 00075 _empty= true; 00076 00077 return tmp; 00078 } 00079 00080 string RingBuffer::get() 00081 { 00082 if(_empty) 00083 return ""; 00084 00085 string str; 00086 while(!_empty) { 00087 str += buf[idxF]; 00088 // idxF++; 00089 modulo(++idxF); 00090 if(idxF == idxR) 00091 _empty= true; 00092 } 00093 return str; 00094 } 00095 00096 string RingBuffer::getLine() 00097 { 00098 if(_empty || !this->chkCR()) 00099 return ""; 00100 00101 string str; 00102 bool breakFlag= false; 00103 char chr; 00104 while(!breakFlag) { 00105 chr= buf[idxF++]; 00106 str += chr; 00107 00108 modulo(idxF); 00109 if(idxF == idxR) { 00110 _empty= true; 00111 breakFlag= true; 00112 } 00113 00114 if(chr == '\r') { 00115 breakFlag= true; 00116 if(!_empty && buf[idxF]=='\n') { 00117 str += buf[idxF++]; 00118 modulo(idxF); 00119 if(idxF == idxR) 00120 _empty= true; 00121 } 00122 } 00123 } 00124 return str; 00125 } 00126 00127 void RingBuffer::modulo(unsigned short &idx) 00128 { 00129 if(isPowers2) 00130 idx= idx & (bufSize-1); 00131 else 00132 idx %= bufSize; 00133 return; 00134 } 00135 00136 //eof
Generated on Fri Jul 22 2022 00:51:56 by 1.7.2