Browse Source

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 '<name>'
	option sysfs '<path>'
	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 <[email protected]>
Florian Eckert 1 year ago
parent
commit
c6fabd0bc3
1 changed files with 35 additions and 0 deletions
  1. 35 0
      package/base-files/files/etc/init.d/led

+ 35 - 0
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 ] || {