#! /usr/bin/perl 

use strict;

my $debug = 0;

my $device = "/dev/ttyS0";

my $command = $ARGV[0];
my $output;

my @HELP = ("
Usage:  next_control.pl [command]

Commands:	Kx		Echo
		B12AB,4000	Goto Azm-Alt
		R34AB,12CE	Goto Ra-Dec            (must be aligned)
		Z		Get Azm-ALt
		E		Get Ra-Dec             (must be aligned)
		M		Cancel Goto
		L		Goto Complete?         (1=yes,0=no)
		J		Alignment Complete?    (1=yes,0=no)

Note: This version supports firmware 1.6 commands only		

");

die "@HELP" unless ($command =~ /^(Kx|(R|B)\w\w\w\w,\w\w\w\w|Z|M|L|J|E)$/);


open(IN,"+<${device}") or die "Couldn't open $device\n";
write_out(${command})or die "Couldn't write to $device\n";

if ($command =~ /^(Z)$/) {
# Alt/Azm
	my ($azm_factor, $alt_factor) = factor();
	
	#calculate azimuth
	my $azm = 360 * $azm_factor;
	$azm = $azm - 360 if ($azm > 90);
	my ($azm_degrees, $azm_seconds_decimal) =  split(/\./, $azm);
	my $azm_seconds = "0.${azm_seconds_decimal}" * 60;

	#calculate altitude
	my $alt = 360 * $alt_factor;
	$alt = $alt - 360 if ($alt > 90);
	my ($alt_degrees, $alt_seconds_decimal) =  split(/\./, $alt);
	my $alt_seconds = "0.${alt_seconds_decimal}" * 60;




	print qq(Azm:  $azm_degrees degrees $azm_seconds arc seconds
Alt: $alt_degrees degrees $alt_seconds arc seconds
);
} elsif ($command =~ /^(E)$/) {
#Ra/Dec
	my ($ra_factor, $dec_factor) = factor();

	# calculate Right Ascension
	my $ra = 24 * $ra_factor;
	my ($hours, $ra_minutes_decimal) =  split(/\./, $ra);
	my $ra_minutes = "0.${ra_minutes_decimal}" * 60;

	# calculate Declication	
	my $dec = 360 * $dec_factor;
	$dec = $dec - 360 if ($dec > 90);
	my ($degrees, $dec_minutes_decimal) =  split(/\./, $dec);
	my $dec_minutes = "0.${dec_minutes_decimal}" * 60;

	print qq(Ra: $hours hours $ra_minutes minutes 
Dec: $degrees degrees $dec_minutes minutes\n);
} elsif ($command =~ /^(M)$/) {
	read (IN, $output, 1);
	print "HC raw output: $output\n" if $debug;
	print qq(Goto Cancelled\n) if ($output =~ /#/);
	close(IN);
} elsif ($command =~ /^(J)$/) {
	read (IN, $output, 2);
	$output = s/#//g;
	print "HC raw output: $output\n" if $debug;
	print qq(Scope is Aligned\n) if ($output =~ /#/);
	close(IN);
} elsif ($command =~ /^(Kx)$/) {
	read (IN, $output, 2);
	print "HC raw output: $output\n" if $debug;
	print qq(Scope Detected\n) if ($output =~ /x#/);
	close(IN);
} elsif ($command =~ /^(L)$/) {
	read (IN, $output, 2);
	print "HC raw output: $output\n" if $debug;
	print qq(Still Tracking...\n) if ($output =~ /1#/);
	print qq(Goto Complete\n) if ($output =~ /0#/);
	close(IN);
} elsif ($command =~ /^((R|B)\w\w\w\w,\w\w\w\w)$/) {
	read (IN, $output, 1);
	print "HC raw output: $output\n" if $debug;
	print qq(Tracking...\n) if ($output =~ /#/);
	close(IN);
} else {
	print qq(byte length not known for that command\n @HELP);
}


sub write_out {

	my $out_command = $_[0];
	chomp($out_command);
	print IN qq(${out_command}\n);
}

sub factor {
#gets data from scope and returns the factor
	my $output;
	read (IN, $output, 10);
	my $rah = hex(substr($output, 0, 2));
	my $ral = hex(substr($output, 2, 2));
	my $dech = hex(substr($output, 5, 2));
	my $decl = hex(substr($output, 7, 2));
	close(IN);
	print qq(
###BEGIN DEBUG
###\tHC Raw Output: $output
###\tRaH: ${rah}, RaL: $ral, DecH:$dech, DecL:$decl
###END DEBUG

) if $debug;
	my $ra_factor = (($rah * 256) + $ral) / 65536;
	my $dec_factor = (($dech * 256) + $decl) / 65536;
	return($ra_factor, $dec_factor);
}
