I am about to write a custom ESPHome component. I don't have much experience with C language and I am facing some troubles using external library.
For demonstration, I prepared a simple component class..
class Test: public Component {
public:
auto t = timer_create_default();
void setup() override {
ESP_LOGD("TEST", "Test setup called!");
t.every(1000, TestLog);
}
void loop() override {
ESP_LOGD("TEST", "Test loop called!");
t.tick();
}
bool TestLog(void *) {
ESP_LOGD("TEST", "TestLOG!");
return true;
}
}
With this, I receive:
In file included from src\main.cpp:32:0: src\Test.h:7:35: error: non-static data member declared 'auto' auto t = timer_create_default();
I took it from some example where they did not have the class, but I can't find out, how to use it.
The library is:
https://github.com/contrem/arduino-timer/
I can still rewrite it without this timer completely and handle it only in the loop function, but I would like to understand what I am doing wrong.
If I change the return type to Timer<> I got another error:
src\Test.h: In member function 'virtual void Test::setup()': src\Test.h:11:24: error: no matching function for call to 'Timer<>::every(int, )'
t.every(1000, TestLog);
You can not use
autoto declare non-staticmember variables so you need to replaceautowith the type returned bytimer_create_default().If you are not sure what type it returns, you can simply use
decltypein the declaration:If I read the code in the repo correctly, the returned type is
Timer<>, so this should also work:or simply:
Also: The function pointer passed to
t.every()should be abool (*)(void*)butTestLogis a non-staticmember function and the pointer type isbool (Test::*)(void*)- You can fix that by makingTestLogstatic:If you want to get the
Testinstance in theTestLogcallback, make theTimerand change
TestLog:and in
setup():You'll now get a pointer to the
Testinstance in theTestLogcallback and you can use this to call a non-staticmember function inTest.Full example: