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@4:29917182b5c8, 2016-01-21 (annotated)
- Committer:
- AkinoriHashimoto
- Date:
- Thu Jan 21 06:50:24 2016 +0000
- Revision:
- 4:29917182b5c8
- Parent:
- 3:dced590a2d1b
adj
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AkinoriHashimoto | 0:5373472190f5 | 1 | #include "RingBuffer.h" |
AkinoriHashimoto | 0:5373472190f5 | 2 | |
AkinoriHashimoto | 0:5373472190f5 | 3 | RingBuffer::RingBuffer(unsigned int size) |
AkinoriHashimoto | 0:5373472190f5 | 4 | { |
AkinoriHashimoto | 1:8f2a3144902b | 5 | _empty= true; |
AkinoriHashimoto | 0:5373472190f5 | 6 | idxF= idxR= 0; |
AkinoriHashimoto | 1:8f2a3144902b | 7 | // BufSizeの制限は… |
AkinoriHashimoto | 1:8f2a3144902b | 8 | if(size > MaxBufSize) |
AkinoriHashimoto | 1:8f2a3144902b | 9 | size= MaxBufSize; |
AkinoriHashimoto | 0:5373472190f5 | 10 | buf= new char[size]; |
AkinoriHashimoto | 0:5373472190f5 | 11 | bufSize= size; |
AkinoriHashimoto | 1:8f2a3144902b | 12 | isPowers2= false; |
AkinoriHashimoto | 1:8f2a3144902b | 13 | if( (size & (size- 1)) == 0) |
AkinoriHashimoto | 1:8f2a3144902b | 14 | isPowers2= true; |
AkinoriHashimoto | 0:5373472190f5 | 15 | } |
AkinoriHashimoto | 0:5373472190f5 | 16 | RingBuffer::~RingBuffer() |
AkinoriHashimoto | 0:5373472190f5 | 17 | { |
AkinoriHashimoto | 0:5373472190f5 | 18 | delete [] buf; |
AkinoriHashimoto | 0:5373472190f5 | 19 | } |
AkinoriHashimoto | 0:5373472190f5 | 20 | |
AkinoriHashimoto | 1:8f2a3144902b | 21 | bool RingBuffer::empty() |
AkinoriHashimoto | 1:8f2a3144902b | 22 | { |
AkinoriHashimoto | 1:8f2a3144902b | 23 | return this->_empty; |
AkinoriHashimoto | 1:8f2a3144902b | 24 | } |
AkinoriHashimoto | 0:5373472190f5 | 25 | |
AkinoriHashimoto | 3:dced590a2d1b | 26 | bool RingBuffer::chkCR() |
AkinoriHashimoto | 3:dced590a2d1b | 27 | { |
AkinoriHashimoto | 3:dced590a2d1b | 28 | unsigned short idx= idxF; |
AkinoriHashimoto | 3:dced590a2d1b | 29 | while(true) { |
AkinoriHashimoto | 3:dced590a2d1b | 30 | if(buf[idx] == '\r') |
AkinoriHashimoto | 3:dced590a2d1b | 31 | return true; |
AkinoriHashimoto | 3:dced590a2d1b | 32 | idx++; |
AkinoriHashimoto | 3:dced590a2d1b | 33 | modulo(idx); |
AkinoriHashimoto | 3:dced590a2d1b | 34 | if(idx == idxR) |
AkinoriHashimoto | 3:dced590a2d1b | 35 | return false; |
AkinoriHashimoto | 3:dced590a2d1b | 36 | } |
AkinoriHashimoto | 3:dced590a2d1b | 37 | |
AkinoriHashimoto | 3:dced590a2d1b | 38 | } |
AkinoriHashimoto | 3:dced590a2d1b | 39 | |
AkinoriHashimoto | 0:5373472190f5 | 40 | bool RingBuffer::set(string &str) |
AkinoriHashimoto | 0:5373472190f5 | 41 | { |
AkinoriHashimoto | 0:5373472190f5 | 42 | int size= str.size(); |
AkinoriHashimoto | 0:5373472190f5 | 43 | for(int idx= 0; idx < size; idx++) |
AkinoriHashimoto | 0:5373472190f5 | 44 | if(this->set( (char)str[idx] )) // True:FULL |
AkinoriHashimoto | 0:5373472190f5 | 45 | return true; |
AkinoriHashimoto | 0:5373472190f5 | 46 | return false; |
AkinoriHashimoto | 0:5373472190f5 | 47 | } |
AkinoriHashimoto | 0:5373472190f5 | 48 | |
AkinoriHashimoto | 0:5373472190f5 | 49 | bool RingBuffer::set(char chr) |
AkinoriHashimoto | 0:5373472190f5 | 50 | { |
AkinoriHashimoto | 1:8f2a3144902b | 51 | if((idxR == idxF) && !_empty) // R==F: empty or full |
AkinoriHashimoto | 0:5373472190f5 | 52 | return true; // means FULL |
AkinoriHashimoto | 0:5373472190f5 | 53 | |
AkinoriHashimoto | 2:db4675083c8c | 54 | if(chr == NULL) |
AkinoriHashimoto | 2:db4675083c8c | 55 | return false; |
AkinoriHashimoto | 2:db4675083c8c | 56 | |
AkinoriHashimoto | 0:5373472190f5 | 57 | buf[idxR] = chr; |
AkinoriHashimoto | 4:29917182b5c8 | 58 | // idxR++; |
AkinoriHashimoto | 4:29917182b5c8 | 59 | modulo(++idxR); |
AkinoriHashimoto | 1:8f2a3144902b | 60 | _empty= false; |
AkinoriHashimoto | 4:29917182b5c8 | 61 | |
AkinoriHashimoto | 0:5373472190f5 | 62 | if(idxR == idxF) |
AkinoriHashimoto | 0:5373472190f5 | 63 | return true; |
AkinoriHashimoto | 0:5373472190f5 | 64 | return false; |
AkinoriHashimoto | 0:5373472190f5 | 65 | } |
AkinoriHashimoto | 0:5373472190f5 | 66 | |
AkinoriHashimoto | 3:dced590a2d1b | 67 | char RingBuffer::getc() |
AkinoriHashimoto | 3:dced590a2d1b | 68 | { |
AkinoriHashimoto | 3:dced590a2d1b | 69 | if(_empty) |
AkinoriHashimoto | 3:dced590a2d1b | 70 | return NULL; |
AkinoriHashimoto | 3:dced590a2d1b | 71 | |
AkinoriHashimoto | 3:dced590a2d1b | 72 | char tmp= buf[idxF]; |
AkinoriHashimoto | 3:dced590a2d1b | 73 | modulo(++idxF); |
AkinoriHashimoto | 3:dced590a2d1b | 74 | if(idxF == idxR) |
AkinoriHashimoto | 3:dced590a2d1b | 75 | _empty= true; |
AkinoriHashimoto | 3:dced590a2d1b | 76 | |
AkinoriHashimoto | 3:dced590a2d1b | 77 | return tmp; |
AkinoriHashimoto | 3:dced590a2d1b | 78 | } |
AkinoriHashimoto | 3:dced590a2d1b | 79 | |
AkinoriHashimoto | 0:5373472190f5 | 80 | string RingBuffer::get() |
AkinoriHashimoto | 0:5373472190f5 | 81 | { |
AkinoriHashimoto | 1:8f2a3144902b | 82 | if(_empty) |
AkinoriHashimoto | 0:5373472190f5 | 83 | return ""; |
AkinoriHashimoto | 0:5373472190f5 | 84 | |
AkinoriHashimoto | 0:5373472190f5 | 85 | string str; |
AkinoriHashimoto | 1:8f2a3144902b | 86 | while(!_empty) { |
AkinoriHashimoto | 0:5373472190f5 | 87 | str += buf[idxF]; |
AkinoriHashimoto | 4:29917182b5c8 | 88 | // idxF++; |
AkinoriHashimoto | 4:29917182b5c8 | 89 | modulo(++idxF); |
AkinoriHashimoto | 0:5373472190f5 | 90 | if(idxF == idxR) |
AkinoriHashimoto | 1:8f2a3144902b | 91 | _empty= true; |
AkinoriHashimoto | 0:5373472190f5 | 92 | } |
AkinoriHashimoto | 0:5373472190f5 | 93 | return str; |
AkinoriHashimoto | 0:5373472190f5 | 94 | } |
AkinoriHashimoto | 0:5373472190f5 | 95 | |
AkinoriHashimoto | 3:dced590a2d1b | 96 | string RingBuffer::getLine() |
AkinoriHashimoto | 3:dced590a2d1b | 97 | { |
AkinoriHashimoto | 3:dced590a2d1b | 98 | if(_empty || !this->chkCR()) |
AkinoriHashimoto | 3:dced590a2d1b | 99 | return ""; |
AkinoriHashimoto | 3:dced590a2d1b | 100 | |
AkinoriHashimoto | 3:dced590a2d1b | 101 | string str; |
AkinoriHashimoto | 3:dced590a2d1b | 102 | bool breakFlag= false; |
AkinoriHashimoto | 3:dced590a2d1b | 103 | char chr; |
AkinoriHashimoto | 3:dced590a2d1b | 104 | while(!breakFlag) { |
AkinoriHashimoto | 3:dced590a2d1b | 105 | chr= buf[idxF++]; |
AkinoriHashimoto | 3:dced590a2d1b | 106 | str += chr; |
AkinoriHashimoto | 3:dced590a2d1b | 107 | |
AkinoriHashimoto | 3:dced590a2d1b | 108 | modulo(idxF); |
AkinoriHashimoto | 3:dced590a2d1b | 109 | if(idxF == idxR) { |
AkinoriHashimoto | 3:dced590a2d1b | 110 | _empty= true; |
AkinoriHashimoto | 4:29917182b5c8 | 111 | breakFlag= true; |
AkinoriHashimoto | 3:dced590a2d1b | 112 | } |
AkinoriHashimoto | 3:dced590a2d1b | 113 | |
AkinoriHashimoto | 3:dced590a2d1b | 114 | if(chr == '\r') { |
AkinoriHashimoto | 3:dced590a2d1b | 115 | breakFlag= true; |
AkinoriHashimoto | 3:dced590a2d1b | 116 | if(!_empty && buf[idxF]=='\n') { |
AkinoriHashimoto | 3:dced590a2d1b | 117 | str += buf[idxF++]; |
AkinoriHashimoto | 3:dced590a2d1b | 118 | modulo(idxF); |
AkinoriHashimoto | 3:dced590a2d1b | 119 | if(idxF == idxR) |
AkinoriHashimoto | 3:dced590a2d1b | 120 | _empty= true; |
AkinoriHashimoto | 3:dced590a2d1b | 121 | } |
AkinoriHashimoto | 3:dced590a2d1b | 122 | } |
AkinoriHashimoto | 3:dced590a2d1b | 123 | } |
AkinoriHashimoto | 3:dced590a2d1b | 124 | return str; |
AkinoriHashimoto | 3:dced590a2d1b | 125 | } |
AkinoriHashimoto | 3:dced590a2d1b | 126 | |
AkinoriHashimoto | 1:8f2a3144902b | 127 | void RingBuffer::modulo(unsigned short &idx) |
AkinoriHashimoto | 1:8f2a3144902b | 128 | { |
AkinoriHashimoto | 1:8f2a3144902b | 129 | if(isPowers2) |
AkinoriHashimoto | 1:8f2a3144902b | 130 | idx= idx & (bufSize-1); |
AkinoriHashimoto | 1:8f2a3144902b | 131 | else |
AkinoriHashimoto | 1:8f2a3144902b | 132 | idx %= bufSize; |
AkinoriHashimoto | 1:8f2a3144902b | 133 | return; |
AkinoriHashimoto | 1:8f2a3144902b | 134 | } |
AkinoriHashimoto | 1:8f2a3144902b | 135 | |
AkinoriHashimoto | 0:5373472190f5 | 136 | //eof |