Reputation: 41
Using the RPI Pico SDK, I have three files. I want to use a callback function and access private members of a class. The callback function is in an SDK and I can not modify it. How do I do this?
/////// test.h
bool main_loop_timer_callback(struct repeating_timer *t);
class MyClass {
private:
static int count;
struct repeating_timer main_loop_timer;
public:
MyClass();
friend bool main_loop_timer_callback(struct repeating_timer *t);
};
//////// test.cc
#include <iostream>
#include "pico/stdlib.h"
#include "test.h"
using namespace std;
bool main_loop_timer_callback(struct repeating_timer *t) {
MyClass::count++;
cout << "callback " << MyClass::count << endl;
return true;
}
MyClass::MyClass() {
add_repeating_timer_us(-50000,
main_loop_timer_callback,
NULL,
&main_loop_timer);
count = 0;
};
/////// test-main.cc
#include "pico/stdlib.h"
#include "test.h"
using namespace std;
int main() {
MyClass test;
stdio_init_all();
}
Upvotes: 0
Views: 835
Reputation: 36412
The Pi Pico repeating_timer
API allows you to pass a user_data
argument when adding a timer (the third parameter to add_repeating_timer_us
).
This value is later returned in the user_data
member of the struct repeating_timer
passed to your callback.
This can be anything you want, e.g. in your case, a pointer to MyClass
, so long as you cast it to and from void *
.
Upvotes: 1