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: RingBuffer_RTOS_test
Fork of RingBuffer by
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 mutex.lock(); 00034 if( (ep==sp-1)|| 00035 ((sp==buf)&& 00036 (ep==buf+bufsize-1)) ) { /* buffer full */ 00037 mutex.unlock(); 00038 return 0; 00039 } 00040 00041 *ep = c; 00042 ep++; 00043 00044 if(ep > buf+bufsize) 00045 ep = buf; 00046 00047 mutex.unlock(); 00048 return 1; 00049 } 00050 00051 unsigned char 00052 RingBuffer::read(void) 00053 { 00054 unsigned char ret; 00055 00056 mutex.lock(); 00057 if(sp == ep){ 00058 mutex.unlock(); 00059 return 0; /* buffer empty */ 00060 } 00061 ret = *sp; 00062 *sp = 0; 00063 sp++; 00064 00065 if(sp > buf+bufsize) 00066 sp = buf; 00067 00068 mutex.unlock(); 00069 return ret; 00070 } 00071 00072 int 00073 RingBuffer::check(void) 00074 { 00075 mutex.lock(); 00076 int n = ep-sp; 00077 if(n<0) 00078 n = bufsize-n; 00079 00080 mutex.unlock(); 00081 return n; 00082 } 00083
Generated on Sun Jul 17 2022 12:27:47 by
1.7.2
