-
Notifications
You must be signed in to change notification settings - Fork 0
/
electronics-led-volts-nm
executable file
·133 lines (111 loc) · 4.65 KB
/
electronics-led-volts-nm
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
#!/usr/bin/perl
use v5.24; use strict; use warnings;
use bansi; # Used for colors (whi, rst, etc. yel, bmag, bgmag (background))
# Todo:
# * Add additional units if needed to usage() and our support
# * If frequency given in a hz unit convert to needed wavelength first
# * Perform calc() which evaluates number, units, and calculation
# (Which I believe is (1240/nm)
# * Output result to user
sub usage {
print <<~"EOT";
${whi}Shows voltage needed for producing light at a given wavelength$rst
led-volts-nm [#] - Plain number taken as nm
led-volts-nm [#[nm,um,mm,m,km]] - Units provided
led-volts-nm [#[M,k]hz] - User provides frequency
led-volts-nm -e - Give explanation
led-volts-nm -E - Give explanation without utf8
Options:
-e - Output explanation/derivation
-E - Output explanation without utf8
EOT
}
our $sym_lambda = 'λ';
our $sym_nu = 'ν';
our $sym_e = 'E';
our $sym_h = 'h';
our $sym_c = 'c';
# Subroutine to switch symbols to ASCII
sub syms_to_ascii {
$sym_lambda = 'lambda';
$sym_nu = 'nu';
$sym_e = 'E';
$sym_h = 'h';
$sym_c = 'c';
}
sub str_help { # String of full explanation
my $sect = "$bgblu$whi"; # Section headings
my $nums = "$yel"; # Numbers
my $sym = "$bcya"; # Symbols
my $equ = "$whi"; # Equations
my $unit = "$bmag"; # Units
return <<~"END_HELP";
${sect} The Planck-Einstein Relation ${rst}
The energy ($equ$sym_e$rst) of a photon is given by the Planck-Einstein relation:
$equ$sym_e = $sym_h * $sym_nu$rst
where $equ$sym_h$rst is Planck's constant (${nums}6.626 x 10^-34 ${unit}Js$rst)
and $equ$sym_nu$rst is the frequency of the photon.
The frequency ($sym$sym_nu$rst) can be related to the wavelength ($sym$sym_lambda$rst)
by the speed of light ($sym$sym_c$rst): $equ$sym_nu = $sym_c / $sym_lambda$rst
Therefore, the energy of a photon can also be written as: $equ$sym_e = ($sym_h * $sym_c) / $sym_lambda$rst
${sect} Conversion to Electron Volts ${rst}
The energy calculated from the Planck-Einstein relation is in joules ($equ${unit}J$rst)
when using SI units. However, it is often more convenient to express
photon energy in electron volts (${unit}eV$rst).
${sect} The Simplified Formula ${rst}
By substituting $sym$sym_h$rst, $sym$sym_c$rst, and the conversion factor from joules to electron volts
into the equation, and setting $sym$sym_lambda$rst in nanometers (${unit}nm$rst) for practical use, we
derive a simplified formula: $equ$sym_e (eV) = 1240 / $sym_lambda (nm)$rst
${sect} Application to LEDs ${rst}
For LEDs and other semiconductor devices, the energy calculated by this
formula represents the photon energy corresponding to the band gap of
the material, which determines the color (wavelength) of the light emitted.
END_HELP
}
sub calc {
my ($us) = @_; # User-provided string (number or number with units)
my $wavelength_nm; # Wavelength in nanometers
# Regular expressions for detecting units and converting to nm
if ($us =~ /^(\d+\.?\d*)\s*(nm)?$/i) {
$wavelength_nm = $1; # Directly in nm
} elsif ($us =~ /^(\d+\.?\d*)\s*um$/i) {
$wavelength_nm = $1 * 1000; # Micrometers to nm
} elsif ($us =~ /^(\d+\.?\d*)\s*mm$/i) {
$wavelength_nm = $1 * 1_000_000; # Millimeters to nm
} elsif ($us =~ /^(\d+\.?\d*)\s*m$/i) {
$wavelength_nm = $1 * 1_000_000_000; # Meters to nm
} elsif ($us =~ /^(\d+\.?\d*)\s*km$/i) {
$wavelength_nm = $1 * 1_000_000_000_000; # Kilometers to nm
} elsif ($us =~ /^(\d+\.?\d*)\s*(M|k)?hz$/i) {
my $frequency = $1;
my $prefix = $2 // '';
$prefix = lc $prefix;
$frequency *= 1_000_000 if $prefix eq 'm';
$frequency *= 1_000 if $prefix eq 'k';
# Convert frequency to wavelength
my $speed_of_light_m_s = 299_792_458; # Speed of light in m/s
my $wavelength_m = $speed_of_light_m_s / $frequency;
$wavelength_nm = $wavelength_m * 1_000_000_000;
} else {
print "Invalid input or unsupported units.\n";
return;
}
# Perform calculation (1240 / wavelength in nm)
my $voltage = 1240 / $wavelength_nm;
my $vfmt;
if ($voltage > -.05 && $voltage < .05) { $vfmt = "%E eV"; }
elsif ($voltage > -1 && $voltage < 1) { $vfmt = "%.5f eV"; }
elsif ($voltage > -2 && $voltage < 2) { $vfmt = "%.4f eV"; }
elsif ($voltage > -5 && $voltage < 5) { $vfmt = "%.3f eV"; }
elsif ($voltage > -1000 && $voltage < 1000) { $vfmt = "%.2f eV"; }
else { $vfmt = "%E eV"; }
printf "Voltage needed: ".$vfmt."\n", $voltage;
}
if (@ARGV<1 || grep {/^-(h|-help)$/} @ARGV) { usage(); exit; }
if (grep {/^-E$/} @ARGV) { syms_to_ascii(); }
if (grep {/^-e$/i} @ARGV) {
print str_help();
} else {
my $s = shift @ARGV;
calc($s);
}