diff --git a/openpilot/common/libcommon.a b/openpilot/common/libcommon.a index 434e5601dc..96546f4733 100644 Binary files a/openpilot/common/libcommon.a and b/openpilot/common/libcommon.a differ diff --git a/openpilot/common/libparams_c.so b/openpilot/common/libparams_c.so index 20b36ca02d..623191ac67 100755 Binary files a/openpilot/common/libparams_c.so and b/openpilot/common/libparams_c.so differ diff --git a/openpilot/common/params_keys.h b/openpilot/common/params_keys.h index e29570cd28..7ff163bd96 100644 --- a/openpilot/common/params_keys.h +++ b/openpilot/common/params_keys.h @@ -270,6 +270,7 @@ inline static std::unordered_map keys = { {"IsLdwEnabled", {PERSISTENT, BOOL}}, {"IsLiveStreaming", {CLEAR_ON_MANAGER_START | CLEAR_ON_IGNITION_ON, BOOL}}, {"IsMetric", {PERSISTENT, BOOL}}, + {"TemperatureGaugeUnit", {PERSISTENT, STRING, "auto"}}, {"IsOffroad", {CLEAR_ON_MANAGER_START, BOOL}}, {"IsRhdDetected", {PERSISTENT, BOOL}}, {"IsReleaseBranch", {CLEAR_ON_MANAGER_START, BOOL}}, diff --git a/openpilot/selfdrive/ui/layouts/sidebar.py b/openpilot/selfdrive/ui/layouts/sidebar.py index 4dbbcde514..f729efba62 100644 --- a/openpilot/selfdrive/ui/layouts/sidebar.py +++ b/openpilot/selfdrive/ui/layouts/sidebar.py @@ -4,6 +4,7 @@ from dataclasses import dataclass from collections.abc import Callable from openpilot.cereal import log from openpilot.selfdrive.ui.ui_state import ui_state +from openpilot.starpilot.ui.temperature import format_temperature from openpilot.system.ui.lib.application import gui_app, FontWeight, MousePos, FONT_SCALE from openpilot.system.ui.lib.multilang import tr, tr_noop from openpilot.system.ui.lib.text_measure import measure_text_cached @@ -119,8 +120,7 @@ class Sidebar(Widget): self._net_strength = max(0, min(5, strength.raw + 1)) if strength.raw > 0 else 0 def _update_temperature_status(self, device_state): - temperature = device_state.maxTempC - value = f"{temperature:.0f}°C" if ui_state.is_metric else f"{temperature * 1.8 + 32:.0f}°F" + value = format_temperature(device_state.maxTempC, ui_state.temperature_metric) color = Colors.GOOD if device_state.thermalStatus == ThermalStatus.ok else Colors.DANGER self._temp_status.update(tr_noop("TEMP"), value, color) diff --git a/openpilot/selfdrive/ui/ui_state.py b/openpilot/selfdrive/ui/ui_state.py index 7c7d7c4a1d..065759d8c2 100644 --- a/openpilot/selfdrive/ui/ui_state.py +++ b/openpilot/selfdrive/ui/ui_state.py @@ -14,6 +14,7 @@ from openpilot.common.params import Params from openpilot.starpilot.schema_cache import get_cache, prewarm_cache_contracts from openpilot.starpilot.feature_runtime import enabled as feature_enabled from openpilot.starpilot.ui.display_preferences import AUTO, DisplayPreferences, read_preferences +from openpilot.starpilot.ui.temperature import read_temperature_metric from openpilot.common.realtime import drop_realtime from openpilot.common.swaglog import cloudlog from openpilot.selfdrive.ui.lib.prime_state import PrimeState @@ -157,6 +158,7 @@ class UIState: # Core state variables self.is_metric: bool = self.params.get_bool("IsMetric") + self.temperature_metric = read_temperature_metric(self.params, self.is_metric) self.initial_cruise_speed: int = self.params.get("InitialCruiseSpeedKph", return_default=True) self.is_release = self.params.get_bool("IsReleaseBranch") self.always_on_dm: bool = self.params.get_bool("AlwaysOnDM") @@ -476,6 +478,7 @@ class UIState: self.aol_runtime = bool(self.CP is not None and feature_enabled(self.params, self.CP, "aol", os.environ)) self.recording_audio = self.params.get_bool("RecordAudio") and self.started self.is_metric = self.params.get_bool("IsMetric") + self.temperature_metric = read_temperature_metric(self.params, self.is_metric) self.initial_cruise_speed = self.params.get("InitialCruiseSpeedKph", return_default=True) self.always_on_dm = self.params.get_bool("AlwaysOnDM") self.experimental_mode = self.params.get_bool("ExperimentalMode") diff --git a/openpilot/starpilot/galaxy/localization_settings.py b/openpilot/starpilot/galaxy/localization_settings.py index 559eec7674..cceca5a2b7 100644 --- a/openpilot/starpilot/galaxy/localization_settings.py +++ b/openpilot/starpilot/galaxy/localization_settings.py @@ -7,6 +7,7 @@ from openpilot.starpilot.saved_document import commit_exact from openpilot.starpilot.saved_source import read_saved from openpilot.starpilot.ui.feature_settings_state import FeatureRow, FeatureSettingsState from openpilot.starpilot.ui.slc_offset_feature import SlcOffsetOwner +from openpilot.starpilot.ui.temperature import FOLLOW_METRIC, TEMPERATURE_UNIT_KEY, TEMPERATURE_UNITS LANGUAGES = json.loads(files("openpilot.selfdrive.ui").joinpath("translations/languages.json").read_text(encoding="utf-8")) @@ -22,7 +23,7 @@ class LocalizationSettingsOwner: def _wheel_side_allowed(self): return self.authorized() and self.parked() and not self.params.get_bool("IsDriverViewEnabled") - def snapshot(self): + def snapshot(self, *, include_temperature=False): rows = [] for key, label, reason in ( ("IsMetric", "Use Metric", "On uses kilometers; Off uses miles. Saved speed-limit offsets keep their physical values."), @@ -36,6 +37,12 @@ class LocalizationSettingsOwner: rows.append(FeatureRow(key, label, ("On" if raw == b"1" else "Off") if valid else "Invalid saved value", raw, ("Off", "On") if valid else (), available=valid and allowed, reason=reason if valid else "Invalid saved value", default_value="Off")) + if include_temperature: + raw, readable = read_saved(self.params, TEMPERATURE_UNIT_KEY, 8) + name = next((label for label, value in TEMPERATURE_UNITS.items() if value == raw), FOLLOW_METRIC) + rows.insert(1, FeatureRow(TEMPERATURE_UNIT_KEY, "TEMP Gauge Units", name, raw, tuple(TEMPERATURE_UNITS), + available=readable and self.authorized(), + reason="Changes only the device TEMP gauge.", default_value=FOLLOW_METRIC)) raw, readable = read_saved(self.params, "LanguageSetting", 32) code = (raw or b"en").decode("utf-8", errors="replace").removeprefix("main_") name = next((name for name, value in LANGUAGES.items() if value == code), "English") @@ -47,6 +54,13 @@ class LocalizationSettingsOwner: tuple(rows), self.parked()) def apply(self, request): + if request.key == TEMPERATURE_UNIT_KEY: + raw = TEMPERATURE_UNITS.get(request.value) + if raw is None: + return False + result = commit_exact(self.params, key=request.key, max_bytes=8, raw=raw, expected=request.expected, + authorized=self.authorized, temp_prefix=".temperature-unit-") + return result.committed and result.verified if request.key == "LanguageSetting": if request.value not in LANGUAGES: return False diff --git a/openpilot/starpilot/galaxy/settings.py b/openpilot/starpilot/galaxy/settings.py index 62d881e330..f3d4004ca5 100644 --- a/openpilot/starpilot/galaxy/settings.py +++ b/openpilot/starpilot/galaxy/settings.py @@ -651,7 +651,7 @@ class SettingsGateway: def _state(self, page: str, ctx: AuthorityContext): if page == "localization": - return self._localization_owner(ctx).snapshot() + return self._localization_owner(ctx).snapshot(include_temperature=True) if page == "starport_options": return self._device_settings_owner(ctx).snapshot(page) if page == "developer": diff --git a/openpilot/starpilot/galaxy/tests/test_localization_settings.py b/openpilot/starpilot/galaxy/tests/test_localization_settings.py index d54ea4d488..3a724169b2 100644 --- a/openpilot/starpilot/galaxy/tests/test_localization_settings.py +++ b/openpilot/starpilot/galaxy/tests/test_localization_settings.py @@ -11,6 +11,7 @@ from openpilot.starpilot.galaxy.settings import AuthorityContext, ContextSource, from openpilot.starpilot.speed_limits import offset_document as od from openpilot.starpilot.ui.feature_settings_state import FeatureSettingsRequest from openpilot.starpilot.ui.slc_offset_feature import DOCUMENT_KEY +from openpilot.starpilot.ui.temperature import FOLLOW_METRIC, TEMPERATURE_UNIT_KEY, format_temperature, read_temperature_metric def test_real_gateway_units_and_wheel_side_without_a_vehicle(tmp_path): @@ -23,15 +24,15 @@ def test_real_gateway_units_and_wheel_side_without_a_vehicle(tmp_path): def preview(index, value): page = gateway.page("localization", token, generation) assert page["title"] == "Localization" and not page["vehicleIdentified"] - assert [row["label"] for row in page["rows"]] == ["Use Metric", "Right-Hand Drive", "Language"] + assert [row["label"] for row in page["rows"]] == ["Use Metric", "TEMP Gauge Units", "Right-Hand Drive", "Language"] return gateway.preview(page["view"], index, 0, token, generation, value=value)["intent"] assert gateway.confirm(preview(0, "On"), token, generation) assert params.get_bool("IsMetric") assert not Path(params.get_param_path(DOCUMENT_KEY)).exists() - assert gateway.confirm(preview(1, "On"), token, generation) + assert gateway.confirm(preview(2, "On"), token, generation) assert params.get_bool("IsRhdDetected") and params.get_bool("OnroadCycleRequested") - pending = preview(1, "Off") + pending = preview(2, "Off") context.value = replace(context.value, parked=False) with pytest.raises(SettingsChanged): gateway.confirm(pending, token, generation) @@ -69,7 +70,7 @@ def test_settings_session_rechecked_before_localization_commit(tmp_path): context = SimpleNamespace(sample=lambda: AuthorityContext(True, None, None)) gateway = SettingsGateway(params, cast(ContextSource, context)) page = gateway.page("localization", "session", b"generation") - pending = gateway.preview(page["view"], 1, 0, "session", b"generation", value="On")["intent"] + pending = gateway.preview(page["view"], 2, 0, "session", b"generation", value="On")["intent"] with pytest.raises(SettingsChanged): gateway.confirm(pending, "session", b"generation", session_valid=lambda: False) assert not params.get_bool("IsRhdDetected") @@ -126,12 +127,39 @@ def test_language_dropdown_through_gateway_rechecks_session(tmp_path): gateway = SettingsGateway(params, cast(ContextSource, context)) token, generation = "language-session", b"generation" page = gateway.page("localization", token, generation) - assert page["rows"][2]["choices"] == list(LocalizationSettingsOwner(params, lambda: True, lambda: True).snapshot().rows[2].choices) - pending = gateway.preview(page["view"], 2, 0, token, generation, value="日本語")["intent"] + assert page["rows"][3]["choices"] == list(LocalizationSettingsOwner(params, lambda: True, lambda: True).snapshot().rows[2].choices) + pending = gateway.preview(page["view"], 3, 0, token, generation, value="日本語")["intent"] with pytest.raises(SettingsChanged): gateway.confirm(pending, token, generation, session_valid=lambda: False) assert not Path(params.get_param_path("LanguageSetting")).exists() page = gateway.page("localization", token, generation) - pending = gateway.preview(page["view"], 2, 0, token, generation, value="日本語")["intent"] + pending = gateway.preview(page["view"], 3, 0, token, generation, value="日本語")["intent"] assert gateway.confirm(pending, token, generation) assert params.get("LanguageSetting") == "ja" + + +@pytest.mark.parametrize("metric", [False, True]) +def test_temperature_gauge_units_change_without_changing_metric(tmp_path, metric): + params = Params(str(tmp_path)) + params.put_bool("IsMetric", metric, block=True) + context = SimpleNamespace(sample=lambda: AuthorityContext(False, None, None)) + gateway = SettingsGateway(params, cast(ContextSource, context)) + token, generation = "temperature-session", b"generation" + + def preview(value): + page = gateway.page("localization", token, generation) + assert page["rows"][1]["choices"] == [FOLLOW_METRIC, "Fahrenheit", "Celsius"] + return gateway.preview(page["view"], 1, 0, token, generation, value=value)["intent"] + + assert read_temperature_metric(params, metric) == metric + assert params.get_default_value(TEMPERATURE_UNIT_KEY) == "auto" + for choice, unit in (("Celsius", True), ("Fahrenheit", False), (FOLLOW_METRIC, metric)): + assert gateway.confirm(preview(choice), token, generation) + assert params.get_bool("IsMetric") == metric + assert format_temperature(50., read_temperature_metric(params, metric)) == ("50\u00b0C" if unit else "122\u00b0F") + pending = preview("Celsius") + with pytest.raises(SettingsChanged): + gateway.confirm(pending, token, generation, session_valid=lambda: False) + assert params.get(TEMPERATURE_UNIT_KEY) == "auto" + Path(params.get_param_path(TEMPERATURE_UNIT_KEY)).write_bytes(b"invalid") + assert read_temperature_metric(params, metric) == metric diff --git a/openpilot/starpilot/ui/home.py b/openpilot/starpilot/ui/home.py index e9091419d7..c37621e201 100644 --- a/openpilot/starpilot/ui/home.py +++ b/openpilot/starpilot/ui/home.py @@ -10,6 +10,7 @@ import math from pathlib import Path from openpilot.starpilot.ui.raylib import rl +from openpilot.starpilot.ui.temperature import format_temperature from openpilot.starpilot.ui import clip @@ -157,9 +158,7 @@ class HomeView: rl.draw_circle(71 + index * 37, 209, 13, color) network_labels = {"none": "--", "wifi": "Wi-Fi", "ethernet": "ETH", "cell2G": "2G", "cell3G": "3G", "cell4G": "LTE", "cell5G": "5G"} self._text(network_labels.get(state.network, "Unknown"), FontRole.NORMAL, 35, 58, 247) - temperature = "--" - if state.temperature_c is not None: - temperature = f"{state.temperature_c:.0f}°C" if state.metric else f"{state.temperature_c * 1.8 + 32:.0f}°F" + temperature = format_temperature(state.temperature_c, state.temperature_metric) metrics = [("TEMP", temperature, rl.Color(201, 34, 49, 255) if state.temperature_high else rl.WHITE, 338), ("VEHICLE" if state.vehicle_online else "NO", "ONLINE" if state.vehicle_online else "PANDA", rl.WHITE if state.vehicle_online else rl.Color(201, 34, 49, 255), 496), diff --git a/openpilot/starpilot/ui/home_state.py b/openpilot/starpilot/ui/home_state.py index bc3494466f..d626f5595f 100644 --- a/openpilot/starpilot/ui/home_state.py +++ b/openpilot/starpilot/ui/home_state.py @@ -77,7 +77,7 @@ class HomeState: recording_audio: bool = False branch: str = "" gpu_state: str = "disconnected" - metric: bool = False + temperature_metric: bool = False temperature_high: bool = False jetlink: str = "off" diff --git a/openpilot/starpilot/ui/runtime_snapshot.py b/openpilot/starpilot/ui/runtime_snapshot.py index c7d4f79e26..72a1b0aecc 100644 --- a/openpilot/starpilot/ui/runtime_snapshot.py +++ b/openpilot/starpilot/ui/runtime_snapshot.py @@ -864,7 +864,8 @@ class RuntimeSnapshotAdapter: experimental_enabled=experimental, experimental_available=bool(ui.has_longitudinal_control), stats=self.home_stats(bool(ui.is_metric)) if mode == ShellMode.HOME and self.home_stats is not None else None, paired=bool(ui.prime_state.is_paired()), network=network, network_strength=strength, - temperature_c=device.maxTempC if device is not None else None, metric=bool(ui.is_metric), + temperature_c=device.maxTempC if device is not None else None, + temperature_metric=bool(getattr(ui, "temperature_metric", ui.is_metric)), temperature_high=device is not None and device.thermalStatus != log.DeviceState.ThermalStatus.ok, vehicle_online=panda_connected, connection=connection, recording_audio=bool(ui.recording_audio), bluetooth=bluetooth, diff --git a/openpilot/starpilot/ui/temperature.py b/openpilot/starpilot/ui/temperature.py new file mode 100644 index 0000000000..77b360cc64 --- /dev/null +++ b/openpilot/starpilot/ui/temperature.py @@ -0,0 +1,16 @@ +from openpilot.starpilot.saved_source import read_saved + +TEMPERATURE_UNIT_KEY = "TemperatureGaugeUnit" +FOLLOW_METRIC = "Follow Use Metric" +TEMPERATURE_UNITS = {FOLLOW_METRIC: b"auto", "Fahrenheit": b"f", "Celsius": b"c"} + + +def read_temperature_metric(params, metric: bool) -> bool: + raw, readable = read_saved(params, TEMPERATURE_UNIT_KEY, 8) + return raw == b"c" if readable and raw in (b"c", b"f") else metric + + +def format_temperature(celsius: float | None, metric: bool) -> str: + if celsius is None: + return "--" + return f"{celsius:.0f}\u00b0C" if metric else f"{celsius * 1.8 + 32:.0f}\u00b0F" diff --git a/openpilot/starpilot/ui/tests/test_large_onroad_navigation.py b/openpilot/starpilot/ui/tests/test_large_onroad_navigation.py index 91c6f982a1..ecb3071b5c 100644 --- a/openpilot/starpilot/ui/tests/test_large_onroad_navigation.py +++ b/openpilot/starpilot/ui/tests/test_large_onroad_navigation.py @@ -70,18 +70,30 @@ def tap(n, x, y, *, dx=0, dy=0): (ThermalStatus.overheated, Colors.DANGER), (ThermalStatus.critical, Colors.DANGER)]) @pytest.mark.parametrize('temperature,celsius,fahrenheit', [(0, '0°C', '32°F'), (65.6, '66°C', '150°F'), (107, '107°C', '225°F')]) -def test_sidebar_temperature_follows_metric_setting(thermal_status, color, temperature, celsius, fahrenheit): +def test_sidebar_temperature_uses_independent_gauge_unit(thermal_status, color, temperature, celsius, fahrenheit): with patch('openpilot.selfdrive.ui.layouts.sidebar.gui_app'): sidebar = Sidebar() assert sidebar._temp_status.value == '--' device_state = NS(maxTempC=temperature, thermalStatus=thermal_status) for metric, value in ((True, celsius), (False, fahrenheit), (True, celsius)): - with patch('openpilot.selfdrive.ui.layouts.sidebar.ui_state', NS(is_metric=metric)): + with patch('openpilot.selfdrive.ui.layouts.sidebar.ui_state', NS(is_metric=not metric, temperature_metric=metric)): sidebar._update_temperature_status(device_state) assert (sidebar._temp_status.label, sidebar._temp_status.value) == ('TEMP', value) assert sidebar._temp_status.color == color +@pytest.mark.parametrize("metric", [False, True]) +def test_home_snapshot_projects_temperature_override_without_changing_onroad_units(metric): + from openpilot.starpilot.ui.tests.test_runtime_snapshot import NOW, ShellMode, RuntimeSnapshotAdapter, ui_fake + + ui = ui_fake() + ui.is_metric = metric + ui.temperature_metric = not metric + state = RuntimeSnapshotAdapter(ui).build(ShellMode.HOME, now_ns=NOW) + assert state.home.temperature_metric is (not metric) + assert state.onroad.metric is metric + + def test_background_tap_opens_settings_sidebar_and_can_close_repeatedly(large_layout): n = large_layout for _ in range(3): diff --git a/openpilot/system/loggerd/bootlog b/openpilot/system/loggerd/bootlog index 7d93fc1721..953c6e3aca 100755 Binary files a/openpilot/system/loggerd/bootlog and b/openpilot/system/loggerd/bootlog differ diff --git a/openpilot/system/loggerd/encoderd b/openpilot/system/loggerd/encoderd index e29dcf2178..0103cee884 100755 Binary files a/openpilot/system/loggerd/encoderd and b/openpilot/system/loggerd/encoderd differ diff --git a/openpilot/system/loggerd/loggerd b/openpilot/system/loggerd/loggerd index 6c5ea06f59..0fdc0c1a16 100755 Binary files a/openpilot/system/loggerd/loggerd and b/openpilot/system/loggerd/loggerd differ diff --git a/prebuilt.json b/prebuilt.json index 465b688034..cd0866a084 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "cbd35189096c5a77ef088856429ee9cdef492cbd055885141a3f4b776329993e", "schema_version": 1} +{"protected_inventory_sha256": "8fa77790709cb3879be7597162de364705ee1c55cd79d5c43d5428d13ff6f8f1", "schema_version": 1}