#! /bin/csh -f
#
# vlan_control_script
# $Author: dave $
# $Revision: 1.1.1.1 $
#
# args:
#	open							<or>
#	close							<or>
#	get_cell				temp_file_name	<or>
#	set_cell smpte_timecode					<or>
#	read     smpte_timecode vfr_mode_string temp_file_name	<or>
#	write    smpte_timecode vfr_mode_string	temp_file_name	<or>
# 
# This script controls a VLAN device and acts as the interface between
# nTITLE and the Video Framer and tape decks.
# Assumes the video framer software has been installed in /usr/video/vfr.
# 10/17/91	Keith Gordon

# Set the following variable to one of ntsc, NTSC, 525, pal, PAL, 625,
# depending on whether you have an NTSC or PAL network...
set VLAN_MODE = ntsc

# Set the following variable to the node ID of the VLAN device to control
set NODE_ID = 1

# Set the following variable to false to just dump to/from the Video Framer
# without doing any VLAN control, and without having the 'Framer grab video.
# (Good for testing, or creating a custom setup)
set USE_VLAN = true

# $CMD is set equal to the program used to issue vlan command.
# To use a different control program, change the following lines and
# the usages in do_write and do_read: (below)
set INIT = /usr/video/vfr/bin/vfr_vlan_init
set CMD  = /usr/video/vfr/bin/vfr_vlan_cmd

# Go to the correct action based on 1st arg to script.
switch( $1 )
    case "open":
        goto do_open
    case "close":
        goto do_close
    case "get_cell"
        goto do_getcell
    case "set_cell"
        goto do_setcell
   case "read":
        goto do_read
    case "write":
        goto do_write
endsw

################################################################################
# Initialize the VLAN to ntsc mode.
# args: none
do_open:
    if( $USE_VLAN == "true" ) then
        $INIT -q $VLAN_MODE
        $CMD EF
	$CMD ND$NODE_ID
    endif
    exit 0


################################################################################
# Perform any exit time clean up.  For VLAN, there's nothing to
# do.  For some user created stuff, there might be...
# args: none
do_close:
    if( $USE_VLAN == "true" ) then
    endif
    exit 0


################################################################################
# Query the vlan for the current frame.  The calling process will
# read the cell from the sdout of this script.
# args: filename
do_getcell:
    if( $USE_VLAN == "true" ) then
        $CMD EF
        $CMD ND$NODE_ID
        `$CMD LR > $2`
    else
        `echo 0 > $2`
    endif
    exit 0


################################################################################
# Send the vlan to a certain location.
# args: smpte time code
# This is commented out, since with a tape device, it results in uneeded
# tape motion.
do_setcell:
#   if( $USE_VLAN == "true" ) then
#       $CMD EF
#       $CMD ND$NODE_ID
#       $CMD GT$2
#   endif
    exit 0


################################################################################
# Play back a tape and grab a frame
# args: smpte time code -1, 'Framer mode string, (optional) filename
do_read:
    # Get the Video Framer mode - defaults to vfr_ntsc
    if( $3 == "" ) then
	set VFR_MODE = "vfr_ntsc"
    else
	set VFR_MODE = $3
    endif

    if( $USE_VLAN == "true" ) then
#       Set up the edit... 
        $CMD EF
	$CMD ND$NODE_ID
        $CMD CL
        $CMD TSV
        $CMD SI$2
        $CMD SD00:00:00:10
        $CMD ECGP

#       Set for frame accurate grab...
#       Assumes a pre-roll of 5:00  See Video Framer manual...
        $CMD CO
        $CMD SC00:00:04:29

        $CMD RV

#       Upon receiving a trigger, grab the frame and dump to file or stdout.  
        vfr_control -r -g -t -b $VFR_MODE $4
    else
#       Immediately grab a frame of video and dump it to file or stdout.
#	vfr_control -r -g -b $VFR_MODE $4

#	Dump VideoFramer buffer to file or stdout w/o doing a grab.
	vfr_control -r    -b $VFR_MODE $4
    endif

    exit 0


################################################################################
# A script to put a frame into the VFramer and record it using the vlan.
# Assumes the Video Framer software has been installed in /usr/video/vfr.
# args: smpte time code (Allow for 5 second pre-roll...), 'Framer mode string,
# (optional) file name
do_write:
    # Get the Video Framer mode - defaults to vfr_ntsc
    if( $3 == "" ) then
	set VFR_MODE = "vfr_ntsc"
    else
	set VFR_MODE = $3
    endif

#   Fill the Video Framer from file or stdin (if $4 empty)
    vfr_control -w -b $VFR_MODE $4

#   Tell the VTR to record the frame...
    if( $USE_VLAN == "true" ) then
        $CMD EF
	$CMD ND$NODE_ID
        $CMD CL
        $CMD TSV
        $CMD SI$2
        $CMD SD1
        $CMD ECPS
        $CMD PF

#       Wait for the deck to finish the edit before exiting so that the next
#       frame won't interfere with this one...
#       Note that the response string "PAUSE OK" may change if the control
#       program used ($CMD) is changed...
        set ans = `$CMD SR`
        while ( "$ans" != "PAUSE OK")
            sleep 1
            set ans = `$CMD SR`
        end
    endif

    exit 0

