-
Notifications
You must be signed in to change notification settings - Fork 5
/
tryPF
143 lines (125 loc) · 3.75 KB
/
tryPF
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
#!/bin/bash -ue
# Script_name : tryPF
# Author : Bert Lindeman
# Description : Set an output port for Power-Functions led or motor
# Version : v1.3
# Date : 2015-01-16
# Usage : tryPF <portletter> <led | motor>
# default is "tryPF A led"
#
# Notes : This script is used by showactivity
#
# Copyright : 2015 Bert Lindeman
# License : GPL-3.0+
#
#
#
# Changelog
# Date By Description
# 20160103 BL adapt to image 20151230 (change filenames)
#
#
. /usr/local/bin/bert_ev3dev_functions
#
showattr() {
for f in ${1}
do
attr=`cat $f` # get the field and then do echo to get uevent on one line
printf "%10s %-20s %s\n" " " "`basename $f`" "`echo ${attr}`"
done
}
showmotor_attr() {
ctlpath=${1:-"??"}
set +x
echo -en " Current directory: `pwd`; "
echo -e " Potential mode: `cat modes`"
# printf format to ESCAPE a string:
# %q quote the argument in a way that can be reused as shell input
echo " Switch to ctlpath \"${ctlpath}\""
cd `printf "%q" "${ctlpath}"`
echo -e " Port:${port} ${motordir} controldir: ${controldir}"
echo -e " attributes:"
attr=`find ${controldir}/ -maxdepth 1 -type f`
showattr "$attr"
}
findPortnumber () {
# find on which port* the address is controlled
local port portctl
local address
local intl_port_dir intl_address
local curmodes wantmode
port="${1}"
portctl=/sys/class/lego-port/port*/address
CTL_PATH=""
# set -x
for IOPORT in ${portctl}
do
address="`cat \"${IOPORT}\"`"
intl_port_dir="`dirname ${IOPORT}`"
intl_address="`basename ${intl_port_dir}`"
if [ "${address}" == "${port}" ]; then
echo "port \"${port}\" found on \"${intl_address}\""
# ls -l ${intl_port_dir}/
curmodes=`cat ${intl_port_dir}/modes`
wantmode="rcx-motor"
if listcontains "$curmodes" "$wantmode" ; then
CTL_PATH="${intl_port_dir}"
fi
break
fi
done
}
################# mainline #################
port="${1:-outA}"
type="${2:-led}" # led or motor
# set -x
if [ "$type" == "motor" ]; then
echo "# PF-motor ###################################################################"
findPortnumber ${port}
cd ${CTL_PATH}
motorbase="`basename ${CTL_PATH}`"
# echo -e "currentdir=`pwd`"
motormode="rcx-motor"
motordir="dc-motor"
echo ${motormode} > mode
# sleep 0.75s
found=0
while : ; do
[[ -e `find /sys/devices/platform/legoev3-ports/lego-port/${motorbase}/${port}*/${motordir} -name "motor*"` ]] && found=1 && break
echo "Pausing until led / motor directory exists."
sleep 1
done
# realpath:
# /sys/devices/platform/legoev3-ports/lego-port/port6/outC:rcx-motor/dc-motor/motor10
controldir=`find /sys/devices/platform/legoev3-ports/lego-port/${motorbase}/${port}*/${motordir} -name "motor*"`
if [ ${found} == 1 ] && [ -d ${controldir} ]; then
showmotor_attr ${controldir}
else
echo -e "Cannot find \"${controldir}\""
find /sys/class/${motordir} -name "motor*"
fi
set _x
elif [ "$type" == "led" ]; then
echo "# PF-led ###################################################################"
findPortnumber ${port}
cd ${CTL_PATH}
motormode="rcx-led"
motordir="leds"
echo ${motormode} > mode
#sleep 0.75s
found=0
while : ; do
[[ -e `find /sys/class/${motordir} -name "*${port}*"` ]] && found=1 && break
echo "Pausing until led / motor directory exists."
sleep 1
done
controldir=`find /sys/class/${motordir} -name "*${port}*"`
if [ ${found} == 1 ] && [ -d ${controldir} ]; then
showmotor_attr ${controldir}
else
echo -e "Cannot find \"${controldir}\""
find /sys/class/${motordir} -name "*${port}*"
fi
fi
#
exit