#!/usr/bin/perl

## This Script is free for all. Please feel free to distribute it to all of your friends! I do not warantee this program
## in any way or for any reason, if it blows up and ruins your entire business database, I accept no responsibility!
## Please email cronus@pcis.net with any problems or comments. 

## NOTE: This is not meant to run on anything but linux with perl 5.0 or greater, if you need to to run on win32
## good luck!

##getopt
use Getopt::Std;
getopt( 'hsdcv', \%opt );

##version
if ( exists( $opt{"v"} ) ) {
    print '
  rescalc 1.0  (cronus@pcis.net)' . "\n\n";
    exit;
}

##usage
if ( exists( $opt{"h"} ) ) {
    print "
  Usage: rescalc -s supply V -d drop V -c max current mA (-h) Usage\n
  Example: rescalc -s 12 -d 1.7 -c 35 \n
  If you fail to provide all three arguments you will get an interactive script.\n\n";
    exit;
}

##run calculation sub from arguments
if ( $opt{"s"} gt 0 && $opt{"d"} gt 0 && $opt{"c"} gt 0 ) {
    $supply  = $opt{"s"};
    $vdrop   = $opt{"d"};
    $current = $opt{"c"};
    &calc;
    die "\n";
}

##set up the constants
%drop = (
    "red",    1.7, "brightred", 1.9, "orange",      2,
    "yellow", 2,   "green",     2.1, "brightgreen", 3.4,
    "white",  3.4, "blue",      3.4, "430nM",       4.6
);
@colors = qw/ red brightred orange yellow green brightgreen white blue 430nM/;

##get the LED color or allow custom input
print "Please select LED color or type X if you know the voltage drop.\n";
foreach (@colors) {
    printf "%12s\n", $_;
}
print "\n", "color: ";
chomp( $color = <STDIN> );

##check the color for validity
foreach (@colors) {
    $colorcheck++ if $color eq $_;
    ( $colorcheck = 1 ) if $color eq "X";
}
die "Bad color: \"$color\" $colorcheck" unless $colorcheck eq 1;

##get custom input
if ( $color eq "X" ) {
    print "What is the voltage drop?: \n";
    chomp( $vdrop = <STDIN> );
}
else {
    $vdrop = $drop{$color};
}

die "Bad Voltage Drop!\n" unless $vdrop gt 0;

##set the current by the LED's voltage, these values are optimistic!
if ( $vdrop lt 3 ) {
    $current = 25;
}
if ( $vdrop gt 3 ) {
    $current = 18;
}
if ( $vdrop gt 4 ) {
    $current = 15;
}

##allow custom current
print "Do you want to set the max current?(y/N): ";
chomp( $setcurrent = <STDIN> );
if ( $setcurrent eq "y" ) {
    print "What is the max current?: ";
    chomp( $current = <STDIN> );
}
if ( $setcurrent eq "Y" ) {
    print "What is the max current?: ";
    chomp( $current = <STDIN> );
}

die "Bad current!\n" unless $current gt 0;
&calc;

##validate the voltage drop and calculate
sub calc {
    unless ($supply) {
        print "What is your supply voltage?: ";
        chomp( $supply = <STDIN> );
    }
    if ( $supply gt 0 ) {
        $resdrop  = ( $supply - $vdrop );
        $resistor = ( $resdrop / $current ) * 1000;
        print "You need a ";
        printf "%1.1f", $resistor;
        print " ohm resistor.\n";
    }
    else { die "Invalid Voltage\n"; }
}

=head1 NAME

rescalc 1.0 - a script to calculate the limiting resistor values for LEDs

=head1 DESCRIPTION

See README

=head1 README

calculates resistance values based on color or voltage drop of LED in combination with supply voltage 
and max current values. I've included common color defaults.
  
=head1 PREREQUISITES

This script requires Getopt::Std
  
=pod OSNAMES

Linux, Win32

=pod SCRIPT CATEGORIES

Educational : ComputerScience

=cut
