I’m trying to pass a parameter along with a function pointer.
Currently I have this code that works, but I wanted to refactor it to avoid creating footprints(?) for the doUserAnimation calls.
#include <string>
#include <iostream>
typedef void (*AnimationFunction) ();
int gDefaultAnimationCount = 0;
struct LedAnimationArray
{
std::string name;
AnimationFunction function;
};
void A_AllDark()
{
std::cout << "BuiltInAllDark";
std::cout << 'n';
}
void A_AllLight()
{
std::cout << "BuiltInAllLight";
std::cout << 'n';
}
void doUserAnimation(int animationNumber)
{
std::cout << "UserAnimation_";
std::cout << animationNumber;
std::cout << 'n';
}
LedAnimationArray _builtInAnimations[]
{
{"Off", A_AllDark},
{"On", A_AllLight}
};
// [Start want to remove this code]
void UserAni1()
{
doUserAnimation(0);
}
void UserAni2()
{
doUserAnimation(1);
}
LedAnimationArray _userAnimations[]
{
{"UserAni1",UserAni1},
{"UserAni2",UserAni2}
};
// [End want to remove this code]
LedAnimationArray getAnimation(int animationNumber)
{
if (animationNumber > gDefaultAnimationCount - 1)
{
LedAnimationArray b = {"usertest",_userAnimations[animationNumber-gDefaultAnimationCount].function};
// I want to try and do something like this, but that works :)
//LedAnimationArray b = {"usertest", doUserAnimation(animationNumber-gDefaultAnimationCount)};
return b;
}
else
{
return _builtInAnimations[animationNumber];
}
}
int main()
{
gDefaultAnimationCount = sizeof(_builtInAnimations) / sizeof(_builtInAnimations[0]) ;
getAnimation(0).function();
getAnimation(1).function();
getAnimation(2).function();
getAnimation(3).function();
return 0;
}
So I currently pass a pointer to a function within an array like this:
LedAnimationArray b = {"usertest",_userAnimations[animationNumber-gDefaultAnimationCount].function};
But this means I have to pre-define an array of all the possibilities.
I want to try and remove the _userAnimation
array, and pass a function with a parameter instead.
Something like this:
LedAnimationArray b = {"usertest",doUserAnimation(animationNumber-gDefaultAnimationCount)};
1