Files
StarPilot/openpilot/starpilot/galaxy/localization_settings.py
T

58 lines
3.1 KiB
Python

"""Global display units and the initial driver-monitoring wheel side."""
from openpilot.starpilot.saved_document import commit_exact
from openpilot.starpilot.saved_source import read_saved
from openpilot.starpilot.speed_limits import offset_document as od
from openpilot.starpilot.ui.feature_settings_state import FeatureRow, FeatureSettingsState
from openpilot.starpilot.ui.slc_offset_feature import SlcOffsetOwner
class LocalizationSettingsOwner:
def __init__(self, params, authorized, parked):
self.params = params
self.authorized = authorized
self.parked = parked
self.units = SlcOffsetOwner(params, parked, lambda: None)
def _wheel_side_allowed(self):
return self.authorized() and self.parked() and not self.params.get_bool("IsDriverViewEnabled")
def snapshot(self):
rows = []
for key, label, reason in (
("IsMetric", "Use Kilometers (km/h)", "On uses kilometers; Off uses miles. Saved speed-limit offsets keep their physical values."),
("IsRhdDetected", "Right-Hand Drive", "Sets the initial driver-monitoring side for the next drive. Automatic wheel-side detection continues."),
):
raw, readable = read_saved(self.params, key, 8)
valid = readable and raw in (None, b"0", b"1")
allowed = self.authorized() if key == "IsMetric" else self._wheel_side_allowed()
if key == "IsMetric":
saved = self.units.snapshot()
custom_offsets = isinstance(saved.document, od.OffsetDocument) and any(saved.document.offsets_mps)
preserving = saved.raw_document is None and (custom_offsets or saved.raw_control not in (None, b"0"))
if preserving and not self.parked():
allowed = False
reason = "Park once to preserve your saved speed-limit offsets before switching units."
if key == "IsRhdDetected" and not allowed:
reason = "Park the vehicle and close driver camera preview before changing the steering-wheel side."
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"))
return FeatureSettingsState("localization", "Localization", "Units and steering-wheel side for every vehicle.",
tuple(rows), self.parked())
def apply(self, request):
if request.value not in ("Off", "On") or request.expected not in (None, b"0", b"1") or not self.authorized():
return False
if request.key == "IsMetric":
return self.units.change_units(request.value == "On", request.expected)
if request.key != "IsRhdDetected" or not self._wheel_side_allowed():
return False
result = commit_exact(self.params, key=request.key, max_bytes=8,
raw=b"1" if request.value == "On" else b"0", expected=request.expected,
authorized=self._wheel_side_allowed, temp_prefix=".wheel-side-")
if not result.committed or not result.verified:
return False
self.params.put_bool("OnroadCycleRequested", True, block=True)
return True