Hello,
I'm currently having some issues creating some callback functions in cpp to use within the ino file.
I have it working using void functions with no parameters, but as soon as I try to add params as part of the callback, it starts throwing invalid use of Void Expression errors. Not sure what is up at this point, but I'm sure I'm probably screwing up the function pointer syntax.
The use case:
I'm currently using some rotary encoders and created a class to set them up and track all variables relevant to each encoder etc. I would like to create a callback function that takes in a encoder class reference as a parameter, so that I can have a handful of functions for the encoder class rather than a function for each specific encoder.
What's working:
The simplified C++ Declaration:
Class Encoder
{
public:
Encoder();
void SetCallbackLeft (void (void));
void SetCallbackRight (void (void));
private:
void (*LeftTurn) (void) = NULL;
void (*RightTurn) (void) = NULL;
C++ Definition:
void Encoder::SetCallbackLeft (void (*FuncPtr) (void) )
{
// Function LeftTurn is called when some additional functions determine the encoder has
// been turned counter counter-clockwise.
// Using this as I am variably changing the response of various encoders depending on use.
LeftTurn = FuncPtr;
}
void Encoder::SetCallbackRight (void (*FuncPtr) (void) )
{
// Function RightTurn is called when some additional functions determine the encoder has
// been turned counter clockwise.
// Using this as I am variably changing the response of various encoders depending on use.
RightTurn = FuncPtr
}
Within the INO:
Encoder Encoder1 ();
void setup()
{
Encoder1.SetCallbackRight(IncrementStoredVariable);
Encoder1.SetCallbackLeft(DecrementStoredVariable);
}
void loop()
{
Encoder1.EvaluateState();
}
void IncrementStoredVariable (void)
{
Encoder1.CurrentVarValue++;
}
All the above works fine. The problem is if I try to parametrize the ino function IncrementStoredVariable to take in any sort of parameters—let alone a reference to the invoking encoder, it throws an Invalid Use of Void Expression Error.
The goal is for me to just pass a reference of the invoking class instance to modify the variable as so.
INO
Encoder Encoder1 ();
void setup()
{
Encoder1.SetCallbackRight(IncrementStoredVariable(Encoder1));
Encoder1.SetCallbackLeft(DecrementStoredVariable(Encoder1));
}
void loop()
{
Encoder1.EvaluateState();
}
void IncrementStoredVariable (Encoder& TriggeringEncoder)
{
Encoder* CallingEval = TriggeringEncoder
CallingEval.CurrentVarValue++;
}
I have tried adjusting the cpp declaration and definitions to account for the encoder reference; however, no matter what I put, it keeps throwing a void expression error. I tried changing the return type of the class pointer itself, and that just doesnt compile with any usable error data. To simplify things even further, I tried just doing with with a simple uint8_t and that wont work either, so I'm at a loss. The only error it shows is an invalid use of void expression pointed at the callback binding in the ino setup: nothing else. It even throws that as the error if I try to change the function pointers to be uint8_t. So it shouldnt even be a void expression anymore.
A void function can still take in a parameter and return void, but the Arduino IDE seems to disagree with this. Again, I'm sure I am doing something wonky with my function pointer syntax and class reference, but at this point I'm out of ideas. I really do not want to be creating duplicate increment and decrement functions for the amount of encoders that will be used as input controls.
-- EDIT--
I tried bolding the areas that were changed in the final code block and realized that didnt work the way I wanted. Removed extra * marks.