Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: iSerial DGWWebServer iSerial Dumb_box_rev2 ... more
RingBuffer.cpp
00001 // 00002 // RingBuffer.cpp ... General purpose ring buffer library 00003 // 00004 // Copyright 2012 Yoji KURODA 00005 // 00006 // 2009.11.13 ... Originally written in C by Y.Kuroda for Renesas H83664 00007 // 2012.08.31 ... Code convert for mbed in C++ 00008 // 00009 00010 #include "string.h" 00011 #include "RingBuffer.h" 00012 00013 00014 /* 00015 * Machine Independent Area 00016 */ 00017 RingBuffer::RingBuffer(int _bufsize) 00018 : bufsize(_bufsize) 00019 { 00020 buf = new unsigned char [bufsize+1]; 00021 sp = ep = buf; 00022 memset(buf,0,bufsize); 00023 } 00024 00025 RingBuffer::~RingBuffer() 00026 { 00027 delete [] buf; 00028 } 00029 00030 int 00031 RingBuffer::save(unsigned char c) 00032 { 00033 if( (ep==sp-1)|| 00034 ((sp==buf)&& 00035 (ep==buf+bufsize-1)) ) /* buffer full */ 00036 return 0; 00037 00038 *ep = c; 00039 ep++; 00040 00041 if(ep > buf+bufsize) 00042 ep = buf; 00043 00044 return 1; 00045 } 00046 00047 unsigned char 00048 RingBuffer::read(void) 00049 { 00050 unsigned char ret; 00051 00052 if(sp == ep) 00053 return 0; /* buffer empty */ 00054 00055 ret = *sp; 00056 *sp = 0; 00057 sp++; 00058 00059 if(sp > buf+bufsize) 00060 sp = buf; 00061 00062 return ret; 00063 } 00064 00065 int 00066 RingBuffer::check(void) 00067 { 00068 int n = ep-sp; 00069 if(n<0) 00070 n = bufsize-n; 00071 00072 return n; 00073 } 00074
Generated on Tue Jul 19 2022 02:56:40 by
1.7.2