So I'm trying to make a program to provide a interface between a raspberry pi and a accelerometer/gyroscope. I need this function to update as often as possible and I want to set a timer for when it will run.
And here is my code:
void updateGyro(Gyro gyro) {
gyro.update();
}
int main() {
if (gpioInitialise() < 0)
return -1;
// Creates new gyro object
Gyro gyro;
// Sets a timer to update every 10 milliseconds
gpioSetTimerFuncEx(0, 10, updateGyro, &gyro);
time_sleep(10);
gpioTerminate();
}
But this doesn't seem to work and I get the error:
gyro.cpp: In function ‘int main()’:
gyro.cpp:113:31: error: invalid conversion from ‘void (*)(Gyro)’ to ‘gpioTimerFuncEx_t’ {aka ‘void (*)(void*)’} [-fpermissive]
113 | gpioSetTimerFuncEx(0, 10, updateGyro, &gyro);
| ^~~~~~~~~~
| |
| void (*)(Gyro)
In file included from gyro.cpp:2:
/usr/include/pigpio.h:3751:55: note: initializing argument 3 of ‘int gpioSetTimerFuncEx(unsigned int, unsigned int, gpioTimerFuncEx_t, void*)’
3751 | unsigned timer, unsigned millis, gpioTimerFuncEx_t f, void *userdata);
| ~~~~~~~~~~~~~~~~~~^
I've tried passing in the gyro.update function in directly into the function but that also didn't work. Does anyone know how to fix this?
void*
is not a placeholder for any number of any arguments. It means exactly that, single pointer to unknown type. updateGyro
doesn't accept argument of type void*
, it accepts argument of type Gyro
.
You need to change your function to accept void*
instead:
void updateGyro(void* arg) {
Gyro* gyro = static_cast<Gyro*>(arg);
gyro->update();
}