|
| 1 | +# SPDX-FileCopyrightText: Copyright (C) 2025 ARDUINO SA <http://www.arduino.cc> |
| 2 | +# SPDX-FileCopyrightText: Copyright (C) ARDUINO SRL (http://www.arduino.cc) |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: MPL-2.0 |
| 5 | + |
| 6 | +from arduino.app_utils import Logger |
| 7 | + |
| 8 | +logger = Logger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +class Leds: |
| 12 | + """ |
| 13 | + A utility class for controlling LED colors on Arduino hardware. |
| 14 | +
|
| 15 | + This class provides static methods to control two RGB LEDs by writing to system |
| 16 | + brightness files. LED1 and LED2 can be controlled directly by the MPU, while |
| 17 | + LED3 and LED4 require MCU control via Bridge. |
| 18 | +
|
| 19 | + Attributes: |
| 20 | + _led_ids (list): List of supported LED IDs [1, 2]. |
| 21 | + _led1_brightness_files (list): System file paths for LED1 RGB channels |
| 22 | + (red:user, green:user, blue:user). |
| 23 | + _led2_brightness_files (list): System file paths for LED2 RGB channels |
| 24 | + (red:panic, green:wlan, blue:bt). |
| 25 | +
|
| 26 | + Methods: |
| 27 | + set_led1_color(r, g, b): Set the RGB color state for LED1. |
| 28 | + set_led2_color(r, g, b): Set the RGB color state for LED2. |
| 29 | +
|
| 30 | + Example: |
| 31 | + >>> Leds.set_led1_color(True, False, True) # LED1 shows magenta |
| 32 | + >>> Leds.set_led2_color(False, True, False) # LED2 shows green |
| 33 | + """ |
| 34 | + |
| 35 | + _led_ids = [1, 2] # Supported LED IDs (Led 3 and 4 can't be controlled directly by MPU but only by MCU via Bridge) |
| 36 | + |
| 37 | + _led1_brightness_files = [ |
| 38 | + "/sys/class/leds/red:user/brightness", |
| 39 | + "/sys/class/leds/green:user/brightness", |
| 40 | + "/sys/class/leds/blue:user/brightness", |
| 41 | + ] |
| 42 | + _led2_brightness_files = [ |
| 43 | + "/sys/class/leds/red:panic/brightness", |
| 44 | + "/sys/class/leds/green:wlan/brightness", |
| 45 | + "/sys/class/leds/blue:bt/brightness", |
| 46 | + ] |
| 47 | + |
| 48 | + @staticmethod |
| 49 | + def _write_led_file(led_file, value: bool): |
| 50 | + try: |
| 51 | + with open(led_file, "w") as f: |
| 52 | + f.write(f"{int(value)}\n") |
| 53 | + except Exception as e: |
| 54 | + print(f"Error writing to {led_file}: {e}") |
| 55 | + |
| 56 | + @staticmethod |
| 57 | + def set_led1_color(r: bool, g: bool, b: bool): |
| 58 | + Leds._write_led_file(Leds._led1_brightness_files[0], r) |
| 59 | + Leds._write_led_file(Leds._led1_brightness_files[1], g) |
| 60 | + Leds._write_led_file(Leds._led1_brightness_files[2], b) |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + def set_led2_color(r: bool, g: bool, b: bool): |
| 64 | + Leds._write_led_file(Leds._led2_brightness_files[0], r) |
| 65 | + Leds._write_led_file(Leds._led2_brightness_files[1], g) |
| 66 | + Leds._write_led_file(Leds._led2_brightness_files[2], b) |
0 commit comments