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.
jmRingBuffer.c
00001 /************************************************************************* 00002 * @file jmRingBuffer.c 00003 * @brief Command Line Rx Ring Buffer 00004 * 00005 * @date December 27,2010 00006 */ 00007 00008 00009 #include "jmRingBuffer.h" 00010 #include "stdio.h" 00011 00012 struct RingBuffer Line, *pLine; 00013 00014 /** @brief Command line ring buffer initialization. 00015 * @param none 00016 * @returns none 00017 */ 00018 void InitCommandLineRingBuffer(void){ 00019 pLine = &Line; 00020 FlushRingBuffer(pLine); 00021 } 00022 00023 /** @brief Move ring head pointer foward. 00024 * @param *p pointer to ring buffer 00025 * @returns none 00026 */ 00027 void NextHead(struct RingBuffer *p) 00028 { p->head++; 00029 if(p->head >= DimRingBuffer) 00030 p->head=0; 00031 } 00032 00033 /** @brief Move ring tail pointer foward. 00034 * @param pointer to ring buffer 00035 * @returns none 00036 */ 00037 void NextTail(struct RingBuffer *p) 00038 { p->tail++; 00039 if(p->tail >= DimRingBuffer) 00040 p->tail=0; 00041 } 00042 00043 /** @brief Check if buffer full. 00044 * @param *p pointer to ring buffer 00045 * @returns true if full, false otherwise 00046 */ 00047 bool Full(struct RingBuffer *p) 00048 { if(p->qty >= DimRingBuffer) 00049 return true; 00050 else 00051 return false; 00052 } 00053 00054 /** @brief Insert a char in buffer. 00055 * @param c unsigned char to be inserted 00056 * @param *p pointer to ring buffer 00057 * @returns none 00058 */ 00059 void Insert(unsigned char c, struct RingBuffer *p) 00060 { if(Full(p)) 00061 NextHead(p); 00062 else 00063 p->qty++; 00064 00065 p->Buffer[p->tail]=c; 00066 NextTail(p); 00067 } 00068 00069 /** @brief Check if ring buffer not empty. 00070 * @param *p pointer to ring buffer 00071 * @returns true if full, false otherwise 00072 */ 00073 bool NotEmpty(struct RingBuffer *p) 00074 { if(p->qty == 0) 00075 return false ; 00076 else 00077 return true; 00078 } 00079 00080 /** @brief Extract a char from ring buffer. 00081 * @param *p pointer to ring buffer 00082 * @returns unsigned char 00083 */ 00084 unsigned char Extract(struct RingBuffer *p) 00085 { unsigned char c; 00086 c = p->Buffer[p->head]; 00087 if(NotEmpty(p)) 00088 { NextHead(p); 00089 p->qty--; 00090 } 00091 return c; 00092 } 00093 00094 /** @brief Flush ring buffer. 00095 * @param *p pointer to ring buffer 00096 * @returns none 00097 */ 00098 void FlushRingBuffer(struct RingBuffer *p) 00099 { int i; 00100 p->head = 0; 00101 p->tail = 0; 00102 p->qty = 0; 00103 for(i=0;i<DimRingBuffer;i++)p->Buffer[i]=0; 00104 } 00105 00106 /** @brief Delete last char from ring buffer. 00107 * @param *p pointer to ring buffer 00108 * @returns none 00109 */ 00110 void DelChar(struct RingBuffer *p) 00111 { if(p->qty != 0){ 00112 if(p->tail==0)p->tail=DimRingBuffer; 00113 else p->tail--; 00114 p->qty--; 00115 } 00116 } 00117 00118 /** @brief Remove a command line from ring buffer. 00119 * @param c end command identifier unsigned char 00120 * @param *p pointer to ring buffer 00121 * @returns none 00122 */ 00123 void NextCommand(unsigned char c, struct RingBuffer *p){ 00124 // remove all char till end identifier is found 00125 while(NotEmpty(p) && p->Buffer[p->head] != c) 00126 { NextHead(p); 00127 p->qty--; 00128 } 00129 00130 // remove end identifier 00131 if(NotEmpty(p)&& p->Buffer[p->head] == c) 00132 { NextHead(p); 00133 p->qty--; 00134 } 00135 } 00136 00137 /** @brief View ring buffer content. 00138 * Print ring buffer content 00139 * @param *p pointer to ring buffer 00140 * @returns none 00141 */ 00142 void ViewRingBuffer(struct RingBuffer *p){ 00143 int i,j; 00144 00145 printf("\nRingBuffer Qty: %d \nContent: ",p->qty); 00146 00147 for(j=0,i=p->head;j<p->qty;j++){ 00148 printf("%c",p->Buffer[i]); 00149 if(i++>DimRingBuffer)i=0; 00150 } 00151 printf("\n"); 00152 } 00153 00154 /** @brief Extract a word from ring buffer. 00155 * The extracted word is put in the array pointed by word 00156 * @param p pointer to a ring buffer 00157 * @param word pointer to array of char 00158 * @returns true if a word is extracted otherwise returns false 00159 */ 00160 bool ExtractWord(struct RingBuffer *p, char * word){ 00161 unsigned char c; 00162 int i,j; 00163 j=0; 00164 00165 if(NotEmpty(p)){ 00166 for(i=0;i<WordMaxSize-1;i++){ 00167 // extract a char from Rx ring buffer 00168 c=Extract(p); 00169 00170 // remove leading blanks 00171 if(c==' ' && j==0)continue; 00172 00173 // end of word or end of command line 00174 if(c==' ' || c==nl)break; 00175 00176 // build the Word 00177 word[j++]=c; 00178 } 00179 // 0 string termination 00180 word[j]=0; 00181 if(j>0)return true; 00182 } 00183 return false; 00184 } 00185 00186 /** @brief Extract an unsigned int from ring buffer. 00187 * Convert a word from buffer into an integer between min and max values 00188 * Value converted should be between min and max values. 00189 * Value should be decimal or hexadecimal (beginning by 0x or 0X) 00190 * @param p pointer to ring buffer 00191 * @param result pointer to unsigned int 00192 * @param min minimum limit 00193 * @param max maximum limit 00194 * @returns true if value is converted beetween limits, including limits. 00195 */ 00196 bool ExtractUInteger(struct RingBuffer *p, unsigned int *result, unsigned int min, unsigned int max){ 00197 unsigned int i ; 00198 char word[WordMaxSize-1]; 00199 00200 if(ExtractWord(p,word)){ // Extract string value 00201 if(word[0]=='0' && (word[1]=='x' || word[1]=='X')) { 00202 sscanf(word,"%x",&i); // convert hexadecimal input 00203 } 00204 else 00205 sscanf(word,"%d",&i); // convert decimal input 00206 00207 if(i>=min && i<=max){ 00208 *result = i; 00209 return true; 00210 } 00211 } 00212 *result = 0; 00213 return false; 00214 }
Generated on Tue Jul 12 2022 21:02:31 by
1.7.2