#========================================================================= 
#          Copyright © 2000-2002 QUALCOMM Incorporated.
#                     All Rights Reserved.
#========================================================================= 

# A very simple Perl5 script for Grinder
#! /usr/local/bin/perl -w 

#Please keep the first two lines in all the sample scripts that you are writing for this version of Grinder
use GrinderPl;
use enum qw( AVK_ASTERISK=1000 AVK_NUMBER_SIGN AVK_SELECT AVK_UP AVK_DOWN AVK_LEFT AVK_RIGHT AVK_SEND AVK_CLR AVK_END);

#Used to copy the screen shot to the PC.
use lib "$ENV{BREWTOOLSDIR}\\BREW Perl Interface";
use BREWPerlIF;

###########################################################################
# Sample prel application to illustrate the shaker screen capture mechanism 
#
#	1) It initializes the perl module.
#	2) Loads the default settings.
#	3) Configures the shaker.
#	4) Does the image capture.
#	5) The user can customize the name of the captured screen's image by
#	   changing the sshot variable.
#	
###########################################################################

my $sshot = "sshot.bmp";

#Initialize the perl module. This should be the first function to be called.
if(GrinderPl::InitPerlModule()) {

	#Load the default settings. 
	GrinderPl::LoadSettings(0);

	if(GrinderPl::InitDevice("COM1")) {

		#Start configuring shaker
		if (GrinderPl::StartShaker())  {
			if(GrinderPl::CaptureScreen($sshot)) {
				print "Screen shot successful \n";			
			}
		}
	}
}
GrinderPl::ReleasePerlModule();

###########################################################################
# Sample perl application to copy the screen shot to PC 
#
#        1) It connects to the device.
#        2) Sets the mode for the file.
#        3) Copies the file to the PC.
#        4) Releases the connection.
#
###########################################################################

###########################################################################
# Variable Declaration Section
###########################################################################

# serial port name to which the device is connected
my $port = "COM1";

my $dllName = "$ENV{BTILDIR}\\OEMLayer\\QCOMOEM.dll";

# destination directory on the device
my $deviceDes = "/shaker";

# destination directory on PC
my $pcDes   = "C:\\tmp";

# place holder for storing basename of device file
#my $baseName;

# place holder for storing file name while iterating thru filename array
my $file;

my $UNRESTRICTED_FILE_ATTRIBUTE = 0x00FF;

##########################################################################
# Here starts the actual processing
##########################################################################

# connect to the device
printf "Connecting to device\n" ;
if( BREWPerlIF::ConnecttoDevice($port,$dllName )) {
	print "Connection to the device successful\n";
} else {
	die "ERROR - connect to the device on specified port $port\n";
}

# Set filest to UNRESTRICTED mode

   if (BREWPerlIF::SetDeviceFileAttribute("$deviceDes/$sshot", $UNRESTRICTED_FILE_ATTRIBUTE)) {
       printf "Successfully set mode for file: $deviceDes/$sshot\n\n";
   } 
   else {
       printf "ERROR - Failed to set mode for file: $deviceDes/$sshot\n\n";
   }


   if (BREWPerlIF::CopyFileFromDevice("$deviceDes/$sshot", "$pcDes")) {
       printf "Successfully copied $deviceDes/$sshot to PC\n\n";
   } 
   else {
       printf "ERROR -  in copying $deviceDes/$sshot to PC\n\n";
   }

printf "disconnecting from device\n" ;
if( BREWPerlIF::ReleaseDevice() ) {
	print "disconnect from the device successful\n";
} else {
	die "Could not disconnect from the device\n";
}