From b850ddd28996880181db1740d63e81cfdf57b918 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Mon, 27 May 2019 01:32:21 -0500 Subject: [PATCH] Whoah, you're loaded! --- examples/cluster.c | 100 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 76 insertions(+), 24 deletions(-) diff --git a/examples/cluster.c b/examples/cluster.c index 5d2b09b..505f2fc 100644 --- a/examples/cluster.c +++ b/examples/cluster.c @@ -1,12 +1,15 @@ +#include +#include +#include +#include + #include #include #include #include #include -#include -#include -#include +#include static void draw_needle(cairo_t *cr, double x, @@ -53,9 +56,14 @@ static void draw_face_number(cairo_t *cr, static void draw_tachometer(cairo_t *cr, double x, double y, - double r) { + double r, + double rpm) { int i; + if (rpm > 8000) { + rpm = 8000; + } + cairo_select_font_face(cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); @@ -87,7 +95,8 @@ static void draw_tachometer(cairo_t *cr, */ draw_needle(cr, x, y, 0.77 * r, 232 * (M_PI/180), - 488 * (M_PI/180), 0.0); + 488 * (M_PI/180), + rpm / 8000); /* * Draw a tiny boi circle @@ -144,10 +153,17 @@ static void draw_mfd(cairo_t *cr, } static void draw_gauge_cluster(cairo_t *cr, + struct can_frame *frame, double x, double y, double width, double height) { + static double rpm = 0; + + if (frame->can_id == 0x280) { + rpm = 0.25 * (double)(frame->data[2] | (frame->data[3] << 8)); + } + /* * Paint canvas black */ @@ -157,7 +173,8 @@ static void draw_gauge_cluster(cairo_t *cr, draw_tachometer(cr, x + width * 0.2, y + height * 0.5, - height * 0.4); + height * 0.4, + rpm); draw_speedometer(cr, x + width * 0.8, @@ -186,8 +203,11 @@ int main(int argc, char **argv) { Window win; Pixmap buf; GC gc; + fd_set rfds, wfds, rready, wready; - int screen, + hexagram_can_if *can_if; + + int screen, fd, fd2, width = 1024, height = 480; @@ -200,11 +220,15 @@ int main(int argc, char **argv) { cairo_surface_t *sfc; cairo_t *cr; - XEvent e; + if ((can_if = hexagram_can_if_open("vcan0")) == NULL) + exit(1); if ((display = XOpenDisplay(NULL)) == NULL) exit(1); + fd = hexagram_can_if_fd(can_if); + fd2 = ConnectionNumber(display); + screen = DefaultScreen(display); win = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, width, height, 0, 0, 0); gc = XCreateGC(display, win, GCForeground | GCBackground | GCGraphicsExposures, &values); @@ -218,29 +242,57 @@ int main(int argc, char **argv) { cr = cairo_create(sfc); - draw_gauge_cluster(cr, 0, 0, width, height); + FD_ZERO(&rfds); + + FD_SET(fd, &rfds); + FD_SET(fd2, &rfds); while (1) { - XExposeEvent *expose; + int nready, pending; - XNextEvent(cairo_xlib_surface_get_display(sfc), &e); + while ((pending = XPending(display)) != 0) { + XEvent e; - switch (e.type) { - case Expose: - expose = (XExposeEvent *)&e; + XNextEvent(display, &e); - XCopyArea(display, buf, win, gc, expose->x, expose->y, - expose->width, expose->height, - expose->x, expose->y); + switch (e.type) { + case Expose: + fprintf(stderr, "Event %d\n", e.type); - break; - - case ButtonPress: - case KeyPress: - goto done; + XCopyArea(display, buf, win, gc, + e.xexpose.x, + e.xexpose.y, + e.xexpose.width, + e.xexpose.height, + e.xexpose.x, + e.xexpose.y); - default: - fprintf(stderr, "Dropping unhandled XEvent.type = %d.\n", e.type); + break; + + case ButtonPress: + case KeyPress: + break; + + default: + fprintf(stderr, "Dropping unhandled XEvent.type = %d.\n", e.type); + } + } + + memcpy(&rready, &rfds, sizeof(rfds)); + + if (select(fd2, &rready, NULL, NULL, NULL) < 0) { + break; + } + + if (FD_ISSET(fd, &rready)) { + struct can_frame frame; + + hexagram_can_if_read(can_if, &frame); + draw_gauge_cluster(cr, &frame, 0, 0, width, height); + + XCopyArea(display, buf, win, gc, 0, 0, + 1024, 480, + 0, 0); } }