test
Dependencies: Nanopb iSerial mbed BaseJpegDecode FatFileSystem SDFileSystem RingBuffer Camera_LS_Y201
Diff: iSerial/RingBuffer/RingBuffer.cpp
- Revision:
- 0:d69efd0ee139
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/iSerial/RingBuffer/RingBuffer.cpp Thu Sep 18 15:21:47 2014 +0000 @@ -0,0 +1,73 @@ +// +// 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; +} +