I have run into a problem using SimpleTimer. I need to call a function this is a private member of a class I am using.
The definition of the function is:
void MyClass::a() {
...
}
Since it is not just
void a() {
...
}
I am unable to pass the name of the function to SimpleTimer.setTimeout() method.
I have tried:
//TimID = Tim.setTimeout(10000L,a);
which results in the error: 'a' was not declared in this scope
and I have tried:
TimID = Tim.setTimeout(10000L,MyClass::a)
; which results in the error: error: invalid use of non-static member function 'void MyClass::a()'
I have created a bare-bones script that contains just the code to define a class and some functions to demonstarte the problem:
#include <SimpleTimer.h>
SimpleTimer Tim;
int TimID = -1;
class MyClass {
private:
void thing(String t);
void a();
public:
MyClass();
void begin();
};
MyClass::MyClass() {
}
void MyClass::thing(String t){
Serial.println(t);
}
void MyClass::a() {
thing("hello");
}
void b(){
// Start a timer to call function a() after 10s
//Since function a() is part of MyClass, the setTimeout code cannot find it
// Both lines below fail to compile with the error in the respective comments
//TimID = Tim.setTimeout(10000L,a); // 'a' was not declared in this scope
//TimID = Tim.setTimeout(10000L,MyClass::a); // error: invalid use of non-static member function 'void MyClass::a()'
}
void MyClass::begin() {
Serial.println();
Serial.println("Started");
MyClass::a(); // prints Hello as expected
b();
}
MyClass Sample;
void setup() {
Serial.begin(115200);
Sample.begin();
}
void loop() {
}
I have also tried to create a 'helper' function such as:
void helper() {
MyClass::a();
}
And then defining helper in the setTimeout method, but a() is private, so helper won't compile.
I have spent hours trying to understand some posts that look like they may be related, especially:
https://forum.arduino.cc/index.php?topic=86758.0
But I can't understand how to solve the problem.
Is there some way of doing what I need to do?
Is there a different Timer library that does not have this issue?
I may have to do my own 'timer' and check for them in .run method of my main class.