-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled.sh
executable file
·111 lines (99 loc) · 2.89 KB
/
led.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/sh
# path: /home/klassiker/.local/share/repos/raspberrypi/led.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/raspberrypi
# date: 2022-09-15T08:42:56+0200
# speed up script by using standard c
LC_ALL=C
LANG=C
# config
# led0 = ACT (green), led1 = PWR (red)
led0_path="/sys/class/leds/led0"
led1_path="/sys/class/leds/led1"
# cat /sys/class/leds/led0/trigger
# none cpu kbd-scrolllock
# usb-gadget cpu0 kbd-numlock
# usb-host cpu1 kbd-capslock
# rfkill-any cpu2 kbd-kanalock
# rfkill-none cpu3 kbd-shiftlock
# timer default-on kbd-altgrlock
# oneshot input kbd-ctrllock
# heartbeat panic kbd-altlock
# backlight actpwr kbd-shiftllock
# gpio mmc0 kbd-shiftrlock
# kbd-ctrlllock
# kbd-ctrlrlock
led0_default="mmc0"
led1_default="input"
script=$(basename "$0")
help="$script [-h/--help] -- script to change status leds
Usage:
$script [--act] [0/1] [--pwr] [0/1] [--defaults] [--status]
Settings:
--act = set activity [green] led off [0] or on [1]
--pwr = set power [red] led off [0] or on [1]
--defaults = reset led settings to default values
--status = displays status for brightness and trigger
Example:
$script --act 0
$script --pwr 1
$script --pwr 0 --act 1
$script --defaults
$script --status"
print_help() {
printf "%s\n" "$help"
exit "$1"
}
check_root() {
[ "$(id -u)" -ne 0 ] \
&& printf "this script needs root privileges to run\n" \
&& exit 1
}
set_trigger() {
grep -q "\[$1\]" "$2/trigger" \
|| printf "%s\n" "$1" > "$2/trigger"
}
set_led() {
set_trigger "none" "$2"
if [ "$1" = 0 ] || [ "$1" = 1 ]; then
printf "%s\n" "$1" > "$2/brightness"
else
print_help 1
fi
}
[ -z "$1" ] \
&& print_help 1
while [ $# -ge 1 ]; do
case "$1" in
-h | --help)
print_help 0
;;
--act)
check_root
shift
set_led "$1" "$led0_path"
;;
--pwr)
check_root
shift
set_led "$1" "$led1_path"
;;
--defaults)
check_root
set_trigger "$led0_default" "$led0_path"
set_trigger "$led1_default" "$led1_path"
;;
--status)
printf "setting ACT LED to brightness %s with trigger %s\n" \
"$(cat "$led0_path/brightness")" \
"$(grep -oP '\[\K[^\]]+' "$led0_path/trigger")"
printf "setting PWR LED to brightness %s with trigger %s\n" \
"$(cat "$led1_path/brightness")" \
"$(grep -oP '\[\K[^\]]+' "$led1_path/trigger")"
;;
*)
print_help 1
;;
esac
shift
done