From 750758ba844ec3548de6c07a385c74b1f877c151 Mon Sep 17 00:00:00 2001 From: Jeremy Karst Date: Tue, 12 Aug 2025 20:17:23 -0400 Subject: [PATCH] Added debugging and error handling to get_monitor_brightness --- monitors.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/monitors.py b/monitors.py index 0f9f193..36f6f19 100644 --- a/monitors.py +++ b/monitors.py @@ -126,16 +126,34 @@ class BatteryMonitor: def get_monitor_brightness(): + """ + Gets the brightness of the main laptop display so that we can scale the LED Matrix + brightness to match. Handles operating system or configuration specific logic to + retrieve the brightness value. + + Returns: + A value between 0.0 and 1.0 which is scaled to the monitor's brightness + """ + brightness_value = 1.0 try: if os.name == 'nt': return wmi.WMI(namespace='wmi').WmiMonitorBrightness()[0].CurrentBrightness / 100.0 else: try: # First try the dGPU brightness - return int(open('/sys/class/backlight/amdgpu_bl2/brightness', 'r').read()) / 255.0 + brightness_value = int(open('/sys/class/backlight/amdgpu_bl2/brightness', 'r').read()) / 255.0 except: # If that doesn't work, try the iGPU brightness - return int(open('/sys/class/backlight/amdgpu_bl1/brightness', 'r').read()) / 255.0 + brightness_value = int(open('/sys/class/backlight/amdgpu_bl1/brightness', 'r').read()) / 255.0 except Exception as e: - return 1.0 + print(f"Error in get_monitor_brightness(): {e}") + + if brightness_value < 0.0: + print(f"Invalid brightness value returned by get_monitor_brightness(): {brightness_value}") + brightness_value = 0.0 + elif brightness_value > 1.0: + print(f"Invalid brightness value returned by get_monitor_brightness(): {brightness_value}") + brightness_value = 1.0 + + return brightness_value if __name__ == "__main__": print(get_monitor_brightness()) \ No newline at end of file