#!/usr/bin/perl -w
#
# 5334a.pl
# version 0.7 -- 26 February 2005
#
# Record time interval readings from HP 5334a counters
# 
# Copyright 2005 by John R. Ackermann  N8UR (jra@febo.com)
#
# This program may be copied, modified, distributed and used for 
# any legal purpose provided that (a) the copyright notice above as well
# as these terms are retained on all copies; (b) any modifications that 
# correct bugs or errors, or increase the program's functionality, are 
# sent via email to the author at the address above; and (c) such 
# modifications are made subject to these license terms.

use strict;
use POSIX qw(setsid);
use Getopt::Std;
use Time::HiRes qw(usleep);
use LinuxGpib;
use n8ur qw(trim squash collapse round);
use n8ur_gpib qw(checkSRQ serviceSRQ logline);

# device name from /etc/gpib.conf
my $board = "gpib0";
my $device1 = "5334a1";
my $device2 = "5334a2";
my $device3 = "5334a3";

my $brd;
my $dev1;
my $dev2;
my $dev3;
#----------
# 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';
#----------

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

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

usage: $0 [-h] [-a samples to average] [t iso|mjd] -f logfile

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

EOF
}

# main loop
#----------

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};

my $tag = "mjd";
if ($opt{t} eq "iso") {
	$tag = "iso";
	}

#----------


#----------
# initialize variables
my $reading;
my $time_int;
my $last_time_int;
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 counter
$brd = LinuxGpib::ibfind($board);
$dev1 = LinuxGpib::ibfind($device1);
$dev2 = LinuxGpib::ibfind($device2);
$dev3 = LinuxGpib::ibfind($device3);
# clear
LinuxGpib::ibclr($dev1);
LinuxGpib::ibclr($dev2);
LinuxGpib::ibclr($dev3);
# set up
$command = "INREWA1FN5AU0SM1";
# average 100 readings
$command = $command . "GV1";
LinuxGpib::ibwrt($dev1,$command,length($command));
LinuxGpib::ibwrt($dev2,$command,length($command));
LinuxGpib::ibwrt($dev3,$command,length($command));
usleep(50000);
#----------

my $result1;
my $result2;
my $result3;

while (1) {

	if (checkSRQ($brd)) {
		if (!$result1) {
			$result1 = substr(squash(trim(serviceSRQ($dev1))),1);
		}
		if (!$result2) {
			$result2 = substr(squash(trim(serviceSRQ($dev2))),1);
		}
		if (!$result3) {
			$result3 = substr(squash(trim(serviceSRQ($dev3))),1);
		}
	}
	if ($result1 && $result2 && $result3) {
		print LOG logline($tag,4,$result1,$result2,$result3);
		$result1 = 0;
		$result2 = 0;
		$result3 = 0;
	}
	usleep(5000);
}

#----------

exit 0;
