From cec20b55bd9c5b18dc8f22ddfade8e2195758a6f Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Mon, 1 Jan 2024 01:47:56 -0500 Subject: [PATCH] Ugly hack but I have a fuel gauge??? --- py/hexagram/cluster.py | 8 +++++- py/hexagram/fuel.py | 63 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 py/hexagram/fuel.py diff --git a/py/hexagram/cluster.py b/py/hexagram/cluster.py index 2d4e1d4..faafa3c 100644 --- a/py/hexagram/cluster.py +++ b/py/hexagram/cluster.py @@ -8,6 +8,7 @@ from hexagram.gauge import Gauge from hexagram.dial import Dial from hexagram.speedo import Speedo from hexagram.tacho import Tacho +from hexagram.fuel import FuelGauge class ShiftIndicator(Gauge): __slots__ = 'x', 'y', 'rpm_redline', 'rpm_max', @@ -64,7 +65,7 @@ class Cluster(): SHIFT_INDICATOR_X = 392 SHIFT_INDICATOR_Y = 24 - __slots__ = 'gauges', 'speedo', 'tacho', + __slots__ = 'gauges', 'speedo', 'tacho', 'fuel', def __init__(self, rpm_redline: float, rpm_max: float): self.gauges: List[Gauge] = list() @@ -79,7 +80,12 @@ class Cluster(): self.HEIGHT / 2, 8000) + self.fuel = FuelGauge(self.HEIGHT / 2, + self.HEIGHT / 2, + self.HEIGHT / 2) + self.gauges.append(self.speedo) + self.gauges.append(self.fuel) self.gauges.append(self.tacho) self.gauges.append(ShiftIndicator(self.SHIFT_INDICATOR_X, diff --git a/py/hexagram/fuel.py b/py/hexagram/fuel.py new file mode 100644 index 0000000..9ff71b8 --- /dev/null +++ b/py/hexagram/fuel.py @@ -0,0 +1,63 @@ +import enum +import math +import cairo + +from hexagram.dial import Dial + +class FuelGauge(Dial): + MIN_ANGLE = 144.0 * (math.pi / 180.0) + MAX_ANGLE = 216.0 * (math.pi / 180.0) + + MAX_VALUE = 15 + + FONT_FACE = "Muli" + + def __init__(self, x: float, y: float, radius: float): + super().__init__(x, y, radius, self.MIN_ANGLE, self.MAX_ANGLE, self.MAX_VALUE) + + def draw_value(self, cr: cairo.Context, radius: float, value: float, text: str, x_offset: float): + scale = (self.max_value - value) / self.max_value + angle = self.min_angle + ((self.max_angle - self.min_angle) * scale) - self.ANGLE_OFFSET + + cr.move_to(self.x + radius * self.radius * math.cos(angle) + x_offset, + self.y + radius * self.radius * math.sin(angle)) + + cr.show_text(text) + cr.stroke() + + def draw_mark(self, cr: cairo.Context, min_radius: float, max_radius: float, value: float): + angle = self.min_angle + \ + ((self.max_angle - self.min_angle) * (value / self.max_value)) - self.ANGLE_OFFSET + + cr.move_to(self.x + (min_radius * self.radius) * math.cos(angle), + self.y + (min_radius * self.radius) * math.sin(angle)) + + cr.line_to(self.x + (max_radius * self.radius) * math.cos(angle), + self.y + (max_radius * self.radius) * math.sin(angle)) + + cr.stroke() + + def draw_bg(self, cr: cairo.Context): + cr.select_font_face(self.FONT_FACE, + cairo.FontSlant.NORMAL, + cairo.FontWeight.BOLD) + + cr.set_font_size(self.radius * 0.1) + + cr.set_source_rgb(1, 0, 0) + self.draw_value(cr, 0.75, 0, "0", -10) + + cr.set_source_rgb(1, 1, 1) + self.draw_value(cr, 0.75, 15, "1", 0) + + cr.set_line_width(6.0) + + for level in range(0, self.MAX_VALUE+1): + if level < 4: + min_radius = 0.84 + cr.set_source_rgb(1, 0, 0) + else: + min_radius = 0.82 + cr.set_source_rgb(1, 1, 1) + + self.draw_mark(cr, min_radius, 0.87, self.max_value - level)