Pages: [1]   Go Down
Author Topic: Is it possible dynamically create the name of a function?  (Read 1035 times)
0 Members and 1 Guest are viewing this topic.
Logroño - Spain
Offline Offline
Sr. Member
****
Karma: 4
Posts: 333
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Shelby Township, Disco, Michigan
Offline Offline
God Member
*****
Karma: 26
Posts: 778
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Offline Offline
Edison Member
*
Karma: 62
Posts: 1035
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

Logroño - Spain
Offline Offline
Sr. Member
****
Karma: 4
Posts: 333
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26620
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Logroño - Spain
Offline Offline
Sr. Member
****
Karma: 4
Posts: 333
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Full Member
***
Karma: 9
Posts: 226
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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++);
}
Logged

Please don't PM me asking for help. Ask questions in the forum.

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 54
Posts: 1857
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Pages: [1]   Go Up
Jump to: