![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fish with BBBcamshift
Dependencies: L3G4200D L3GD20 LSM303DLHC LSM303DLM PwmIn Servo mbed
iSerial/RingBuffer/RingBuffer.cpp
- Committer:
- tzxl10000
- Date:
- 2015-04-17
- Revision:
- 0:8f37781c0054
File content as of revision 0:8f37781c0054:
// // RingBuffer.cpp ... General purpose ring buffer library // // Copyright 2012 Yoji KURODA // // 2009.11.13 ... Originally written in C by Y.Kuroda for Renesas H83664 // 2012.08.31 ... Code convert for mbed in C++ // #include "string.h" #include "RingBuffer.h" /* * Machine Independent Area */ RingBuffer::RingBuffer(int _bufsize) : bufsize(_bufsize) { buf = new unsigned char [bufsize+1]; sp = ep = (unsigned int)buf; memset(buf,0,bufsize); } RingBuffer::~RingBuffer() { delete [] buf; } int RingBuffer::save(unsigned char c) { if( (ep==sp-1)|| ((sp==(unsigned int)buf)&& (ep==(unsigned int)buf+bufsize-1)) ) /* buffer full */ return 0; *(unsigned char*)ep = c; ep++; if(ep > (unsigned int)buf+bufsize) ep = (unsigned int)buf; return 1; } unsigned char RingBuffer::read(void) { unsigned char ret; if(sp == ep) return 0; /* buffer empty */ ret = *(unsigned char*)sp; *(unsigned char*)sp = 0; sp++; if(sp> (unsigned int)buf+bufsize) sp = (unsigned int)buf; return ret; } int RingBuffer::check(void) { int n = ep-sp; if(n<0) n = bufsize-n; return n; }