#!/usr/bin/perl -w

use strict;
use Eseries qw(@E6);
use Opt qw/%OPT $PRG /;
use Prefix;

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

    if ($str) {
        print $str;
        print "\n\n";
    }

    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() {
    if (@ARGV == 0) { Usage(); }

    %OPT = (
	Vref => 1.25,
	Rbot => "10k",
    );

    Opt::opt(\&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__
