|
| 1 | +#!/bin/bash -ue |
| 2 | +# Script_name : tryPF |
| 3 | +# Author : Bert Lindeman |
| 4 | +# Description : Set an output port for Power-Functions led or motor |
| 5 | +# Version : v1.3 |
| 6 | +# Date : 2015-01-16 |
| 7 | +# Usage : tryPF <portletter> <led | motor> |
| 8 | +# default is "tryPF A led" |
| 9 | +# |
| 10 | +# Notes : This script is used by showactivity |
| 11 | +# |
| 12 | +# Copyright : 2015 Bert Lindeman |
| 13 | +# License : GPL-3.0+ |
| 14 | +# |
| 15 | +# |
| 16 | +# |
| 17 | +# Changelog |
| 18 | +# Date By Description |
| 19 | +# |
| 20 | +# |
| 21 | +# |
| 22 | +. /usr/local/bin/bert_ev3dev_functions |
| 23 | +# |
| 24 | +showattr() { |
| 25 | + for f in ${1} |
| 26 | + do |
| 27 | + attr=`cat $f` # get the field and then do echo to get uevent on one line |
| 28 | + printf "%10s %-20s %s\n" " " "`basename $f`" "`echo ${attr}`" |
| 29 | + done |
| 30 | +} |
| 31 | + |
| 32 | +showmotor_attr() { |
| 33 | + ctlpath=${1:-"??"} |
| 34 | + set +x |
| 35 | + |
| 36 | + echo -en " Current directory: `pwd`; " |
| 37 | + echo -e " Potential mode: `cat modes`" |
| 38 | + |
| 39 | +# printf format to ESCAPE a string: |
| 40 | +# %q quote the argument in a way that can be reused as shell input |
| 41 | + echo " Switch to ctlpath \"${ctlpath}\"" |
| 42 | + cd `printf "%q" "${ctlpath}"` |
| 43 | + echo -e " Port:${port} ${motordir} controldir: ${controldir}" |
| 44 | + echo -e " attributes:" |
| 45 | + attr=`find ${controldir}/ -maxdepth 1 -type f` |
| 46 | + showattr "$attr" |
| 47 | +} |
| 48 | + |
| 49 | +findPortnumber () { |
| 50 | + # find on which port* the port_name is controlled |
| 51 | + local port portctl |
| 52 | + local port_name |
| 53 | + local intl_port_dir intl_port_name |
| 54 | + local curmodes wantmode |
| 55 | + port="${1}" |
| 56 | + portctl=/sys/class/lego-port/port*/port_name |
| 57 | + CTL_PATH="" |
| 58 | + # set -x |
| 59 | + for IOPORT in ${portctl} |
| 60 | + do |
| 61 | + port_name="`cat \"${IOPORT}\"`" |
| 62 | + intl_port_dir="`dirname ${IOPORT}`" |
| 63 | + intl_port_name="`basename ${intl_port_dir}`" |
| 64 | + if [ "${port_name}" == "${port}" ]; then |
| 65 | + echo "port \"${port}\" found on \"${intl_port_name}\"" |
| 66 | + # ls -l ${intl_port_dir}/ |
| 67 | + curmodes=`cat ${intl_port_dir}/modes` |
| 68 | + wantmode="rcx-motor" |
| 69 | + if listcontains "$curmodes" "$wantmode" ; then |
| 70 | + CTL_PATH="${intl_port_dir}" |
| 71 | + fi |
| 72 | + break |
| 73 | + fi |
| 74 | + done |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +################# mainline ################# |
| 79 | +port="${1:-outA}" |
| 80 | +type="${2:-led}" # led or motor |
| 81 | + |
| 82 | +# set -x |
| 83 | +if [ "$type" == "motor" ]; then |
| 84 | + echo "# PF-motor ###################################################################" |
| 85 | + |
| 86 | + findPortnumber ${port} |
| 87 | + cd ${CTL_PATH} |
| 88 | + motorbase="`basename ${CTL_PATH}`" |
| 89 | + # echo -e "currentdir=`pwd`" |
| 90 | + |
| 91 | + motormode="rcx-motor" |
| 92 | + motordir="dc-motor" |
| 93 | + |
| 94 | + echo ${motormode} > mode |
| 95 | + # sleep 0.75s |
| 96 | + found=0 |
| 97 | + |
| 98 | + while : ; do |
| 99 | + [[ -e `find /sys/devices/platform/legoev3-ports/lego-port/${motorbase}/${port}*/${motordir} -name "motor*"` ]] && found=1 && break |
| 100 | + echo "Pausing until led / motor directory exists." |
| 101 | + sleep 1 |
| 102 | + done |
| 103 | + |
| 104 | +# realpath: |
| 105 | +# /sys/devices/platform/legoev3-ports/lego-port/port6/outC:rcx-motor/dc-motor/motor10 |
| 106 | + controldir=`find /sys/devices/platform/legoev3-ports/lego-port/${motorbase}/${port}*/${motordir} -name "motor*"` |
| 107 | + |
| 108 | + if [ ${found} == 1 ] && [ -d ${controldir} ]; then |
| 109 | + showmotor_attr ${controldir} |
| 110 | + else |
| 111 | + echo -e "Cannot find \"${controldir}\"" |
| 112 | + find /sys/class/${motordir} -name "motor*" |
| 113 | + fi |
| 114 | + set _x |
| 115 | +elif [ "$type" == "led" ]; then |
| 116 | + echo "# PF-led ###################################################################" |
| 117 | + |
| 118 | + findPortnumber ${port} |
| 119 | + cd ${CTL_PATH} |
| 120 | + |
| 121 | + motormode="rcx-led" |
| 122 | + motordir="leds" |
| 123 | + |
| 124 | + echo ${motormode} > mode |
| 125 | + #sleep 0.75s |
| 126 | + found=0 |
| 127 | + |
| 128 | + while : ; do |
| 129 | + [[ -e `find /sys/class/${motordir} -name "*${port}*"` ]] && found=1 && break |
| 130 | + echo "Pausing until led / motor directory exists." |
| 131 | + sleep 1 |
| 132 | + done |
| 133 | + |
| 134 | + controldir=`find /sys/class/${motordir} -name "*${port}*"` |
| 135 | + if [ ${found} == 1 ] && [ -d ${controldir} ]; then |
| 136 | + showmotor_attr ${controldir} |
| 137 | + else |
| 138 | + echo -e "Cannot find \"${controldir}\"" |
| 139 | + find /sys/class/${motordir} -name "*${port}*" |
| 140 | + fi |
| 141 | +fi |
| 142 | +# |
| 143 | +exit |
0 commit comments