Consolidate cluster state into cluster object
This commit is contained in:
parent
d167b58b24
commit
7cd217bef4
3 changed files with 26 additions and 25 deletions
|
@ -10,34 +10,31 @@
|
||||||
#include <hexagram/window.h>
|
#include <hexagram/window.h>
|
||||||
#include <hexagram/cluster.h>
|
#include <hexagram/cluster.h>
|
||||||
|
|
||||||
static hexagram_cluster_state state = {
|
static void cluster_update(hexagram_cluster *cluster,
|
||||||
0, 0, 0, 0
|
struct can_frame *frame) {
|
||||||
};
|
|
||||||
|
|
||||||
static void cluster_update(struct can_frame *frame) {
|
|
||||||
switch (frame->can_id) {
|
switch (frame->can_id) {
|
||||||
case 0x280: {
|
case 0x280: {
|
||||||
state.rpm = 0.25 *
|
cluster->state.rpm = 0.25 *
|
||||||
(double)(frame->data[2] | (frame->data[3] << 8));
|
(double)(frame->data[2] | (frame->data[3] << 8));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x288: {
|
case 0x288: {
|
||||||
state.temp = (double)(frame->data[1] - 48 * 0.75);
|
cluster->state.temp = (double)(frame->data[1] - 48 * 0.75);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x320: {
|
case 0x320: {
|
||||||
state.fuel = (double)(frame->data[2] & 0xf) / 16.0;
|
cluster->state.fuel = (double)(frame->data[2] & 0xf) / 16.0;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x5a0: {
|
case 0x5a0: {
|
||||||
state.rps = 0.001 * (double)(frame->data[1]
|
cluster->state.rps = 0.001 * (double)(frame->data[1]
|
||||||
| (frame->data[2] << 8));
|
| (frame->data[2] << 8));
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -136,7 +133,7 @@ int main(int argc, char **argv) {
|
||||||
case MapNotify:
|
case MapNotify:
|
||||||
case Expose:
|
case Expose:
|
||||||
hexagram_window_refresh_bg(window);
|
hexagram_window_refresh_bg(window);
|
||||||
hexagram_cluster_draw_fg(cluster, fg, &state);
|
hexagram_cluster_draw_fg(cluster, fg);
|
||||||
hexagram_window_swap_buffer(window);
|
hexagram_window_swap_buffer(window);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
@ -168,14 +165,14 @@ int main(int argc, char **argv) {
|
||||||
case 0x5a0: {
|
case 0x5a0: {
|
||||||
struct timeval now;
|
struct timeval now;
|
||||||
|
|
||||||
cluster_update(&frame);
|
cluster_update(cluster, &frame);
|
||||||
|
|
||||||
(void)gettimeofday(&now, NULL);
|
(void)gettimeofday(&now, NULL);
|
||||||
|
|
||||||
if (now.tv_sec - last.tv_sec
|
if (now.tv_sec - last.tv_sec
|
||||||
|| now.tv_usec - last.tv_usec > 16666) {
|
|| now.tv_usec - last.tv_usec > 16666) {
|
||||||
hexagram_window_refresh_bg(window);
|
hexagram_window_refresh_bg(window);
|
||||||
hexagram_cluster_draw_fg(cluster, fg, &state);
|
hexagram_cluster_draw_fg(cluster, fg);
|
||||||
hexagram_window_swap_buffer(window);
|
hexagram_window_swap_buffer(window);
|
||||||
|
|
||||||
(void)memcpy(&last, &now, sizeof(now));
|
(void)memcpy(&last, &now, sizeof(now));
|
||||||
|
|
|
@ -9,10 +9,6 @@
|
||||||
#include <hexagram/fuel.h>
|
#include <hexagram/fuel.h>
|
||||||
#include <hexagram/mfd.h>
|
#include <hexagram/mfd.h>
|
||||||
|
|
||||||
typedef struct _hexagram_cluster_state {
|
|
||||||
double rpm, rps, temp, fuel;
|
|
||||||
} hexagram_cluster_state;
|
|
||||||
|
|
||||||
typedef struct _hexagram_cluster {
|
typedef struct _hexagram_cluster {
|
||||||
hexagram_tacho tacho;
|
hexagram_tacho tacho;
|
||||||
hexagram_speedo speedo;
|
hexagram_speedo speedo;
|
||||||
|
@ -22,6 +18,10 @@ typedef struct _hexagram_cluster {
|
||||||
|
|
||||||
double width,
|
double width,
|
||||||
height;
|
height;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
double rpm, rps, temp, fuel;
|
||||||
|
} state;
|
||||||
} hexagram_cluster;
|
} hexagram_cluster;
|
||||||
|
|
||||||
hexagram_cluster *hexagram_cluster_new(double width,
|
hexagram_cluster *hexagram_cluster_new(double width,
|
||||||
|
@ -31,7 +31,6 @@ void hexagram_cluster_draw_bg(hexagram_cluster *cluster,
|
||||||
cairo_t *cr);
|
cairo_t *cr);
|
||||||
|
|
||||||
void hexagram_cluster_draw_fg(hexagram_cluster *cluster,
|
void hexagram_cluster_draw_fg(hexagram_cluster *cluster,
|
||||||
cairo_t *cr,
|
cairo_t *cr);
|
||||||
hexagram_cluster_state *state);
|
|
||||||
|
|
||||||
#endif /* _HEXAGRAM_CLUSTER_H */
|
#endif /* _HEXAGRAM_CLUSTER_H */
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <hexagram/cluster.h>
|
#include <hexagram/cluster.h>
|
||||||
|
|
||||||
|
@ -41,6 +42,11 @@ hexagram_cluster *hexagram_cluster_new(double width, double height) {
|
||||||
cluster->width = width;
|
cluster->width = width;
|
||||||
cluster->height = height;
|
cluster->height = height;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Initialize mutable state
|
||||||
|
*/
|
||||||
|
memset(&cluster->state, '\0', sizeof(cluster->state));
|
||||||
|
|
||||||
return cluster;
|
return cluster;
|
||||||
|
|
||||||
error_malloc_cluster:
|
error_malloc_cluster:
|
||||||
|
@ -63,16 +69,15 @@ void hexagram_cluster_draw_bg(hexagram_cluster *cluster,
|
||||||
}
|
}
|
||||||
|
|
||||||
void hexagram_cluster_draw_fg(hexagram_cluster *cluster,
|
void hexagram_cluster_draw_fg(hexagram_cluster *cluster,
|
||||||
cairo_t *cr,
|
cairo_t *cr) {
|
||||||
hexagram_cluster_state *state) {
|
hexagram_tacho_draw_needle(&cluster->tacho, cr, cluster->state.rpm);
|
||||||
hexagram_tacho_draw_needle(&cluster->tacho, cr, state->rpm);
|
|
||||||
|
|
||||||
hexagram_speedo_draw_needle(&cluster->speedo, cr,
|
hexagram_speedo_draw_needle(&cluster->speedo, cr,
|
||||||
(2.032 * state->rps * 3600) / 1000.0);
|
(2.032 * cluster->state.rps * 3600) / 1000.0);
|
||||||
|
|
||||||
hexagram_thermo_draw_needle(&cluster->thermo, cr,
|
hexagram_thermo_draw_needle(&cluster->thermo, cr,
|
||||||
state->temp);
|
cluster->state.temp);
|
||||||
|
|
||||||
hexagram_fuel_draw_needle(&cluster->fuel, cr,
|
hexagram_fuel_draw_needle(&cluster->fuel, cr,
|
||||||
state->fuel);
|
cluster->state.fuel);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue