hexagram/src/speedo.c

77 lines
2 KiB
C
Raw Normal View History

#include <stdio.h>
#include <math.h>
#include <hexagram/gauge.h>
#include <hexagram/speedo.h>
2019-06-09 15:46:41 -05:00
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",
2019-06-09 15:46:41 -05:00
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_NORMAL);
2019-06-09 15:46:41 -05:00
cairo_set_font_size(cr, speedo->gauge.radius * 0.125);
cairo_set_source_rgb(cr, 1, 1, 1);
2019-06-09 15:46:41 -05:00
cairo_arc(cr,
speedo->gauge.x,
speedo->gauge.y,
speedo->gauge.radius,
0,
2.0 * M_PI);
cairo_stroke(cr);
/*
* Draw face numbers
*/
for (i=0; i<=180; i+=20) {
char text[5];
snprintf(text, 4, "%02d", i);
2019-06-09 15:46:41 -05:00
hexagram_gauge_draw_number(&speedo->gauge,
cr,
0.85,
i / 180.0,
text);
}
for (i=0; i<=180; i+=2) {
2019-06-09 15:46:41 -05:00
hexagram_gauge_draw_mark(&speedo->gauge,
cr,
(i % 10)? 0.75: 0.7,
0.8,
i / 180.0);
}
}
2019-06-09 15:46:41 -05:00
void hexagram_speedo_draw_needle(hexagram_speedo *speedo,
cairo_t *cr,
2019-06-14 01:11:08 -05:00
double rps) {
double kph = (2.00152 * rps * 3600) / 1000.0;
2019-06-14 00:40:59 -05:00
if (kph < 0) {
kph = 0;
} else if (kph > 290) {
kph = 290;
}
2019-06-09 15:46:41 -05:00
hexagram_gauge_draw_needle(&speedo->gauge,
cr,
0.8,
2019-06-14 01:11:08 -05:00
kph / HEXAGRAM_SPEEDO_MAX_KPH);
}