-
Notifications
You must be signed in to change notification settings - Fork 3
/
DiodeElm.java
149 lines (123 loc) · 3.65 KB
/
DiodeElm.java
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
/*
Copyright (C) Paul Falstad and Iain Sharp
Modified by Vinyasi on 13/Sep/2017 8:13
// Mod.Begin
// Mod.End
This file is part of CircuitJS1.
CircuitJS1 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
CircuitJS1 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CircuitJS1. If not, see <http://www.gnu.org/licenses/>.
*/
package com.lushprojects.circuitjs1.client;
//import java.awt.*;
//import java.util.StringTokenizer;
class DiodeElm extends CircuitElm {
Diode diode;
static final int FLAG_FWDROP = 1;
final double defaultdrop = .805904783;
double fwdrop, zvoltage;
public DiodeElm(int xx, int yy) {
super(xx, yy);
diode = new Diode(sim);
fwdrop = defaultdrop;
zvoltage = 0;
setup();
}
public DiodeElm(int xa, int ya, int xb, int yb, int f,
StringTokenizer st) {
super(xa, ya, xb, yb, f);
diode = new Diode(sim);
fwdrop = defaultdrop;
zvoltage = 0;
if ((f & FLAG_FWDROP) > 0) {
try {
fwdrop = new Double(st.nextToken()).doubleValue();
} catch (Exception e) {
}
}
setup();
}
boolean nonLinear() { return true; }
void setup() {
diode.setup(fwdrop, zvoltage);
}
int getDumpType() { return 'd'; }
String dump() {
flags |= FLAG_FWDROP;
return super.dump() + " " + fwdrop;
}
final int hs = 8;
Polygon poly;
Point cathode[];
void setPoints() {
super.setPoints();
calcLeads(16);
cathode = newPointArray(2);
Point pa[] = newPointArray(2);
interpPoint2(lead1, lead2, pa[0], pa[1], 0, hs);
interpPoint2(lead1, lead2, cathode[0], cathode[1], 1, hs);
poly = createPolygon(pa[0], pa[1], lead2);
}
void draw(Graphics g) {
drawDiode(g);
doDots(g);
drawPosts(g);
}
void reset() {
diode.reset();
volts[0] = volts[1] = curcount = 0;
}
void drawDiode(Graphics g) {
setBbox(point1, point2, hs);
double v1 = volts[0];
double v2 = volts[1];
draw2Leads(g);
// draw arrow thingy
setPowerColor(g, true);
setVoltageColor(g, v1);
g.fillPolygon(poly);
// draw thing arrow is pointing to
setVoltageColor(g, v2);
drawThickLine(g, cathode[0], cathode[1]);
}
void stamp() { diode.stamp(nodes[0], nodes[1]); }
void doStep() {
diode.doStep(volts[0]-volts[1]);
}
void calculateCurrent() {
current = diode.calculateCurrent(volts[0]-volts[1]);
}
void getInfo(String arr[]) {
arr[0] = "diode";
arr[1] = "I = " + getCurrentText(getCurrent());
arr[2] = "Vd = " + getVoltageText(getVoltageDiff());
arr[3] = "P = " + getUnitText(getPower(), "W");
arr[4] = "Vf = " + getVoltageText(fwdrop);
}
public EditInfo getEditInfo(int n) {
if (n == 0)
return new EditInfo("Fwd Voltage @ 1A", fwdrop, 10, 1000);
return null;
}
public void setEditValue(int n, EditInfo ei) {
fwdrop = ei.value;
setup();
}
int getShortcut() { return 'd'; }
void stepFinished() {
// stop for huge currents that make simulator act weird [Falstad]
// What's so weird? I can't see it. If it's infinite surge, than that's not weird; that's good!
// Better to remove this error / stop message!!
// Mod.Begin
// if (Math.abs(current) > 1e12)
// sim.stop("max current exceeded", this);
// Mod.End
}
}