This is Webservice SDK for mbed. LPCXpresso1769/LPC1768/FRDM-K64F/LPC4088

Dependents:   MbedFileServer_1768MiniDK2 RedWireBridge IssueDebug_gcc MiMicRemoteMCU-for-Mbed ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NyLPC_cRingBuffer.c Source File

NyLPC_cRingBuffer.c

00001 /*********************************************************************************
00002  * PROJECT: MiMic
00003  * --------------------------------------------------------------------------------
00004  *
00005  * This file is part of MiMic
00006  * Copyright (C)2011 Ryo Iizuka
00007  *
00008  * MiMic is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License as published
00010  * by the Free Software Foundation, either version 3 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public License
00019  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020  *
00021  * For further information please contact.
00022  *  http://nyatla.jp/
00023  *  <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
00024  *
00025  *********************************************************************************/
00026 #include "NyLPC_cRingBuffer.h"
00027 
00028 
00029 #if NyLPC_ARCH==NyLPC_ARCH_FREERTOS
00030 #elif NyLPC_ARCH==NyLPC_ARCH_WIN32
00031 void NyLPC_cRingBuffer_dump(NyLPC_TcRingBuffer_t* i_inst)
00032 {
00033     NyLPC_TUInt8* s=((NyLPC_TUInt8*)(i_inst+1));//バッファ開始位置
00034     int i;
00035     if(i_inst->ro<=i_inst->wo){
00036         for(i=0;i<i_inst->ro;i++){
00037             printf("-- ");
00038         }
00039         for(i=i_inst->ro;i<i_inst->wo;i++){
00040             printf("%02X ",*(s+i));
00041         }
00042         for(i=i_inst->wo;i<i_inst->bl;i++){
00043             printf("-- ");
00044         }
00045     }else{
00046         for(i=0;i<i_inst->wo;i++){
00047             printf("%02X ",*(s+i));
00048         }
00049         for(i=i_inst->wo;i<i_inst->ro;i++){
00050             printf("-- ");
00051         }
00052         for(i=i_inst->ro;i<i_inst->bl;i++){
00053             printf("%02X ",*(s+i));
00054         }
00055     }
00056     printf("\n");
00057 }
00058 #else
00059 #endif
00060 
00061 void NyLPC_cRingBuffer_reset(NyLPC_TcRingBuffer_t* i_inst)
00062 {
00063     i_inst->ro=i_inst->wo=0;
00064 }
00065 
00066 NyLPC_TInt16 NyLPC_cRingBuffer_getReadableSize(NyLPC_TcRingBuffer_t* i_inst)
00067 {
00068     volatile NyLPC_TUInt16 wo=(i_inst)->wo;
00069     volatile NyLPC_TUInt16 ro=(i_inst)->ro;
00070     if(wo>=ro)
00071     {
00072         return wo-ro;
00073     }else{
00074         return (i_inst)->bl-ro;
00075     }
00076 }
00077 
00078 NyLPC_TInt16 NyLPC_cRingBuffer_getWritableSize(const NyLPC_TcRingBuffer_t* i_inst)
00079 {
00080     volatile NyLPC_TUInt16 wo=(i_inst)->wo;
00081     volatile NyLPC_TUInt16 ro=(i_inst)->ro;
00082     if(wo>=ro){
00083         //書込み可能サイズの計算
00084         return i_inst->bl-(wo-ro)-1;
00085     }else{
00086         return ro-wo-1;
00087     }
00088 
00089 }
00090 
00091 void NyLPC_cRingBuffer_initialize(NyLPC_TcRingBuffer_t* i_inst,void* i_buf,NyLPC_TUInt16 sizeof_buf)
00092 {
00093     //バッファの開始位置と終了位置の計算
00094     i_inst->bl=sizeof_buf;
00095     i_inst->ro=0;
00096     i_inst->wo=0;
00097     i_inst->buf=i_buf;
00098 }
00099 
00100 int NyLPC_cRingBuffer_write(NyLPC_TcRingBuffer_t* i_inst,NyLPC_TUInt8* i_data,const int i_len)
00101 {
00102     NyLPC_TUInt8* s=(NyLPC_TUInt8*)i_inst->buf;
00103     NyLPC_TUInt8* p;
00104     NyLPC_TUInt16 wo=i_inst->wo;
00105     NyLPC_TUInt16 ro=i_inst->ro;
00106     NyLPC_TUInt16 wsize;//書込み可能サイズ
00107     int l,i;
00108     NyLPC_TUInt16 rw;
00109 
00110     if(wo>=ro){
00111         //書込み可能サイズの計算
00112         wsize=i_inst->bl-(wo-ro)-1;
00113         if(wsize<1){
00114             return 0;
00115         }
00116         //書込みサイズの調整
00117         if(wsize>i_len){
00118             wsize=i_len;
00119         }
00120         //右側の書込みサイズの計算
00121         rw=i_inst->bl-wo;
00122         l=(wsize<rw)?wsize:rw;
00123         //書込みポインタを設定
00124         p=(s+wo);
00125         for(i=l-1;i>=0;i--){
00126             *(p++)=*(i_data++);
00127         }
00128         //書込み位置の調整
00129         wo=(wo+l)%i_inst->bl;
00130         l=wsize-l;//lに左側の書込みサイズストア
00131     }else{
00132         wsize=ro-wo-1;
00133         if(wsize>i_len){
00134             wsize=i_len;
00135         }
00136         l=wsize;
00137     }
00138     if(l>0){
00139         //左側の書込み
00140         p=(s+wo);
00141         for(i=l-1;i>=0;i--){
00142             *(p++)=*(i_data++);
00143         }
00144         wo+=l;
00145     }
00146     i_inst->wo=wo;
00147     return wsize;
00148 }
00149 //読出しポインタを得る。
00150 NyLPC_TUInt8* NyLPC_cRingBuffer_pread(NyLPC_TcRingBuffer_t* i_inst,NyLPC_TUInt16 *len)
00151 {
00152     NyLPC_TUInt16 ro=i_inst->ro;
00153     *len=NyLPC_cRingBuffer_getReadableSize(i_inst);
00154     return ((NyLPC_TUInt8*)(i_inst->buf))+ro;
00155 }
00156 
00157 //前方シークする。
00158 void NyLPC_cRingBuffer_preadSeek(NyLPC_TcRingBuffer_t* i_inst,NyLPC_TUInt16 i_seek)
00159 {
00160     NyLPC_Assert(NyLPC_cRingBuffer_getReadableSize(i_inst)>=i_seek);
00161     i_inst->ro=(i_inst->ro+i_seek)%i_inst->bl;
00162 }
00163 
00164 #define TEST
00165 #ifndef TEST
00166 void main(void)
00167 {
00168     NyLPC_TUInt16 l;
00169     NyLPC_TUInt8* b;
00170     int c;
00171     char buf[sizeof(NyLPC_TcRingBuffer_t)+5];
00172     NyLPC_TcRingBuffer_t* s;
00173     s=NyLPC_cRingBuffer_initialize(buf,sizeof(buf));
00174     for(;;){
00175         b=NyLPC_cRingBuffer_getReadPtr(s,&l);
00176         printf("readable:%d\n",l);
00177         c=NyLPC_cRingBuffer_write(s,"0123456789",3);
00178         NyLPC_cRingBuffer_dump(s);
00179         b=NyLPC_cRingBuffer_getReadPtr(s,&l);
00180         printf("readable:%d\n",l);
00181         NyLPC_cRingBuffer_seekReadPtr(s,(l>1)?l-1:1);
00182         printf("read:%d\n",(l>1)?l-1:1);
00183         NyLPC_cRingBuffer_dump(s);
00184     }
00185 
00186 }
00187 #endif