-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSweepElm.java
More file actions
191 lines (184 loc) · 4.92 KB
/
SweepElm.java
File metadata and controls
191 lines (184 loc) · 4.92 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.awt.*;
import java.util.StringTokenizer;
class SweepElm extends CircuitElm {
double maxV, maxF, minF, sweepTime, frequency;
final int FLAG_LOG = 1;
final int FLAG_BIDIR = 2;
public SweepElm(int xx, int yy) {
super(xx, yy);
minF = 20; maxF = 4000;
maxV = 5;
sweepTime = .1;
flags = FLAG_BIDIR;
reset();
}
public SweepElm(int xa, int ya, int xb, int yb, int f, StringTokenizer st) {
super(xa, ya, xb, yb, f);
minF = new Double(st.nextToken()).doubleValue();
maxF = new Double(st.nextToken()).doubleValue();
maxV = new Double(st.nextToken()).doubleValue();
sweepTime = new Double(st.nextToken()).doubleValue();
reset();
}
int getDumpType() { return 170; }
int getPostCount() { return 1; }
final int circleSize = 17;
String dump() {
return super.dump() + " " + minF + " " + maxF + " " + maxV + " " +
sweepTime;
}
void setPoints() {
super.setPoints();
lead1 = interpPoint(point1, point2, 1-circleSize/dn);
}
void draw(Graphics g) {
setBbox(point1, point2, circleSize);
setVoltageColor(g, volts[0]);
drawThickLine(g, point1, lead1);
g.setColor(needsHighlight() ? selectColor : Color.gray);
setPowerColor(g, false);
int xc = point2.x; int yc = point2.y;
drawThickCircle(g, xc, yc, circleSize);
int wl = 8;
adjustBbox(xc-circleSize, yc-circleSize,
xc+circleSize, yc+circleSize);
int i;
int xl = 10;
int ox = -1, oy = -1;
long tm = System.currentTimeMillis();
//double w = (this == mouseElm ? 3 : 2);
tm %= 2000;
if (tm > 1000)
tm = 2000-tm;
double w = 1+tm*.002;
if (!sim.stoppedCheck.getState())
w = 1+2*(frequency-minF)/(maxF-minF);
for (i = -xl; i <= xl; i++) {
int yy = yc+(int) (.95*Math.sin(i*pi*w/xl)*wl);
if (ox != -1)
drawThickLine(g, ox, oy, xc+i, yy);
ox = xc+i; oy = yy;
}
if (sim.showValuesCheckItem.getState()) {
String s = getShortUnitText(frequency, "Hz");
if (dx == 0 || dy == 0)
drawValues(g, s, circleSize);
}
drawPosts(g);
curcount = updateDotCount(-current, curcount);
if (sim.dragElm != this)
drawDots(g, point1, lead1, curcount);
}
void stamp() {
sim.stampVoltageSource(0, nodes[0], voltSource);
}
double fadd, fmul, freqTime, savedTimeStep;
int dir = 1;
void setParams() {
if (frequency < minF || frequency > maxF) {
frequency = minF;
freqTime = 0;
dir = 1;
}
if ((flags & FLAG_LOG) == 0) {
fadd = dir*sim.timeStep*(maxF-minF)/sweepTime;
fmul = 1;
} else {
fadd = 0;
fmul = Math.pow(maxF/minF, dir*sim.timeStep/sweepTime);
}
savedTimeStep = sim.timeStep;
}
void reset() {
frequency = minF;
freqTime = 0;
dir = 1;
setParams();
}
double v;
void startIteration() {
// has timestep been changed?
if (sim.timeStep != savedTimeStep)
setParams();
v = Math.sin(freqTime)*maxV;
freqTime += frequency*2*pi*sim.timeStep;
frequency = frequency*fmul+fadd;
if (frequency >= maxF && dir == 1) {
if ((flags & FLAG_BIDIR) != 0) {
fadd = -fadd;
fmul = 1/fmul;
dir = -1;
} else
frequency = minF;
}
if (frequency <= minF && dir == -1) {
fadd = -fadd;
fmul = 1/fmul;
dir = 1;
}
}
void doStep() {
sim.updateVoltageSource(0, nodes[0], voltSource, v);
}
double getVoltageDiff() { return volts[0]; }
int getVoltageSourceCount() { return 1; }
boolean hasGroundConnection(int n1) { return true; }
void getInfo(String arr[]) {
arr[0] = "sweep " + (((flags & FLAG_LOG) == 0) ? "(linear)" : "(log)");
arr[1] = "I = " + getCurrentDText(getCurrent());
arr[2] = "V = " + getVoltageText(volts[0]);
arr[3] = "f = " + getUnitText(frequency, "Hz");
arr[4] = "range = " + getUnitText(minF, "Hz") + " .. " +
getUnitText(maxF, "Hz");
arr[5] = "time = " + getUnitText(sweepTime, "s");
}
public EditInfo getEditInfo(int n) {
if (n == 0)
return new EditInfo("Min Frequency (Hz)", minF, 0, 0);
if (n == 1)
return new EditInfo("Max Frequency (Hz)", maxF, 0, 0);
if (n == 2)
return new EditInfo("Sweep Time (s)", sweepTime, 0, 0);
if (n == 3) {
EditInfo ei = new EditInfo("", 0, -1, -1);
ei.checkbox = new Checkbox("Logarithmic", (flags & FLAG_LOG) != 0);
return ei;
}
if (n == 4)
return new EditInfo("Max Voltage", maxV, 0, 0);
if (n == 5) {
EditInfo ei = new EditInfo("", 0, -1, -1);
ei.checkbox = new Checkbox("Bidirectional", (flags & FLAG_BIDIR) != 0);
return ei;
}
return null;
}
public void setEditValue(int n, EditInfo ei) {
double maxfreq = 1/(8*sim.timeStep);
if (n == 0) {
minF = ei.value;
if (minF > maxfreq)
minF = maxfreq;
}
if (n == 1) {
maxF = ei.value;
if (maxF > maxfreq)
maxF = maxfreq;
}
if (n == 2)
sweepTime = ei.value;
if (n == 3) {
flags &= ~FLAG_LOG;
if (ei.checkbox.getState())
flags |= FLAG_LOG;
}
if (n == 4)
maxV = ei.value;
if (n == 5) {
flags &= ~FLAG_BIDIR;
if (ei.checkbox.getState())
flags |= FLAG_BIDIR;
}
setParams();
}
}