#!/usr/bin/perl -w

use strict;
use Eseries qw(@E6);
use Opt;
use Prefix;

my %opt;
sub Usage(;$) {
    my $str = shift;

    if ($str) {
        print $str;
        print "\n\n";
    }
    my $prg = Opt::prg();

    print
"Usage:
\t$prg [Vref=..] [Rbot=..] Vout1 ...
Description:
\tCalculate Rtop for given Vout values
\tVout = Vref * (1 + Rtop / Rbot)
\tRtop = Rbot * (Vout / Vref - 1)

\twhere Rtop/Rbot is a voltage divider for pwr supply
\tregulation feedback and Vref should match is the ref. voltage
\tof the regulator.
Defaults:
\tVref: 1.25V
\tRbot: 10k
";

    if ($str) {
        exit 1;
    }
    exit 0;
}

sub Vout() {
}
sub Rtop() {
}
sub Vrange() {
    $opt{Rmin} = $opt{Rbot} * ($opt{Vmin} / $opt{Vref} - 1);
    $opt{Rmax} = $opt{Rbot} * ($opt{Vmax} / $opt{Vref} - 1);

    my @arr = Eseries::E_range($opt{Rmin}, $opt{Rmax}, @E6);

    for my $rr (@arr) {
	my $v = Prefix::pfx_en($rr, "");
	$v =~ tr/ //d;
	$opt{$v} = $opt{Vref} * ( 1 + $rr / $opt{Rbot});
    }
}

sub main() {
    %opt = (
	Vref => 1.25,
	Rbot => "10k",
    );

    @ARGV = Opt::opt(\%opt,0,@ARGV);
    if (@ARGV == 0) { Usage(); }

    Prefix::decode_hash(\%opt);
    Prefix::println( 1, [ \%opt ], " ", qw/Rbot Ohm /);
    Prefix::println( 1, [ \%opt ], " ", qw/Vref V /);

    print "    Rtop          Vout\n";
    for my $arg (@ARGV) {
	my %data;
	$data{Vout} = Prefix::pfx_de($arg);
	if ($data{Vout} >= $opt{Vref}) {
	    $data{Rtop} = $opt{Rbot} * ($data{Vout} / $opt{Vref} - 1);
	}
	Prefix::println( 0, [ \%data ], " ", qw/Rtop Ohm Vout V /);
    }
}


main();
__END__
