#!/usr/bin/perl
#
# hp8566b-plot.pl

#----------

use strict;
use POSIX qw(setsid);
use Getopt::Std;
use Time::HiRes qw(usleep time gettimeofday);
use LinuxGpib;
use Number::Format;
use n8ur qw(trim squash parse_value);
use n8ur_gpib qw(checkSRQ serviceSRQ);

my $board = "gpib0";
my $device = "hp8566b";
my $command;
my $status;
my $device_status;
my $done = 0;
my $plot;


#----------
# handle signals and errors -- mainly to clear lockfile on termination
sub sig_handler {
	sleep 2;
	close TMPFILE;
	exit(0);
}

# there's got to be a better way!
$SIG{'HUP'} = 'sig_handler';
$SIG{'INT'} = 'sig_handler';
$SIG{'KILL'} = 'sig_handler';
$SIG{'STOP'} = 'sig_handler';
$SIG{'TERM'} = 'sig_handler';
#----------
sub timestamp() {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
	if ($mon < 10) { $mon = "0$mon"; }
	if ($hour < 10) { $hour = "0$hour"; }
	if ($min < 10) { $min = "0$min"; }
	if ($sec < 10) { $sec = "0$sec"; }
	$year=$year+1900;
	return $year . $mon . $mday . $hour . $min . $sec;
	}

#----------
# display usage
my $opt_string = 'h:s:f:';

sub usage() {
print STDERR << "EOF";

usage: $0 [-h] [-s size] -f output file 

-h      : this (help) message
-s	: size; default of 200 yields xxx x xxx
-f      : output file name; format depends on extension

EOF
}

getopts( "$opt_string", \my %opt ) or usage() and exit;

# print usage
usage() and exit if $opt{h};
usage() and exit if !$opt{f};

# set variables to command line params
my $size = 200;
if ($opt{s}) {
	$size = $opt{s};
	}

my $outfile;
$outfile = $opt{f};

# get board
my $brd = LinuxGpib::ibfind($board);

# initialize instrument
my $dev = LinuxGpib::ibfind($device) ||
	die "Can't open device $device!\n";
usleep(5000);
#----------

# turn on SRQ
$command = "R2;";
LinuxGpib::ibwrt($dev,$command,length($command));
# poll to clear status bit
$status = LinuxGpib::ibrsp($dev,$device_status);

# trigger sweep
$command = "TS";
LinuxGpib::ibwrt($dev,$command,length($command));

while ($done == 0) {
	usleep(5000);
	if (checkSRQ($brd)) {
		$status = LinuxGpib::ibrsp($dev,$device_status);
		# 0x40 is bit 6, "service me, please"
		if ($device_status & 0x40) {
			$done = 1;
	 	}
	}
}

usleep(5000);
$command = "PLOT 0,0,33333,25000;";
LinuxGpib::ibwrt($dev,$command,length($command));
usleep(5000);
LinuxGpib::ibrd($dev,$plot,65534);
usleep(50000);

# Switch back to normal
# turn off SRQ
$command = "R1;";
LinuxGpib::ibwrt($dev,$command,length($command));

usleep(5000);
# poll to clear status bit
$status = LinuxGpib::ibrsp($dev,$device_status);

# go local
LinuxGpib::ibloc($dev);

# remove annoying "SRQ 104" text from file
substr($plot,index($plot,'LBSRQ'),41,'');

# output routine
my $tmpfile = "/var/tmp/" . timestamp();
my $hpgl = $tmpfile . ".hpgl";
my $eps = $tmpfile . ".eps";

open (TMPFILE, ">$hpgl") || die "Can't open $hpgl!\n";
print TMPFILE $plot;
close TMPFILE;

system("hp2xx","-m","eps","-c","2513","-w",$size,"-h",$size,"-f",$eps,$hpgl);
system("convert",$eps,"-bordercolor","#FFF","-border","50",$outfile);

unlink "$hpgl";
unlink "$eps";

exit 0;
