summaryrefslogtreecommitdiff
path: root/scripts/pyvcp_demo
blob: 003ec4d9c5230348b5a955110a7609bec1e66f0f (plain)
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
#!/bin/bash

# This file is used for scripts that demonstrate pyvcp
# The scripts typically use an extension of .demo
# 1) start realtime
# 2) run pyvcp with supplied xml and hal files
# 3) stop realtime

prog=$(basename $0)

function usage () {
  cat <<EOF
Usage:
      $prog filename1.xml filename2.hal [compname]

If not provided, use compname == pyvcp

EOF
  exit 1
} ;# usage

function popup () {
  msg="$*"
  wish <<EOF &
  wm withdraw .
  tk_messageBox \
    -title "$prog" \
    -message "$msg" \
    -icon error \
    -type ok
  destroy .
EOF
} ;# popup

# begin----------------------------------------------------
REALTIME=$(linuxcnc_var REALTIME)
$REALTIME status >/dev/null
status=$?
if [ $status = 0 ] ; then
  msg="$prog: Realtime is already active"
  echo "$msg"
  popup "$msg"
  exit 1
else
  $REALTIME start
fi

case $# in
  0|1) usage;;
    2) XMLFILE=${1}
       HALFILE=${2}
       COMPNAME=pyvcp
       ;;
    3) XMLFILE=${1}
       HALFILE=${2}
       COMPNAME=${3}
       ;;
    *) usage;;
esac

if [ ! -z "$debug" ] ; then
  echo debug=$debug
  echo REALTIME=$REALTIME
  echo XMLFILE=$XMLFILE
  echo HALFILE=$HALFILE
  echo COMPNAME=$COMPNAME
fi

cd $(dirname "$XMLFILE") ;# to allow relative includes
pyvcp -c $COMPNAME $XMLFILE &
pyvcpjob=$!
ct=0
# wait for pins to be created before creating signals
while true ; do
   halcmd show|grep "${COMPNAME}.*ready" >/dev/null 2>&1
   status=$?
   sleep 1
   if [ $status == 0 ] ; then
      #echo "ready after ct=$ct"
      break
   fi
   if [ $ct -ge 10 ] ; then
      echo "$0: hal-pyvcp startup failed"
      exit 1
   fi
   ct=$(($ct+1))
done

cd $(dirname "$HALFILE") ;# so HALFILE can source relative files
halmsg=$(halcmd -f $HALFILE 2>&1)
halstatus=$?
if [ $halstatus != 0 ] ; then
  IFS=$'\n' # split lines
  newmsg=
  # put extra blank line between lines of output
  for line in $halmsg ; do
newmsg="$newmsg
$line
"
  done
  popup "$newmsg"
  kill $pyvcpjob
  exit 1
fi

wait $pyvcpjob
halrun -U
exit 0