#!/usr/bin/perl -w
use strict;
use English;
use Tk;

# Available drivers
my %maxvol = (
	'az', 3,
	'zx', 16,
	'rt', 10,
	'rtII', 1,
	'gt', 1,
	'sf2d', 1,
	'sfr', 15,
	'sf2r', 1,
	'mr', 1,
	'bktr', 1
);

# Configuration section
my $drv = 'rtII';
# for mr and bktr
# my $port = "";
my $port = 2;
my $fmio = "/usr/local/bin/fmio";
my $volume = $maxvol{$drv};

#predeclared scalars
my $mute_bttn = "Mute";
my $stereo = "s";

#Opening sequence, reading .tkradio for last tuned frequency and tuning it.
open (OPENCONFIG, "<.ptkradio"); # || die;
my $tmp = <OPENCONFIG>;
close (OPENCONFIG);
my @mem = split(/ /,$tmp);
my $freq = $mem[0];
system ("$fmio -d $drv$port -f $freq -v $volume -$stereo");


#Sets up the MainWindow and binds some commands.    
my $MW = MainWindow->new;
$MW->title($PROGRAM_NAME);
$MW->bind('<Key-x>' => sub { &off });
$MW->bind('<Key-Left>' => sub { $freq = $freq-0.1, &tuning });
$MW->bind('<Key-Right>' => sub { $freq = $freq+0.1, &tuning });
#$MW->bind('<Key-m>' => sub { system "$fmio -d $drv$port -v 0" });

#Create a top frame to hold all other frames.
my $top_frame = $MW->Frame(-borderwidth => '4', -relief => 'groove', -width => '400', -height => '200', -background => "blue");
$top_frame->pack(-side => 'bottom', -expand => '1', -fill => 'both');

	#Create a frame for the memory buttons.
	my $memory_frame = $top_frame->Frame(-borderwidth => '4', -relief => 'groove', 
								  -width => '100', -height => '200', -background => "blue");
	$memory_frame->pack(-side => 'right', -expand => '1', -fill => 'both');

		#Text lable for the memory frame
		$memory_frame->Label(-text => "Presets", -background => "blue", -foreground => "green")->pack();

		#Puts memory buttons in $memory_frame.
		foreach my $memory ($mem[1], $mem[2], $mem[3], $mem[4], $mem[5], $mem[6], $mem[7]) {  
			my $b = $memory_frame->Button(
							-text    => $memory,
							-relief => 'groove',
							-pady =>0,
							-height => 1,
   		   					-width   => 4,
							-command => sub { $freq = $memory, &tuning } );
							$b->pack(qw/-side top/);
		}

	#Create control frame for the rest of the stuff.
	my $control_frame = $top_frame->Frame(-borderwidth => '4', -relief => 'groove', -width => '100', -height => '200', -background => "blue");
	$control_frame->pack(-side => 'bottom', -expand => '1', -fill => 'both');
	
		#Text lable for the control frame
		$control_frame->Label(-text => "Control Panel", -background => "blue", -foreground => "green")->pack();

			my $control_button_frame = $control_frame->Frame(-borderwidth => '4', -relief => 'flat', -background => "blue");
			$control_button_frame->pack(-side => 'bottom', -expand => '1', -fill => 'none');
	
			#Put an exit button in the control frame.
			$control_button_frame->Button(-text => 'Exit', -width => '5',
							-command => sub { &off }, -relief => 'groove',
							-pady =>0)->pack(-side => 'left');

			#Mute button.
			$control_button_frame->Button(-textvariable => \$mute_bttn, -width => '5',
							-command => sub { &mute }, -relief => 'groove',
							-pady =>0)->pack(-side => 'left');
							
			#Setup button.
			$control_button_frame->Button(-text => 'Setup', -width => '5',
							-command => sub { &do_toplevel }, -relief => 'groove',
							-pady =>0)->pack(-side => 'left');

	#Create a frame for the entry entry.
	my $entry_frame = $top_frame->Frame(-borderwidth => '4', -relief => 'groove', -width => '100', -height => '200', -background => "blue");
	$entry_frame->pack(-side => 'bottom', -expand => '1', -fill => 'both');

		#Text lable for the entry frame
		$entry_frame->Label(-text => "Direct entry", -background => "blue", -foreground => "green")->pack();

		#Put a frequency entry box in the entry frame.
		$entry_frame->Label(-text => "Enter frequency:", -background => "blue", 
  		            -foreground => "white")->pack(-side => 'left', -anchor => 'n');
		my $freq_entry = $entry_frame->Entry(-width => '5', -relief => 'sunken')
  		                            ->pack(-side => 'left', -anchor => 'n');
		#This bind function takes the place of the frequency enter button
		$freq_entry->bind('<Return>'=> sub { &entry });
		$freq_entry->bind('<KP_Enter>'=> sub { &entry });

	#Create the frame displaying the frequency and pack it left.
	my $freq_frame = $top_frame->Frame(-borderwidth => '4', -relief => 'groove', -background => "blue");
	$freq_frame->pack(-side => 'left', -expand => '1', -fill => 'both');

	#Text lable for the frequency frame
	$freq_frame->Label(-text => "Frequency", -background => "blue", -foreground => "green")->pack();

      	#Create the frame for stereo/mono radiobuttons and pack it right.
		my $stereo_frame = $freq_frame->Frame(-borderwidth => '4', -background => "blue");
		$stereo_frame->pack(-side => 'right', -expand => '0', -fill => 'none');
		#radiobuttons
		my $stereo_bttn = $stereo_frame->Radiobutton(-selectcolor => "green", -highlightthickness => '0', -foreground => "white", -background => "blue", -text => "Stereo", -value => 's', -variable => \$stereo, -command => \&tuning)->pack(-side => 'top');
		my $mono_bttn = $stereo_frame->Radiobutton(-selectcolor => "green", -highlightthickness => '0', -foreground => "white", -background => "blue", -text => "Mono", -value => 'm', -variable => \$stereo, -command => \&tuning)->pack(-side => 'top');

		#Create the frame for canvas.
		my $canvas_frame = $freq_frame->Frame(-borderwidth => '4', -relief => 'flat', -width => '100', 
									 -height => '200', -background => "blue");
		$canvas_frame->pack(-side => 'top', -expand => '1', -fill => 'none');
		#Put a canvas in the canvas frame
		my $canvas = $canvas_frame->Canvas(-background => "black", -height => 18, -width => 60) ->pack();
		my $id = $canvas->createText(32,11, -anchor => "center", -text => "$freq",
			    					 -font => "-misc-fixed-medium-r-normal--20-200-75-75-c-100-iso8859-1", 
								 -fill => "white", -tag => "frequency");

		#Create the frame for tuning buttons and pack it right.
		my $tune_frame = $freq_frame->Frame(-borderwidth => '4', -relief => 'flat', -width => '100', 
									 -height => '200', -background => "blue");
		$tune_frame->pack(-side => 'bottom', -expand => '1', -fill => 'none');
		#Buttons for tuning which appear in the tuning frame.
		$tune_frame->Button(-text => "<",
  			               -command => sub { $freq = $freq-0.1; &tuning }, -relief => 'groove',
     		               -pady =>0)->pack(-side => 'left', -pady =>1);
		$tune_frame->Button(-text => ">",
   			               -command => sub { $freq = $freq+0.1, &tuning }, -relief => 'groove',
      		               -pady =>0)->pack(-side => 'right', -pady =>1);
      		               

MainLoop();

sub tuning {
    $freq = sprintf ("%.1f", $freq);
    &display_update;
    system ("$fmio -d $drv$port -f $freq -v $volume -$stereo");
}

sub display_update {
    $canvas->delete("frequency");
    $id = $canvas->createText(32,11, -anchor => "center", -text => "$freq", -font => "-misc-fixed-medium-r-normal--20-200-75-75-c-100-iso8859-1", 
						-fill => "white", -tag => "frequency");
}

sub mute {
	if ($mute_bttn eq "Mute") { $mute_bttn = "Unmute";
			system ("$fmio -d $drv$port -v 0");
	} else { $mute_bttn = "Mute";
			system ("$fmio -d $drv$port -f $freq -v $volume -$stereo");
	}
}

sub entry {
    $freq = $freq_entry->get;
    $freq = sprintf ("%.1f", $freq);
    system("$fmio -d $drv$port -f $freq -v $volume -$stereo");
    $freq_entry->delete(0, 'end');
    &display_update;	
}

sub off {
    $mem[0] = $freq;
    open (CONFIGFILE, ">.ptkradio"); # || die;
    print (CONFIGFILE "@mem");
    close (CONFIGFILE);
    system("$fmio -d $drv$port -v0");
    exit;
}

sub do_toplevel {
	if (! Exists(my $tl)) {
		$tl = $MW->Toplevel();
		$tl->title("Setup");
		my $frame0 = $tl->Frame(-borderwidth => 4, -width => 20, -background => 'blue', -foreground => 'white', -relief => 'groove')->pack;
		$frame0->Label(-text => 'Presets', -background => 'blue', -foreground => 'white')->pack;
		my $frame1 = $frame0->Frame(-borderwidth => 4, -width => 20, -background => 'blue')->pack(-side => 'top');
		$frame1->Label(-text => 'M1', -background => 'blue', -foreground => 'white')->pack(-side => 'left');
		$frame1->Entry(-width => 10, -textvariable => \$mem[1])->pack(-side => 'right');
		my $frame2 = $frame0->Frame(-borderwidth => 4, -width => 20, -background => 'blue')->pack(-side => 'top');
		$frame2->Label(-text => 'M2', -background => 'blue', -foreground => 'white')->pack(-side => 'left');
		$frame2->Entry(-width => 10, -textvariable => \$mem[2])->pack(-side => 'right');
		my $frame3 = $frame0->Frame(-borderwidth => 4, -width => 20, -background => 'blue')->pack(-side => 'top');
		$frame3->Label(-text => 'M3', -background => 'blue', -foreground => 'white')->pack(-side => 'left');
		$frame3->Entry(-width => 10, -textvariable => \$mem[3])->pack(-side => 'right');
		my $frame4 = $frame0->Frame(-borderwidth => 4, -width => 20, -background => 'blue')->pack(-side => 'top');
		$frame4->Label(-text => 'M4', -background => 'blue', -foreground => 'white')->pack(-side => 'left');
		$frame4->Entry(-width => 10, -textvariable => \$mem[4])->pack(-side => 'right');
		my $frame5 = $frame0->Frame(-borderwidth => 4, -width => 20, -background => 'blue')->pack(-side => 'top');
		$frame5->Label(-text => 'M5', -background => 'blue', -foreground => 'white')->pack(-side => 'left');
		$frame5->Entry(-width => 10, -textvariable => \$mem[5])->pack(-side => 'right');
		my $frame6 = $frame0->Frame(-borderwidth => 4, -width => 20, -background => 'blue')->pack(-side => 'top');
		$frame6->Label(-text => 'M6', -background => 'blue', -foreground => 'white')->pack(-side => 'left');
		$frame6->Entry(-width => 10, -textvariable => \$mem[6])->pack(-side => 'right');
		my $frame7 = $frame0->Frame(-borderwidth => 4, -width => 20, -background => 'blue')->pack(-side => 'top');
		$frame7->Label(-text => 'M7', -background => 'blue', -foreground => 'white')->pack(-side => 'left');
		$frame7->Entry(-width => 10, -textvariable => \$mem[7])->pack(-side => 'right');

		$frame0->Button(-text => "Write", -command => sub { &write_presets }, -relief => 'groove', -pady =>0)->pack(-side => 'left');
		$frame0->Button(-text => "Close", -command => sub { $tl->withdraw }, -relief => 'groove', -pady =>0)->pack(-side => 'right');
	} else {
		$tl->deiconify();
		$tl->raise();
	}
}

sub write_presets {

	open (CONFIGFILE, ">.ptkradio"); # || die;
	print (CONFIGFILE "@mem");
	close (CONFIGFILE);

}
