![](/media/cache/profiles/0f0ee3310223fe38a989b2c818709393.50x50_q85.jpg)
Generate up to 8 different digital waeforms from serial port
jmRingBuffer.c@0:374d47623fab, 2011-02-12 (annotated)
- Committer:
- jm
- Date:
- Sat Feb 12 16:41:07 2011 +0000
- Revision:
- 0:374d47623fab
jmPulse Command Line Interface Module
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jm | 0:374d47623fab | 1 | /************************************************************************* |
jm | 0:374d47623fab | 2 | * @file jmRingBuffer.c |
jm | 0:374d47623fab | 3 | * @brief Command Line Rx Ring Buffer |
jm | 0:374d47623fab | 4 | * |
jm | 0:374d47623fab | 5 | * @date Feb 12, 2011 |
jm | 0:374d47623fab | 6 | */ |
jm | 0:374d47623fab | 7 | |
jm | 0:374d47623fab | 8 | |
jm | 0:374d47623fab | 9 | #include "jmRingBuffer.h" |
jm | 0:374d47623fab | 10 | #include "stdio.h" |
jm | 0:374d47623fab | 11 | |
jm | 0:374d47623fab | 12 | // static creation of Command Line Buffer |
jm | 0:374d47623fab | 13 | struct RingBuffer Line, *pLine; |
jm | 0:374d47623fab | 14 | |
jm | 0:374d47623fab | 15 | /** @brief Command line ring buffer initialization. |
jm | 0:374d47623fab | 16 | * @param none |
jm | 0:374d47623fab | 17 | * @returns none |
jm | 0:374d47623fab | 18 | */ |
jm | 0:374d47623fab | 19 | void InitCommandLineRingBuffer(void){ |
jm | 0:374d47623fab | 20 | pLine = &Line; |
jm | 0:374d47623fab | 21 | FlushRingBuffer(pLine); |
jm | 0:374d47623fab | 22 | } |
jm | 0:374d47623fab | 23 | |
jm | 0:374d47623fab | 24 | /** @brief Move ring head pointer foward. |
jm | 0:374d47623fab | 25 | * @param *p pointer to ring buffer |
jm | 0:374d47623fab | 26 | * @returns none |
jm | 0:374d47623fab | 27 | */ |
jm | 0:374d47623fab | 28 | void NextHead(struct RingBuffer *p) |
jm | 0:374d47623fab | 29 | { p->head++; |
jm | 0:374d47623fab | 30 | if(p->head >= DimRingBuffer) |
jm | 0:374d47623fab | 31 | p->head=0; |
jm | 0:374d47623fab | 32 | } |
jm | 0:374d47623fab | 33 | |
jm | 0:374d47623fab | 34 | /** @brief Move ring tail pointer foward. |
jm | 0:374d47623fab | 35 | * @param pointer to ring buffer |
jm | 0:374d47623fab | 36 | * @returns none |
jm | 0:374d47623fab | 37 | */ |
jm | 0:374d47623fab | 38 | void NextTail(struct RingBuffer *p) |
jm | 0:374d47623fab | 39 | { p->tail++; |
jm | 0:374d47623fab | 40 | if(p->tail >= DimRingBuffer) |
jm | 0:374d47623fab | 41 | p->tail=0; |
jm | 0:374d47623fab | 42 | } |
jm | 0:374d47623fab | 43 | |
jm | 0:374d47623fab | 44 | /** @brief Check if buffer full. |
jm | 0:374d47623fab | 45 | * @param *p pointer to ring buffer |
jm | 0:374d47623fab | 46 | * @returns true if full, false otherwise |
jm | 0:374d47623fab | 47 | */ |
jm | 0:374d47623fab | 48 | bool Full(struct RingBuffer *p) |
jm | 0:374d47623fab | 49 | { if(p->qty >= DimRingBuffer) |
jm | 0:374d47623fab | 50 | return true; |
jm | 0:374d47623fab | 51 | else |
jm | 0:374d47623fab | 52 | return false; |
jm | 0:374d47623fab | 53 | } |
jm | 0:374d47623fab | 54 | |
jm | 0:374d47623fab | 55 | /** @brief Insert a char in buffer. |
jm | 0:374d47623fab | 56 | * @param c unsigned char to be inserted |
jm | 0:374d47623fab | 57 | * @param *p pointer to ring buffer |
jm | 0:374d47623fab | 58 | * @returns none |
jm | 0:374d47623fab | 59 | */ |
jm | 0:374d47623fab | 60 | void Insert(unsigned char c, struct RingBuffer *p) |
jm | 0:374d47623fab | 61 | { if(Full(p)) |
jm | 0:374d47623fab | 62 | NextHead(p); |
jm | 0:374d47623fab | 63 | else |
jm | 0:374d47623fab | 64 | p->qty++; |
jm | 0:374d47623fab | 65 | |
jm | 0:374d47623fab | 66 | p->Buffer[p->tail]=c; |
jm | 0:374d47623fab | 67 | NextTail(p); |
jm | 0:374d47623fab | 68 | } |
jm | 0:374d47623fab | 69 | |
jm | 0:374d47623fab | 70 | /** @brief Check if ring buffer not empty. |
jm | 0:374d47623fab | 71 | * @param *p pointer to ring buffer |
jm | 0:374d47623fab | 72 | * @returns true if full, false otherwise |
jm | 0:374d47623fab | 73 | */ |
jm | 0:374d47623fab | 74 | bool NotEmpty(struct RingBuffer *p) |
jm | 0:374d47623fab | 75 | { if(p->qty == 0) |
jm | 0:374d47623fab | 76 | return false ; |
jm | 0:374d47623fab | 77 | else |
jm | 0:374d47623fab | 78 | return true; |
jm | 0:374d47623fab | 79 | } |
jm | 0:374d47623fab | 80 | |
jm | 0:374d47623fab | 81 | /** @brief Extract a char from ring buffer. |
jm | 0:374d47623fab | 82 | * @param *p pointer to ring buffer |
jm | 0:374d47623fab | 83 | * @returns unsigned char |
jm | 0:374d47623fab | 84 | */ |
jm | 0:374d47623fab | 85 | unsigned char Extract(struct RingBuffer *p) |
jm | 0:374d47623fab | 86 | { unsigned char c; |
jm | 0:374d47623fab | 87 | c = p->Buffer[p->head]; |
jm | 0:374d47623fab | 88 | if(NotEmpty(p)) |
jm | 0:374d47623fab | 89 | { NextHead(p); |
jm | 0:374d47623fab | 90 | p->qty--; |
jm | 0:374d47623fab | 91 | } |
jm | 0:374d47623fab | 92 | return c; |
jm | 0:374d47623fab | 93 | } |
jm | 0:374d47623fab | 94 | |
jm | 0:374d47623fab | 95 | /** @brief Flush ring buffer. |
jm | 0:374d47623fab | 96 | * @param *p pointer to ring buffer |
jm | 0:374d47623fab | 97 | * @returns none |
jm | 0:374d47623fab | 98 | */ |
jm | 0:374d47623fab | 99 | void FlushRingBuffer(struct RingBuffer *p) |
jm | 0:374d47623fab | 100 | { int i; |
jm | 0:374d47623fab | 101 | p->head = 0; |
jm | 0:374d47623fab | 102 | p->tail = 0; |
jm | 0:374d47623fab | 103 | p->qty = 0; |
jm | 0:374d47623fab | 104 | for(i=0;i<DimRingBuffer;i++)p->Buffer[i]=0; |
jm | 0:374d47623fab | 105 | } |
jm | 0:374d47623fab | 106 | |
jm | 0:374d47623fab | 107 | /** @brief Delete last char from ring buffer. |
jm | 0:374d47623fab | 108 | * @param *p pointer to ring buffer |
jm | 0:374d47623fab | 109 | * @returns none |
jm | 0:374d47623fab | 110 | */ |
jm | 0:374d47623fab | 111 | void DelChar(struct RingBuffer *p) |
jm | 0:374d47623fab | 112 | { if(p->qty != 0){ |
jm | 0:374d47623fab | 113 | if(p->tail==0)p->tail=DimRingBuffer; |
jm | 0:374d47623fab | 114 | else p->tail--; |
jm | 0:374d47623fab | 115 | p->qty--; |
jm | 0:374d47623fab | 116 | } |
jm | 0:374d47623fab | 117 | } |
jm | 0:374d47623fab | 118 | |
jm | 0:374d47623fab | 119 | /** @brief Remove a command line from ring buffer. |
jm | 0:374d47623fab | 120 | * @param c end command identifier unsigned char |
jm | 0:374d47623fab | 121 | * @param *p pointer to ring buffer |
jm | 0:374d47623fab | 122 | * @returns none |
jm | 0:374d47623fab | 123 | */ |
jm | 0:374d47623fab | 124 | void NextCommand(unsigned char c, struct RingBuffer *p){ |
jm | 0:374d47623fab | 125 | // remove all char till end identifier is found |
jm | 0:374d47623fab | 126 | while(NotEmpty(p) && p->Buffer[p->head] != c) |
jm | 0:374d47623fab | 127 | { NextHead(p); |
jm | 0:374d47623fab | 128 | p->qty--; |
jm | 0:374d47623fab | 129 | } |
jm | 0:374d47623fab | 130 | |
jm | 0:374d47623fab | 131 | // remove end identifier |
jm | 0:374d47623fab | 132 | if(NotEmpty(p)&& p->Buffer[p->head] == c) |
jm | 0:374d47623fab | 133 | { NextHead(p); |
jm | 0:374d47623fab | 134 | p->qty--; |
jm | 0:374d47623fab | 135 | } |
jm | 0:374d47623fab | 136 | } |
jm | 0:374d47623fab | 137 | |
jm | 0:374d47623fab | 138 | /** @brief View ring buffer content. |
jm | 0:374d47623fab | 139 | * Print ring buffer content |
jm | 0:374d47623fab | 140 | * @param *p pointer to ring buffer |
jm | 0:374d47623fab | 141 | * @returns none |
jm | 0:374d47623fab | 142 | */ |
jm | 0:374d47623fab | 143 | void ViewRingBuffer(struct RingBuffer *p){ |
jm | 0:374d47623fab | 144 | int i,j; |
jm | 0:374d47623fab | 145 | |
jm | 0:374d47623fab | 146 | printf("\nRingBuffer Qty: %d \nContent: ",p->qty); |
jm | 0:374d47623fab | 147 | |
jm | 0:374d47623fab | 148 | for(j=0,i=p->head;j<p->qty;j++){ |
jm | 0:374d47623fab | 149 | printf("%c",p->Buffer[i]); |
jm | 0:374d47623fab | 150 | if(i++>DimRingBuffer)i=0; |
jm | 0:374d47623fab | 151 | } |
jm | 0:374d47623fab | 152 | printf("\n"); |
jm | 0:374d47623fab | 153 | } |
jm | 0:374d47623fab | 154 | |
jm | 0:374d47623fab | 155 | /** @brief Extract a word from ring buffer. |
jm | 0:374d47623fab | 156 | * The extracted word is put in the array pointed by word |
jm | 0:374d47623fab | 157 | * @param p pointer to a ring buffer |
jm | 0:374d47623fab | 158 | * @param word pointer to array of char |
jm | 0:374d47623fab | 159 | * @returns true if a word is extracted otherwise returns false |
jm | 0:374d47623fab | 160 | */ |
jm | 0:374d47623fab | 161 | bool ExtractWord(struct RingBuffer *p, char * word){ |
jm | 0:374d47623fab | 162 | unsigned char c; |
jm | 0:374d47623fab | 163 | int i,j; |
jm | 0:374d47623fab | 164 | j=0; |
jm | 0:374d47623fab | 165 | |
jm | 0:374d47623fab | 166 | if(NotEmpty(p)){ |
jm | 0:374d47623fab | 167 | for(i=0;i<WordMaxSize-1;i++){ |
jm | 0:374d47623fab | 168 | // extract a char from Rx ring buffer |
jm | 0:374d47623fab | 169 | c=Extract(p); |
jm | 0:374d47623fab | 170 | |
jm | 0:374d47623fab | 171 | // remove leading blanks |
jm | 0:374d47623fab | 172 | if(c==' ' && j==0)continue; |
jm | 0:374d47623fab | 173 | |
jm | 0:374d47623fab | 174 | // end of word or end of command line |
jm | 0:374d47623fab | 175 | if(c==' ' || c==nl)break; |
jm | 0:374d47623fab | 176 | |
jm | 0:374d47623fab | 177 | // build the Word |
jm | 0:374d47623fab | 178 | word[j++]=c; |
jm | 0:374d47623fab | 179 | } |
jm | 0:374d47623fab | 180 | // 0 string termination |
jm | 0:374d47623fab | 181 | word[j]=0; |
jm | 0:374d47623fab | 182 | if(j>0)return true; |
jm | 0:374d47623fab | 183 | } |
jm | 0:374d47623fab | 184 | return false; |
jm | 0:374d47623fab | 185 | } |
jm | 0:374d47623fab | 186 | |
jm | 0:374d47623fab | 187 | /** @brief Extract an unsigned int from ring buffer. |
jm | 0:374d47623fab | 188 | * Convert a word from buffer into an integer between min and max values |
jm | 0:374d47623fab | 189 | * Value converted should be between min and max values. |
jm | 0:374d47623fab | 190 | * Value should be decimal or hexadecimal (beginning by 0x or 0X) |
jm | 0:374d47623fab | 191 | * @param p pointer to ring buffer |
jm | 0:374d47623fab | 192 | * @param result pointer to unsigned int |
jm | 0:374d47623fab | 193 | * @param min minimum limit |
jm | 0:374d47623fab | 194 | * @param max maximum limit |
jm | 0:374d47623fab | 195 | * @returns true if value is converted beetween limits, including limits. |
jm | 0:374d47623fab | 196 | */ |
jm | 0:374d47623fab | 197 | bool ExtractUInteger(struct RingBuffer *p, unsigned int *result, unsigned int min, unsigned int max){ |
jm | 0:374d47623fab | 198 | unsigned int i ; |
jm | 0:374d47623fab | 199 | char word[WordMaxSize-1]; |
jm | 0:374d47623fab | 200 | |
jm | 0:374d47623fab | 201 | if(ExtractWord(p,word)){ // Extract string value |
jm | 0:374d47623fab | 202 | if(word[0]=='0' && (word[1]=='x' || word[1]=='X')) { |
jm | 0:374d47623fab | 203 | sscanf(word,"%x",&i); // convert hexadecimal input |
jm | 0:374d47623fab | 204 | } |
jm | 0:374d47623fab | 205 | else |
jm | 0:374d47623fab | 206 | sscanf(word,"%d",&i); // convert decimal input |
jm | 0:374d47623fab | 207 | |
jm | 0:374d47623fab | 208 | if(i>=min && i<=max){ |
jm | 0:374d47623fab | 209 | *result = i; |
jm | 0:374d47623fab | 210 | return true; |
jm | 0:374d47623fab | 211 | } |
jm | 0:374d47623fab | 212 | } |
jm | 0:374d47623fab | 213 | *result = 0; |
jm | 0:374d47623fab | 214 | return false; |
jm | 0:374d47623fab | 215 | } |