-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathee
executable file
·122 lines (110 loc) · 3.24 KB
/
ee
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
#!/usr/bin/perl
use strict;
# chip => { #_of_args => [[qw/Inputs Outputs/], sub { calculation }]}
my %expr = (
parallel_res => { '*' => [[qw/R/], sub { my $tmp = 0; map { $tmp += (1/$_) } @_; 1/$tmp } ]},
#series_res => { '*' => [[qw/R/], sub { my $tmp = 0; map { $tmp += (1/$_) } @_; return 1/$tmp } ]},
bat_j => {
2 => [ [qw/V Ah J/],
sub { $_[1] * $_[0] * 3600 } ]},
bat_wh => {
2 => [ [qw/V Ah Wh/],
sub { $_[1] * $_[0] } ]},
cap_wh => {
2 => [ [qw/V Farad Wh/],
sub { 1/2 * $_[1] * $_[0] ** 2 / 3600 } ]},
cap_j => {
2 => [ [qw/V Farad J/],
sub { 1/2 * $_[1] * $_[0] ** 2 } ]},
voltage_div => {
3 => [ [qw/Vin R1 R2 Vout/],
sub { $_[0] * ($_[2] / ($_[1] + $_[2])) } ]},
lm317 => {
2 => [ [qw/R1 R2 Voltage/],
sub { 1.25 * ($_[1] / $_[0] + 1) }],
1 => [ [qw/Voltage R1 R2/],
sub { (240, ($_[0] / 1.25 - 1) * 240) }],
},
esr => {
2 => [ [ qw/SignalV VDiff ESR/], sub { (($_[1] * 50) / ($_[0] - $_[1])) } ],
3 => [ [ qw/SignalV VDiff InlineRes ESR/], sub { (($_[1] * $_[2]) / ($_[0] - $_[1])) } ],
},
_uf => { 1 => [ [qw/uF nF pF/], sub { ($_[0] * 1e3, $_[0] * 1e6) } ] },
_nf => { 1 => [ [qw/nF uF pF/], sub { ($_[0] / 1e3, $_[0] * 1e3) } ] },
_pf => { 1 => [ [qw/pF uF nF/], sub { ($_[0] / 1e6, $_[0] / 1e3) } ] },
);
# get function, conversion, args
my ($fn, $conv, @args) = handle_args(@ARGV);
sub handle_args
{
my $conv = lc shift;
my ($end) = ($conv =~ /([a-z]+)$/i);
if ($expr{"_$end"})
{
$conv =~ s/^(.*)$end/_$end/;
unshift(@_, $1);
}
my @args;
foreach my $arg (@_)
{
push @args, ($arg =~ /^(.*)x(\d+)$/ ? ($1) x $2 : $arg);
}
my $fn = $expr{$conv}{@args} || $expr{$conv}{'*'};
return ($fn, $conv, @args);
}
usage() if !$fn;
my ($txt, $sub) = @{$fn};
push @args, &$sub(@args);
foreach my $i (0 .. $#args)
{
# conversion or calculate
print ($conv =~ /^_/ ? "$args[$i]$txt->[$i]\n" : $txt->[$i] . ": $args[$i]\n");
}
exit;
sub usage
{
die "usage: $0 <arg> [opts]
XuF # converts 'X'uF to nF/pF
bat_wh V # get watt-hours from battery
bat_j V Ah # get joules from battery
cap_wh V Farad # get watt-hours of capacitor
cap_j V Farad # get joules from capacitor
voltage_div V R1 R2 # get voltage from voltage divider (R2 to gnd)
parallel_res R1 R2 # get total resistance of parallel resistors
series_res R1 R2 # get total resistance of series resistors
lm317 V # calculate R1 and R2 to produce voltage
lm317 R1 R2 # voltage created from R1 and R2
esr V VDiff [Res] # calculate ESR of cap given V signal and VDiff when AC coupled (Res=50)
";
}
=cut
# LM217/LM317 adjustable voltage regulator
if ($arg eq 'lm317' && @ARGV == 1)
{
my $v = shift;
my $r1 = 240;
my $r2 = ($v / 1.25 - 1) * $r1;
print "R1: $r1\nR2: $r2\nVoltage: ${v}V\n";
}
elsif ($arg eq 'lm317' && @ARGV == 2)
{
my ($r1, $r2) = @ARGV;
my $v = 1.25 * ($r2 / $r1 + 1);
print "R1: $r1\nR2: $r2\nVoltage: ${v}V\n";
}
elsif ($arg =~ /^(\d+|\d*\.\d+|\d+\.\d*)(u|n|p)?f$/i)
{
my ($num, $unp) = ($1, lc($2));
my $uf;
$uf = $num/1e3 if $unp eq "n";
$uf = $num/1e6 if $unp eq "p";
my $nf = $uf * 1000;
my $pf = $nf * 1000;
print "${uf}uF\n";
print "${nf}nF\n";
print "${pf}pF\n";
}
else
{
usage();
}