#!/usr/bin/perl -w

use POSIX qw(setsid pause);
use Getopt::Std;
use Time::HiRes qw(setitimer ITIMER_REAL time usleep);
use LinuxGpib;
use n8ur qw(trim squash collapse round);
use n8ur_gpib qw(checkSRQ serviceSRQ logline timetag);

#----------
my $tags = "mjd";
my $interval = 30;

my $freq1 = "3.330MZ";
my $freq2 = "7.335MZ";
my $freq3 = "14.670MZ";
#----------


# device name from /etc/gpib.conf
my $board = "gpib0";
my $device1 = "hp3586c_1";
my $device2 = "hp3586c_2";
my $device3 = "hp3586c_3";

my $brd;
my $dev1;
my $dev2;
my $dev3;
my $tmp;
my $reading1;
my $reading2;
my $reading3;
my $fr1;
my $fr2;
my $fr3;
my $amp1;
my $amp2;
my $amp3;
my $valid1;
my $valid2;
my $valid3;
my $first = 1;

#----------
# 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';
#----------
$SIG{ALRM} = sub { };
#----------
# display usage
my $opt_string = 'h:f:';

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

usage: $0 [-h] [-a samples to average] [-g gate time] -f logfile

-h	: this (help) message
-f	: logfile using full pathname;
	  for output to console, use "-f -"

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 $logfile;

$logfile = $opt{f};

setitimer(ITIMER_REAL, $interval, $interval);

#----------
# initialize variables
my $command;
my $gpib_status;

#----------
# set up logfile
open (LOG, ">>$logfile") ||
	die "Can't open logfile $logfile!\n";
# set nonbuffered mode
select(LOG), $| = 1;

#----------
# initialize
$brd = LinuxGpib::ibfind($board);
$dev1 = LinuxGpib::ibfind($device1);
$dev2 = LinuxGpib::ibfind($device2);
$dev3 = LinuxGpib::ibfind($device3);
# clear
LinuxGpib::ibclr($dev1);
usleep(2500);
LinuxGpib::ibclr($dev2);
usleep(2500);
LinuxGpib::ibclr($dev3);
usleep(2500);
# set up
# Cal off, 50 ohm term, average on, 20 Hz bandwidth, counter on
$command = "CA0T1A1B1CN1";
LinuxGpib::ibwrt($dev1,$command,length($command));
usleep(2500);
LinuxGpib::ibwrt($dev2,$command,length($command));
usleep(2500);
LinuxGpib::ibwrt($dev3,$command,length($command));
usleep(2500);

# set frequency, do a cal
$command = "FR" . $freq1 . "CA1";
LinuxGpib::ibwrt($dev1,$command,length($command));
usleep(2500);
$command = "FR" . $freq2 . "CA1";
LinuxGpib::ibwrt($dev2,$command,length($command));
usleep(2500);
$command = "FR" . $freq3 . "CA1";
LinuxGpib::ibwrt($dev3,$command,length($command));
sleep(15);

# turn cal off
$command = "CA0";
LinuxGpib::ibwrt($dev1,$command,length($command));
usleep(2500);
LinuxGpib::ibwrt($dev2,$command,length($command));
usleep(2500);
LinuxGpib::ibwrt($dev3,$command,length($command));
sleep(1);

#----------

while (1) {
pause;
# write log file at top of loop for consistent timing
if (!$first) {
print LOG timetag($tags)," ",$fr1," ",$amp1," ",$fr2," ",$amp2," ",
	$fr3," ",$amp3,"\n";
	}

# trigger reading
$reading1 = "";
while (!$reading1) {
	$tmp = "";
	$command = "TR";
	LinuxGpib::ibwrt($dev1,$command,length($command));
	usleep(100000);
	LinuxGpib::ibrd($dev1,$tmp,23);
	$reading1 = trim($tmp);
	usleep(250000);
}

$reading2 = "";
while (!$reading2) {
	$tmp = "";
	$command = "TR";
	LinuxGpib::ibwrt($dev2,$command,length($command));
	usleep(100000);
	LinuxGpib::ibrd($dev2,$tmp,23);
	$reading2 = trim($tmp);
	usleep(250000);
}

$reading3 = "";
while (!$reading3) {
	$tmp = "";
	$command = "TR";
	LinuxGpib::ibwrt($dev3,$command,length($command));
	usleep(100000);
	LinuxGpib::ibrd($dev3,$tmp,23);
	$reading3 = trim($tmp);
	usleep(250000);
}

$valid1 = substr($reading1,0,1);
$valid2 = substr($reading2,0,1);
$valid3 = substr($reading3,0,1);
$amp1 = sprintf("%.1f",round(1,substr($reading1,1,8)));
$amp2 = sprintf("%.1f",round(1,substr($reading2,1,8)));
$amp3 = sprintf("%.1f",round(1,substr($reading3,1,8)),6);
$fr1 = substr($reading1,11,10);
$fr2 = substr($reading2,11,10);
$fr3 = substr($reading3,11,10);

$first = 0;
}

#----------

exit 0;
