#!/usr/bin/perl -w
#
# 5370-tint.pl
# version 0.6 -- 28 September 2003
#
# Record time interval readings from HP 5370b counter
# 
# Copyright 2003 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;

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

#----------

# device name from /etc/gpib.conf
my $device = "counter";

#----------
# 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
my $dev = LinuxGpib::ibfind($device);
# clear
LinuxGpib::ibclr($dev);
# set for + time interval
$command = "FN1" . "AR1" . "MD2";
LinuxGpib::ibwrt($dev,$command,length($command));
usleep(50000);
#----------

$last_time_int = 0;

# main loop
while (1) {
	# poll status
#	LinuxGpib::ibrsp($dev,$gpib_status);
	# trigger a sweep
	$command = "MR";
	LinuxGpib::ibwrt($dev,$command,length($command));
	usleep(20000);
	# wait until finished
#	$gpib_status = LinuxGpib::ibwait($dev,(0x100 | 0x4000));
#		if ($gpib_status & 0x4000) { ;}
	# get reading
	LinuxGpib::ibrd($dev,$reading,22);
	usleep(5000);
	if (substr($reading,0,2) eq "TI") {
		$time_int = substr($reading,4,18); 
		# to avoid zero phase, add 1 fs if same as last value
		if ($time_int == $last_time_int) {
			$time_int += 0.000000000000001;
		}
		$last_time_int = $time_int;
		my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
	  		= gmtime;
		printf LOG 
	  	"%4.4u-%2.2u-%2.2uT%2.2u:%2.2u:%2.2u %+2.15E\n",
	  	$year+1900,$mon+1,$mday,$hour,$min,$sec,$time_int;
	}
	else {
		my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
	  		= gmtime;
		printf LOG 
	  	"%4.4u-%2.2u-%2.2uT%2.2u:%2.2u:%2.2u Error!!!\n",
	  	$year+1900,$mon+1,$mday,$hour,$min,$sec;
	}

}	
#----------

exit 0;
