-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_sys.sh
308 lines (299 loc) · 10.6 KB
/
led_sys.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/bash
TYPE=$1
COLOR=$2
MODIFIER=$3
DELAY=$4
DEBUG=$5
if [ "$TYPE" == "" ]; then
echo "OpenHD LED Control"
echo "__________________"
echo "usage:"
echo "led TYPE COLOR DELAY DEBUG"
echo "available types:"
echo "________________"
echo "on|off|toggle|manual|warning|error|flashing"
fi
# Kill all previous instances
pgrep -f led_sys.sh | grep -v $$ | xargs --no-run-if-empty kill -9
debugMessage() {
if [ "$DEBUG" == "debug" ]; then
echo "$1"
fi
}
# Detect platform and LED-type
if grep -q "Raspberry Pi" /proc/cpuinfo; then
PLATFORM="pi"
debugMessage "Platform: RPI"
if [ -d /sys/class/leds/PWR ]; then
PLATFORM="pi"
debugMessage "Platform: RPI"
fi
elif [ -d /sys/class/leds/openhd-x20dev ]; then
PLATFORM="x20"
FILE="/sys/class/leds/openhd-x20dev"
debugMessage "Platform: X20"
elif [ -f /sys/class/leds/user-led2/brightness ]; then
PLATFORM="rock5"
FILE="/sys/class/leds/user-led2/brightness"
debugMessage "Platform: Rock5"
elif [ -f /sys/class/leds/pi-led-green/brightness ]; then
PLATFORM="cm3"
FILE="/sys/class/leds/pi-led-green/brightness"
debugMessage "Platform: CM3"
elif [ -f /sys/class/leds/board-led/brightness ]; then
PLATFORM="zero3w"
FILE="/sys/class/leds/board-led/brightness"
debugMessage "Platform: Zero3w"
else
echo "Platform not found"
fi
# Main functions
LED_ON() {
if [ "$PLATFORM" == "x20" ]; then
if [ "$COLOR" == "red" ]; then
echo 1 > /sys/class/leds/openhd-x20dev:red:usr/brightness
echo 0 > /sys/class/leds/openhd-x20dev:green:usr/brightness
echo 0 > /sys/class/leds/openhd-x20dev:blue:usr/brightness
debugMessage "Red LED on"
elif [ "$COLOR" == "green" ]; then
echo 0 > /sys/class/leds/openhd-x20dev:red:usr/brightness
echo 1 > /sys/class/leds/openhd-x20dev:green:usr/brightness
echo 0 > /sys/class/leds/openhd-x20dev:blue:usr/brightness
debugMessage "Green LED on"
elif [ "$COLOR" == "blue" ]; then
echo 0 > /sys/class/leds/openhd-x20dev:red:usr/brightness
echo 0 > /sys/class/leds/openhd-x20dev:green:usr/brightness
echo 1 > /sys/class/leds/openhd-x20dev:blue:usr/brightness
debugMessage "Blue LED on"
elif [ "$COLOR" == "cyan" ]; then
echo 0 > /sys/class/leds/openhd-x20dev:red:usr/brightness
echo 1 > /sys/class/leds/openhd-x20dev:green:usr/brightness
echo 1 > /sys/class/leds/openhd-x20dev:blue:usr/brightness
debugMessage "Cyan LED on"
elif [ "$COLOR" == "magenta" ]; then
echo 1 > /sys/class/leds/openhd-x20dev:red:usr/brightness
echo 0 > /sys/class/leds/openhd-x20dev:green:usr/brightness
echo 1 > /sys/class/leds/openhd-x20dev:blue:usr/brightness
debugMessage "Magenta LED on"
elif [ "$COLOR" == "yellow" ]; then
echo 1 > /sys/class/leds/openhd-x20dev:red:usr/brightness
echo 1 > /sys/class/leds/openhd-x20dev:green:usr/brightness
echo 0 > /sys/class/leds/openhd-x20dev:blue:usr/brightness
debugMessage "Yellow LED on"
elif [ "$COLOR" == "white" ]; then
echo 1 > /sys/class/leds/openhd-x20dev:red:usr/brightness
echo 1 > /sys/class/leds/openhd-x20dev:green:usr/brightness
echo 1 > /sys/class/leds/openhd-x20dev:blue:usr/brightness
debugMessage "White LED on"
else
# Set it to white if no or wrong argument is passed
echo 1 > /sys/class/leds/openhd-x20dev:red:usr/brightness
echo 1 > /sys/class/leds/openhd-x20dev:green:usr/brightness
echo 1 > /sys/class/leds/openhd-x20dev:blue:usr/brightness
debugMessage "White LED on"
fi
elif [ "$PLATFORM" == "pi" ]; then
if [ "$COLOR" == "green" ]; then
echo 1 > /sys/class/leds/ACT/brightness
debugMessage "Green LED on"
elif [ "$COLOR" == "red" ]; then
echo 1 > /sys/class/leds/PWR/brightness
debugMessage "Red LED on"
else
echo 1 > /sys/class/leds/ACT/brightness
echo 1 > /sys/class/leds/PWR/brightness
fi
elif [ "$PLATFORM" == "cm3" ]; then
if [ "$COLOR" == "green" ]; then
echo 1 > /sys/class/leds/user-led/brightness
debugMessage "Green LED on"
elif [ "$COLOR" == "green-pi" ]; then
echo 1 > /sys/class/leds/pi-led-green/brightness
debugMessage "Green Pi LED on"
elif [ "$COLOR" == "red" ]; then
echo 1 > /sys/class/leds/pwr-led-red/brightness
debugMessage "Red LED on"
else
echo 1 > /sys/class/leds/user-led/brightness
echo 1 > /sys/class/leds/pi-led-green/brightness
echo 1 > /sys/class/leds/pwr-led-red/brightness
fi
elif [ "$PLATFORM" == "zero3w" ]; then
echo 1 > /sys/class/leds/board-led/brightness
elif [ "$PLATFORM" == "rock5" ]; then
echo 1 > /sys/class/leds/user-led2/brightness
fi
}
LED_OFF() {
if [ "$PLATFORM" == "x20" ]; then
if [ "$COLOR" == "red" ]; then
echo 0 > /sys/class/leds/openhd-x20dev:red:usr/brightness
debugMessage "Red LED off"
elif [ "$COLOR" == "green" ]; then
echo 0 > /sys/class/leds/openhd-x20dev:green:usr/brightness
debugMessage "Green LED off"
elif [ "$COLOR" == "blue" ]; then
echo 0 > /sys/class/leds/openhd-x20dev:blue:usr/brightness
debugMessage "Blue LED off"
elif [ "$COLOR" == "cyan" ]; then
echo 0 > /sys/class/leds/openhd-x20dev:green:usr/brightness
echo 0 > /sys/class/leds/openhd-x20dev:blue:usr/brightness
debugMessage "Cyan LED off"
elif [ "$COLOR" == "magenta" ]; then
echo 0 > /sys/class/leds/openhd-x20dev:red:usr/brightness
echo 0 > /sys/class/leds/openhd-x20dev:blue:usr/brightness
debugMessage "Magenta LED off"
elif [ "$COLOR" == "yellow" ]; then
echo 0 > /sys/class/leds/openhd-x20dev:red:usr/brightness
echo 0 > /sys/class/leds/openhd-x20dev:green:usr/brightness
debugMessage "Yellow LED off"
elif [ "$COLOR" == "white" ]; then
echo 0 > /sys/class/leds/openhd-x20dev:red:usr/brightness
echo 0 > /sys/class/leds/openhd-x20dev:green:usr/brightness
echo 0 > /sys/class/leds/openhd-x20dev:blue:usr/brightness
debugMessage "White LED off"
else
# Set it to white if no or wrong argument is passed
echo 0 > /sys/class/leds/openhd-x20dev:red:usr/brightness
echo 0 > /sys/class/leds/openhd-x20dev:green:usr/brightness
echo 0 > /sys/class/leds/openhd-x20dev:blue:usr/brightness
debugMessage "White LED off"
fi
elif [ "$PLATFORM" == "pi" ]; then
if [ "$COLOR" == "green" ]; then
echo 0 > /sys/class/leds/ACT/brightness
debugMessage "Green LED off"
elif [ "$COLOR" == "red" ]; then
echo 0 > /sys/class/leds/PWR/brightness
debugMessage "Red LED off"
else
echo 0 > /sys/class/leds/ACT/brightness
echo 0 > /sys/class/leds/PWR/brightness
fi
elif [ "$PLATFORM" == "cm3" ]; then
if [ "$COLOR" == "green" ]; then
echo 0 > /sys/class/leds/user-led/brightness
debugMessage "Green LED off"
elif [ "$COLOR" == "green-pi" ]; then
echo 0 > /sys/class/leds/pi-led-green/brightness
debugMessage "Green Pi LED off"
elif [ "$COLOR" == "red" ]; then
echo 0 > /sys/class/leds/pwr-led-red/brightness
debugMessage "Red LED off"
else
echo 0 > /sys/class/leds/user-led/brightness
echo 0 > /sys/class/leds/pi-led-green/brightness
echo 0 > /sys/class/leds/pwr-led-red/brightness
fi
elif [ "$PLATFORM" == "zero3w" ]; then
echo 0 > /sys/class/leds/board-led/brightness
elif [ "$PLATFORM" == "rock5" ]; then
echo 0 > /sys/class/leds/user-led2/brightness
fi
}
BLINK_LED() {
while true; do
LED_ON
sleep $(echo "$MODIFIER * 0.01" | bc)
LED_OFF
sleep $(echo "$MODIFIER * 0.01" | bc)
done
}
BLINK_LED_ASYNC() {
while true; do
LED_ON
sleep $(echo "$DELAY1 * 0.01" | bc)
LED_OFF
sleep $(echo "$DELAY2 * 0.01" | bc)
done
}
BLINK_LED_ASYNC_MULTI() {
while true; do
COLOR=$COLOR1
LED_ON
sleep $(echo "$DELAY1 * 0.01" | bc)
LED_OFF
sleep $(echo "$DELAY2 * 0.01" | bc)
COLOR=$COLOR2
LED_ON
sleep $(echo "$DELAY1 * 0.01" | bc)
LED_OFF
sleep $(echo "$DELAY2 * 0.01" | bc)
done
}
BLINK_LED_ASYNC_MULTI_2() {
while true; do
COLOR=$COLOR1
LED_ON
sleep $(echo "$DELAY1 * 0.01" | bc)
COLOR=$COLOR2
LED_ON
sleep $(echo "$DELAY1 * 0.01" | bc)
COLOR=$COLOR1
LED_OFF
sleep $(echo "$DELAY2 * 0.01" | bc)
COLOR=$COLOR2
LED_OFF
sleep $(echo "$DELAY2 * 0.01" | bc)
done
}
BLINK_LED_ASYNC_MULTI_SWITCHING() {
while true; do
COLOR=$COLOR1
LED_ON
COLOR=$COLOR2
LED_OFF
sleep $(echo "$DELAY1 * 0.01" | bc)
COLOR=$COLOR2
LED_ON
COLOR=$COLOR1
LED_OFF
sleep $(echo "$DELAY2 * 0.01" | bc)
done
}
# LED mode Selection
if [ "$TYPE" == "on" ]; then
LED_ON
elif [ "$TYPE" == "off" ]; then
LED_OFF
elif [ "$TYPE" == "toggle" ]; then
if [[ $(cat /sys/class/leds/pi-led-green/brightness) == "0" ]]; then
LED_ON
else
LED_OFF
fi
elif [ "$TYPE" == "manual" ]; then
if [ -z "$MODIFIER" ]; then
echo "Missing delay value for MANUAL mode."
else
BLINK_LED
fi
elif [ "$TYPE" == "warning" ]; then
if [ -z "$MODIFIER" ]; then
echo "Missing value for Warning mode."
else
COLOR="green"
DELAY1="50"
DELAY2="200"
BLINK_LED_ASYNC
debugMessage "LED Warning $MODIFIER"
fi
elif [ "$TYPE" == "error" ]; then
if [ -z "$MODIFIER" ]; then
echo "Missing value for Error mode."
else
DELAY1="50"
DELAY2="100"
COLOR1="green"
COLOR2="red"
BLINK_LED_ASYNC_MULTI_2
debugMessage "LED Error $MODIFIER"
fi
elif [ "$TYPE" == "flashing" ]; then
DELAY1="1"
DELAY2="1"
COLOR1="green"
COLOR2="blue"
BLINK_LED_ASYNC_MULTI_SWITCHING
debugMessage "LED $MODIFIER"
fi