#!/bin/sh 
################################################################################
# Device control script to read/record images using the Video Framer's VLAN
# transmitter
# 
# NOTE: The VLAN network must be initialized before this script is used
#
# args:	-r | w		Reading or recording a frame
#	-s start_cell	Start cell of animation (not used here)
#	-i current_cell	Current frame being read/recorded
#
#   $Date: 93/11/15 18:52:04 $
#   $Revision: 11.0 $
#
#   (/bin/sh syntax...)
################################################################################

################################################################################
# Usage message...
################################################################################
USAGE="usage: `basename $0` -r|w -s start_cell -i current_cell"

ME=`basename $0`
START_CELL=-1
CELL=-1

################################################################################
# Parse the command line args
################################################################################
while getopts rwf:s:i: flag
do
    case $flag in
	r | w)	RW_MODE=$flag;;		# Are we a post-write or pre-read script?
	f)	FILENAME=$OPTARG;;	# Filename not used in this script...
	s)	START_CELL=$OPTARG;;	# Start cell of entire animation
	i)	CELL=$OPTARG;;		# Cell # of current frame
	\?)	echo $USAGE
		exit 2;;
    esac
done

# Make sure we got the start and current cell numbers.
if [ $START_CELL = -1 -o $CELL = -1 ]
then
    echo $USAGE
    exit 2
fi

################################################################################
# Import some utilities for working with SMPTE codes and VLAN
################################################################################
. $PANDEMONIUM_HOME/util/SmpteUtilities 
. $PANDEMONIUM_HOME/util/VLANUtilities

################################################################################
################################################################################
##
## USER ADJUSTABLE VARIABLES
##
################################################################################
################################################################################
FRAMES_PER_SECOND=30	# Frame rate for creating SMPTE timecodes (25 for PAL)
EDIT_TIMEOUT=30		# Number seconds to wait for deck to finish edits. 
VLAN_CMD=/usr/video/vfr/bin/vfr_vlan_cmd    # Binary to send the VLAN commands
NODE_ID=1				    # Node ID of the device to control


################################################################################
################################################################################
## Output a frame by doing a VLAN single-frame edit.
##
## Returns: 0 if OK, 1 if error
##
## Args:    $1  Command to execute to issue a VLAN command
##	    $2  Inpoint
##	    $3  VLAN node ID
##	    $4  Edit timeout
##	    $5  Wait string
##	    $6  Timout error string
##	    
################################################################################
################################################################################
RecordFrame_VLAN( )
{
    ############################################################################
    # Check calling syntax...
    ############################################################################
    if [ "$#" -ne "6" ]
    then
	echo "Bad call to RecordFrame_VLAN()"
	return 1
    fi

    ############################################################################
    # Read the args...
    ############################################################################
    __send=$1
    __inpoint=$2
    __node_id=$3
    __edit_timeout=$4
    __wait_string=$5
    __waiterror_string=$6

    # Turn echo off and set the VLAN node
    $__send EF
    $__send ND$__node_id
    
    # Set up the edit
    $__send CL
    $__send TSV
    $__send SI$__inpoint
    $__send SD00:00:00:01
# Some decks have problems with the GP end condition, so we'll just stop at 
# the end of the edit.  You may be able to tweek this for better throughput
#   $__send ECGP
    $__send ECST
    
    # Do the edit
    $__send PF
    
    # Wait for the edit to finish so that the framebuffer won't
    # be messed with before the frame is recorded to tape...
    WaitForEdit $__send $__edit_timeout "$__waitstring"
    if [ $? = "0" ]
    then
	echo "$__waiterror_string"
	return 1
    fi

    return 0
}

################################################################################
################################################################################
## Read in a frame.
##
## Returns: 0 if OK, 1 if error
##
## Args:    $1  Command to execute to issue a VLAN command
##	    $2  Inpoint
##	    $3  VLAN node ID
##	    $4  Edit timeout
##	    $5  Wait string
##	    $6  Timout error string
##	    
################################################################################
################################################################################
ReadFrame_VLAN( )
{

    ############################################################################
    # Check calling syntax...
    ############################################################################
    if [ "$#" -ne "6" ]
    then
	echo "Bad call to ReadFrame_VLAN()"
	return 1
    fi

    ############################################################################
    # Read the args...
    ############################################################################
    __send=$1
    __inpoint=$2
    __node_id=$3
    __edit_timeout=$4
    __wait_string=$5
    __waiterror_string=$6

    # Turn echo off and set the VLAN node
    $__send EF
    $__send ND$__node_id
    
    # Wait for the tape deck to be ready...
    WaitForEdit $__send $__edit_timeout "$__waitstring"
    if [ $? = "0" ]
    then
	echo "$__waiterror_string"
	return 1
    fi
    
    # Set up the edit
    $__send CL
    $__send TSV
    $__send SI$__inpoint
    $__send SD00:00:01:00
    $__send PR00:00:05:00    
    
    # Set for frame accurate grab...
    # Assumes a pre-roll of 5:00  See Video Framer manual...
    $__send SC00:00:04:29
    $__send CO

# Some decks have problems with the GP end condition, so we'll just stop at 
# the end of the edit.  You may be able to tweek this for better throughput
#   $__send ECGP
    $__send ECST

    # Start the tape rolling...
    $__send RV
    
    return 0
}

################################################################################
################################################################################
## The main entry...
################################################################################
################################################################################

if [ $RW_MODE = "r" ] 
then
    # Calculate the SMPTE code for the desired frame - 1.
    NumToSMPTE $FRAMES_PER_SECOND `expr $CELL - 1` ":"
    INPOINT=$_RESULT
    ReadFrame_VLAN "$VLAN_CMD" $INPOINT $NODE_ID $EDIT_TIMEOUT "Waiting for deck to report edit done..." "$ME: ERROR: Gave up waiting for deck to report edit done."
else
    NumToSMPTE $FRAMES_PER_SECOND $CELL ":"
    INPOINT=$_RESULT
    RecordFrame_VLAN "$VLAN_CMD" $INPOINT $NODE_ID $EDIT_TIMEOUT "Waiting for deck to report edit done..." "$ME: ERROR: Gave up waiting for deck to report edit done."
fi

exit $?

