-
Notifications
You must be signed in to change notification settings - Fork 0
/
electronics-led-r-and-w
executable file
·174 lines (155 loc) · 5.47 KB
/
electronics-led-r-and-w
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/perl
use v5.26;
use warnings;
use File::Basename;
use Math::Round;
my $rst = "\033[;m";
my $whi = "\033[37;1m";
my $yel = "\033[33;1m";
my $bcya = "\033[36;1m";
my $bred = "\033[31;1m";
my $bgblu = "\033[44m";
if (! -t *STDOUT) {
$rst = $whi = $yel = $bcya = $bred = $bgblu = "";
}
# The series values are calculated with round(10^n)^(1/m))
# Where m is the series and n is 0-(series-1)
# For example (from wikipedia):
# To calculate the E48 series: m m is 48, then
# n is incremented from 0 to 47 through the formula.
# HOWEVER, there are a whole 1 or more exceptions, so I just used wiki's tables
# ALSO, higher series nowadays sometimes include common lower-series values
# for manufacture/design portability between series.
# ^^^^^^ this is NOT accounted for by this script (at the time of this writing)
# (tol)erance
# (sig)nificant digits in value
# (vals): resistance values
my %series_sets = (
"e12" => {
"tol" => "10",
"sig" => 2, # Significant figures
"vals" => [1.0, 1.2, 1.5, 1.8, 2.2, 2.7, 3.3, 3.9, 4.7, 5.6, 6.8, 8.2],
},
"e24" => {
"tol" => "5",
"vals" => [1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1],
},
"e48" => {
"tol" => "2",
"vals" => [1.00, 1.05, 1.10, 1.15, 1.21, 1.27, 1.33, 1.40, 1.47, 1.54, 1.62, 1.69, 1.78, 1.87, 1.96, 2.05, 2.15, 2.26, 2.37, 2.49, 2.61, 2.74, 2.87, 3.01, 3.16, 3.32, 3.48, 3.65, 3.83, 4.02, 4.22, 4.42, 4.64, 4.87, 5.11, 5.36, 5.62, 5.90, 6.19, 6.49, 6.81, 7.15, 7.50, 7.87, 8.25, 8.66, 9.09, 9.53],
},
"e96" => {
"tol" => "1",
"vals" => [1.00, 1.02, 1.05, 1.07, 1.10, 1.13, 1.15, 1.18, 1.21, 1.24, 1.27, 1.30, 1.33, 1.37, 1.40, 1.43, 1.47, 1.50, 1.54, 1.58, 1.62, 1.65, 1.69, 1.74, 1.78, 1.82, 1.87, 1.91, 1.96, 2.00, 2.05, 2.10, 2.15, 2.21, 2.26, 2.32, 2.37, 2.43, 2.49, 2.55, 2.61, 2.67, 2.74, 2.80, 2.87, 2.94, 3.01, 3.09, 3.16, 3.24, 3.32, 3.40, 3.48, 3.57, 3.65, 3.74, 3.83, 3.92, 4.02, 4.12, 4.22, 4.32, 4.42, 4.53, 4.64, 4.75, 4.87, 4.99, 5.11, 5.23, 5.36, 5.49, 5.62, 5.76, 5.90, 6.04, 6.19, 6.34, 6.49, 6.65, 6.81, 6.98, 7.15, 7.32, 7.50, 7.68, 7.87, 8.06, 8.25, 8.45, 8.66, 8.87, 9.09, 9.31, 9.53, 9.76],
},
);
my $scriptname=basename $0;
if (@ARGV < 3) {
say STDERR "\nMissing values on commandline!\n";
usage();
exit;
}
my $series_default = 'e24';
my $vin=$ARGV[0]; # ex. 12
my $vf=$ARGV[1]; # 3.2
my $i=$ARGV[2]; # 0.300
my $series;
if ($#ARGV < 3) { # Series NOT provided:
$series = $series_default;
} else {
$series = $ARGV[3];
die "Invalid series given ($series). Run without options for help."
if !exists $series_sets{$series};
}
sub usage {
my $series_list = join(", ", sort keys %series_sets);
print <<~"EOT";
Calculate Diode/LED needed resistance and power use
Usage: $scriptname V Vf I [resistor series]
Where: V is the supply voltage
Vf is the diode (LED) forward voltage
I is the desired current
Series selection [default is e24]:
Available series: $series_list
Example: $scriptname 12 3.2 0.300
(or: $scriptname 12 3.2 0.300 e96)
Will output:
Supply voltage: 12 volts
Diode forward voltage: 3.2 volts
Desired current: .300 amps
${bcya}29.333333 ohms$rst needed
${yel}2.639999 W$rst consumed (and needing dissipation)
{$whi... and E-series output goes here ...$rst}
EOT
}
sub rwv_from_ivinvf {
# Resistance, watts, vdrop, from current, Vin, Vforward
my ($i, $vin, $vf) = @_;
my $vdrop = $vin-$vf;
my $r = $vdrop/$i;
my $w = $i*$i*$r;
return ($r, $w, $vdrop);
}
sub iw_from_rvinvf {
# Current and watts given Resistance, Vin, and Vforward
my ($r, $vin, $vf) = @_;
my $vdrop = $vin-$vf;
# say "vin:$vin, vdrop: $vdrop, r:$r";
my $i = ($vdrop)/$r;
my $w = $i*$i*$r;
return ($i, $w);
}
sub rnearest_higher_from_series_r {
# Find nearest resistance value in series given resistance
my ($series, $r) = @_;
my %serinfo = %{$series_sets{$series} // die "Invalid series: $series"};
my $found=0;
my $mult;
say "Using series: ", lc($series);
say " Tolerance: $serinfo{tol}";
my @vals = @{$serinfo{vals}};
my $i;
my @mults = (10, 10e2, 10e3, 10e4, 10e5, 10e6);
say "Series ($series) values:";
say " ", map {"$_ ";} @vals;
# for $mult (@mults) { say " ", join(", ", map { $_*$mult } @vals); }
for (@mults) {
$mult = $_;
for (0 .. $#vals) {
$i = $_;
# say " Testing $r against ", ($vals[$i]*$mult), " ($vals[$i] * $mult)";
if ($vals[$i]*$mult > $r) { $found=1; }
last if $found;
}
last if $found;
}
die "Unfound or bad resistance value ($r) in series $series"
if !$found || $i == 0;
# $i--;
my $val = $vals[$i] * $mult;
say " Nearest series $series value ($vals[$i] * $mult): $val";
return $val;
}
# bc versions:
#bc <<< 'vin=12;vf=3.2;i=0.300;vdrop=(vin-vf);r=vdrop/i;print r," ohms needed\n"; print i*i*r," W consumed\n"'
my ($r, $w, $vdrop) = rwv_from_ivinvf($i, $vin, $vf);
say " Supply voltage: $vin volts";
say "Diode forward voltage: $vf volts";
say " Desired current: $i amps";
say "";
printf "$bcya%.7f ohms$rst needed\n", $r;
say "$yel$w W$rst consumed (and needing dissipation)";
say '';
my $rnew = rnearest_higher_from_series_r($series, $r);
my ($inew, $wnew) = iw_from_rvinvf($rnew, $vin, $vf);
$inew = round(($inew*10e7))/10e7;
$wnew = round(($wnew*10e7))/10e7;
say <<~"EOT";
Nearest in series:
Vin: $vin
Vf: $vf
Series: $whi$series$rst
Resistor: $bcya$rnew ohms$rst
Current: $yel$inew amps$rst
Power: $bred$wnew watts$rst
EOT