From 1a1a789030c4c6589c278893dd374cb1c65c7e3a Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Tue, 2 Jan 2024 17:15:12 -0500 Subject: [PATCH] Make shift indicator addressable and functional --- py/hexagram/cluster.py | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/py/hexagram/cluster.py b/py/hexagram/cluster.py index 42ee02c..6cd1e58 100644 --- a/py/hexagram/cluster.py +++ b/py/hexagram/cluster.py @@ -45,9 +45,14 @@ class ShiftIndicator(Gauge): self.rpm_max = rpm_max def draw_level(self, cr: cairo.Context, level: int, on: bool=False): - r = self.LIGHT_COLORS[level][0] - g = self.LIGHT_COLORS[level][1] - b = self.LIGHT_COLORS[level][2] + max_level = len(self.LIGHT_LEVELS) - 1 + + if level < 0: + level = 0 + elif level >= max_level: + level = max_level + + r, g, b = self.LIGHT_COLORS[level] if not on: r *= self.LIGHT_LOW @@ -60,9 +65,9 @@ class ShiftIndicator(Gauge): x = i * (self.LIGHT_WIDTH + self.LIGHT_SPACING) cr.rectangle(self.x + x, - self.y, - self.LIGHT_WIDTH, - self.LIGHT_HEIGHT) + self.y, + self.LIGHT_WIDTH, + self.LIGHT_HEIGHT) cr.fill() @@ -74,10 +79,12 @@ class ShiftIndicator(Gauge): if self.value < self.rpm_min: return - level = math.ceil((self.value - self.rpm_min) / self.rpm_redline) + adj = self.value - self.rpm_min + adj_max = self.rpm_max - self.rpm_min + level = int((adj / adj_max) * len(self.LIGHT_LEVELS)) for l in range(0, len(self.LIGHT_LEVELS)): - self.draw_level(cr, level, l >= level) + self.draw_level(cr, l, level >= l) class Odometer(TextGauge): def __init__(self, x: float, y: float, align: Align): @@ -107,7 +114,7 @@ class Clock(TextGauge): class Cluster(): __slots__ = 'gauges', 'speedo', 'fuel', 'tacho', 'thermo', 'odo', \ - 'ambient', 'clock' + 'ambient', 'clock', 'shift_indicator', WIDTH = 1280 HEIGHT = 480 @@ -140,6 +147,12 @@ class Cluster(): self.clock = Clock(self.WIDTH / 2, self.HEIGHT - 16, Align.MIDDLE) self.ambient = AmbientTemp(self.WIDTH - 0.8 * self.HEIGHT, self.HEIGHT - 16, Align.RIGHT) + self.shift_indicator = ShiftIndicator(self.SHIFT_INDICATOR_X, + self.SHIFT_INDICATOR_Y, + rpm_min, + rpm_redline, + rpm_max) + self.gauges.append(self.speedo) self.gauges.append(self.fuel) self.gauges.append(self.tacho) @@ -147,12 +160,7 @@ class Cluster(): self.gauges.append(self.odo) self.gauges.append(self.ambient) self.gauges.append(self.clock) - - self.gauges.append(ShiftIndicator(self.SHIFT_INDICATOR_X, - self.SHIFT_INDICATOR_Y, - rpm_min, - rpm_redline, - rpm_max)) + self.gauges.append(self.shift_indicator) def draw_bg(self, cr: cairo.Context): cr.set_source_rgb(0.1, 0.1, 0.1)