Page 1 of 1

Timer callback as class instance

Posted: Tue Aug 16, 2022 2:04 pm
by glrtheil
I'm having a really hard time passing an instance of a callback method that's in a class (C++), oto an esp_timer callback. I'm essentially trying to port some code from an old project to ESP and this old project uses a Timer class that allows you to pass in the function and the instance of that class then uses std::bind() to point to the callback method, and that's what gets passed into the Timer. However I seem to be unable to do this with esp_timer, regardless of my attempts.

I've created the esp_timer_create_args_t, and tried passing in any non-static function, but that never works. I even went as far as passing the return of std::bind() as the arg of the timer_args, and calling it within there, but I get an panic abort when I try to call the function.

Is there any simple way to create esp_timer_create_args_t and pass in an instance of a callback function?

Re: Timer callback as class instance

Posted: Tue Aug 16, 2022 2:50 pm
by vanBassum
https://github.com/vanBassum/ESP_Compon ... os/timer.h
https://github.com/vanBassum/ESP_Compon ... callback.h

This is how I do this. :)
Especially the callback class is handy.

I think the code is quite straight forward, however if you need some help, do ask.

Re: Timer callback as class instance

Posted: Tue Aug 16, 2022 5:05 pm
by glrtheil
Wow, thanks so much for that. Looking through it now.

Re: Timer callback as class instance

Posted: Wed Aug 17, 2022 6:37 am
by ESP_igrr
As another reference, you could consider the ESPTimer class from esp-idf-cxx project: https://github.com/espressif/esp-idf-cx ... er_cxx.hpp