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:
Mon Nov 02 06:59:10 2015 +0000
Revision:
2:db4675083c8c
Parent:
1:8f2a3144902b
Child:
3:dced590a2d1b
make sample code

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 2:db4675083c8c 40 if(chr == NULL)
AkinoriHashimoto 2:db4675083c8c 41 return false;
AkinoriHashimoto 2:db4675083c8c 42
AkinoriHashimoto 0:5373472190f5 43 // mutex for RTOS
AkinoriHashimoto 0:5373472190f5 44 #ifdef RTOS_H
AkinoriHashimoto 0:5373472190f5 45 mutex.lock();
AkinoriHashimoto 0:5373472190f5 46 #endif
AkinoriHashimoto 0:5373472190f5 47 buf[idxR] = chr;
AkinoriHashimoto 0:5373472190f5 48 idxR++;
AkinoriHashimoto 1:8f2a3144902b 49 // idxR %= bufSize;
AkinoriHashimoto 1:8f2a3144902b 50 modulo(idxR);
AkinoriHashimoto 1:8f2a3144902b 51 _empty= false;
AkinoriHashimoto 0:5373472190f5 52 #ifdef RTOS_H
AkinoriHashimoto 0:5373472190f5 53 mutex.unlock();
AkinoriHashimoto 0:5373472190f5 54 #endif
AkinoriHashimoto 0:5373472190f5 55 if(idxR == idxF)
AkinoriHashimoto 0:5373472190f5 56 return true;
AkinoriHashimoto 0:5373472190f5 57 return false;
AkinoriHashimoto 0:5373472190f5 58 }
AkinoriHashimoto 0:5373472190f5 59
AkinoriHashimoto 0:5373472190f5 60 string RingBuffer::get()
AkinoriHashimoto 0:5373472190f5 61 {
AkinoriHashimoto 1:8f2a3144902b 62 if(_empty)
AkinoriHashimoto 0:5373472190f5 63 return "";
AkinoriHashimoto 0:5373472190f5 64
AkinoriHashimoto 0:5373472190f5 65 string str;
AkinoriHashimoto 0:5373472190f5 66 // int idx= idxF;
AkinoriHashimoto 1:8f2a3144902b 67 while(!_empty) {
AkinoriHashimoto 0:5373472190f5 68 str += buf[idxF];
AkinoriHashimoto 0:5373472190f5 69 idxF++;
AkinoriHashimoto 1:8f2a3144902b 70 // idxF %= bufSize;
AkinoriHashimoto 1:8f2a3144902b 71 modulo(idxF);
AkinoriHashimoto 0:5373472190f5 72 if(idxF == idxR)
AkinoriHashimoto 1:8f2a3144902b 73 _empty= true;
AkinoriHashimoto 0:5373472190f5 74 }
AkinoriHashimoto 0:5373472190f5 75 return str;
AkinoriHashimoto 0:5373472190f5 76 }
AkinoriHashimoto 0:5373472190f5 77
AkinoriHashimoto 1:8f2a3144902b 78 void RingBuffer::modulo(unsigned short &idx)
AkinoriHashimoto 1:8f2a3144902b 79 {
AkinoriHashimoto 1:8f2a3144902b 80 if(isPowers2)
AkinoriHashimoto 1:8f2a3144902b 81 idx= idx & (bufSize-1);
AkinoriHashimoto 1:8f2a3144902b 82 else
AkinoriHashimoto 1:8f2a3144902b 83 idx %= bufSize;
AkinoriHashimoto 1:8f2a3144902b 84 return;
AkinoriHashimoto 1:8f2a3144902b 85 }
AkinoriHashimoto 1:8f2a3144902b 86
AkinoriHashimoto 0:5373472190f5 87 //eof