diff --git a/src/arduino/app_utils/__init__.py b/src/arduino/app_utils/__init__.py index f7aca94d..02e234d9 100644 --- a/src/arduino/app_utils/__init__.py +++ b/src/arduino/app_utils/__init__.py @@ -13,6 +13,7 @@ from .logger import * from .slidingwindowbuffer import * from .userinput import * +from .leds import * __all__ = [ "App", @@ -31,4 +32,5 @@ "SineGenerator", "SlidingWindowBuffer", "UserTextInput", + "Leds", ] diff --git a/src/arduino/app_utils/leds.py b/src/arduino/app_utils/leds.py new file mode 100644 index 00000000..3ca2036c --- /dev/null +++ b/src/arduino/app_utils/leds.py @@ -0,0 +1,66 @@ +# SPDX-FileCopyrightText: Copyright (C) 2025 ARDUINO SA +# SPDX-FileCopyrightText: Copyright (C) ARDUINO SRL (http://www.arduino.cc) +# +# SPDX-License-Identifier: MPL-2.0 + +from arduino.app_utils import Logger + +logger = Logger(__name__) + + +class Leds: + """ + A utility class for controlling LED colors on Arduino hardware. + + This class provides static methods to control two RGB LEDs by writing to system + brightness files. LED1 and LED2 can be controlled directly by the MPU, while + LED3 and LED4 require MCU control via Bridge. + + Attributes: + _led_ids (list): List of supported LED IDs [1, 2]. + _led1_brightness_files (list): System file paths for LED1 RGB channels + (red:user, green:user, blue:user). + _led2_brightness_files (list): System file paths for LED2 RGB channels + (red:panic, green:wlan, blue:bt). + + Methods: + set_led1_color(r, g, b): Set the RGB color state for LED1. + set_led2_color(r, g, b): Set the RGB color state for LED2. + + Example: + >>> Leds.set_led1_color(True, False, True) # LED1 shows magenta + >>> Leds.set_led2_color(False, True, False) # LED2 shows green + """ + + _led_ids = [1, 2] # Supported LED IDs (Led 3 and 4 can't be controlled directly by MPU but only by MCU via Bridge) + + _led1_brightness_files = [ + "/sys/class/leds/red:user/brightness", + "/sys/class/leds/green:user/brightness", + "/sys/class/leds/blue:user/brightness", + ] + _led2_brightness_files = [ + "/sys/class/leds/red:panic/brightness", + "/sys/class/leds/green:wlan/brightness", + "/sys/class/leds/blue:bt/brightness", + ] + + @staticmethod + def _write_led_file(led_file, value: bool): + try: + with open(led_file, "w") as f: + f.write(f"{int(value)}\n") + except Exception as e: + print(f"Error writing to {led_file}: {e}") + + @staticmethod + def set_led1_color(r: bool, g: bool, b: bool): + Leds._write_led_file(Leds._led1_brightness_files[0], r) + Leds._write_led_file(Leds._led1_brightness_files[1], g) + Leds._write_led_file(Leds._led1_brightness_files[2], b) + + @staticmethod + def set_led2_color(r: bool, g: bool, b: bool): + Leds._write_led_file(Leds._led2_brightness_files[0], r) + Leds._write_led_file(Leds._led2_brightness_files[1], g) + Leds._write_led_file(Leds._led2_brightness_files[2], b)