-
Notifications
You must be signed in to change notification settings - Fork 5
/
setled
180 lines (159 loc) · 4.4 KB
/
setled
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
#!/bin/bash -u
# Script_name : setled
# Author : Bert Lindeman
# Description : Set an EV3-led
# Version : v1.3
# Date : 2014-11-04
# Usage : setled
# Notes : 4 optional parameters:
# color [side [ON | OFF [valid MODE]]]
# Copyright : 2014 Bert Lindeman
# License : GPL-3.0+
#
#============================================================================
#
# Changelog
# Date By Description
# 20141104 BL prepare for release v1.3
# 20140910 BL Set ON to a higher value than 1
# brightness since kernel 3.16.1 is honoured
# and no longer a switch
# 20140826 BL add script header
# 20160103 BL adapt to image 20151230 (change filenames)
#
#============================================================================
. /usr/local/bin/bert_ev3dev_functions
#
# get parameters and translate to lowercase:
#
# set default if parameter has not been specified:
if [ "$#" -lt 1 ]; then : ${p1:=green}; else p1=$1; fi
if [ "$#" -lt 2 ]; then : ${p2:=left} ; else p2=$2; fi
if [ "$#" -lt 3 ]; then : ${p3:=on} ; else p3=$3; fi
if [ "$#" -lt 4 ]; then : ${p4:=none} ; else p4=$4; fi
tosetColor="$(echo $p1 | tr 'A-Z' 'a-z')"
side="$(echo $p2 | tr 'A-Z' 'a-z')"
mode="$(echo $p3 | tr 'A-Z' 'a-z')"
tosetTrigger="$(echo $p4 | tr 'A-Z' 'a-z')"
usageEXIT () {
echo "$1"
echo
echo "USAGE: $0 color [side [\"ON\"|\"OFF\" [valid MODE]]]"
exit 1
}
#set -v -x
#showLEDtrigger () {
# # echo "showing trigger from $1"
# for word in `cat $1` ;
# do
# delim=${word:0:1}${word:${#word}-1:1}
# if [ $delim == "[]" ]; then
# trigger=${word:1:${#word}-2}
# # echo "current trigger now: $trigger"
# break # do not search further
# else
# trigger=$word
# fi
# done
# currenttrigger=$trigger
# echo " current trigger=$currenttrigger"
# }
verifyLEDtrigger () {
for word in `cat $2` ;
do
delim=${word:0:1}${word:${#word}-1:1}
if [ $delim == "[]" ]; then
trigger=${word:1:${#word}-2} # drop the first and last char: [ and ]
else
trigger=$word
fi
if [ "$1" == "$trigger" ]; then
: echo " Trigger $1 is valid"
return 0
fi
done
# invalid trigger specified; show the valid ones:
echo "specified trigger \"$1\" "
for word in `cat $2` ;
do
delim=${word:0:1}${word:${#word}-1:1}
if [ $delim == "[]" ]; then
trigger=${word:1:${#word}-2} # drop the first and last char: [ and ]
else
trigger=$word
fi
echo " acceptable trigger: \"$trigger\" "
done
usageEXIT "specified trigger \"$1\" is not recognized."
}
# start of mainline:
if [ "$#" -gt "4" ]; then
usageEXIT "Too many parameters; Allowed 4 Got $#: $*"
fi
case $mode in
on)
command="echo"; valueORzero=127
;;
off)
command="echo"; valueORzero=0
;;
"")
command="cat" # default
;;
*)
usageEXIT "MODE should be on or off! Got \"$mode\""
;;
esac
case $tosetColor in
red)
;;
green)
;;
"")
color="green" # default
;;
*)
usageEXIT "COLOR should be red or green! Got \"$tosetColor\""
;;
esac
case $side in
left)
;;
right)
;;
*)
usageEXIT "SIDE should be left or right! Got \"$side\""
;;
esac
ledbase="/sys/class/leds/ev3:$side:$tosetColor:ev3dev"
ledbrightness="$ledbase/brightness"
ledtriggerpath="$ledbase/trigger"
if [ "$tosetTrigger" == "" ]; then
tosetTrigger ="none"
else
verifyLEDtrigger "$tosetTrigger" "$ledtriggerpath"
fi
echo "base directory $ledbase"
if [ $command == "cat" ]; then
echo " brightness=`cat $ledbrightness`"
showtrigger "`cat $ledtriggerpath`"
else
echo "BEFORE:"
echo " brightness=`cat $ledbrightness`"
showtrigger "`cat $ledtriggerpath`"
# echo "setting trigger to $tosetTrigger > $ledtriggerpath"
if [ "$p4" != "" ]; then
echo $tosetTrigger > $ledtriggerpath
if [ $? -ne 0 ]; then
echo -e "!!!!!!!!returncode of trigger: $?"
fi
fi
# showLEDtrigger $ledtriggerpath
echo $valueORzero > $ledbrightness
if [ $? -ne 0 ]; then
echo -e "!!!!!!!!returncode of command: $?"
fi
echo "AFTER:"
echo " brightness=`cat $ledbrightness`"
showtrigger "`cat $ledtriggerpath`"
fi