#!/usr/bin/env perl

## Cellular phone transmit/recieve frequency, channel number manager,
## and FOVC hex code to channel number converter

## This file Copyright 2000 <contact@gbppr.org> under the GPL
## NO WARRANTY. Please send bug reports / patches / reports.

sub Tx_Low {
$ch = ((($tx * 100) - 82404) / 3) + 800;
printf "\n\n----------\n\n".
       "   Cellular channel : %d\n".
       " Motorola test mode : 11%04.0f#\n",$ch,$ch + 191;
}

sub Tx_High {
$ch = ((($tx * 100) - 82503) / 3) + 1;
printf "\n\n----------\n\n".
       "   Cellular channel : %d\n".
       " Motorola test mode : 11%04.0f#\n",$ch,$ch;
}

sub Test_Tx_Freq {
if ($tx == 825) {
	print "\n\n----------\n\n".
	      "   Cellular channel : 832 or 0\n".
	      " Motorola test mode : 111023#\n";

}

elsif ($tx < 825) {
	&Tx_Low;
}

else {
	&Tx_High;
}

&Print_Freq;    
}

sub Print_Freq {
printf "\n Tower transmit / Mobile recieve frequency : %.2f MHz\n".
       " Mobile transmit / Tower recieve frequency : %.2f MHz\n\n",$rx,$tx;
}

sub Channel_Test {
if ($ch == 0) {
	$rx = 870;
        $tx = 825;
}

elsif ($ch >= 1 && $ch <= 666) {
        $tx = (.03 * $ch) + 825;
        $rx = $tx + 45;
}                     

elsif ($ch >= 667 && $ch <= 799) {
        $tx = (.03 * ($ch - 666)) + 844.98;
        $rx = $tx + 45;
}

elsif ($ch >= 800 && $ch <= 832) {
        $tx = (.03 * ($ch - 799)) + 824.01;
        $rx = $tx + 45;
 }              
}

while () {

while (!$ans || $ans > 5 || $ans =~ m/[a-z]/i) {
	print "\33[H\33[J".
	      "\n\nCellular phone transmit/recieve frequency, channel number manager\n".
      	      "and FOVC hex code to channel number converter\n----\n\n".
      	      "1) Calculate tower transmit / mobile recieve frequency\n".
              "   (given mobile transmit frequency)\n\n".
              "2) Calculate mobile transmit / tower recieve frequency\n".
              "   (given tower transmit frequency)\n\n".
              "3) Calculate transmit / recieve frequencies\n".
              "   (given channel number)\n\n".
              "4) Convert FOVC hex codes to channel number\n\n".
	      "5) Quit\n\n".
              "Enter your selection: ";
	chomp($ans = <STDIN>);
}

if ($ans == 1) {

	while (!$tx || $tx < 82404 || $tx > 84897 || $tx % 3) {
		print "\n(824.04 - 848.97 MHz)\n".
		      "Enter cellular mobile transmit frequency (in MHz): ";
		chomp($tx = <STDIN>);
		$tx =~ tr/0-9.//csd;
		$tx = $tx * 100;
	}

	$tx = $tx / 100;
	$rx = $tx + 45;

	&Test_Tx_Freq;

}

elsif ($ans == 2) {
	
	while (!$rx || $rx < 86904 || $rx > 89397 || $rx % 3) {
		print "\n(869.04 - 893.97 MHz)\n".
		      "Enter cellular tower transmit frequency (in MHz): ";
		chomp($rx = <STDIN>);
		$rx =~ tr/0-9.//csd;
		$rx = $rx * 100;
        }

	$rx = $rx / 100;
	$tx = $rx - 45;

	&Test_Tx_Freq;

}

elsif ($ans == 3) {

	while (!$ch || $ch > 832) {
		print "\n(1-832)\n".
		      "Enter cellular channel number: ";
		chomp($ch = <STDIN>);
		$ch =~ tr/0-9//csd;
        }

	&Channel_Test;
	&Test_Tx_Freq;                    
}

elsif ($ans == 4) {
	
	while (!$hex || $dec > 832) {
		print "\nxxxxNNNxxx\n\n".
	              "Enter hex digits NNN: ";
		chomp($hex = <STDIN>);
		$dec = hex $hex;
        }

	$ch = $dec & 1023;

	&Channel_Test;
	&Test_Tx_Freq;
}

elsif ($ans == 5) {

	exit;
}

print "\n\nPress ENTER to continue...";
chomp($_ = <STDIN>);
undef $ans; undef $tx; undef $rx; undef $ch; undef $hex;

}
