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@3:dced590a2d1b, 2016-01-21 (annotated)
- Committer:
- AkinoriHashimoto
- Date:
- Thu Jan 21 06:21:06 2016 +0000
- Revision:
- 3:dced590a2d1b
- Parent:
- 2:db4675083c8c
- Child:
- 4:29917182b5c8
delete MUTEX.; Add chkCR(); // '\r'
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 | // mutex for RTOS |
AkinoriHashimoto | 3:dced590a2d1b | 58 | /* |
AkinoriHashimoto | 3:dced590a2d1b | 59 | #ifdef RTOS_H |
AkinoriHashimoto | 3:dced590a2d1b | 60 | mutex.lock(); |
AkinoriHashimoto | 3:dced590a2d1b | 61 | #endif |
AkinoriHashimoto | 3:dced590a2d1b | 62 | */ |
AkinoriHashimoto | 0:5373472190f5 | 63 | buf[idxR] = chr; |
AkinoriHashimoto | 0:5373472190f5 | 64 | idxR++; |
AkinoriHashimoto | 1:8f2a3144902b | 65 | // idxR %= bufSize; |
AkinoriHashimoto | 1:8f2a3144902b | 66 | modulo(idxR); |
AkinoriHashimoto | 1:8f2a3144902b | 67 | _empty= false; |
AkinoriHashimoto | 3:dced590a2d1b | 68 | /* |
AkinoriHashimoto | 3:dced590a2d1b | 69 | #ifdef RTOS_H |
AkinoriHashimoto | 3:dced590a2d1b | 70 | mutex.unlock(); |
AkinoriHashimoto | 3:dced590a2d1b | 71 | #endif |
AkinoriHashimoto | 3:dced590a2d1b | 72 | */ |
AkinoriHashimoto | 0:5373472190f5 | 73 | if(idxR == idxF) |
AkinoriHashimoto | 0:5373472190f5 | 74 | return true; |
AkinoriHashimoto | 0:5373472190f5 | 75 | return false; |
AkinoriHashimoto | 0:5373472190f5 | 76 | } |
AkinoriHashimoto | 0:5373472190f5 | 77 | |
AkinoriHashimoto | 3:dced590a2d1b | 78 | char RingBuffer::getc() |
AkinoriHashimoto | 3:dced590a2d1b | 79 | { |
AkinoriHashimoto | 3:dced590a2d1b | 80 | if(_empty) |
AkinoriHashimoto | 3:dced590a2d1b | 81 | return NULL; |
AkinoriHashimoto | 3:dced590a2d1b | 82 | |
AkinoriHashimoto | 3:dced590a2d1b | 83 | // string str; |
AkinoriHashimoto | 3:dced590a2d1b | 84 | // int idx= idxF; |
AkinoriHashimoto | 3:dced590a2d1b | 85 | // while(!_empty) { |
AkinoriHashimoto | 3:dced590a2d1b | 86 | char tmp= buf[idxF]; |
AkinoriHashimoto | 3:dced590a2d1b | 87 | // idxF++; |
AkinoriHashimoto | 3:dced590a2d1b | 88 | // idxF %= bufSize; |
AkinoriHashimoto | 3:dced590a2d1b | 89 | modulo(++idxF); |
AkinoriHashimoto | 3:dced590a2d1b | 90 | if(idxF == idxR) |
AkinoriHashimoto | 3:dced590a2d1b | 91 | _empty= true; |
AkinoriHashimoto | 3:dced590a2d1b | 92 | |
AkinoriHashimoto | 3:dced590a2d1b | 93 | return tmp; |
AkinoriHashimoto | 3:dced590a2d1b | 94 | } |
AkinoriHashimoto | 3:dced590a2d1b | 95 | |
AkinoriHashimoto | 0:5373472190f5 | 96 | string RingBuffer::get() |
AkinoriHashimoto | 0:5373472190f5 | 97 | { |
AkinoriHashimoto | 1:8f2a3144902b | 98 | if(_empty) |
AkinoriHashimoto | 0:5373472190f5 | 99 | return ""; |
AkinoriHashimoto | 0:5373472190f5 | 100 | |
AkinoriHashimoto | 0:5373472190f5 | 101 | string str; |
AkinoriHashimoto | 0:5373472190f5 | 102 | // int idx= idxF; |
AkinoriHashimoto | 1:8f2a3144902b | 103 | while(!_empty) { |
AkinoriHashimoto | 0:5373472190f5 | 104 | str += buf[idxF]; |
AkinoriHashimoto | 0:5373472190f5 | 105 | idxF++; |
AkinoriHashimoto | 1:8f2a3144902b | 106 | // idxF %= bufSize; |
AkinoriHashimoto | 1:8f2a3144902b | 107 | modulo(idxF); |
AkinoriHashimoto | 0:5373472190f5 | 108 | if(idxF == idxR) |
AkinoriHashimoto | 1:8f2a3144902b | 109 | _empty= true; |
AkinoriHashimoto | 0:5373472190f5 | 110 | } |
AkinoriHashimoto | 0:5373472190f5 | 111 | return str; |
AkinoriHashimoto | 0:5373472190f5 | 112 | } |
AkinoriHashimoto | 0:5373472190f5 | 113 | |
AkinoriHashimoto | 3:dced590a2d1b | 114 | string RingBuffer::getLine() |
AkinoriHashimoto | 3:dced590a2d1b | 115 | { |
AkinoriHashimoto | 3:dced590a2d1b | 116 | if(_empty || !this->chkCR()) |
AkinoriHashimoto | 3:dced590a2d1b | 117 | return ""; |
AkinoriHashimoto | 3:dced590a2d1b | 118 | |
AkinoriHashimoto | 3:dced590a2d1b | 119 | string str; |
AkinoriHashimoto | 3:dced590a2d1b | 120 | // int idx= idxF; |
AkinoriHashimoto | 3:dced590a2d1b | 121 | bool breakFlag= false; |
AkinoriHashimoto | 3:dced590a2d1b | 122 | char chr; |
AkinoriHashimoto | 3:dced590a2d1b | 123 | while(!breakFlag) { |
AkinoriHashimoto | 3:dced590a2d1b | 124 | chr= buf[idxF++]; |
AkinoriHashimoto | 3:dced590a2d1b | 125 | str += chr; |
AkinoriHashimoto | 3:dced590a2d1b | 126 | |
AkinoriHashimoto | 3:dced590a2d1b | 127 | modulo(idxF); |
AkinoriHashimoto | 3:dced590a2d1b | 128 | if(idxF == idxR) { |
AkinoriHashimoto | 3:dced590a2d1b | 129 | _empty= true; |
AkinoriHashimoto | 3:dced590a2d1b | 130 | breakFalg= true; |
AkinoriHashimoto | 3:dced590a2d1b | 131 | } |
AkinoriHashimoto | 3:dced590a2d1b | 132 | |
AkinoriHashimoto | 3:dced590a2d1b | 133 | if(chr == '\r') { |
AkinoriHashimoto | 3:dced590a2d1b | 134 | breakFlag= true; |
AkinoriHashimoto | 3:dced590a2d1b | 135 | if(!_empty && buf[idxF]=='\n') { |
AkinoriHashimoto | 3:dced590a2d1b | 136 | str += buf[idxF++]; |
AkinoriHashimoto | 3:dced590a2d1b | 137 | modulo(idxF); |
AkinoriHashimoto | 3:dced590a2d1b | 138 | if(idxF == idxR) |
AkinoriHashimoto | 3:dced590a2d1b | 139 | _empty= true; |
AkinoriHashimoto | 3:dced590a2d1b | 140 | } |
AkinoriHashimoto | 3:dced590a2d1b | 141 | } |
AkinoriHashimoto | 3:dced590a2d1b | 142 | } |
AkinoriHashimoto | 3:dced590a2d1b | 143 | return str; |
AkinoriHashimoto | 3:dced590a2d1b | 144 | } |
AkinoriHashimoto | 3:dced590a2d1b | 145 | |
AkinoriHashimoto | 1:8f2a3144902b | 146 | void RingBuffer::modulo(unsigned short &idx) |
AkinoriHashimoto | 1:8f2a3144902b | 147 | { |
AkinoriHashimoto | 1:8f2a3144902b | 148 | if(isPowers2) |
AkinoriHashimoto | 1:8f2a3144902b | 149 | idx= idx & (bufSize-1); |
AkinoriHashimoto | 1:8f2a3144902b | 150 | else |
AkinoriHashimoto | 1:8f2a3144902b | 151 | idx %= bufSize; |
AkinoriHashimoto | 1:8f2a3144902b | 152 | return; |
AkinoriHashimoto | 1:8f2a3144902b | 153 | } |
AkinoriHashimoto | 1:8f2a3144902b | 154 | |
AkinoriHashimoto | 0:5373472190f5 | 155 | //eof |