#!/bin/sh 
################################################################################
# Device control script to record images using the Avanzar's VLAN transmitter.
# The Avanzar can't grab, so this script does not do reads.
#
# args:	-w		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
#			    the number of frames in the Avanzar flipbook.
#
#   $Date: 93/11/15 18:51:59 $
#   $Revision: 11.0 $
#
#   (/bin/sh syntax...)
################################################################################

################################################################################
# Usage message...
################################################################################
USAGE="usage: `basename $0` -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 wf:s:i:d: flag
do
    case $flag in
	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=30		# Number seconds to wait for deck to finish edits. 
AVANZAR_HOME=/usr/avanzar	# Where the Avanzar software lives
VLAN_CMD=$AVANZAR_HOME/bin/vcmd	# Binary to send the VLAN commands
FLIP_CMD=$AVANZAR_HOME/bin/flip	# 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  Command to execute to control the flipbook
##	    $3  Inpoint
##	    $4  Duration
##	    $5  VLAN node ID
##	    $6  Edit timeout
##	    $7  Wait string
##	    $8  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

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

    # Set up the flipbook machine to play back the desired frames when it
    # gets the VLAN trigger signal...
    $__flip direction forward
    $__flip frame
    $__flip repeat 1
    $__flip loop off
    $__flip in 1
    $__flip out $__duration
    $__flip wait
    
    # 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 SD$__smpte_duration
    
    # Set VLAN controller to trigger the flipbook...
    # Assumes a pre-roll of 5:00
    $__send SC00:00:04:29
    $__send CO

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

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

if [ $RW_MODE = "r" ] 
then
    # The Avanzar board cannot do grabs...
    echo "$ME: Sorry, the Avanzar is output-only..."
    exit 1
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 $?

