v1.0
Dependencies: SDFileSystem mbed
RingBuffer/RingBuffer.cpp@0:0073c8def9f1, 2016-05-09 (annotated)
- Committer:
- jehoon
- Date:
- Mon May 09 00:13:40 2016 +0000
- Revision:
- 0:0073c8def9f1
WiFi Tracker
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jehoon | 0:0073c8def9f1 | 1 | #include <RingBuffer.h> |
jehoon | 0:0073c8def9f1 | 2 | |
jehoon | 0:0073c8def9f1 | 3 | RingBuffer::RingBuffer() |
jehoon | 0:0073c8def9f1 | 4 | { |
jehoon | 0:0073c8def9f1 | 5 | |
jehoon | 0:0073c8def9f1 | 6 | } |
jehoon | 0:0073c8def9f1 | 7 | |
jehoon | 0:0073c8def9f1 | 8 | RingBuffer::RingBuffer(int length) |
jehoon | 0:0073c8def9f1 | 9 | { |
jehoon | 0:0073c8def9f1 | 10 | this->buffer = (byte *)malloc(length); |
jehoon | 0:0073c8def9f1 | 11 | this->max = length; |
jehoon | 0:0073c8def9f1 | 12 | this->fill = 0; |
jehoon | 0:0073c8def9f1 | 13 | this->ptr = 0; |
jehoon | 0:0073c8def9f1 | 14 | } |
jehoon | 0:0073c8def9f1 | 15 | |
jehoon | 0:0073c8def9f1 | 16 | RingBuffer::~RingBuffer() |
jehoon | 0:0073c8def9f1 | 17 | { |
jehoon | 0:0073c8def9f1 | 18 | free (this->buffer); |
jehoon | 0:0073c8def9f1 | 19 | } |
jehoon | 0:0073c8def9f1 | 20 | |
jehoon | 0:0073c8def9f1 | 21 | void RingBuffer::init(int length) |
jehoon | 0:0073c8def9f1 | 22 | { |
jehoon | 0:0073c8def9f1 | 23 | this->buffer = (byte *)malloc(length); |
jehoon | 0:0073c8def9f1 | 24 | this->max = length; |
jehoon | 0:0073c8def9f1 | 25 | this->fill = 0; |
jehoon | 0:0073c8def9f1 | 26 | this->ptr = 0; |
jehoon | 0:0073c8def9f1 | 27 | } |
jehoon | 0:0073c8def9f1 | 28 | |
jehoon | 0:0073c8def9f1 | 29 | void RingBuffer::clear() |
jehoon | 0:0073c8def9f1 | 30 | { |
jehoon | 0:0073c8def9f1 | 31 | this->fill = 0; |
jehoon | 0:0073c8def9f1 | 32 | } |
jehoon | 0:0073c8def9f1 | 33 | |
jehoon | 0:0073c8def9f1 | 34 | bool RingBuffer::isFull() |
jehoon | 0:0073c8def9f1 | 35 | { |
jehoon | 0:0073c8def9f1 | 36 | return (this->max == this->fill); |
jehoon | 0:0073c8def9f1 | 37 | } |
jehoon | 0:0073c8def9f1 | 38 | |
jehoon | 0:0073c8def9f1 | 39 | bool RingBuffer::hasData() |
jehoon | 0:0073c8def9f1 | 40 | { |
jehoon | 0:0073c8def9f1 | 41 | return (this->fill != 0); |
jehoon | 0:0073c8def9f1 | 42 | } |
jehoon | 0:0073c8def9f1 | 43 | |
jehoon | 0:0073c8def9f1 | 44 | bool RingBuffer::addByte(byte b) |
jehoon | 0:0073c8def9f1 | 45 | { |
jehoon | 0:0073c8def9f1 | 46 | if (this->max == this->fill) |
jehoon | 0:0073c8def9f1 | 47 | return false; |
jehoon | 0:0073c8def9f1 | 48 | |
jehoon | 0:0073c8def9f1 | 49 | int idx = (this->ptr + this->fill) % this->max; |
jehoon | 0:0073c8def9f1 | 50 | this->buffer[idx] = b; |
jehoon | 0:0073c8def9f1 | 51 | this->fill++; |
jehoon | 0:0073c8def9f1 | 52 | return true; |
jehoon | 0:0073c8def9f1 | 53 | } |
jehoon | 0:0073c8def9f1 | 54 | |
jehoon | 0:0073c8def9f1 | 55 | byte RingBuffer::consumeByte() |
jehoon | 0:0073c8def9f1 | 56 | { |
jehoon | 0:0073c8def9f1 | 57 | if (this->fill == 0) |
jehoon | 0:0073c8def9f1 | 58 | return 0; |
jehoon | 0:0073c8def9f1 | 59 | |
jehoon | 0:0073c8def9f1 | 60 | byte ret = this->buffer[this->ptr]; |
jehoon | 0:0073c8def9f1 | 61 | this->fill--; |
jehoon | 0:0073c8def9f1 | 62 | this->ptr++; |
jehoon | 0:0073c8def9f1 | 63 | this->ptr %= this->max; |
jehoon | 0:0073c8def9f1 | 64 | return ret; |
jehoon | 0:0073c8def9f1 | 65 | } |
jehoon | 0:0073c8def9f1 | 66 | |
jehoon | 0:0073c8def9f1 | 67 | byte RingBuffer::peek(int idx) |
jehoon | 0:0073c8def9f1 | 68 | { |
jehoon | 0:0073c8def9f1 | 69 | byte p = (this->ptr + idx) % this->max; |
jehoon | 0:0073c8def9f1 | 70 | return this->buffer[p]; |
jehoon | 0:0073c8def9f1 | 71 | } |
jehoon | 0:0073c8def9f1 | 72 | |
jehoon | 0:0073c8def9f1 | 73 | int RingBuffer::findBuf(char * str) |
jehoon | 0:0073c8def9f1 | 74 | { |
jehoon | 0:0073c8def9f1 | 75 | int i=0; |
jehoon | 0:0073c8def9f1 | 76 | int cnt = 0; |
jehoon | 0:0073c8def9f1 | 77 | int len = strlen(str); |
jehoon | 0:0073c8def9f1 | 78 | byte *tmp = (byte *) str; |
jehoon | 0:0073c8def9f1 | 79 | |
jehoon | 0:0073c8def9f1 | 80 | if( this->fill == 0 ) |
jehoon | 0:0073c8def9f1 | 81 | return -1; |
jehoon | 0:0073c8def9f1 | 82 | |
jehoon | 0:0073c8def9f1 | 83 | for( i=0; i<this->fill; i++ ) |
jehoon | 0:0073c8def9f1 | 84 | { |
jehoon | 0:0073c8def9f1 | 85 | if( peek(i) == *tmp ) |
jehoon | 0:0073c8def9f1 | 86 | { |
jehoon | 0:0073c8def9f1 | 87 | tmp++; |
jehoon | 0:0073c8def9f1 | 88 | cnt++; |
jehoon | 0:0073c8def9f1 | 89 | |
jehoon | 0:0073c8def9f1 | 90 | if(cnt == len) |
jehoon | 0:0073c8def9f1 | 91 | { |
jehoon | 0:0073c8def9f1 | 92 | return (i - len + 1); |
jehoon | 0:0073c8def9f1 | 93 | } |
jehoon | 0:0073c8def9f1 | 94 | } |
jehoon | 0:0073c8def9f1 | 95 | else |
jehoon | 0:0073c8def9f1 | 96 | { |
jehoon | 0:0073c8def9f1 | 97 | tmp = (byte *)str; |
jehoon | 0:0073c8def9f1 | 98 | cnt=0; |
jehoon | 0:0073c8def9f1 | 99 | } |
jehoon | 0:0073c8def9f1 | 100 | } |
jehoon | 0:0073c8def9f1 | 101 | |
jehoon | 0:0073c8def9f1 | 102 | return -1; |
jehoon | 0:0073c8def9f1 | 103 | } |
jehoon | 0:0073c8def9f1 | 104 | |
jehoon | 0:0073c8def9f1 | 105 | |
jehoon | 0:0073c8def9f1 | 106 | int RingBuffer::count() |
jehoon | 0:0073c8def9f1 | 107 | { |
jehoon | 0:0073c8def9f1 | 108 | return this->fill; |
jehoon | 0:0073c8def9f1 | 109 | } |