Go Down

Topic: Is it possible dynamically create the name of a function? (Read 1 time) previous topic - next topic

vffgaston

Hi all,

I need to program a "finite state automat". I need to call a function depending on the value of a variable (say "Status" that can take integer values: 0, 1, 2, . . .).

Is it possible to form the name of the function to call -and call it (i.e. Transition0,  Transition1, . . .)- from the value of the variable using C? (I've never been able using other languages)

Thanks in advance

steinie44

I don't think it can be done with flashed Arduino code, but some  languages that use peek and poke to modify memory, can be done.
Need to get the location in memory where the name is stored, then modify it. I have done it with Basic back in the 70's


Hackscribble

HI vffgaston

Quote
I need to call a function depending on the value of a variable


Do you mean you want to have a series of different functions and then call a specific one based on the value of the variable?

You could use a switch statement to do this.  Probably the easiest and most "bulletproof".

Alternatively, you could set up a matrix of function pointers and then use your variable to index into the array and call the relevant function.

Regards

Ray
Hackscribble.  Writing about making things.
arduino@hackscribble.com | www.hackscribble.com

vffgaston

Thanks steinie & Hackscribble,

The idea is to have the transitions (functions) named consecutively (i.e. Tra1, Tra2, Tra3, and so on). As the software modifies a variable (i.e. lastEvent) according to the events (obstacle, light, sound, . . .) the easiest (and, probably, most dangerous; you are rigth) way to do it is to form the name of the function to call by concatenating ("Tra" & text(lastEvent); pseudocode).

I've tried this -along the years- with different languages with no success.

Thanks anyway.

AWOL


Thanks steinie & Hackscribble,

the easiest (and, probably, most dangerous; you are rigth) way to do it is to form the name of the function to call by concatenating ("Tra" & text(lastEvent); pseudocode).

It isn't at all dangerous, because it simply isn't possible.
C is a compiled language - by the time your code gets to the controller, the function names no longer exist, which is why your approach cannot work, and neither can steinie44's, even if you did have write access to the flash memory.

An array of functions would be one way of doing this however.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

vffgaston

Thanks AWOL,

I'll have a look on the arrays capabilities. (Is the first time I use them -in C-; any case I need them for the rest of the automat: next state, event, . .)

Thanks again.

Regards.

bobcousins

I'll say the answer is sort of yes. You can't create function names to call at run-time, but you can have a function pointer in a variable, and assign values at run time, which does something similar to what you are asking.

Here is a simple example

Code: [Select]

int j;

void func0(int arg)
{
   Serial.print("func 0: ");
   Serial.println (arg);
}

void func1(int arg)
{
   Serial.print("func 1: ");
   Serial.println (arg);
}

void setup()
{
  Serial.begin (115200);
}

void loop() {
   void (*fp)(int);

   fp = func0;
   fp(j++);

   fp = func1;     
   fp(j++);
}
Please don't PM me asking for help. Ask questions in the forum.

MichaelMeissner

Well you can't do this dynamically in a compiled language like C/C++.  You can do it easily in dynamic languages like perl, python, lisp, etc.  However most of those languages won't run on Arduinos.

Now, you can use the class facility of C++ to tie functions to the class (note, I'm typing this free hand, and I am more of a C programmer rather than a C++ programmer, so I may make some mistakes in grammar):

Code: [Select]

class MyClass {
private:
  int data;
public:
  MyClass (int i_data) { data = i_data; }
  ~MyClass () {}
  int get_value (void) { return data; }
  void set_value (int i_data) { data = i_data; }
  int increment (void) { return ++data; }
  int decrement (void) { return --data; }
};

MyClass foo (0);


Now, you can dynamically allocate classes, but you have to make sure when you are done with the class, you free it, or you use higher level methods that involve garbage collection of class data to free the data when no references to it exist.

Go Up