From 750758ba844ec3548de6c07a385c74b1f877c151 Mon Sep 17 00:00:00 2001 From: Jeremy Karst Date: Tue, 12 Aug 2025 20:17:23 -0400 Subject: [PATCH 1/2] 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 From c159417b93b3cbb10e32488a7d25a14627f2f0e2 Mon Sep 17 00:00:00 2001 From: Jeremy Karst Date: Tue, 12 Aug 2025 20:21:08 -0400 Subject: [PATCH 2/2] Added docstrings for each monitor function --- monitors.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/monitors.py b/monitors.py index 36f6f19..414cd4d 100644 --- a/monitors.py +++ b/monitors.py @@ -16,6 +16,14 @@ class DiskMonitor: self.max_history_size = hysterisis_time def get(self): + """ + Gets the percentage of the system's disk throughput for reading and writing. + + Returns: + A tuple containing the percentages of the system's disk throughput for reading + and writing data as floats between 0.0 and 1.0. This is a ratio based on + the highest disk throughput rate over the history of the monitor. + """ try: disk_io = psutil.disk_io_counters() read_usage = disk_io.read_bytes @@ -52,6 +60,14 @@ class NetworkMonitor: self.max_history_size = hysterisis_time def get(self): + """ + Gets the percentage of the system's network usage. + + Returns: + A tuple containing the percentages of the system's network usage for sending + and receiving data as floats between 0.0 and 1.0. This is a ratio based on + the highest network usage rate over the history of the monitor. + """ try: net_io = psutil.net_io_counters() sent_usage = net_io.bytes_sent @@ -86,6 +102,12 @@ class CPUMonitor: self.max_history_size = hysterisis_time def get(self): + """ + Gets the percentage of the system's CPU usage per core. + + Returns: + A list of values between 0.0 and 1.0 which is scaled to the CPU usage per core + """ try: cpu_usage = psutil.cpu_percent(percpu=True) for i in range(self.cpu_count): @@ -108,12 +130,24 @@ class CPUMonitor: class MemoryMonitor: @staticmethod def get(): + """ + Gets the percentage of the system's memory usage. + + Returns: + A value between 0.0 and 1.0 which is scaled to the memory usage + """ return psutil.virtual_memory().percent / 100.0 class BatteryMonitor: @staticmethod def get(): + """ + Gets the percentage of the battery's charge and whether it is plugged in. + + Returns: + A tuple containing the battery percentage as a float between 0.0 and 1.0 and whether it is plugged in + """ battery = psutil.sensors_battery() if battery is not None: battery_percentage = battery.percent / 100.0