Renjian Hao
Dependencies: L3G4200D L3GD20 LSM303DLHC LSM303DLM PwmIn Servo mbed
Fork of Fish_2014Fall by
iSerial/RingBuffer/RingBuffer.cpp@0:8f37781c0054, 2015-04-17 (annotated)
- Committer:
- tzxl10000
- Date:
- Fri Apr 17 21:13:44 2015 +0000
- Revision:
- 0:8f37781c0054
Fish_2014 with BBBCamshift
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tzxl10000 | 0:8f37781c0054 | 1 | // |
tzxl10000 | 0:8f37781c0054 | 2 | // RingBuffer.cpp ... General purpose ring buffer library |
tzxl10000 | 0:8f37781c0054 | 3 | // |
tzxl10000 | 0:8f37781c0054 | 4 | // Copyright 2012 Yoji KURODA |
tzxl10000 | 0:8f37781c0054 | 5 | // |
tzxl10000 | 0:8f37781c0054 | 6 | // 2009.11.13 ... Originally written in C by Y.Kuroda for Renesas H83664 |
tzxl10000 | 0:8f37781c0054 | 7 | // 2012.08.31 ... Code convert for mbed in C++ |
tzxl10000 | 0:8f37781c0054 | 8 | // |
tzxl10000 | 0:8f37781c0054 | 9 | |
tzxl10000 | 0:8f37781c0054 | 10 | #include "string.h" |
tzxl10000 | 0:8f37781c0054 | 11 | #include "RingBuffer.h" |
tzxl10000 | 0:8f37781c0054 | 12 | |
tzxl10000 | 0:8f37781c0054 | 13 | |
tzxl10000 | 0:8f37781c0054 | 14 | /* |
tzxl10000 | 0:8f37781c0054 | 15 | * Machine Independent Area |
tzxl10000 | 0:8f37781c0054 | 16 | */ |
tzxl10000 | 0:8f37781c0054 | 17 | RingBuffer::RingBuffer(int _bufsize) |
tzxl10000 | 0:8f37781c0054 | 18 | : bufsize(_bufsize) |
tzxl10000 | 0:8f37781c0054 | 19 | { |
tzxl10000 | 0:8f37781c0054 | 20 | buf = new unsigned char [bufsize+1]; |
tzxl10000 | 0:8f37781c0054 | 21 | |
tzxl10000 | 0:8f37781c0054 | 22 | sp = ep = (unsigned int)buf; |
tzxl10000 | 0:8f37781c0054 | 23 | memset(buf,0,bufsize); |
tzxl10000 | 0:8f37781c0054 | 24 | } |
tzxl10000 | 0:8f37781c0054 | 25 | |
tzxl10000 | 0:8f37781c0054 | 26 | RingBuffer::~RingBuffer() |
tzxl10000 | 0:8f37781c0054 | 27 | { |
tzxl10000 | 0:8f37781c0054 | 28 | delete [] buf; |
tzxl10000 | 0:8f37781c0054 | 29 | } |
tzxl10000 | 0:8f37781c0054 | 30 | |
tzxl10000 | 0:8f37781c0054 | 31 | int |
tzxl10000 | 0:8f37781c0054 | 32 | RingBuffer::save(unsigned char c) |
tzxl10000 | 0:8f37781c0054 | 33 | { |
tzxl10000 | 0:8f37781c0054 | 34 | if( (ep==sp-1)|| |
tzxl10000 | 0:8f37781c0054 | 35 | ((sp==(unsigned int)buf)&& |
tzxl10000 | 0:8f37781c0054 | 36 | (ep==(unsigned int)buf+bufsize-1)) ) /* buffer full */ |
tzxl10000 | 0:8f37781c0054 | 37 | return 0; |
tzxl10000 | 0:8f37781c0054 | 38 | |
tzxl10000 | 0:8f37781c0054 | 39 | *(unsigned char*)ep = c; |
tzxl10000 | 0:8f37781c0054 | 40 | ep++; |
tzxl10000 | 0:8f37781c0054 | 41 | |
tzxl10000 | 0:8f37781c0054 | 42 | if(ep > (unsigned int)buf+bufsize) |
tzxl10000 | 0:8f37781c0054 | 43 | ep = (unsigned int)buf; |
tzxl10000 | 0:8f37781c0054 | 44 | return 1; |
tzxl10000 | 0:8f37781c0054 | 45 | } |
tzxl10000 | 0:8f37781c0054 | 46 | |
tzxl10000 | 0:8f37781c0054 | 47 | unsigned char |
tzxl10000 | 0:8f37781c0054 | 48 | RingBuffer::read(void) |
tzxl10000 | 0:8f37781c0054 | 49 | { |
tzxl10000 | 0:8f37781c0054 | 50 | unsigned char ret; |
tzxl10000 | 0:8f37781c0054 | 51 | |
tzxl10000 | 0:8f37781c0054 | 52 | if(sp == ep) |
tzxl10000 | 0:8f37781c0054 | 53 | return 0; /* buffer empty */ |
tzxl10000 | 0:8f37781c0054 | 54 | |
tzxl10000 | 0:8f37781c0054 | 55 | ret = *(unsigned char*)sp; |
tzxl10000 | 0:8f37781c0054 | 56 | *(unsigned char*)sp = 0; |
tzxl10000 | 0:8f37781c0054 | 57 | sp++; |
tzxl10000 | 0:8f37781c0054 | 58 | |
tzxl10000 | 0:8f37781c0054 | 59 | if(sp> (unsigned int)buf+bufsize) |
tzxl10000 | 0:8f37781c0054 | 60 | sp = (unsigned int)buf; |
tzxl10000 | 0:8f37781c0054 | 61 | return ret; |
tzxl10000 | 0:8f37781c0054 | 62 | } |
tzxl10000 | 0:8f37781c0054 | 63 | |
tzxl10000 | 0:8f37781c0054 | 64 | int |
tzxl10000 | 0:8f37781c0054 | 65 | RingBuffer::check(void) |
tzxl10000 | 0:8f37781c0054 | 66 | { |
tzxl10000 | 0:8f37781c0054 | 67 | int n = ep-sp; |
tzxl10000 | 0:8f37781c0054 | 68 | if(n<0) |
tzxl10000 | 0:8f37781c0054 | 69 | n = bufsize-n; |
tzxl10000 | 0:8f37781c0054 | 70 | |
tzxl10000 | 0:8f37781c0054 | 71 | return n; |
tzxl10000 | 0:8f37781c0054 | 72 | } |
tzxl10000 | 0:8f37781c0054 | 73 |