Hello,
i am looking for a good guide that is easy to understand about call back functions. I looked on google, but all i can find is for c++ and python?
I am working with the menwiz library, and was told i need to write a call back action?
thanks ![]()
Hello,
i am looking for a good guide that is easy to understand about call back functions. I looked on google, but all i can find is for c++ and python?
I am working with the menwiz library, and was told i need to write a call back action?
thanks ![]()
Did you take a look at the examples ?
All the functions that you attach to the menu data, those are the callback functions.
s1->addVar(MW_ACTION,myfunc);
s1->addVar(MW_ACTION,savevar);
menu.addUsrScreen(msc,10000);
The "addUsrScreen" is a special callback function. It is activated when no buttons are pushed for a certain time.
i did look at the examples, i didnt realize those were the callback functions... the examples i were looking at all looked like
somewords::somewords(x, y)
ill take another look.
but all i can find is for c++
Good thing that the Arduino is programmed in C++, then.
:~
i thought it was just c.
i thought it was just c.
Nope.
Basically they're just function pointers. There is tons of info if you enter that in Google.
I'm sorry Paul. I thought your post would make things more clear, but i'm stuck in the weeds here.
Are there more examples than the two included in the library?
Qdeathstar:
Are there more examples than the two included in the library?
Google "function pointers"
ok
int main()
{
  void (*foo)(int);
  /* the ampersand is actually optional */
  foo = &my_int_func;
  return 0;
}
Why is the (int) there for?
if I say:
void(*foo);
foo = functionIWantToCall
//
if (button is pressed) {
*foo
}
will that call the pointer under the if statement, which in turn calls the function "functionIWantToCall"? Is the (int) there so i can pass a variable to functionIWantToCall ?
Here is an example:
// callback prototype
typedef int (* myfunc) (const int x, const int y);
// prototype
void doCalc (myfunc f, const int x, const int y);
int add (const int x, const int y)
{
return x + y;
}
int subtract (const int x, const int y)
{
return x - y;
}
void doCalc (myfunc f, const int x, const int y)
{
int result = f (x, y); // call the callback function "f"
Serial.println (result);
}
void setup ()
{
Serial.begin (115200);
Serial.println ();
} // end of setup
void loop ()
{
doCalc (add, 20, 22);
doCalc (subtract, 20, 22);
delay (1000);
} // end of loop
Output:
42
-2
void (*foo)(int);...
Why is the (int) there for?
That declares a callback function which takes a single int argument.
In my example the function:
typedef int (* myfunc) (const int x, const int y);
... takes two int arguments, returning an int.
Thus the implemented callback has to look like that:
int add (const int x, const int y)
{
return x + y;
}
So in this case the "add" function takes two int arguments and returns an int, and is thus a valid callback for that prototype.
Qdeathstar:
if (button is pressed) {
*foo
}
Read further down that page. You write:
foo( 2 );
Thank you. That looks familiar to another example i looked at. however, I've tried to bold some questions
// callback prototype
typedef int (* myfunc) (const int x, const int y);
// prototype
void doCalc (myfunc f, const int x, const int y);
int add (const int x, const int y)
{
return x + y;
}
int subtract (const int x, const int y)
{
return x - y;
}
void doCalc (myfunc f, const int x, const int y)
{
int result = f (x, y); // call the callback function "f" where is function f at? I only see the following functions: add, subtract, doCalc, setup, and loop?
Serial.println (result);
}
void setup ()
{
Serial.begin (115200);
Serial.println ();
} // end of setup
void loop ()
{
doCalc (add, 20, 22); Which one of these is the pointer, and which is the callback function (doCalc or add)?
doCalc (subtract, 20, 22);
delay (1000);
} // end of loop
void doCalc (myfunc f, const int x, const int y)
{
int result = f (x, y); // call the callback function "f" where is function f at? I only see the following functions: add, subtract, doCalc, setup, and loop?
Here:
void doCalc (myfunc f, const int x, const int y)
That is the "callback" part. The function (pointer) is passed to doCalc. It "calls back" by invoking f.
doCalc (add, 20, 22); Which one of these is the pointer, and which is the callback function (doCalc or add)?
The function "add" is the callback function. It is in fact a pointer to add. "add" itself is defined further up.
so if i understand what is happening here:
A callback function , doCalc is made before the setup.
doCalc(myfunc f, const int x, cons int y);
and it expects three "inputs". The function name (pointer), and two variables.
then, in the loop, you call doCalc with three inputs
doCalc(add, x, y)
which gets sent backup to the callback function, which in turn calls the add function, to perform the actions on x and y. That result is then sent back to doCalc, who then prints it out via serial?
Is that correct?
Yes, except for your terminology. I would call doCalc an ordinary function that takes as an argument a callback function. So the callback function is the one that is "called back". So "add" and "subtract" are the callback functions. These are not called directly, but via doCalc.
So we are not doing:
add (20, 22);
If we were, then "add" is called directly.
But we are doing:
 doCalc (add, 20, 22);
So doCalc, is "calling back" to "add" to do some work. Thus you could say that doCalc is generic. It could be calling an add, subtract, divide, multiply, maximum, minimum, or all sorts of other functions.
Callback functions are often used when a designer wants to let you do "something" (but s/he doesn't know what that something might be) when a thing happens (eg. an interrupt occurs). So by providing a generic "callback" function they are saying "I'll call your function, you make it do what you want".
You've got it.
Don't feel bad. Function pointers are some of the nastiest syntax in C, and I still struggle with the syntax after writing in C variants for almost 30 years.
(C lambdas/blocks/closures are even worse, but that's another story...)
Qdeathstar:
so if i understand what is happening here:A callback function , doCalc is made before the setup.
doCalc(myfunc f, const int x, cons int y);
and it expects three "inputs". The function name (pointer), and two variables.
then, in the loop, you call doCalc with three inputs
doCalc(add, x, y)
which gets sent backup to the callback function, which in turn calls the add function, to perform the actions on x and y. That result is then sent back to doCalc, who then prints it out via serial?
Is that correct?
Thanks.
In the above example I don't see anything that looks like s1->
What does that part of the menwiz code do?
s1->addVar(MW_ACTION,myfunc);
Translating to our example
s1->DoCalc(Add, x, y);
Is that right?