In my program, I have an enum that is used for indexing my array members. The reason is that it is much easier for me to understand which parameter I am accessing without knowing its index in the array
I can then access any parameter for example:
I will be receiving serial commands such as :
When I receive this command, I must determine what parameter I need to modify based on the first 2 characters, then skip the 3rd character( because it is just ":" and I dont care about it) and the remaining characters will show the new value)
I wonder if there is any easier way to map the enum to a string
My current method:
The above method seems to work fine, However I do not think that this is the best possible solution. I wonder if it is possible to somehow map the string to the enum so that I can know which parameter I need to modify without perform annoying strcmp statements and etc. I hope to get some clarification on this.
If I have 50 parameters, that means that I will need to have to a strcmp block for each parameter which seems like really unconvenient. I hope its possible to do something like ( pseudo code
Code:
enum param_enum
{
AA,
AB,
AC,
AD,
PARAM_COUNT
};
static int16_t parameters[PARAM_COUNT] =
{
[AA] = 5,
[AB] = 3,
[AC] = 4,
[AD] = 8,
};
Code:
parameters[AA] = 10; // Update AA parameter to value 10.
Code:
"AA:15"
I wonder if there is any easier way to map the enum to a string
My current method:
Code:
// line holds the string data
// cmd_size is the length of string data
static void parse_data(uint8_t* data, uint16_t length){
int16_t temp_var;
char temp_storage[3]={0};
if( strncmp((char*)data,"AA:",3) == 0 ){
printf("AA: detect\n");
for(int i = 3;i<=length; i++){
temp_storage[i-3]=data[i];
if(data[i] == 0){
temp_var = strtol(temp_storage,NULL,10);
printf("temp var is set to =%u \n",temp_var);
parameters[AA] = temp_var;
}
}
}
}
If I have 50 parameters, that means that I will need to have to a strcmp block for each parameter which seems like really unconvenient. I hope its possible to do something like ( pseudo code
Code:
static void parse_data(uint8_t* data, uint16_t length){
int16_t temp_var;
// pass data, length do the function and empty int16_t variable which will be assigned to a updated data
/* map_to_string will need to map string for example "AB" to the which number it corresponds in param_enum. For example "AB" should correspond to 1 according to:
enum param_enum
{
AA, //0
AB, //1
AC,// 2
AD,// 3
PARAM_COUNT
};
*/
parameter_to_change = map_string_to_enum(data,length,&temp_var) // map_string_to_enum returns parameter and data
parameters[parameter_to_change ] = temp_var;
}