#!/bin/sh 
################################################################################
# Device control script to read/record images using the Centaur's VLAN
# transmitter.
#
# 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
#	-d edit_length	Number of frames to lay down.  This can be between
#			    0 and 2.
#
#   $Date: 94/01/02 17:03:30 $
#   $Revision: 11.1 $
#
#   (/bin/sh syntax...)
################################################################################

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

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

################################################################################
# Parse the command line args
################################################################################
while getopts rwf:s:i:d: 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
	d)	DURATION=$OPTARG;;	# # of frames to lay down
	\?)	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=15		# Number seconds to wait for deck to finish edits. 
CENTAUR_HOME=$PANDEMONIUM_HOME
VLAN_CMD="$CENTAUR_HOME/drivers/devices/chyron-centaurTool"	# Binary to send the VLAN commands
FLIP_CMD="$CENTAUR_HOME/drivers/devices/chyron-centaurTool"	# Binary to control flipbook machine
NODE_ID=1					    	# Node ID of the VLAN 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  Duration
##	    $4  VLAN node ID
##	    $5  Edit timeout
##	    $6  Wait string
##	    $7  Timout error string
##	    
################################################################################
################################################################################
RecordFrame_VLAN( )
{
    ############################################################################
    # Check calling syntax...
    ############################################################################
    if [ "$#" -ne "8" ]
    then
	echo "Bad call to RecordFrame_VLAN( $* )"
	return 1
    fi

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

    # Show the first page of the flipbook...
    $__flip -F DISPLAY1

    # Get the duration as a SMPTE string...
    NumToSMPTE $FRAMES_PER_SECOND $__duration ":"
    __smpte_duration=$_RESULT

    echo "EF" | $__send -v -

    # Turn echo off and set the VLAN node, then set up the edit
    echo "ND$__node_id \nCL \nTSV \nSI$__inpoint \nSD$__smpte_duration" | $__send -v -
        
    # We don't need to set up the coincidence trigger since the Centaur board
    # does this for us based on the VLAN preroll...

# 'ECGI' is slightly faster for a Sony EVO9650, but 'ECGP' is more common... 
# You may be able to tweek this to increase throughput on your tape deck... 
# NOTE: Some decks have problems with 'ECGP' at the end of edits...
#   echo "ECGP" | $__send -v -
#   echo "ECGI" | $__send -v -
    echo "ECST" | $__send -v -

    echo "PF" | $__send -v -
    
    # Set up the flipbook machine to play back the desired frames when it
    # gets the VLAN trigger signal...
    $__flip -F TDISPLAY2
    
    # 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 "7" ]
    then
	echo "Bad call to ReadFrame_VLAN()"
	return 1
    fi

    ############################################################################
    # Read the args...
    ############################################################################
    __send=$1
    __flip=$2
    __inpoint=$3
    __node_id=$4
    __edit_timeout=$5
    __wait_string=$6
    __waiterror_string=$7
    
    # Set the flipbook to frame 1
    $__flip -F DISPLAY1

    # Turn echo off and set the VLAN node
    echo "EF \nND$__node_id" | $__send -v -

    # 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
    echo "CL \nTS \nCO \nPR05:00 \nSI$__inpoint \nSD4" | $__send -v -
    
    # We don't need to set up the coincidence trigger since the Centaur board
    # does this for us based on the VLAN preroll...
    
# 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.
#   echo "ECGP" | $__send -v -
#   echo "ECGI" | $__send -v -
    echo "ECST" | $__send -v -

    # Start the tape rolling...
    echo "RH" | $__send -v -
    
    # Tell the flipbook to grab a frame when it gets the signal...
    $__flip -F TGRAB1
    
    return 0
}

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

if [ $RW_MODE = "r" ] 
then
    # (Duration is unimportant for frame grabbing, since the centaur board
    #  can't do double-frame grabs...)
    # Calculate the SMPTE code for the desired frame...
    NumToSMPTE $FRAMES_PER_SECOND $CELL ":"
    INPOINT=$_RESULT
    ReadFrame_VLAN $VLAN_CMD $FLIP_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
    if [ "$DURATION" -eq "0" ]
    then
	exit 0
    fi
    
    NumToSMPTE $FRAMES_PER_SECOND `expr $CELL - $DURATION + 1` ":"
    INPOINT=$_RESULT
    RecordFrame_VLAN $VLAN_CMD $FLIP_CMD $INPOINT $DURATION $NODE_ID $EDIT_TIMEOUT "Waiting for deck to report edit done..." "$ME: ERROR: Gave up waiting for deck to report edit done."
fi

exit $?

