-
Notifications
You must be signed in to change notification settings - Fork 5
/
listports
267 lines (236 loc) · 10.5 KB
/
listports
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
#!/bin/bash -u
# Script_name : listports
# Author : Bert Lindeman
# Description : List sensors and motors
# Version : v1.3
# Date : 2014-12-28
# Usage : listports
# Notes :
# Copyright : 2014 Bert Lindeman
# License : GPL-3.0+
#
#============================================================================
#
#
# Changelog
# Date By Description
# 20141104 BL o prepare for release v1.3
# o Take terminal-width into account
# 20141020 BL o adapt to motor mappings (kernel 3.16.1-6)
# o add rcx leds and motors
# 20140928 BL o Adapt to updates in
# kernel version 3.16.1-3-ev3dev
# and 3.16.1-4-ev3dev
# o remove field type_id
# o slightly different file names
# o motor type -> device_type
# o issue 123: force mode to input ports and output ports
# 20141228 BL o Adapt to updates in
# kernel version 3.16.1-8-ev3dev
# 20150202 BL Shift some fields in the report to prevent ragged columns
# 20150416 BL update for kernel 3.16.7-ckt9
# 20160103 BL adapt to image 20151230 (change filenames)
#
#============================================================================
. /usr/local/bin/bert_ev3dev_functions
findinport() {
# look in lego-port sysfs directories to determine which lego-port
# is processing the in% port we are looking for (From parameter-1)
portpath=`grep -i "${1}" /sys/class/lego-port/port*/address`
# results in /sys/class/lego-port/port7/address:outD
# take dirname: bla/bla/port7
# take basename
portdir=`dirname ${portpath}`
legoport=`basename ${portdir}`
#set +x
}
haveportsin=""
haveportsout=""
# one extra directory level since 3.16.1-8: -maxdepth 3 instead of 2
findstr=`find /sys/devices/platform/legoev3-ports/ -maxdepth 3 -name "in[1234]\:*" `
#
for p in $findstr
do
echo -en "\r-"
basename="`basename $p`"
dirname="`dirname $p`"
# split basename before and after ":"
porttype1="${basename%%:*}"
porttype2="${p#*:}"
#
if [ ${porttype1:0:2} == "in" ]; then
driver_mode="`cat $dirname/mode`"
driver_status="`cat $dirname/status`" # was state
inportnumber=${porttype1:2:1} # in%
haveportsin="$haveportsin $inportnumber"
Arrindir[$inportnumber]=${dirname}
Arrindrvmode[$inportnumber]=${driver_mode}
Arrindrvstate[$inportnumber]=${driver_status}
Arrinport[$inportnumber]=${porttype1} #${inportnumber}
Arrintype[$inportnumber]=${porttype2}
Arrinbase[$inportnumber]=${basename}
# backup path if no better one is found later
Arrinctlpath[$inportnumber]=`echo -e " ${brblue}/sys/devices/platform/legoev3-ports/in$inportnumber/mode${brwhite}"`
pathColor="$green"
CURSENSOR="*" # set default so we know if it was unknown
findsensorctl ${inportnumber}
inlvl1=`echo -e /sys/class/lego-sensor/sensor${CURSENSOR}/`
Arrinsensormode[${inportnumber}]=`cat /sys/class/lego-sensor/sensor${CURSENSOR}/mode`
if [ -r /sys/class/lego-sensor/sensor${CURSENSOR}/fw_version ]; then
Arrinfw[${inportnumber}]=`cat /sys/class/lego-sensor/sensor${CURSENSOR}/fw_version`
else
Arrinfw[${inportnumber}]=" "
fi
if [ -r /sys/class/lego-sensor/sensor${CURSENSOR}/poll_ms ]; then
Arrinpoll[${inportnumber}]=`cat /sys/class/lego-sensor/sensor${CURSENSOR}/poll_ms 2>/dev/null`
else
Arrinpoll[${inportnumber}]=" "
fi
# if unknown then red else green:
if [ "${CURSENSOR:0:1}" == "?" ];
then pathColor="$brred" ;
else pathColor="$green" ;
fi
Arrinctlpath[$inportnumber]=`echo -e ${pathColor}${inlvl1}$brwhite`
fi
echo -en "\r|"
done
#
# findstr=`find /sys/devices/platform/legoev3-ports/ -maxdepth 2 -name "in[1234]\:*" -o -name "out[ABCD]*" -name "*\:*"`
findstr=`/bin/ls -d /sys/class/tacho-motor/tacho-motor*/ 2>/dev/null` # before kernel 3.16.1-6
if [ $? == 0 ]; then
findstr=`/bin/ls -d /sys/class/tacho-motor/tacho-motor*/`
else # at kernel 3.16.1-6 and later
if [ $? == 0 ]; then
findstr=`/bin/ls -d /sys/class/lego-port/port*/out* 2>/dev/null`
else # at kernel 3.16.1-8 and later
set +e
findstr=`/bin/ls -d /sys/class/*motor/motor*/ 2>/dev/null`
findled=`/bin/ls -d /sys/class/leds/ev3::out*/ 2>/dev/null`
set -e
fi
fi
set +x
# sofar found 3 motor types:
# /bin/ls -ld /sys/class/dc-motor/motor*/ 2>/dev/null
# /bin/ls -ld /sys/class/tacho-motor/motor*/ 2>/dev/null
# /bin/ls -ld /sys/class/servo-motor/motor*/ 2>/dev/null
#
# should include: /sys/class/leds/ev3::out%
# need to declare the arrays (Otherwise the index e.g. C is reported unbound..)
declare -A Arroutbase=( [A]=a [B]=b [C]=c [D]=d )
declare -A Arroutdir=( [A]=a [B]=b [C]=c [D]=d )
declare -A Arroutport=( [A]=a [B]=b [C]=c [D]=d )
declare -A Arroutctlpath=( [A]=a [B]=b [C]=c [D]=d )
declare -A Arrouttype=( [A]=a [B]=b [C]=c [D]=d )
declare -A Arroutrun=( [A]=a [B]=b [C]=c [D]=d )
declare -A Arroutstate=( [A]=a [B]=b [C]=c [D]=d )
declare -A Arroutdrvmode=( [A]=a [B]=b [C]=c [D]=d )
declare -A Arroutdrvstate=([A]=a [B]=b [C]=c [D]=d )
#
#
for p in ${findstr} ${findled}
do
echo -en "\r/"
# set -x
# determine motortype
basename="`basename $p`"
dirname="`dirname $p`"
# echo "Processing \"$p\" dir: $dirname base: $basename "
dir3="`basename ${dirname}`" # last-but-one node in path
if stringcontains '::' "${basename}" ; then # led-type
# echo "================== got :: in ${basename}"
porttype1="${basename%%::*}" # double ::
porttype2="${p#*::}"
portname="${porttype2:0:4}"
else
porttype1="${basename%%:*}"
porttype2="${p#*:}"
portname="`cat $p/address`"
fi
#
outportletter=${portname:3:1} # out%
#
driver_mode="`cat /sys/devices/platform/legoev3-ports/lego-port/port*/${portname}*/../mode`"
set +e
findinport "$portname"
driver_status="`cat /sys/devices/platform/legoev3-ports/lego-port/${legoport}/status`"
set -e
#
Arroutdrvmode[$outportletter]=${driver_mode}
Arroutdrvstate[$outportletter]=${driver_status}
haveportsout="$haveportsout $outportletter"
Arroutbase[$outportletter]=${basename}
Arroutdir[$outportletter]="/sys/devices/platform/legoev3-ports/lego-port/"${legoport}
Arroutport[$outportletter]=${portname} # out%
inlvl1=`echo $p`
Arroutctlpath[$outportletter]=`echo -e ${green}${inlvl1}$brwhite`
if [ "${dir3}" == "dc-motor" ]; then
Arrouttype[$outportletter]="dc-motor"
Arroutrun[$outportletter]="`cat ${inlvl1}/duty_cycle`"
Arroutstate[$outportletter]="`cat ${inlvl1}/command`"
elif [ "${dir3}" == "leds" ]; then
Arrouttype[$outportletter]="led"
Arroutrun[$outportletter]="`cat ${inlvl1}/brightness`"
showtrigger "`cat ${inlvl1}/trigger`"
Arroutstate[$outportletter]="${currenttrigger}"
else
Arrouttype[$outportletter]="`cat ${inlvl1}/driver_name`"
Arroutrun[$outportletter]="---" # DROPPED at 3.16.7-ckt9 "`cat ${inlvl1}/run`"
Arroutstate[$outportletter]="`cat ${inlvl1}/state`"
fi
echo -en "\r\\"
done
set +x
echo -en " \r \r"
# take screenwidth into account:
scrwidth=`tput cols`
if [ $scrwidth -ge 130 ]; then
# width >= tgt-width
# Report input ports:
if [ "$haveportsin" != "" ]; then
echo -e "\n$brblue Input ports recognized:\n"
printf "$brblue%4s %-10s %-10s %-15s %-4s %-21s %-20s %s\n$brwhite" "Port" "drv-mode" "drv-state" "sensor mode" "Poll" "fwVer" "Basename" "Control directory"
for i in $haveportsin; do
printf "%4s %-10s %-10s %-15s %-4s %-21s %-20s %s\n" "${Arrinport[$i]}" " " " " "${Arrinsensormode[$i]}" "${Arrinpoll[$i]}" "${Arrinfw[$i]}" " " "${Arrinctlpath[$i]}"
printf "%4s %-10s %-24s %-01s %-4s %-21s %-20s ${green}%-25s$brwhite\n\n" " " "${Arrindrvmode[$i]}" "${Arrindrvstate[$i]}" " " " " " " "${Arrinbase[$i]}" "${Arrindir[$i]}"
done
fi
# Report motorports:
if [ "$haveportsout" != "" ]; then
echo -e "\n$brblue output ports recognized:\n"
printf "$brblue%4s %-10s %-10s %-16s %-3s %-21s %-20s %s\n$brwhite" "Port" "drv-mode" "drv-state" "Type" "Run" "State/Trig" "Basename" "Control directory"
for i in $haveportsout; do # %4s -10 -10 -16 -3 -21 -20 %s
printf "%4s %-10s %-10s %-16s %-3s %-21s %-20s %s\n" "${Arroutport[$i]}" " " " " "${Arrouttype[$i]}" "${Arroutrun[$i]}" "${Arroutstate[$i]}" "${Arroutbase[$i]}" "${Arroutctlpath[$i]}"
printf "%4s %-10s %-25s %-01s %-3s %-21s %-20s ${green}%-25s$brwhite\n\n" " " "${Arroutdrvmode[$i]}" "${Arroutdrvstate[$i]}" " " " " " " " " "${Arroutdir[$i]}"
done
fi
# width >= tgt-width
else
# width < tgt-width
# Report input ports:
if [ "$haveportsin" != "" ]; then
echo -e "\n$brblue Input ports recognized:\n"
printf "$brblue%4s %-10s %-10s %-14s %-4s %-11s %-25s \n$brwhite" "Port" "drv-mode" "drv-state" "sensor mode" "Poll" "fwVer" "Basename"
# "Control directory"
for i in $haveportsin; do
printf "%4s %-10s %-10s %-15s %-3s %-11s\n" "${Arrinport[$i]}" " " " " "${Arrinsensormode[$i]}" "${Arrinpoll[$i]}" "${Arrinfw[$i]}"
printf "%4s %-10s %-10s %-15s %-3s %-11s %-25s ${green}%s$brwhite\n" " " "${Arrindrvmode[$i]}" "${Arrindrvstate[$i]}" " " " " " " "${Arrinbase[$i]}"
printf "Mod-ctl ${green}%s$brwhite\n" "${Arrindir[$i]}"
printf "Dev-ctl ${green}%s$brwhite\n\n" "${Arrinctlpath[$i]}"
done
fi
# Report motorports:
if [ "$haveportsout" != "" ]; then
echo -e "\n$brblue output ports recognized:\n"
printf "$brblue%4s %-10s %-10s %-15s %-3s %-11s %-25s\n$brwhite" "Port" "drv-mode" "drv-state" "Type" "Run" "State/Trig" "Basename"
# "Control directory"
for i in $haveportsout; do
printf "%4s %-10s %-10s %-15s %3s %-11s %-25s\n" "${Arroutport[$i]}" " " " " "${Arrouttype[$i]}" "${Arroutrun[$i]}" "${Arroutstate[$i]}" "${Arroutbase[$i]}"
printf "%4s %-10s %-10s %-15s %3s %-11s %-25s\n" " " "${Arroutdrvmode[$i]}" "${Arroutdrvstate[$i]}" " " " " " " " "
printf "Mod-ctl ${green}%-25s$brwhite\n" "${Arroutdir[$i]}"
printf "Dev-ctl ${green}%-25s$brwhite\n\n" "${Arroutctlpath[$i]}"
done
fi
# width < tgt-width
fi