Pages: 1 [2]   Go Down
Author Topic: String as a function name ??  (Read 1453 times)
0 Members and 1 Guest are viewing this topic.
New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Or, with function pointers and lookup table:

Code:
struct funcLookup{
  char name[10];
  void (*function)(int);
};

int x = 10;
funcLookup funcTable[3];

void setup(){
  strcpy(funcTable[0].name, "func1");
  strcpy(funcTable[1].name, "func2");
  strcpy(funcTable[2].name, "func3");
 
  funcTable[0].function = &func1;
  funcTable[1].function = &func2;
  funcTable[2].function = &func3;
 
  Serial.begin(115200);
 
  char funcName[] = "func2";
 
  callFunc(funcName, 10);
 
}

void loop(){
 
 
}

void callFunc(char* funcName, int val){
  for(int i = 0; i < 3; i++){
    if(strcmp(funcName, funcTable[i].name) == 0){
      funcTable[i].function(val);
      return;
    }
  }
}

void func1(int val){
  Serial.println("Function 1");
  Serial.println(val);
}

void func2(int val){
  Serial.println("Function 2");
  Serial.println(val);
}

void func3(int val){
  Serial.println("Function 3");
  Serial.println(val);
}
Logged


0
Offline Offline
Full Member
***
Karma: 1
Posts: 225
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, function names can be used as strings in a sense:
http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html
Logged

Pages: 1 [2]   Go Up
Jump to: