#!/usr/bin/perl -w
#
# switch-test.pl
# version 0.8 -- 31 Aug 2012
#
# Record time interval readings from HP 5334a counter
# 
# Copyright 2012 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 DateTime;
use n8ur qw(lower_case trim squash collapse round);
use n8ur_gpib qw(checkSRQ serviceSRQ logline);

# device name from /etc/gpib.conf
my $board = "gpib0";
my $device = "switch_1";

my $command;
my $gpib_status;
my $switch;

# 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 = 'a:';

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

usage: $0 -a switch setting

EOF
}

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

	
my $set = 1;
if ($opt{a}) {
	$set = $opt{a};
	}

#----------
# initialize
my $brd = LinuxGpib::ibfind($board);
my $dev1 = LinuxGpib::ibfind($device);

# clear and set to listen
LinuxGpib::ibclr($dev1);
usleep(10000);
$command = "\$";
LinuxGpib::ibwrt($dev1,$command,length($command));
usleep(10000);

$command = $set;
LinuxGpib::ibwrt($dev1,$command,length($command));
usleep(10000);

# keep program going for a bit

sleep 10;
exit(0);
