Ttelmah Guest
|
|
Posted: Sat Jun 16, 2007 3:20 am |
|
|
If you look in 'input.h', at the source code for 'get_string', you will see how to code a function to build a string, and return when the string is complete (with a length limit for safety as well).
This contains some 'extras' (backspace handling primarily), which you won't need.
The alternative (which is quicker when the command completes), is to instead do a 'tree' comparison, on a per character basis.
If you have an array of commands, listed in alphabetical order, and two 'static' counters, one for 'line', and one for 'column', initialised to zero, then when a character arrives, you search 'down' the lines, till you get a match with the first character, incrementing 'line' as you go. Since this is a single character test, it can be done using '==' (quickly) You then increment the 'column' counter, and test the character it is pointing at. If this is '0', then the whole string has been matched, and the 'line' counter gives the command number. If not, you exit, and wait for the next character.
Code: |
const int8 keywords[8][11] = {
"ACKOFF",
"ACKON",
"AUTOn ",
"Axxxxxxxxx",
"BAUDn ",
"Bxxxxxxxxx",
"CAL_ONn ",
"xxxxxxxxxx"
};
//Typical array of keywords. 'xxxx' markers added see code for why.
int16 ipno; //temporary used to hold an input 'number'
int8 MyIsdigit(int8 val){
if (val>'/' && val<return>15)
phase=0;
//Now scan the incoming character
while (true) {
if (phase==0) {
//Here I am searching for an element in the text array
temp=(cval>='a' && cval<='z')?cval-32:cval;
//Now I have uppercase only.
while (true) {
srch=keywords[row][col];
if (srch=='\0') {
//Have got to the end of the string
have_command(row);
//call the handler, with the row number of the 'match'
ccnt=0;
break;
}
else if (srch==tempch) {
//Here the character matches
col++;
break;
}
else if (srch=='n') {
//Demo of a special command character to read a number
//from the input
col++;
phase=1;
ipno=0;
break;
}
else if (srch=='x') {
//Here match has failed
phase=0;
break;
}
//try next row and loop
row++;
}
//exit the 'while'
break;
}
if (phase==1) {
//Here I am looking for a numeric field - as each digit arrives, ipval
//is multiplied by 10, and the new character added. If I reach a 'non
//numeric' character, it marks the end of the conversion.
if (Myisdigit(cval)) {
ipno*=10;
ipno+=(cval-'0');
break;
}
phase=2;
//Loop again, in case the new character is the start of another
//command
}
}
}
|
If you call this, with each character as it is receiver, it'll call 'have_command', with the row number that matches when a command is seen.
The 'x' lines are added to slightly speed the search.
I wouldn't guarantee this to work, it is cut out of a much larger parser, with checksum handling etc., and I'd not warrant I have got it completely right, but it should be 'close'.
You call 'parse(val)', with each received character.
Best Wishes |
|