From c6fabd0bc3354099bcfc73aa4064139b23fb509d Mon Sep 17 00:00:00 2001 From: Florian Eckert Date: Thu, 1 Feb 2024 12:51:02 +0100 Subject: [PATCH] base-files/leds: add setting the LED color via uci Add the possibility that colored LEDs can also be configured via the uci. config led 'led1' option name '' option sysfs '' option trigger 'default-on' option default '1' --> option color_{$color} '<0-255>' The supported names of the variable "${color}" for the selected LED can be queried in the file with the name 'multi_index'. Signed-off-by: Florian Eckert --- package/base-files/files/etc/init.d/led | 35 +++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/package/base-files/files/etc/init.d/led b/package/base-files/files/etc/init.d/led index 4d3feddf64..ea2688cab2 100755 --- a/package/base-files/files/etc/init.d/led +++ b/package/base-files/files/etc/init.d/led @@ -3,6 +3,39 @@ START=96 +led_color_set() { + local cfg="$1" + local sysfs="$2" + + local max_b + local colors + local color + local multi_intensity + local value + local write + + [ -e /sys/class/leds/${sysfs}/multi_intensity ] || return + [ -e /sys/class/leds/${sysfs}/multi_index ] || return + + max_b="$(cat /sys/class/leds/${sysfs}/max_brightness)" + colors="$(cat /sys/class/leds/${sysfs}/multi_index | tr " " "\n")" + multi_intensity="" + for color in $colors; do + config_get value $1 "color_${color}" "0" + [ "$value" -gt 0 ] && write=1 + [ "$value" -gt "$max_b" ] && value="$max_b" + multi_intensity="${multi_intensity}${value} " + done + + # Check if any color is configured + [ "$write" = 1 ] || return + # Remove last whitespace + multi_intensity="${multi_intensity:0:-1}" + + echo "setting '${name}' led color to '${multi_intensity}'" + echo "${multi_intensity}" > /sys/class/leds/${sysfs}/multi_intensity +} + load_led() { local name local sysfs @@ -68,6 +101,8 @@ load_led() { [ $default = 1 ] && cat /sys/class/leds/${sysfs}/max_brightness > /sys/class/leds/${sysfs}/brightness + led_color_set "$1" "$sysfs" + echo $trigger > /sys/class/leds/${sysfs}/trigger 2> /dev/null ret="$?" [ $ret = 0 ] || {