/ scripts / pyvcp_demo
pyvcp_demo
  1  #!/bin/bash
  2  
  3  # This file is used for scripts that demonstrate pyvcp
  4  # The scripts typically use an extension of .demo
  5  # 1) start realtime
  6  # 2) run pyvcp with supplied xml and hal files
  7  # 3) stop realtime
  8  
  9  # Copyright: 2014
 10  # Author:    Dewey Garrett <dgarrett@panix.com>
 11  #
 12  # This program is free software; you can redistribute it and/or modify
 13  # it under the terms of the GNU General Public License as published by
 14  # the Free Software Foundation; either version 2 of the License, or
 15  # (at your option) any later version.
 16  #
 17  # This program is distributed in the hope that it will be useful,
 18  # but WITHOUT ANY WARRANTY; without even the implied warranty of
 19  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 20  # GNU General Public License for more details.
 21  #
 22  # You should have received a copy of the GNU General Public License
 23  # along with this program; if not, write to the Free Software
 24  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 25  prog=$(basename $0)
 26  
 27  function usage () {
 28    cat <<EOF
 29  Usage:
 30        $prog filename1.xml filename2.hal [compname]
 31  
 32  If not provided, use compname == pyvcp
 33  
 34  EOF
 35    exit 1
 36  } ;# usage
 37  
 38  function popup () {
 39    msg="$*"
 40    wish <<EOF &
 41    wm withdraw .
 42    tk_messageBox \
 43      -title "$prog" \
 44      -message "$msg" \
 45      -icon error \
 46      -type ok
 47    destroy .
 48  EOF
 49  } ;# popup
 50  
 51  # begin----------------------------------------------------
 52  REALTIME=$(linuxcnc_var REALTIME)
 53  $REALTIME status >/dev/null
 54  status=$?
 55  if [ $status = 0 ] ; then
 56    msg="$prog: Realtime is already active"
 57    echo "$msg"
 58    popup "$msg"
 59    exit 1
 60  else
 61    $REALTIME start
 62  fi
 63  
 64  case $# in
 65    0|1) usage;;
 66      2) XMLFILE=${1}
 67         HALFILE=${2}
 68         COMPNAME=pyvcp
 69         ;;
 70      3) XMLFILE=${1}
 71         HALFILE=${2}
 72         COMPNAME=${3}
 73         ;;
 74      *) usage;;
 75  esac
 76  
 77  if [ ! -z "$debug" ] ; then
 78    echo debug=$debug
 79    echo REALTIME=$REALTIME
 80    echo XMLFILE=$XMLFILE
 81    echo HALFILE=$HALFILE
 82    echo COMPNAME=$COMPNAME
 83  fi
 84  
 85  cd $(dirname "$XMLFILE") ;# to allow relative includes
 86  pyvcp -c $COMPNAME $XMLFILE &
 87  pyvcpjob=$!
 88  ct=0
 89  # wait for pins to be created before creating signals
 90  while true ; do
 91     halcmd show|grep "${COMPNAME}.*ready" >/dev/null 2>&1
 92     status=$?
 93     sleep 1
 94     if [ $status == 0 ] ; then
 95        #echo "ready after ct=$ct"
 96        break
 97     fi
 98     if [ $ct -ge 10 ] ; then
 99        echo "$0: hal-pyvcp startup failed"
100        exit 1
101     fi
102     ct=$(($ct+1))
103  done
104  
105  cd $(dirname "$HALFILE") ;# so HALFILE can source relative files
106  
107  halmsg=/tmp/pyvcp_demo_halcmd.err
108  >|$halmsg
109  halcmd -f $HALFILE >$halmsg 2>&1
110  halstatus=$?
111  if [ $halstatus != 0 ] ; then
112    IFS=$'\n' # split lines
113    newmsg=
114    # put extra blank line between lines of output
115    for line in $(cat $halmsg) ; do
116  newmsg="$newmsg
117  $line
118  "
119    done
120    popup "$newmsg"
121    kill $pyvcpjob
122    halrun -U
123    exit 1
124  fi
125  wait $pyvcpjob
126  halrun -U
127  exit 0