sdf
Dependencies: AvailableMemory mbed-rtos mbed
RingBuffer.cpp@0:1c8f2727e9f5, 2014-04-03 (annotated)
- Committer:
- y7jin
- Date:
- Thu Apr 03 22:56:32 2014 +0000
- Revision:
- 0:1c8f2727e9f5
hello
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
y7jin | 0:1c8f2727e9f5 | 1 | #include "RingBuffer.h" |
y7jin | 0:1c8f2727e9f5 | 2 | |
y7jin | 0:1c8f2727e9f5 | 3 | void RingBuffer::insert(int sample){ |
y7jin | 0:1c8f2727e9f5 | 4 | if((end+1)%bufSize==start){ |
y7jin | 0:1c8f2727e9f5 | 5 | start=(start+1)%bufSize; |
y7jin | 0:1c8f2727e9f5 | 6 | cur=start; |
y7jin | 0:1c8f2727e9f5 | 7 | count--; |
y7jin | 0:1c8f2727e9f5 | 8 | } |
y7jin | 0:1c8f2727e9f5 | 9 | buf[end]=sample; |
y7jin | 0:1c8f2727e9f5 | 10 | end=(end+1)%bufSize; |
y7jin | 0:1c8f2727e9f5 | 11 | count++; |
y7jin | 0:1c8f2727e9f5 | 12 | } |
y7jin | 0:1c8f2727e9f5 | 13 | |
y7jin | 0:1c8f2727e9f5 | 14 | int RingBuffer::next(){ |
y7jin | 0:1c8f2727e9f5 | 15 | int ret=buf[cur]; |
y7jin | 0:1c8f2727e9f5 | 16 | cur=(cur+1)%bufSize; |
y7jin | 0:1c8f2727e9f5 | 17 | if ((cur >= end) && (count < bufSize)) cur = start; |
y7jin | 0:1c8f2727e9f5 | 18 | return ret; |
y7jin | 0:1c8f2727e9f5 | 19 | } |
y7jin | 0:1c8f2727e9f5 | 20 | |
y7jin | 0:1c8f2727e9f5 | 21 | void RingBuffer::dump(FILE *fp)const{ |
y7jin | 0:1c8f2727e9f5 | 22 | if(!fp){ |
y7jin | 0:1c8f2727e9f5 | 23 | int i=start; |
y7jin | 0:1c8f2727e9f5 | 24 | while(i!=end){ |
y7jin | 0:1c8f2727e9f5 | 25 | printf("%d\r\n", buf[i]); |
y7jin | 0:1c8f2727e9f5 | 26 | i=(i+1)%bufSize; |
y7jin | 0:1c8f2727e9f5 | 27 | } |
y7jin | 0:1c8f2727e9f5 | 28 | }else{ |
y7jin | 0:1c8f2727e9f5 | 29 | int i=start; |
y7jin | 0:1c8f2727e9f5 | 30 | while(i!=end){ |
y7jin | 0:1c8f2727e9f5 | 31 | fprintf(fp, "%d\r\n", buf[i]); |
y7jin | 0:1c8f2727e9f5 | 32 | i=(i+1)%bufSize; |
y7jin | 0:1c8f2727e9f5 | 33 | } |
y7jin | 0:1c8f2727e9f5 | 34 | } |
y7jin | 0:1c8f2727e9f5 | 35 | } |