Skip to content

Commit 1f26421

Browse files
authored
Improved channel_select script (#3)
1 parent 151b238 commit 1f26421

File tree

1 file changed

+136
-61
lines changed

1 file changed

+136
-61
lines changed

shell_scripts/channel_select

Lines changed: 136 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,16 @@ CHANNEL_STATE=0
2525
print_help() {
2626
cat <<- EOH
2727
Usage:
28-
$0 <[ch/CH]1-8> <on/off/true/false/ON/OFF>
28+
$0 <-1-8> <-e/d>
29+
$0 <--ch1-8> <--enable/--disable>
2930
$0 --help # prints this message and exits
31+
32+
Detailled usage:
33+
This script supports getopt-like arguments.
34+
You may chain short options like the channel selections, to select more than one channel.
35+
36+
To select and enable channels 1, 4, 7, and 8 you can call:
37+
$0 -1478e
3038
EOH
3139
}
3240

@@ -35,75 +43,142 @@ if [[ $# -lt 2 ]]; then
3543
exit
3644
fi
3745

38-
for i in $@; do
39-
case $i in
40-
"ch1"|"CH1"|"1")
41-
echo "Selected channel 1"
42-
eval SELECTED_CHANNEL=${CHANNELS[0]}
43-
;;
44-
"ch2"|"CH2"|"2")
45-
echo "Selected channel 2"
46-
eval SELECTED_CHANNEL=${CHANNELS[1]}
47-
;;
48-
"ch3"|"CH3"|"3")
49-
echo "Selected channel 3"
50-
eval SELECTED_CHANNEL=${CHANNELS[2]}
51-
;;
52-
"ch4"|"CH4"|"4")
53-
echo "Selected channel 4"
54-
eval SELECTED_CHANNEL=${CHANNELS[3]}
55-
;;
56-
"ch5"|"CH5"|"5")
57-
echo "Selected channel 5"
58-
eval SELECTED_CHANNEL=${CHANNELS[4]}
59-
;;
60-
"ch6"|"CH6"|"6")
61-
echo "Selected channel 6"
62-
eval SELECTED_CHANNEL=${CHANNELS[5]}
63-
;;
64-
"ch7"|"CH7"|"7")
65-
echo "Selected channel 7"
66-
eval SELECTED_CHANNEL=${CHANNELS[6]}
67-
;;
68-
"ch8"|"CH8"|"8")
69-
echo "Selected channel 8"
70-
eval SELECTED_CHANNEL=${CHANNELS[7]}
71-
;;
72-
"--help"|"-h")
73-
print_help $0
74-
exit
75-
;;
76-
"ON"|"on"|"true")
77-
echo "Channel state = on"
78-
eval CHANNEL_STATE=0
79-
;;
80-
"OFF"|"off"|"false")
81-
echo "Channel state = off"
82-
eval CHANNEL_STATE=1
83-
;;
84-
*)
46+
SHORT_OPTS="h13245678edEr"
47+
LONG_OPTS="help,ch1,ch2,ch3,ch4,ch5,ch6,ch7,ch7,ch8,enable,disable,export-only,read-state"
48+
49+
readonly SHORT_OPTS
50+
readonly LONG_OPTS
51+
52+
REQUESTED_CHANNELS=()
53+
54+
#=======================================
55+
# Handle command-line arguments (getopt)
56+
#=======================================
57+
TEMP_OPT=$(getopt -o "$SHORT_OPTS" --long "$LONG_OPTS" -n "$(basename "$0")" -- "$@")
58+
error=$?
59+
60+
eval set -- "$TEMP_OPT"
61+
unset TEMP_OPT
62+
63+
if [[ $error -ne 0 ]]; then
64+
echo "Invalid arguments!" >&2
65+
print_help
66+
exit $error
67+
fi
68+
69+
while true; do
70+
case "$1" in
71+
"-h"|"--help")
8572
print_help
86-
exit
87-
;;
73+
exit 0
74+
;;
75+
76+
# Actual channel selection
77+
"-1"|"--ch1")
78+
REQUESTED_CHANNELS+=("${CHANNELS[0]}")
79+
shift
80+
;;
81+
"-2"|"--ch2")
82+
REQUESTED_CHANNELS+=("${CHANNELS[1]}")
83+
shift
84+
;;
85+
"-3"|"--ch3")
86+
REQUESTED_CHANNELS+=("${CHANNELS[2]}")
87+
shift
88+
;;
89+
"-4"|"--ch4")
90+
REQUESTED_CHANNELS+=("${CHANNELS[3]}")
91+
shift
92+
;;
93+
"-5"|"--ch5")
94+
REQUESTED_CHANNELS+=("${CHANNELS[4]}")
95+
shift
96+
;;
97+
"-6"|"--ch6")
98+
REQUESTED_CHANNELS+=("${CHANNELS[5]}")
99+
shift
100+
;;
101+
"-7"|"--ch7")
102+
REQUESTED_CHANNELS+=("${CHANNELS[6]}")
103+
shift
104+
;;
105+
"-8"|"--ch8")
106+
REQUESTED_CHANNELS+=("${CHANNELS[7]}")
107+
shift
108+
;;
109+
110+
# Modifier
111+
"-e"|"--enable")
112+
CHANNEL_STATE=0
113+
shift
114+
;;
115+
"-d"|"--disable")
116+
CHANNEL_STATE=1
117+
shift
118+
;;
119+
120+
# More options
121+
"-E"|"--export-only")
122+
EXPORT_ONLY=1
123+
shift
124+
;;
125+
"-r"|"--read-state")
126+
READ_STATE=1
127+
shift
128+
;;
129+
130+
"--")
131+
shift
132+
break
133+
;;
88134
esac
89135
done
90136

137+
readonly REQUESTED_CHANNELS
138+
readonly CHANNEL_STATE
139+
readonly EXPORT_ONLY
140+
91141
############################
92142
## Do actual work here
93143
############################
94144

95-
# Detect if GPIO pin is already exported
96-
if [[ ! -d "/sys/class/gpio/gpio$SELECTED_CHANNEL" ]]; then
97-
echo "$SELECTED_CHANNEL" > "/sys/class/gpio/export"
98-
#else
99-
#&2 < echo "Channel $SELECTED_CHANNEL is already exported!"
100-
#exit
101-
fi
145+
###
146+
# Converts an integer to a human-readable state
147+
#
148+
# $1 => The state
149+
###
150+
human_readable_state() {
151+
if [[ $1 -ge 1 ]]; then
152+
echo "OFF"
153+
else
154+
echo "ON"
155+
fi
156+
}
157+
158+
printf "%d channels have been selected.\n" ${#REQUESTED_CHANNELS[@]}
102159

103-
# Now dump value to GPIO
104-
echo "out" > "/sys/class/gpio/gpio$SELECTED_CHANNEL/direction"
105-
echo "$CHANNEL_STATE" > "/sys/class/gpio/gpio$SELECTED_CHANNEL/value"
160+
for selected_channel in "${REQUESTED_CHANNELS[@]}"; do
161+
# Detect if GPIO pin is already exported
162+
if [[ ! -d "/sys/class/gpio/gpio$selected_channel" ]]; then
163+
echo "Exporting channel $selected_channel"
164+
printf "%s" "$selected_channel" > "/sys/class/gpio/export"
165+
fi
106166

107-
echo "GPIO pin $SELECTED_CHANNEL set to $CHANNEL_STATE"
167+
if [[ $READ_STATE -ge 1 ]]; then
168+
printf "Channel %d has direction %s.\n\t-> Current state %s\n\n" "$selected_channel" \
169+
"$(cat "/sys/class/gpio/gpio$selected_channel/direction")" \
170+
"$(human_readable_state "$(cat "/sys/class/gpio/gpio$selected_channel/value")")"
171+
continue;
172+
fi
108173

174+
if [[ $EXPORT_ONLY -ge 1 ]]; then
175+
continue;
176+
fi
109177

178+
# Now dump value to GPIO
179+
echo "out" > "/sys/class/gpio/gpio$selected_channel/direction"
180+
echo "$CHANNEL_STATE" > "/sys/class/gpio/gpio$selected_channel/value"
181+
182+
echo "GPIO pin $selected_channel set to $CHANNEL_STATE"
183+
184+
done

0 commit comments

Comments
 (0)