From 0299922b887e32e5d4f246d9adf53410cea8115c Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sun, 9 Jun 2019 15:46:41 -0500 Subject: [PATCH] Why not go for a massive refactor? --- examples/cluster.c | 11 +++- include/hexagram/cluster.h | 32 +++++++--- include/hexagram/fuel.h | 28 ++++++--- include/hexagram/gauge.h | 45 +++++++------- include/hexagram/mfd.h | 12 +++- include/hexagram/speedo.h | 23 +++++--- include/hexagram/tacho.h | 26 +++++--- include/hexagram/thermo.h | 26 +++++--- src/cluster.c | 118 +++++++++++++++++++------------------ src/fuel.c | 75 +++++++++++++++-------- src/gauge.c | 102 ++++++++++++++++++++------------ src/mfd.c | 30 ++++++---- src/speedo.c | 65 ++++++++++---------- src/tacho.c | 85 +++++++++++++------------- src/thermo.c | 80 ++++++++++++++----------- 15 files changed, 452 insertions(+), 306 deletions(-) diff --git a/examples/cluster.c b/examples/cluster.c index 4e800a2..41fe651 100644 --- a/examples/cluster.c +++ b/examples/cluster.c @@ -65,6 +65,7 @@ int main(int argc, char **argv) { hexagram_can_if *can_if; hexagram_window *window; + hexagram_cluster *cluster; int fd, width = 1024, @@ -89,6 +90,10 @@ int main(int argc, char **argv) { return 1; } + if ((cluster = hexagram_cluster_new(width, height)) == NULL) { + return 1; + } + display = hexagram_window_display(window); fd = hexagram_can_if_fd(can_if); @@ -102,7 +107,7 @@ int main(int argc, char **argv) { /* * Draw the background layer */ - hexagram_cluster_draw_bg(bg, width, height); + hexagram_cluster_draw_bg(cluster, bg); /* * Present the background layer @@ -136,7 +141,7 @@ int main(int argc, char **argv) { case MapNotify: case Expose: hexagram_window_refresh_bg(window); - hexagram_cluster_draw_fg(&state, fg, 0, 0, width, height); + hexagram_cluster_draw_fg(cluster, fg, &state); hexagram_window_swap_buffer(window); break; @@ -171,7 +176,7 @@ int main(int argc, char **argv) { if (now.tv_sec - last.tv_sec || now.tv_usec - last.tv_usec > 16666) { hexagram_window_refresh_bg(window); - hexagram_cluster_draw_fg(&state, fg, 0, 0, width, height); + hexagram_cluster_draw_fg(cluster, fg, &state); hexagram_window_swap_buffer(window); (void)memcpy(&last, &now, sizeof(now)); diff --git a/include/hexagram/cluster.h b/include/hexagram/cluster.h index 7fac712..f66c0d8 100644 --- a/include/hexagram/cluster.h +++ b/include/hexagram/cluster.h @@ -3,19 +3,35 @@ #include +#include +#include +#include +#include +#include + typedef struct _hexagram_cluster_state { double rpm, rps, temp, fuel; } hexagram_cluster_state; -void hexagram_cluster_draw_bg(cairo_t *cr, - double width, - double height); +typedef struct _hexagram_cluster { + hexagram_tacho tacho; + hexagram_speedo speedo; + hexagram_thermo thermo; + hexagram_fuel fuel; + hexagram_mfd mfd; -void hexagram_cluster_draw_fg(hexagram_cluster_state *state, + double width, + height; +} hexagram_cluster; + +hexagram_cluster *hexagram_cluster_new(double width, + double height); + +void hexagram_cluster_draw_bg(hexagram_cluster *cluster, + cairo_t *cr); + +void hexagram_cluster_draw_fg(hexagram_cluster *cluster, cairo_t *cr, - double x, - double y, - double width, - double height); + hexagram_cluster_state *state); #endif /* _HEXAGRAM_CLUSTER_H */ diff --git a/include/hexagram/fuel.h b/include/hexagram/fuel.h index 26002c7..03d2037 100644 --- a/include/hexagram/fuel.h +++ b/include/hexagram/fuel.h @@ -1,16 +1,26 @@ #ifndef _HEXAGRAM_FUEL_H #define _HEXAGRAM_FUEL_H -void hexagram_fuel_draw_face(cairo_t *cr, - double x, - double y, - double r, - double redline); +#include -void hexagram_fuel_draw_needle(cairo_t *cr, - double x, - double y, - double r, +#include + +typedef struct _hexagram_fuel { + hexagram_gauge gauge; + double redline; +} hexagram_fuel; + +void hexagram_fuel_init(hexagram_fuel *fuel, + double x, + double y, + double radius, + double redline); + +void hexagram_fuel_draw_face(hexagram_fuel *fuel, + cairo_t *cr); + +void hexagram_fuel_draw_needle(hexagram_fuel *fuel, + cairo_t *cr, double level); #endif /* _HEXAGRAM_FUEL_H */ diff --git a/include/hexagram/gauge.h b/include/hexagram/gauge.h index 10bc657..12ad1e0 100644 --- a/include/hexagram/gauge.h +++ b/include/hexagram/gauge.h @@ -3,30 +3,35 @@ #include -void hexagram_gauge_draw_needle(cairo_t *cr, - double x, - double y, - double r, - double min_angle, - double max_angle, - double value); +typedef struct _hexagram_gauge { + double x, y; + double radius; + double min_angle; + double max_angle; +} hexagram_gauge; -void hexagram_gauge_draw_number(cairo_t *cr, - double x, - double y, - double r, - double min_angle, - double max_angle, +void hexagram_gauge_init(hexagram_gauge *gauge, + double x, + double y, + double radius, + double min_angle, + double max_angle); + +void hexagram_gauge_draw_number(hexagram_gauge *gauge, + cairo_t *cr, + double radius, double value, const char *text); -void hexagram_gauge_draw_mark(cairo_t *cr, - double x, - double y, - double min_r, - double max_r, - double min_angle, - double max_angle, +void hexagram_gauge_draw_mark(hexagram_gauge *gauge, + cairo_t *cr, + double min_radius, + double max_radius, double value); +void hexagram_gauge_draw_needle(hexagram_gauge *gauge, + cairo_t *cr, + double radius, + double value); + #endif /* _HEXAGRAM_GAUGE_H */ diff --git a/include/hexagram/mfd.h b/include/hexagram/mfd.h index 44e6c81..dd783d7 100644 --- a/include/hexagram/mfd.h +++ b/include/hexagram/mfd.h @@ -3,10 +3,20 @@ #include -void hexagram_mfd_draw(cairo_t *cr, +typedef struct _hexagram_mfd { + double x, y; + + double width, + height; +} hexagram_mfd; + +void hexagram_mfd_init(hexagram_mfd *mfd, double x, double y, double width, double height); +void hexagram_mfd_draw(hexagram_mfd *mfd, + cairo_t *cr); + #endif /* _HEXAGRAM_MFD_H */ diff --git a/include/hexagram/speedo.h b/include/hexagram/speedo.h index 30fc9c0..86588bd 100644 --- a/include/hexagram/speedo.h +++ b/include/hexagram/speedo.h @@ -3,15 +3,22 @@ #include -void hexagram_speedo_draw_face(cairo_t *cr, - double x, - double y, - double r); +#include -void hexagram_speedo_draw_needle(cairo_t *cr, - double x, - double y, - double r, +typedef struct _hexagram_speedo { + hexagram_gauge gauge; +} hexagram_speedo; + +void hexagram_speedo_init(hexagram_speedo *speedo, + double x, + double y, + double radius); + +void hexagram_speedo_draw_face(hexagram_speedo *speedo, + cairo_t *cr); + +void hexagram_speedo_draw_needle(hexagram_speedo *speedo, + cairo_t *cr, double kph); #endif /* _HEXAGRAM_SPEEDO_H */ diff --git a/include/hexagram/tacho.h b/include/hexagram/tacho.h index 4dbb7d3..52455f3 100644 --- a/include/hexagram/tacho.h +++ b/include/hexagram/tacho.h @@ -3,16 +3,24 @@ #include -void hexagram_tacho_draw_face(cairo_t *cr, - double x, - double y, - double r, - double redline); +#include -void hexagram_tacho_draw_needle(cairo_t *cr, - double x, - double y, - double r, +typedef struct _hexagram_tacho { + hexagram_gauge gauge; + double redline; +} hexagram_tacho; + +void hexagram_tacho_init(hexagram_tacho *tacho, + double x, + double y, + double radius, + double redline); + +void hexagram_tacho_draw_face(hexagram_tacho *tacho, + cairo_t *cr); + +void hexagram_tacho_draw_needle(hexagram_tacho *tacho, + cairo_t *cr, double rpm); #endif /* _HEXAGRAM_TACHO_H */ diff --git a/include/hexagram/thermo.h b/include/hexagram/thermo.h index fcc4c58..85d87cf 100644 --- a/include/hexagram/thermo.h +++ b/include/hexagram/thermo.h @@ -3,16 +3,24 @@ #include -void hexagram_thermo_draw_face(cairo_t *cr, - double x, - double y, - double r, - double redline); +#include -void hexagram_thermo_draw_needle(cairo_t *cr, - double x, - double y, - double r, +typedef struct _hexagram_thermo { + hexagram_gauge gauge; + double redline; +} hexagram_thermo; + +void hexagram_thermo_init(hexagram_thermo *thermo, + double x, + double y, + double radius, + double redline); + +void hexagram_thermo_draw_face(hexagram_thermo *thermo, + cairo_t *cr); + +void hexagram_thermo_draw_needle(hexagram_thermo *thermo, + cairo_t *cr, double temp); #endif /* _HEXAGRAM_THERMO_H */ diff --git a/src/cluster.c b/src/cluster.c index 5e1ff4c..edef543 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -1,76 +1,78 @@ -#include -#include -#include -#include -#include +#include + #include -void hexagram_cluster_draw_bg(cairo_t *cr, - double width, - double height) { +hexagram_cluster *hexagram_cluster_new(double width, double height) { + hexagram_cluster *cluster; + + if ((cluster = malloc(sizeof(*cluster))) == NULL) { + goto error_malloc_cluster; + } + + hexagram_tacho_init(&cluster->tacho, + width * 0.2, + height * 0.5, + height * 0.4, + 6500.0); + + hexagram_speedo_init(&cluster->speedo, + width * 0.8, + height * 0.5, + height * 0.4); + + hexagram_thermo_init(&cluster->thermo, + width * 0.43, + height * 0.76, + height * 0.13, + 240.0); + + hexagram_fuel_init(&cluster->fuel, + width * 0.57, + height * 0.76, + height * 0.13, + 0.125); + + hexagram_mfd_init(&cluster->mfd, + width * 0.42, + height * 0.10, + width * 0.16, + height * 0.50); + + cluster->width = width; + cluster->height = height; + + return cluster; + +error_malloc_cluster: + return NULL; +} + +void hexagram_cluster_draw_bg(hexagram_cluster *cluster, + cairo_t *cr) { /* * Paint canvas black */ cairo_set_source_rgb(cr, 0, 0, 0); cairo_paint(cr); - hexagram_tacho_draw_face(cr, - width * 0.2, - height * 0.5, - height * 0.4, - 6500); - - hexagram_speedo_draw_face(cr, - width * 0.8, - height * 0.5, - height * 0.4); - - hexagram_thermo_draw_face(cr, - width * 0.43, - height * 0.76, - height * 0.13, - 240.0); - - hexagram_fuel_draw_face(cr, - width * 0.57, - height * 0.76, - height * 0.13, - 0.125); - - hexagram_mfd_draw(cr, - width * 0.42, - height * 0.1, - width * 0.16, - height * 0.5); + hexagram_tacho_draw_face(&cluster->tacho, cr); + hexagram_speedo_draw_face(&cluster->speedo, cr); + hexagram_thermo_draw_face(&cluster->thermo, cr); + hexagram_fuel_draw_face(&cluster->fuel, cr); + hexagram_mfd_draw(&cluster->mfd, cr); } -void hexagram_cluster_draw_fg(hexagram_cluster_state *state, +void hexagram_cluster_draw_fg(hexagram_cluster *cluster, cairo_t *cr, - double x, - double y, - double width, - double height) { - hexagram_tacho_draw_needle(cr, - x + width * 0.2, - y + height * 0.5, - height * 0.4, - state->rpm); + hexagram_cluster_state *state) { + hexagram_tacho_draw_needle(&cluster->tacho, cr, state->rpm); - hexagram_speedo_draw_needle(cr, - width * 0.8, - height * 0.5, - height * 0.4, + hexagram_speedo_draw_needle(&cluster->speedo, cr, (2.032 * state->rps * 3600) / 1000.0); - hexagram_thermo_draw_needle(cr, - width * 0.43, - height * 0.76, - height * 0.13, + hexagram_thermo_draw_needle(&cluster->thermo, cr, state->temp); - hexagram_fuel_draw_needle(cr, - width * 0.57, - height * 0.76, - height * 0.13, + hexagram_fuel_draw_needle(&cluster->fuel, cr, state->fuel); } diff --git a/src/fuel.c b/src/fuel.c index 3771a44..0ba6741 100644 --- a/src/fuel.c +++ b/src/fuel.c @@ -3,50 +3,71 @@ #include #include -void hexagram_fuel_draw_face(cairo_t *cr, - double x, - double y, - double r, - double redline) { +void hexagram_fuel_init(hexagram_fuel *fuel, + double x, + double y, + double radius, + double redline) { + hexagram_gauge_init(&fuel->gauge, x, y, radius, + 300.0 * (M_PI / 180.0), + 420.0 * (M_PI / 180.0)); + + fuel->redline = redline; +} + +void hexagram_fuel_draw_face(hexagram_fuel *fuel, + cairo_t *cr) { int i; cairo_set_source_rgb(cr, 1, 1, 1); - cairo_arc(cr, x, y, r, 0, 2*M_PI); + cairo_arc(cr, + fuel->gauge.x, + fuel->gauge.y, + fuel->gauge.radius, + 0, + 2.0 * M_PI); + cairo_stroke(cr); - cairo_move_to(cr, x - 0.8 * r, y - 0.1 * r); + cairo_move_to(cr, + fuel->gauge.x - 0.8 * fuel->gauge.radius, + fuel->gauge.y - 0.1 * fuel->gauge.radius); + cairo_show_text(cr, "0"); - cairo_move_to(cr, x - 0.15 * r, y - 0.5 * r); + cairo_move_to(cr, + fuel->gauge.x - 0.15 * fuel->gauge.radius, + fuel->gauge.y - 0.50 * fuel->gauge.radius); + cairo_show_text(cr, "1/2"); - cairo_move_to(cr, x + 0.65 * r, y - 0.1 * r); + cairo_move_to(cr, + fuel->gauge.x + 0.65 * fuel->gauge.radius, + fuel->gauge.y - 0.10 * fuel->gauge.radius); + cairo_show_text(cr, "1"); /* * Draw gauge graduations */ for (i=0; i<=16; i++) { - if (i <= (16.0 * redline)) { + if (i <= (16.0 * fuel->redline)) { cairo_set_source_rgb(cr, 1, 0, 0); } else { cairo_set_source_rgb(cr, 1, 1, 1); } - hexagram_gauge_draw_mark(cr, x, y, - ((i % 4)? 0.8: 0.7) * r, - 0.90 * r, - 300 * (M_PI/180), - 420 * (M_PI/180), + hexagram_gauge_draw_mark(&fuel->gauge, + cr, + (i % 4)? 0.8: 0.7, + 0.9, (double)i / 16.0); } } -void hexagram_fuel_draw_needle(cairo_t *cr, - double x, - double y, - double r, +void hexagram_fuel_draw_needle(hexagram_fuel *fuel, + cairo_t *cr, double level) { if (level > 1.0) { level = 1.0; @@ -56,12 +77,18 @@ void hexagram_fuel_draw_needle(cairo_t *cr, * Draw a tiny boi circle */ cairo_set_source_rgb(cr, 1, 1, 1); - cairo_arc(cr, x, y, 0.08 * r, 0, 2*M_PI); + + cairo_arc(cr, + fuel->gauge.x, + fuel->gauge.y, + fuel->gauge.radius * 0.08, + 0, + 2.0 * M_PI); + cairo_stroke(cr); - hexagram_gauge_draw_needle(cr, x, y, 0.9 * r, - 300 * (M_PI/180), - 420 * (M_PI/180), + hexagram_gauge_draw_needle(&fuel->gauge, + cr, + 0.9, level / 1.0); } - diff --git a/src/gauge.c b/src/gauge.c index 65637aa..74d9695 100644 --- a/src/gauge.c +++ b/src/gauge.c @@ -3,41 +3,49 @@ #include -void hexagram_gauge_draw_needle(cairo_t *cr, - double x, - double y, - double r, - double min_angle, - double max_angle, - double value) { - double angle = min_angle + ((max_angle - min_angle) * value) - (M_PI/2); - - cairo_move_to(cr, x, y); - - cairo_set_source_rgb(cr, 0, 0.25, 1.0); - - cairo_line_to(cr, - x + r * cos(angle), - y + r * sin(angle)); - - cairo_stroke(cr); +void hexagram_gauge_init(hexagram_gauge *gauge, + double x, + double y, + double radius, + double min_angle, + double max_angle) { + gauge->x = x; + gauge->y = y; + gauge->radius = radius; + gauge->min_angle = min_angle; + gauge->max_angle = max_angle; } -void hexagram_gauge_draw_number(cairo_t *cr, - double x, - double y, - double r, - double min_angle, - double max_angle, +void hexagram_gauge_draw_number(hexagram_gauge *gauge, + cairo_t *cr, + double radius, double value, const char *text) { - double angle = min_angle + ((max_angle - min_angle) * value) - 1.658; + double angle = gauge->min_angle + + ((gauge->max_angle - gauge->min_angle) * value) - 1.658; + /* + * Draw the gauge pivot + */ + cairo_set_source_rgb(cr, 1, 1, 1); + + cairo_arc(cr, + gauge->x, + gauge->y, + gauge->radius * 0.08, + 0, + 2.0 * M_PI); + + cairo_stroke(cr); + + /* + * Draw the needle atop the gauge pivot + */ cairo_set_source_rgb(cr, 1, 1, 1); cairo_move_to(cr, - x + r * cos(angle), - y + r * sin(angle)); + gauge->x + (radius * gauge->radius) * cos(angle), + gauge->y + (radius * gauge->radius) * sin(angle)); cairo_save(cr); cairo_rotate(cr, angle + 1.658); @@ -46,23 +54,39 @@ void hexagram_gauge_draw_number(cairo_t *cr, cairo_restore(cr); } -void hexagram_gauge_draw_mark(cairo_t *cr, - double x, - double y, - double min_r, - double max_r, - double min_angle, - double max_angle, +void hexagram_gauge_draw_mark(hexagram_gauge *gauge, + cairo_t *cr, + double min_radius, + double max_radius, double value) { - double angle = min_angle + ((max_angle - min_angle) * value) - (M_PI/2); + double angle = gauge->min_angle + + ((gauge->max_angle - gauge->min_angle) * value) - (M_PI / 2.0); cairo_move_to(cr, - x + min_r * cos(angle), - y + min_r * sin(angle)); + gauge->x + (min_radius * gauge->radius) * cos(angle), + gauge->y + (min_radius * gauge->radius) * sin(angle)); cairo_line_to(cr, - x + max_r * cos(angle), - y + max_r * sin(angle)); + gauge->x + (max_radius * gauge->radius) * cos(angle), + gauge->y + (max_radius * gauge->radius) * sin(angle)); + + cairo_stroke(cr); +} + +void hexagram_gauge_draw_needle(hexagram_gauge *gauge, + cairo_t *cr, + double radius, + double value) { + double angle = gauge->min_angle + + ((gauge->max_angle - gauge->min_angle) * value) - (M_PI / 2.0); + + cairo_move_to(cr, gauge->x, gauge->y); + + cairo_set_source_rgb(cr, 0, 0.25, 1.0); + + cairo_line_to(cr, + gauge->x + (radius * gauge->radius) * cos(angle), + gauge->y + (radius * gauge->radius) * sin(angle)); cairo_stroke(cr); } diff --git a/src/mfd.c b/src/mfd.c index f5679ae..77e938e 100644 --- a/src/mfd.c +++ b/src/mfd.c @@ -1,23 +1,31 @@ #include -void hexagram_mfd_draw(cairo_t *cr, +void hexagram_mfd_init(hexagram_mfd *mfd, double x, double y, double width, double height) { + mfd->x = x; + mfd->y = y; + mfd->width = width; + mfd->height = height; +} + +void hexagram_mfd_draw(hexagram_mfd *mfd, + cairo_t *cr) { cairo_set_source_rgb(cr, 0.75, 0, 0); - cairo_move_to(cr, x, y); - cairo_line_to(cr, x + width, y); - cairo_line_to(cr, x + width, y + height); - cairo_line_to(cr, x, y + height); - cairo_line_to(cr, x, y); + cairo_move_to(cr, mfd->x, mfd->y); + cairo_line_to(cr, mfd->x + mfd->width, mfd->y); + cairo_line_to(cr, mfd->x + mfd->width, mfd->y + mfd->height); + cairo_line_to(cr, mfd->x, mfd->y + mfd->height); + cairo_line_to(cr, mfd->x, mfd->y); cairo_fill(cr); cairo_set_source_rgb(cr, 1, 1, 1); - cairo_move_to(cr, x, y); - cairo_line_to(cr, x + width, y); - cairo_line_to(cr, x + width, y + height); - cairo_line_to(cr, x, y + height); - cairo_line_to(cr, x, y); + cairo_move_to(cr, mfd->x, mfd->y); + cairo_line_to(cr, mfd->x + mfd->width, mfd->y); + cairo_line_to(cr, mfd->x + mfd->width, mfd->y + mfd->height); + cairo_line_to(cr, mfd->x, mfd->y + mfd->height); + cairo_line_to(cr, mfd->x, mfd->y); cairo_stroke(cr); } diff --git a/src/speedo.c b/src/speedo.c index 2cb7cef..f56e0ba 100644 --- a/src/speedo.c +++ b/src/speedo.c @@ -4,20 +4,34 @@ #include #include -void hexagram_speedo_draw_face(cairo_t *cr, - double x, - double y, - double r) { +void hexagram_speedo_init(hexagram_speedo *speedo, + double x, + double y, + double radius) { + hexagram_gauge_init(&speedo->gauge, x, y, radius, + 232.0 * (M_PI / 180.0), + 488.0 * (M_PI / 180.0)); +} + +void hexagram_speedo_draw_face(hexagram_speedo *speedo, + cairo_t *cr) { int i; cairo_select_font_face(cr, "Helvetica", - CAIRO_FONT_SLANT_NORMAL, - CAIRO_FONT_WEIGHT_NORMAL); + CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); - cairo_set_font_size(cr, r * 0.125); + cairo_set_font_size(cr, speedo->gauge.radius * 0.125); cairo_set_source_rgb(cr, 1, 1, 1); - cairo_arc(cr, x, y, r, 0, 2*M_PI); + + cairo_arc(cr, + speedo->gauge.x, + speedo->gauge.y, + speedo->gauge.radius, + 0, + 2.0 * M_PI); + cairo_stroke(cr); /* @@ -28,42 +42,31 @@ void hexagram_speedo_draw_face(cairo_t *cr, snprintf(text, 4, "%02d", i); - hexagram_gauge_draw_number(cr, x, y, - 0.85 * r, - 232 * (M_PI/180), - 488 * (M_PI/180), + hexagram_gauge_draw_number(&speedo->gauge, + cr, + 0.85, i / 180.0, text); } for (i=0; i<=180; i+=2) { - hexagram_gauge_draw_mark(cr, x, y, - ((i % 10)? 0.75: 0.7) * r, - 0.8 * r, - 232 * (M_PI/180), - 488 * (M_PI/180), + hexagram_gauge_draw_mark(&speedo->gauge, + cr, + (i % 10)? 0.75: 0.7, + 0.8, i / 180.0); } } -void hexagram_speedo_draw_needle(cairo_t *cr, - double x, - double y, - double r, +void hexagram_speedo_draw_needle(hexagram_speedo *speedo, + cairo_t *cr, double kph) { if (kph > 290) { kph = 290; } - /* - * Draw a tiny boi circle - */ - cairo_set_source_rgb(cr, 1, 1, 1); - cairo_arc(cr, x, y, 0.08 * r, 0, 2*M_PI); - cairo_stroke(cr); - - hexagram_gauge_draw_needle(cr, x, y, 0.8 * r, - 232 * (M_PI/180), - 488 * (M_PI/180), + hexagram_gauge_draw_needle(&speedo->gauge, + cr, + 0.8, kph / 290); } diff --git a/src/tacho.c b/src/tacho.c index c226e62..549cd68 100644 --- a/src/tacho.c +++ b/src/tacho.c @@ -4,21 +4,40 @@ #include #include -void hexagram_tacho_draw_face(cairo_t *cr, - double x, - double y, - double r, - double redline) { +void hexagram_tacho_init(hexagram_tacho *tacho, + double x, + double y, + double radius, + double redline) { + hexagram_gauge_init(&tacho->gauge, + x, + y, + radius, + 232.0 * (M_PI / 180.0), + 488.0 * (M_PI / 180.0)); + + tacho->redline = redline; +} + +void hexagram_tacho_draw_face(hexagram_tacho *tacho, + cairo_t *cr) { int i; cairo_select_font_face(cr, "Helvetica", - CAIRO_FONT_SLANT_NORMAL, - CAIRO_FONT_WEIGHT_NORMAL); + CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); - cairo_set_font_size(cr, r * 0.125); + cairo_set_font_size(cr, tacho->gauge.radius * 0.125); cairo_set_source_rgb(cr, 1, 1, 1); - cairo_arc(cr, x, y, r, 0, 2*M_PI); + + cairo_arc(cr, + tacho->gauge.x, + tacho->gauge.y, + tacho->gauge.radius, + 0, + 2.0 * M_PI); + cairo_stroke(cr); /* @@ -29,55 +48,35 @@ void hexagram_tacho_draw_face(cairo_t *cr, snprintf(text, 3, "%02d", i); - hexagram_gauge_draw_number(cr, x, y, - 0.85 * r, - 232 * (M_PI/180), - 488 * (M_PI/180), + hexagram_gauge_draw_number(&tacho->gauge, + cr, + 0.85, i / 80.0, text); } for (i=0; i<=80; i++) { - if (i * 100 >= redline) { + if (i * 100 >= tacho->redline) { cairo_set_source_rgb(cr, 1, 0, 0); } - if (i % 5 == 0) { - hexagram_gauge_draw_mark(cr, x, y, - 0.7 * r, - 0.8 * r, - 232 * (M_PI/180), - 488 * (M_PI/180), - i / 80.0); - } else { - hexagram_gauge_draw_mark(cr, x, y, - 0.75 * r, - 0.8 * r, - 232 * (M_PI/180), - 488 * (M_PI/180), - i / 80.0); - } + hexagram_gauge_draw_mark(&tacho->gauge, + cr, + i % 5? 0.75: 0.7, + 0.8, + i / 80.0); } } -void hexagram_tacho_draw_needle(cairo_t *cr, - double x, - double y, - double r, +void hexagram_tacho_draw_needle(hexagram_tacho *tacho, + cairo_t *cr, double rpm) { if (rpm > 8000) { rpm = 8000; } - /* - * Draw a tiny boi circle - */ - cairo_set_source_rgb(cr, 1, 1, 1); - cairo_arc(cr, x, y, 0.08 * r, 0, 2*M_PI); - cairo_stroke(cr); - - hexagram_gauge_draw_needle(cr, x, y, 0.8 * r, - 232 * (M_PI/180), - 488 * (M_PI/180), + hexagram_gauge_draw_needle(&tacho->gauge, + cr, + 0.8, rpm / 8000); } diff --git a/src/thermo.c b/src/thermo.c index f983a76..71f7120 100644 --- a/src/thermo.c +++ b/src/thermo.c @@ -3,71 +3,85 @@ #include #include -void hexagram_thermo_draw_face(cairo_t *cr, - double x, - double y, - double r, - double redline) { +void hexagram_thermo_init(hexagram_thermo *thermo, + double x, + double y, + double radius, + double redline) { + hexagram_gauge_init(&thermo->gauge, x, y, radius, + 300.0 * (M_PI / 180.0), + 420.0 * (M_PI / 180.0)); + + thermo->redline = redline; +} + +void hexagram_thermo_draw_face(hexagram_thermo *thermo, + cairo_t *cr) { int i; cairo_select_font_face(cr, "Helvetica", - CAIRO_FONT_SLANT_NORMAL, - CAIRO_FONT_WEIGHT_NORMAL); + CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); - cairo_set_font_size(cr, r * 0.2); + cairo_set_font_size(cr, thermo->gauge.radius * 0.2); cairo_set_source_rgb(cr, 1, 1, 1); - cairo_arc(cr, x, y, r, 0, 2*M_PI); + + cairo_arc(cr, + thermo->gauge.x, + thermo->gauge.y, + thermo->gauge.radius, + 0, + 2.0 * M_PI); + cairo_stroke(cr); /* * Draw face numbers */ - cairo_move_to(cr, x - 0.8 * r, y - 0.1 * r); + cairo_move_to(cr, + thermo->gauge.x - 0.8 * thermo->gauge.radius, + thermo->gauge.y - 0.1 * thermo->gauge.radius); + cairo_show_text(cr, "120"); - cairo_move_to(cr, x - 0.15 * r, y - 0.5 * r); + cairo_move_to(cr, + thermo->gauge.x - 0.15 * thermo->gauge.radius, + thermo->gauge.y - 0.5 * thermo->gauge.radius); + cairo_show_text(cr, "190"); - cairo_move_to(cr, x + 0.5 * r, y - 0.1 * r); + cairo_move_to(cr, + thermo->gauge.x + 0.5 * thermo->gauge.radius, + thermo->gauge.y - 0.1 * thermo->gauge.radius); + cairo_show_text(cr, "260"); /* * Draw gauge graduations */ for (i=0; i<=140; i+=7) { - if (i + 120 >= redline) { + if (i + 120 >= thermo->redline) { cairo_set_source_rgb(cr, 1, 0, 0); } - hexagram_gauge_draw_mark(cr, x, y, - ((i % 35)? 0.8: 0.7) * r, - 0.90 * r, - 300 * (M_PI/180), - 420 * (M_PI/180), + hexagram_gauge_draw_mark(&thermo->gauge, + cr, + (i % 35)? 0.8: 0.7, + 0.90, i / 140.0); } } -void hexagram_thermo_draw_needle(cairo_t *cr, - double x, - double y, - double r, +void hexagram_thermo_draw_needle(hexagram_thermo *thermo, + cairo_t *cr, double temp) { if (temp > 260) { temp = 260; } - /* - * Draw a tiny boi circle - */ - cairo_set_source_rgb(cr, 1, 1, 1); - cairo_arc(cr, x, y, 0.08 * r, 0, 2*M_PI); - cairo_stroke(cr); - - hexagram_gauge_draw_needle(cr, x, y, 0.9 * r, - 300 * (M_PI/180), - 420 * (M_PI/180), + hexagram_gauge_draw_needle(&thermo->gauge, + cr, + 0.9, temp / 260); } -