Timer callback as class instance

glrtheil
Posts: 61
Joined: Tue Dec 07, 2021 2:48 pm

Timer callback as class instance

Postby glrtheil » Tue Aug 16, 2022 2:04 pm

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?

vanBassum
Posts: 68
Joined: Sun Jan 17, 2021 11:59 am

Re: Timer callback as class instance

Postby vanBassum » Tue Aug 16, 2022 2:50 pm

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.

glrtheil
Posts: 61
Joined: Tue Dec 07, 2021 2:48 pm

Re: Timer callback as class instance

Postby glrtheil » Tue Aug 16, 2022 5:05 pm

Wow, thanks so much for that. Looking through it now.

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: Timer callback as class instance

Postby ESP_igrr » Wed Aug 17, 2022 6:37 am

As another reference, you could consider the ESPTimer class from esp-idf-cxx project: https://github.com/espressif/esp-idf-cx ... er_cxx.hpp

Who is online

Users browsing this forum: Baldhead and 112 guests