#!/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 LOG;
	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';
#----------

my $logfile;
$logfile = "test.hpgl";

# set up logfile
open (LOG, ">$logfile") ||
	die "Can't open logfile $logfile!\n";

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

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

#$command = "TS;";
#LinuxGpib::ibwrt($dev,$command,length($command));
$command = "A2;B2;KSj;PLOT 0,0,33333,25000;";
LinuxGpib::ibwrt($dev,$command,length($command));
sleep(1);
LinuxGpib::ibrd($dev,$plot,65534);
sleep(1);

# Switch back to normal
LinuxGpib::ibloc($dev);

print LOG $plot;
system("hp2xx -m eps -c 2413 -d 300 -f test.eps test.hpgl");
system("convert test.eps -bordercolor \"#FFF\" -border 50 test.png");
exit 0;
