Command all jmCLIG modules from serial port

Committer:
jm
Date:
Sat Feb 12 16:50:25 2011 +0000
Revision:
0:9112e09912db
jmAll Command Line Interface Module

Who changed what in which revision?

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