|
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: MPL-2.0 |
4 | 4 |
|
5 | | -from arduino.app_utils import Logger, Bridge |
| 5 | +from arduino.app_utils import Logger |
6 | 6 |
|
7 | 7 | logger = Logger(__name__) |
8 | 8 |
|
9 | | -LED_IDS = [1, 2, 3, 4] # Supported LED IDs |
10 | | - |
11 | | -LED_BRIGHTNESS_FILES = [ |
12 | | - "/sys/class/leds/red:user/brightness", |
13 | | - "/sys/class/leds/green:user/brightness", |
14 | | - "/sys/class/leds/blue:user/brightness", |
15 | | - "/sys/class/leds/red:panic/brightness", |
16 | | - "/sys/class/leds/green:wlan/brightness", |
17 | | - "/sys/class/leds/blue:bt/brightness", |
18 | | -] |
19 | | - |
20 | | - |
21 | | -def write_led_file(led_file, color): |
22 | | - try: |
23 | | - with open(led_file, "w") as f: |
24 | | - f.write(f"{color}\n") |
25 | | - except Exception as e: |
26 | | - print(f"Error writing to {led_file}: {e}") |
27 | | - |
28 | 9 |
|
29 | 10 | class Leds: |
| 11 | + _led_ids = [1, 2] # Supported LED IDs (Led 3 and 4 can't be controlled directly by MPU but only by MCU via Bridge) |
| 12 | + |
| 13 | + _led1_brightness_files = [ |
| 14 | + "/sys/class/leds/red:user/brightness", |
| 15 | + "/sys/class/leds/green:user/brightness", |
| 16 | + "/sys/class/leds/blue:user/brightness", |
| 17 | + ] |
| 18 | + _led2_brightness_files = [ |
| 19 | + "/sys/class/leds/red:panic/brightness", |
| 20 | + "/sys/class/leds/green:wlan/brightness", |
| 21 | + "/sys/class/leds/blue:bt/brightness", |
| 22 | + ] |
| 23 | + |
30 | 24 | @staticmethod |
31 | | - # Led 1 and 2 are controlled by directly (MPU), while Led 3 and 4 are controlled via Bridge (MCU) |
32 | | - def set_led_color(ledid: int, rgb_color: dict): |
| 25 | + def _write_led_file(led_file, value: bool): |
33 | 26 | try: |
34 | | - if ledid not in LED_IDS: |
35 | | - raise ValueError(f"Unknown led '{ledid}'") |
36 | | - |
37 | | - if not rgb_color or not all(k in rgb_color for k in ("r", "g", "b")): |
38 | | - raise ValueError("Color must be an object with 'r', 'g', 'b' keys") |
39 | | - |
40 | | - match ledid: |
41 | | - case 1: |
42 | | - Leds.set_led1_color(rgb_color["r"], rgb_color["g"], rgb_color["b"]) |
43 | | - case 2: |
44 | | - Leds.set_led2_color(rgb_color["r"], rgb_color["g"], rgb_color["b"]) |
45 | | - case 3: |
46 | | - Leds.set_led3_color(rgb_color["r"], rgb_color["g"], rgb_color["b"]) |
47 | | - case 4: |
48 | | - Leds.set_led4_color(rgb_color["r"], rgb_color["g"], rgb_color["b"]) |
49 | | - |
| 27 | + with open(led_file, "w") as f: |
| 28 | + f.write(f"{int(value)}\n") |
50 | 29 | except Exception as e: |
51 | | - Logger(__name__).error(f"LED color set error: {e}") |
| 30 | + print(f"Error writing to {led_file}: {e}") |
52 | 31 |
|
53 | 32 | @staticmethod |
54 | | - def set_led1_color(r, g, b): |
55 | | - write_led_file(LED_BRIGHTNESS_FILES[0], r) |
56 | | - write_led_file(LED_BRIGHTNESS_FILES[1], g) |
57 | | - write_led_file(LED_BRIGHTNESS_FILES[2], b) |
| 33 | + def set_led1_color(r: bool, g: bool, b: bool): |
| 34 | + Leds._write_led_file(Leds._led1_brightness_files[0], r) |
| 35 | + Leds._write_led_file(Leds._led1_brightness_files[1], g) |
| 36 | + Leds._write_led_file(Leds._led1_brightness_files[2], b) |
58 | 37 |
|
59 | 38 | @staticmethod |
60 | | - def set_led2_color(r, g, b): |
61 | | - write_led_file(LED_BRIGHTNESS_FILES[3], r) |
62 | | - write_led_file(LED_BRIGHTNESS_FILES[4], g) |
63 | | - write_led_file(LED_BRIGHTNESS_FILES[5], b) |
64 | | - |
65 | | - def set_led3_color(r, g, b): |
66 | | - Bridge.call("set_led_color", 3, r, g, b) |
| 39 | + def set_led2_color(r: bool, g: bool, b: bool): |
| 40 | + Leds._write_led_file(Leds._led2_brightness_files[0], r) |
| 41 | + Leds._write_led_file(Leds._led2_brightness_files[1], g) |
| 42 | + Leds._write_led_file(Leds._led2_brightness_files[2], b) |
67 | 43 |
|
68 | | - def set_led4_color(r, g, b): |
69 | | - Bridge.call("set_led_color", 4, r, g, b) |
0 commit comments