Whoah, you're loaded!

This commit is contained in:
XANTRONIX Development 2019-05-27 01:32:21 -05:00
parent 1d275dcb5a
commit b850ddd289

View file

@ -1,12 +1,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <hexagram/can.h>
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);
}
}