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

Committer:
AkinoriHashimoto
Date:
Fri Oct 30 06:34:07 2015 +0000
Revision:
1:8f2a3144902b
Parent:
0:5373472190f5
Child:
2:db4675083c8c
include empty(), buf limit 1kB, modulo Speed up.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:5373472190f5 26 bool RingBuffer::set(string &str)
AkinoriHashimoto 0:5373472190f5 27 {
AkinoriHashimoto 0:5373472190f5 28 int size= str.size();
AkinoriHashimoto 0:5373472190f5 29 for(int idx= 0; idx < size; idx++)
AkinoriHashimoto 0:5373472190f5 30 if(this->set( (char)str[idx] )) // True:FULL
AkinoriHashimoto 0:5373472190f5 31 return true;
AkinoriHashimoto 0:5373472190f5 32 return false;
AkinoriHashimoto 0:5373472190f5 33 }
AkinoriHashimoto 0:5373472190f5 34
AkinoriHashimoto 0:5373472190f5 35 bool RingBuffer::set(char chr)
AkinoriHashimoto 0:5373472190f5 36 {
AkinoriHashimoto 1:8f2a3144902b 37 if((idxR == idxF) && !_empty) // R==F: empty or full
AkinoriHashimoto 0:5373472190f5 38 return true; // means FULL
AkinoriHashimoto 0:5373472190f5 39
AkinoriHashimoto 0:5373472190f5 40 // mutex for RTOS
AkinoriHashimoto 0:5373472190f5 41 #ifdef RTOS_H
AkinoriHashimoto 0:5373472190f5 42 mutex.lock();
AkinoriHashimoto 0:5373472190f5 43 #endif
AkinoriHashimoto 0:5373472190f5 44 buf[idxR] = chr;
AkinoriHashimoto 0:5373472190f5 45 idxR++;
AkinoriHashimoto 1:8f2a3144902b 46 // idxR %= bufSize;
AkinoriHashimoto 1:8f2a3144902b 47 modulo(idxR);
AkinoriHashimoto 1:8f2a3144902b 48 _empty= false;
AkinoriHashimoto 0:5373472190f5 49 #ifdef RTOS_H
AkinoriHashimoto 0:5373472190f5 50 mutex.unlock();
AkinoriHashimoto 0:5373472190f5 51 #endif
AkinoriHashimoto 0:5373472190f5 52 if(idxR == idxF)
AkinoriHashimoto 0:5373472190f5 53 return true;
AkinoriHashimoto 0:5373472190f5 54 return false;
AkinoriHashimoto 0:5373472190f5 55 }
AkinoriHashimoto 0:5373472190f5 56
AkinoriHashimoto 0:5373472190f5 57 string RingBuffer::get()
AkinoriHashimoto 0:5373472190f5 58 {
AkinoriHashimoto 1:8f2a3144902b 59 if(_empty)
AkinoriHashimoto 0:5373472190f5 60 return "";
AkinoriHashimoto 0:5373472190f5 61
AkinoriHashimoto 0:5373472190f5 62 string str;
AkinoriHashimoto 0:5373472190f5 63 // int idx= idxF;
AkinoriHashimoto 1:8f2a3144902b 64 while(!_empty) {
AkinoriHashimoto 0:5373472190f5 65 str += buf[idxF];
AkinoriHashimoto 0:5373472190f5 66 idxF++;
AkinoriHashimoto 1:8f2a3144902b 67 // idxF %= bufSize;
AkinoriHashimoto 1:8f2a3144902b 68 modulo(idxF);
AkinoriHashimoto 0:5373472190f5 69 if(idxF == idxR)
AkinoriHashimoto 1:8f2a3144902b 70 _empty= true;
AkinoriHashimoto 0:5373472190f5 71 }
AkinoriHashimoto 0:5373472190f5 72 return str;
AkinoriHashimoto 0:5373472190f5 73 }
AkinoriHashimoto 0:5373472190f5 74
AkinoriHashimoto 1:8f2a3144902b 75 void RingBuffer::modulo(unsigned short &idx)
AkinoriHashimoto 1:8f2a3144902b 76 {
AkinoriHashimoto 1:8f2a3144902b 77 if(isPowers2)
AkinoriHashimoto 1:8f2a3144902b 78 idx= idx & (bufSize-1);
AkinoriHashimoto 1:8f2a3144902b 79 else
AkinoriHashimoto 1:8f2a3144902b 80 idx %= bufSize;
AkinoriHashimoto 1:8f2a3144902b 81 return;
AkinoriHashimoto 1:8f2a3144902b 82 }
AkinoriHashimoto 1:8f2a3144902b 83
AkinoriHashimoto 0:5373472190f5 84 //eof