Hi guys
I often need to implement a way to receive user input from uart and change settings. eg:
I usually write a function to find a match:
So with above case, that's what I need to do:
I often need to implement a way to receive user input from uart and change settings. eg:
Code:
// here are what a user may type in a terminal program
set param1 state1
set param1 state2
....
set param1 state10
set param2 state1
set param2 state2
...
set param2 state10
etc...
Code:
uint8_t isCmd(uint8_t *cmd, const unsigned char *param, uint8_t offset){
// get the size of the command
uint8_t size;
size = strlen(param);
// check if they match
uint8_t i = 0;
for(i = 0; i < size; i++){
if (cmd[i + offset] != param[i])
// no match
return 0;
}
// match found
return 1;
}
Code:
// look for "set" keyword, form location 0
if (isCmd(cmd_string_array, "set", 0){
// look for "param1" keyword, form location 4
// "set" + space = 4
if (isCmd(cmd_string_array, "param1", 4)){
// look for "state1" keyword, from location 11
// "set" + space + "param1" + space = 11
if (isCmd(cmd_string_array, "state1", 11){
// do stuff for state 1
}
else if (isCmd(cmd_string_array, "state2", 11){
// do stuff for state2
}
else if (isCmd(cmd_string_array, "state3", 11){
// do stuff for state3
}
else if (others){
}
}
// look for "param2" etc...
else if (isCmd(cmd_string_array, "param2", 4)){
// look for other state within this param2 etc...
}
}
Last edited: