diff --git a/py/hexagram/tacho.py b/py/hexagram/tacho.py new file mode 100644 index 0000000..a17afd7 --- /dev/null +++ b/py/hexagram/tacho.py @@ -0,0 +1,40 @@ +import enum +import math +import cairo + +from hexagram.dial import Dial + +class Tacho(Dial): + MIN_ANGLE = 232.0 * (math.pi / 180.0) + MAX_ANGLE = 488.0 * (math.pi / 180.0) + + FONT_FACE = "Muli" + + __slots__ = 'units', + + def __init__(self, x: float, y: float, radius: float, max_value: float): + super().__init__(x, y, radius, self.MIN_ANGLE, self.MAX_ANGLE, max_value) + + def draw_number(self, cr: cairo.Context, radius: float, value: float, text: str): + scale = value / self.max_value + angle = self.min_angle + ((self.max_angle - self.min_angle) * scale) - Dial.ANGLE_OFFSET + + cr.set_source_rgb(1, 1, 1) + cr.move_to(self.x + radius * self.radius * math.cos(angle) - 8, + self.y + radius * self.radius * math.sin(angle) + 15) + + cr.show_text(text) + cr.stroke() + + def draw_bg(self, cr: cairo.Context): + super().draw_bg(cr) + + cr.select_font_face(self.FONT_FACE, + cairo.FontSlant.ITALIC, + cairo.FontWeight.BOLD) + + cr.set_font_size(self.radius * 0.15) + cr.set_source_rgb(1, 1, 1) + + for speed in range(0, int(self.max_value)+1, 1000): + self.draw_number(cr, 0.78, speed, "%d" % int(speed / 1000))