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