I’m writing a wrapper around the Arduino Keypad Library. First I created all the new functionality on a normal Sketch which uses the Keypad lib.
Then I modified the sketch to convert it to a library by creating a class.
So far everything is fine except that in the new library I can’t get the callback from the Keypad Lib to compile.
The compiler complains “error: invalid use of non-static member function” in the line where the callback function is set up.
I have looked around the internet, looking into other Arduino libraries’s source code but I can’t find a solution or clear example on how to do this.
All that I need is a way to get the callback work for a member function that is not static but a member of another ‘object’.
I’ll really appreciate any help.
Here is a minimal code where the issue is replicate as it happens in my project:
Hi,
Thanks for your answer.
Yes, you were right, I did miss the function parenthesis () but I still getting the same compile error.
I have change the names of the classes to ‘A’ and ‘B’ and shorter the member functions names so that the program looks more clean. I will re post the code and include an .ino for download.
Thanks again.
Better Answer:
Since you’re defining the interface, create a CallBack class that contains a callBack() method. Then, pass a pointer to an object of this class rather than a pointer to the callback function. Check this: https://forum.arduino.cc/index.php?topic=524167.msg3831858#msg3831858
I have change the names of the classes to 'A' and 'B' and shorter the member functions names so that the program looks more clean.
The code does NOT look "more clean". Good looking code uses names that make sense. A and B as class names mean NOTHING.
THINK about what you are trying to do. You have an interrupt being generated, which is equivalent to a doorbell ringing. You have some undefined number of instances of the class A, equivalent to people in the house. You want ONE of them to deal with the interrupt when it happens, without defining which one. That's like expecting ONE of the people in the house, but not ALL of them, to deal with the doorbell ringing. How can you possibly expect that to work?
A much better approach would be to write your own version of the keypad library that doesn’t use callbacks.
Reading a keypad is not rocket science, you just need two nested for-loops and a 2D array to keep the previous states.