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