I'm trying to define an array of function pointers, where each function contains an int
parameter. I'm also trying to set the value of that int parameter in the array declaration
So I have a TIMED_TASK
struct, that will hold the function pointer and value I want to pass
typedef struct
{
void (*proc)(int);
int delayMsec;
} TIMED_TASK;
Then I have an array of TIMED_TASK
s like this
static const TIMED_TASK attractSequence[] =
{
{ LightsOn, 1000 },
{ LightsOff, 500 },
{ EndSequence, 0 }
};
And I'd like it to call each of those functions in turn, passing the delay value to each function. This is where I expect I have the wrong syntax (I'm still learning C). I seemingly don't hit my LightsOn()
routine at all
void loop() // It's an arduino project :)
{
attractSequence[sequence];
sequence++;
}
void LightsOn(int pause)
{
// I do not hit this routine for some reason?
Serial.print("LIGHTS ON");
Serial.print(pause);
}
void LightsOff(int pause)
{
Serial.print("LIGHTS OFF");
Serial.print(pause);
}
It's entirely possible I'm taking the wrong approach here, but hopefully you can see what I'm trying to achieve. Any advice very welcome!
If you want to go through each item of attractSequence
only once, you can use:
void loop()
{
int count = sizeof(attractSequence)/sizeof(attractSequence[0]);
for (int i = 0; i < count; ++i )
{
attractSequence[i].proc(attractSequence[i].delayMsec);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.