Reputation: 35
The following code executes perfectly on almost the linux platforms I've tested (Ubuntu, Mint, Raspbian x86) but fails completely on Raspbian for Raspberry Pi.
It successfully executes randomly a handful of times then stops triggering. The remainder of the program executes fine including several other timeouts added in the exact same manner and nothing else suffers in any noticeable manner.
Could it be the 5ms timeout that the raspberry pi can't keep up to? Why is it only that one aspect that fails while all other aspects display no glitches etc. Its as if the timeout is being cancelled for some reason or another.
void control_init() {
...
g_timeout_add(20, control_quick_timer_event, NULL);
...
}
void control_quick_timer_event() {
control_read_values();
control_since_last++;
if (control_since_last == 2) {
while (serialDataAvail(control_serial)) {
serialGetchar(control_serial);
}
control_read_state = -1;
}
}
* Edit * The additional of a printf("\n"); "fixes" the issue for an unknown reason but I can't accept it as a solution as I need the serial print out without millions of spaces.
return True;/return 1; at the tail also result in the same error, this has no impact on the result.
Upvotes: 0
Views: 237
Reputation: 3745
Just like @pan-mroku is trying to tell you, you need this code:
bool control_quick_timer_event() {
control_read_values();
control_since_last++;
if (control_since_last == 2) {
while (serialDataAvail(control_serial)) {
serialGetchar(control_serial);
}
control_read_state = -1;
}
return True;
}
If you don't return True
, the timeout will be canceled.
Upvotes: 0
Reputation: 891
I suspect it is because your handler returns void. From g_timeout_add:
The function is called repeatedly until it returns FALSE, at which point the timeout is automatically destroyed and the function will not be called again.
#include <gtk/gtk.h>
gboolean control_quick_timer_event(gpointer data)
{
printf("I'm still alive\n");
return TRUE;
}
int main (int argc, char *argv[])
{
gtk_init (&argc, &argv);
g_timeout_add(20, control_quick_timer_event, NULL);
gtk_main ();
return 0;
}
Upvotes: 1