#!/usr/bin/perl
# -*- mode:perl; tab-width:4; c-basic-offset:4; c-indent-level:4; indent-tabs-mode:nil;  -*-
# Copyright (C) 2009, Bret McDanel <trixter AT 0xdecafbad.com>
#
# Version: MPL 1.1 
#
# The contents of this file are subject to the Mozilla Public License Version 
# 1.1 (the "License"); you may not use this file except in compliance with 
# the License. You may obtain a copy of the License at 
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
# for the specific language governing rights and limitations under the 
# License.
#
# The Original Code is Yaesu CAT EEPROM alignment configuration backup tool
#
# The Initial Developer of the Original Code is 
# Bret McDanel <trixter AT 0xdecafbad.com>
# Portions created by the Initial Developer are Copyright (C)
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Bret McDanel <trixter AT 0xdecafbad.com>

use Getopt::Std;


my $serial_port = undef;
my $baud_rate = 38400;
my $outline="";
my $ascii="";
my %options;


@alignment = ("HF1-RXG", "HF2-RXG", "HF3-RXG", "50M-RXG", "VHF-RXG", 
              "UHF-RXG", "SSB-S9", "SSB-FS", "FM-S1", "FM-FS", "DISC-L", 
              "DISC-H", "FM-TH1", "FM-TH2", "FM-TI1", "FM-TI2", "VCC", 
              "HF1-IC", "HF2-IC", "HF3-IC", "50M-IC", "VHF-IC", "UHF-IC", 
              "HF1-PO-MAX", "HF1-PO-MID2", "HF1-PO-MID1", "HF1-PO-MIN", 
              "HF2-PO-MAX", "HF2-PO-MID2", "HF2-PO-MID1", "HF2-PO-MIN", 
              "HF3-PO-MAX", "HF3-PO-MID2", "HF3-PO-MID1", "HF3-PO-MIN", 
              "50M-PO-MAX", "50M-PO-MID2", "50M-PO-MID1", "50M-PO-MIN", 
              "VHF-PO-MAX", "VHF-PO-MID", "VHF-PO-MIn", "UHF-PO-MAX", 
              "UHF-PO-MIN", "HF1-TXG", "HF2-TXG", "HF3-TXG", "50M-TXG", 
              "VHF-TXG", "UHF-TXG", "ALC1-M", "ALC-M", "HF1-REV-ALC", 
              "HF2-REV-ALC", "HF3-REV-ALC", "50M-REV-ALC", "VHF-REV-ALC", 
              "UHF-REV-ALC", "CW-CAR-LEVEL", "AM-CAR-LEVEL", "DEV-W", 
              "DEV-N", "MOD-MTR", "DTMF-DEV", "CTCSS-DEV", "DCS-DEV", 
              "LSB-CAR-POINT", "USB-CAR-POINT", "VSWR2 AT 10W", "VSWR3 AT 10W", 
              "ATAS-TEST", "AMTR-TEST", "HTEMP-THRESHOLD", "FTEMP-THRESHOLD"
    );


sub usage()
{
    print "Usage: $0 [-b baud] [-d device]\n";
    print "example: $0 -b 38400 -d /dev/ttyS0\n";
    exit;
}
$OS_win = ($^O eq "MSWin32") ? 1 : 0;

if ($OS_win) {
    eval "use Win32::SerialPort";
    die "Must have Win32::SerialPort correctly installed: $@\n" if ($@);
} else {
    eval "use Device::SerialPort";
    die "Must have Device::SerialPort correctly installed: $@\n" if ($@);
}



getopts("b:d:h",\%options);
usage() if defined $options{h};
$serial_port = $options{d} if defined $options{d};
$baud_rate = $options{b} if defined $options{b};
usage()  if ! defined $serial_port;

my $port = ( $OS_win ?
             (new Win32::SerialPort ($serial_port)) :
             (new Device::SerialPort ($serial_port)) )
    || die "Can't open $serial_port: $!\n";
$port->databits(8);
$port->baudrate($baud_rate); # menu item 19
$port->parity("none");
$port->stopbits(2);
$port->read_char_time(0);     # don't wait for each character
$port->read_const_time(1000); # 1 second per unfulfilled "read" call



for(my $lsb=7;$lsb<=80;$lsb+=2) {
    my $count_out = $port->write(chr(0).chr($lsb).chr(0).chr(0).chr(0xBB));
    die "write failed\n"      unless ($count_out);
    die "write incomplete\n"  if ( $count_out != 5 );

    my ($count_in, $string_in) = $port->read(2);
    die "Did not get a proper response from the radio\n"  if $count_in ne 2;

    if($alignment[$lsb-7] =~ m/SB-CAR-POINT/) {
        printf "%02d %-15s: %-15s",$lsb-6,$alignment[$lsb-7],unpack("c", substr($string_in,0,1));
    } else {
        printf "%02d %-15s: %-15s",$lsb-6,$alignment[$lsb-7],unpack("C", substr($string_in,0,1));
    }

    if($alignment[$lsb-6] =~ m/SB-CAR-POINT/) {
        printf "%02d %-15s: %-15s\n",$lsb-5,$alignment[$lsb-6],unpack("c", substr($string_in,1,1));
    } else {
        printf "%02d %-15s: %-15s\n",$lsb-5,$alignment[$lsb-6],unpack("C", substr($string_in,1,1));
    }
}

