diff --git a/openpilot/selfdrive/ui/ui_state.py b/openpilot/selfdrive/ui/ui_state.py index 1b648cde71..bde85c5b6d 100644 --- a/openpilot/selfdrive/ui/ui_state.py +++ b/openpilot/selfdrive/ui/ui_state.py @@ -26,7 +26,9 @@ from openpilot.starpilot.models.manager import ModelManager from openpilot.starpilot.models.jetlink_status import JetlinkStatusFeed from openpilot.tools.replay.display_clock import BOOT_TIME_SERVICES, DisplayClockReader -BACKLIGHT_OFFROAD = 65 if HARDWARE.get_device_type() == "mici" else 50 +MICI_NIGHT_MODE = HARDWARE.get_device_type() == "mici" +BACKLIGHT_OFFROAD = 65 if MICI_NIGHT_MODE else 50 +BACKLIGHT_MIN = 30 PARAM_UPDATE_TIME = 1 / 5.0 @@ -639,6 +641,7 @@ class Device: def _update_brightness(self): clipped_brightness = self._offroad_brightness + night_auto = MICI_NIGHT_MODE and ui_state.started and ui_state.light_sensor >= 0 if ui_state.started and ui_state.light_sensor >= 0: clipped_brightness = ui_state.light_sensor @@ -649,14 +652,24 @@ class Device: else: clipped_brightness = ((clipped_brightness + 16.0) / 116.0) ** 3.0 - clipped_brightness = float(np.interp(clipped_brightness, [0, 1], [30, 100])) + clipped_brightness = float(np.interp(clipped_brightness, [0, 1], [BACKLIGHT_MIN * 0.6 if night_auto else BACKLIGHT_MIN, 100])) + saved = AUTO if self._display_preferences.enabled: saved = (self._display_preferences.driving_brightness if ui_state.started else self._display_preferences.parked_brightness) if saved != AUTO: clipped_brightness = saved - brightness = round(self._brightness_filter.update(clipped_brightness)) + night_auto = False + brightness = self._brightness_filter.update(clipped_brightness) + if night_auto: + critical = ui_state.sm['selfdriveState'].alertStatus == log.SelfdriveState.AlertStatus.critical + gui_app.set_dim(1.0 - brightness / BACKLIGHT_MIN if self._awake and not critical else 0.0) + else: + gui_app.set_dim(0.0) + if MICI_NIGHT_MODE and saved == AUTO: + brightness = max(brightness, BACKLIGHT_MIN) + brightness = round(brightness) if not self._awake: brightness = 0 diff --git a/openpilot/starpilot/ui/tests/test_display_settings.py b/openpilot/starpilot/ui/tests/test_display_settings.py index 3cf96a42b6..341ae7f8a9 100644 --- a/openpilot/starpilot/ui/tests/test_display_settings.py +++ b/openpilot/starpilot/ui/tests/test_display_settings.py @@ -638,3 +638,32 @@ class DisplaySettingsTests(unittest.TestCase): session.profile = Profile.LARGE session._display_ui(FeatureUiAction("back")) self.assertEqual(session.selected, Destination.STAR) + + +def test_compact_night_auto_preserves_manual_brightness_and_critical_visibility(): + from openpilot.selfdrive.ui import ui_state as native + from openpilot.system.ui.lib.application import GuiApplication + + display = object.__new__(native.Device) + display._offroad_brightness = 65 + display._brightness_filter = Mock(update=lambda value: value) + display._display_preferences = native.DisplayPreferences(enabled=True, parked_brightness=65) + display._brightness_event = Mock() + display._last_brightness = -1 + display._awake = True + message = NS(alertStatus=native.log.SelfdriveState.AlertStatus.normal) + state = NS(started=True, light_sensor=0.0, sm={'selfdriveState': message}) + graphics = object.__new__(GuiApplication) + with patch.object(native, 'ui_state', state), patch.object(native, 'gui_app', graphics), patch.object(native, 'MICI_NIGHT_MODE', True): + display._update_brightness() + assert display._brightness_target == 30 + assert graphics._dim_alpha == 102 + message.alertStatus = native.log.SelfdriveState.AlertStatus.critical + display._update_brightness() + assert graphics._dim_alpha == 0 + display._display_preferences = replace(display._display_preferences, driving_brightness=20) + display._update_brightness() + assert display._brightness_target == 20 and graphics._dim_alpha == 0 + display._awake = False + display._update_brightness() + assert display._brightness_target == 0 and graphics._dim_alpha == 0 diff --git a/openpilot/system/ui/lib/application.py b/openpilot/system/ui/lib/application.py index 35137e8d72..9ff1150200 100644 --- a/openpilot/system/ui/lib/application.py +++ b/openpilot/system/ui/lib/application.py @@ -281,6 +281,7 @@ class GuiApplication: self._mouse_history: deque[MousePosWithTime] = deque(maxlen=MOUSE_THREAD_RATE) self._show_touches = SHOW_TOUCHES self._show_fps = SHOW_FPS + self._dim_alpha = 0 self._grid_size = GRID_SIZE self._frame_timing_enabled = UI_FRAME_TIMING self._collect_frame_timing: bool | None = None @@ -304,6 +305,9 @@ class GuiApplication: self._show_fps = show self._frame_timing_enabled = UI_FRAME_TIMING or show + def set_dim(self, dim: float) -> None: + self._dim_alpha = round(255 * max(0.0, min(1.0, dim))) + def measure_frame_phase(self, name: str, callback: Callable, *args, **kwargs): enabled = self._frame_timing_enabled if self._collect_frame_timing is None else self._collect_frame_timing if not enabled: @@ -768,6 +772,9 @@ class GuiApplication: else: rl.draw_texture_pro(texture, src_rect, dst_rect, rl.Vector2(0, 0), 0.0, rl.WHITE) + if self._dim_alpha: + rl.draw_rectangle(0, 0, self._scaled_width, self._scaled_height, rl.Color(0, 0, 0, self._dim_alpha)) + if self._show_fps: rl.draw_fps(10, 10)