version: sunnypilot v2026.002.000 (staging) date: 2026-07-21T07:44:02 master commit: 2c334ede443d7391d27575af8c854a095ba702a8
47 lines
2.1 KiB
Python
47 lines
2.1 KiB
Python
"""
|
|
Copyright (c) 2021-, Haibin Wen, sunnypilot, and a number of other contributors.
|
|
|
|
This file is part of sunnypilot and is licensed under the MIT License.
|
|
See the LICENSE.md file in the root directory for more details.
|
|
"""
|
|
import pyray as rl
|
|
|
|
from openpilot.common.constants import CV
|
|
from openpilot.selfdrive.ui.ui_state import ui_state
|
|
from openpilot.system.ui.lib.application import gui_app, FontWeight
|
|
from openpilot.system.ui.lib.multilang import tr
|
|
from openpilot.system.ui.lib.text_measure import measure_text_cached
|
|
from openpilot.selfdrive.ui.onroad.hud_renderer import FONT_SIZES, COLORS
|
|
|
|
|
|
class SpeedRenderer:
|
|
def __init__(self):
|
|
self.speed: float = 0.0
|
|
self.v_ego_cluster_seen: bool = False
|
|
|
|
self._font_bold: rl.Font = gui_app.font(FontWeight.BOLD)
|
|
self._font_medium: rl.Font = gui_app.font(FontWeight.MEDIUM)
|
|
|
|
def update(self) -> None:
|
|
car_state = ui_state.sm['carState']
|
|
v_ego_cluster = car_state.vEgoCluster
|
|
self.v_ego_cluster_seen = self.v_ego_cluster_seen or v_ego_cluster != 0.0
|
|
v_ego = v_ego_cluster if self.v_ego_cluster_seen and not ui_state.true_v_ego_ui else car_state.vEgo
|
|
speed_conversion = CV.MS_TO_KPH if ui_state.is_metric else CV.MS_TO_MPH
|
|
self.speed = max(0.0, v_ego * speed_conversion)
|
|
|
|
def render(self, rect: rl.Rectangle) -> None:
|
|
if ui_state.hide_v_ego_ui:
|
|
return
|
|
|
|
# Draw current speed and unit
|
|
speed_text = str(round(self.speed))
|
|
speed_text_size = measure_text_cached(self._font_bold, speed_text, FONT_SIZES.current_speed)
|
|
speed_pos = rl.Vector2(rect.x + rect.width / 2 - speed_text_size.x / 2, 180 - speed_text_size.y / 2)
|
|
rl.draw_text_ex(self._font_bold, speed_text, speed_pos, FONT_SIZES.current_speed, 0, COLORS.WHITE)
|
|
|
|
unit_text = tr("km/h") if ui_state.is_metric else tr("mph")
|
|
unit_text_size = measure_text_cached(self._font_medium, unit_text, FONT_SIZES.speed_unit)
|
|
unit_pos = rl.Vector2(rect.x + rect.width / 2 - unit_text_size.x / 2, 290 - unit_text_size.y / 2)
|
|
rl.draw_text_ex(self._font_medium, unit_text, unit_pos, FONT_SIZES.speed_unit, 0, COLORS.WHITE_TRANSLUCENT)
|