diff --git a/examples/cluster.c b/examples/cluster.c index 6af33db..38e5f3b 100644 --- a/examples/cluster.c +++ b/examples/cluster.c @@ -11,7 +11,12 @@ #include static void cluster_update(hexagram_cluster *cluster, - struct can_frame *frame) { + cairo_t *cr, + hexagram_window *window, + struct can_frame *frame, + struct timeval *last) { + struct timeval now; + switch (frame->can_id) { case 0x280: { cluster->state.rpm = 0.25 * @@ -38,6 +43,20 @@ static void cluster_update(hexagram_cluster *cluster, break; } + + default: { + return; + } + } + + gettimeofday(&now, NULL); + + if (now.tv_sec - last->tv_sec || now.tv_usec - last->tv_usec > 16666) { + hexagram_window_refresh_bg(window); + hexagram_cluster_draw_fg(cluster, cr); + hexagram_window_swap_buffer(window); + + memcpy(last, &now, sizeof(now)); } } @@ -68,13 +87,13 @@ int main(int argc, char **argv) { width = 1024, height = 480; + cairo_t *fg, *bg; + struct timeval last = { .tv_sec = 0, .tv_usec = 0 }; - cairo_t *fg, *bg; - if (argc != 2) { return usage(argc, argv, "No CAN interface specified"); } @@ -158,27 +177,7 @@ int main(int argc, char **argv) { hexagram_can_if_read(can_if, &frame); - switch (frame.can_id) { - case 0x280: - case 0x288: - case 0x320: - case 0x5a0: { - struct timeval now; - - cluster_update(cluster, &frame); - - (void)gettimeofday(&now, NULL); - - if (now.tv_sec - last.tv_sec - || now.tv_usec - last.tv_usec > 16666) { - hexagram_window_refresh_bg(window); - hexagram_cluster_draw_fg(cluster, fg); - hexagram_window_swap_buffer(window); - - (void)memcpy(&last, &now, sizeof(now)); - } - } - } + cluster_update(cluster, fg, window, &frame, &last); } }