diff --git a/selfdrive/ui/sunnypilot/ui_state.py b/selfdrive/ui/sunnypilot/ui_state.py index 35d85eca50..7757d57061 100644 --- a/selfdrive/ui/sunnypilot/ui_state.py +++ b/selfdrive/ui/sunnypilot/ui_state.py @@ -4,6 +4,8 @@ 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 numpy as np + from cereal import messaging, log, custom from openpilot.common.params import Params from openpilot.sunnypilot.sunnylink.sunnylink_state import SunnylinkState @@ -88,3 +90,22 @@ class DeviceSP: def _set_awake(self, on: bool): if on and self._params.get("DeviceBootMode", return_default=True) == 1: self._params.put_bool("OffroadMode", True) + + @staticmethod + def update_custom_global_brightness(brightness_override: int) -> float: + """ + Updates the custom global brightness by constraining the value to a predefined range. + + The method takes an integer `brightness` value, adjusts it to ensure it is within the + range of 30 to 100, inclusive, and returns the adjusted value as a float. + + This method only runs if 0 (Auto) is not selected. + + :param brightness_override: The desired brightness level. It is constrained between + a minimum of 30 and a maximum of 100. + :type brightness_override: int + :return: The brightness value adjusted to fit within the allowable range, + converted to a float. + :rtype: float + """ + return float(min(max(brightness_override, 30), 100)) diff --git a/selfdrive/ui/ui_state.py b/selfdrive/ui/ui_state.py index 11e0cce74e..ebd7c0b4fc 100644 --- a/selfdrive/ui/ui_state.py +++ b/selfdrive/ui/ui_state.py @@ -260,6 +260,9 @@ class Device(DeviceSP): clipped_brightness = float(np.interp(clipped_brightness, [0, 1], [30, 100])) + if gui_app.sunnypilot_ui() and ui_state.global_brightness_override != 0: + clipped_brightness = self.update_custom_global_brightness(ui_state.global_brightness_override) + brightness = round(self._brightness_filter.update(clipped_brightness)) if not self._awake: brightness = 0