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 05:20:40 2015 +0000
Revision:
0:5373472190f5
Child:
1:8f2a3144902b
1st publish.

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 0:5373472190f5 5 empty= true;
AkinoriHashimoto 0:5373472190f5 6 idxF= idxR= 0;
AkinoriHashimoto 0:5373472190f5 7 buf= new char[size];
AkinoriHashimoto 0:5373472190f5 8 bufSize= size;
AkinoriHashimoto 0:5373472190f5 9 }
AkinoriHashimoto 0:5373472190f5 10 RingBuffer::~RingBuffer()
AkinoriHashimoto 0:5373472190f5 11 {
AkinoriHashimoto 0:5373472190f5 12 delete [] buf;
AkinoriHashimoto 0:5373472190f5 13 }
AkinoriHashimoto 0:5373472190f5 14
AkinoriHashimoto 0:5373472190f5 15
AkinoriHashimoto 0:5373472190f5 16
AkinoriHashimoto 0:5373472190f5 17 bool RingBuffer::set(string &str)
AkinoriHashimoto 0:5373472190f5 18 {
AkinoriHashimoto 0:5373472190f5 19 int size= str.size();
AkinoriHashimoto 0:5373472190f5 20 for(int idx= 0; idx < size; idx++)
AkinoriHashimoto 0:5373472190f5 21 if(this->set( (char)str[idx] )) // True:FULL
AkinoriHashimoto 0:5373472190f5 22 return true;
AkinoriHashimoto 0:5373472190f5 23 return false;
AkinoriHashimoto 0:5373472190f5 24 }
AkinoriHashimoto 0:5373472190f5 25
AkinoriHashimoto 0:5373472190f5 26 bool RingBuffer::set(char chr)
AkinoriHashimoto 0:5373472190f5 27 {
AkinoriHashimoto 0:5373472190f5 28 if((idxR == idxF) && !empty) // R==F: empty or full
AkinoriHashimoto 0:5373472190f5 29 return true; // means FULL
AkinoriHashimoto 0:5373472190f5 30
AkinoriHashimoto 0:5373472190f5 31 // mutex for RTOS
AkinoriHashimoto 0:5373472190f5 32 #ifdef RTOS_H
AkinoriHashimoto 0:5373472190f5 33 mutex.lock();
AkinoriHashimoto 0:5373472190f5 34 #endif
AkinoriHashimoto 0:5373472190f5 35 buf[idxR] = chr;
AkinoriHashimoto 0:5373472190f5 36 idxR++;
AkinoriHashimoto 0:5373472190f5 37 idxR %= bufSize;
AkinoriHashimoto 0:5373472190f5 38 empty= false;
AkinoriHashimoto 0:5373472190f5 39 #ifdef RTOS_H
AkinoriHashimoto 0:5373472190f5 40 mutex.unlock();
AkinoriHashimoto 0:5373472190f5 41 #endif
AkinoriHashimoto 0:5373472190f5 42 if(idxR == idxF)
AkinoriHashimoto 0:5373472190f5 43 return true;
AkinoriHashimoto 0:5373472190f5 44 return false;
AkinoriHashimoto 0:5373472190f5 45 }
AkinoriHashimoto 0:5373472190f5 46
AkinoriHashimoto 0:5373472190f5 47 string RingBuffer::get()
AkinoriHashimoto 0:5373472190f5 48 {
AkinoriHashimoto 0:5373472190f5 49 if(empty)
AkinoriHashimoto 0:5373472190f5 50 return "";
AkinoriHashimoto 0:5373472190f5 51
AkinoriHashimoto 0:5373472190f5 52 string str;
AkinoriHashimoto 0:5373472190f5 53 // int idx= idxF;
AkinoriHashimoto 0:5373472190f5 54 while(!empty) {
AkinoriHashimoto 0:5373472190f5 55 str += buf[idxF];
AkinoriHashimoto 0:5373472190f5 56 idxF++;
AkinoriHashimoto 0:5373472190f5 57 idxF %= bufSize;
AkinoriHashimoto 0:5373472190f5 58 if(idxF == idxR)
AkinoriHashimoto 0:5373472190f5 59 empty= true;
AkinoriHashimoto 0:5373472190f5 60 }
AkinoriHashimoto 0:5373472190f5 61 return str;
AkinoriHashimoto 0:5373472190f5 62 }
AkinoriHashimoto 0:5373472190f5 63
AkinoriHashimoto 0:5373472190f5 64 //eof