-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAMElm.java
More file actions
125 lines (111 loc) · 3.47 KB
/
AMElm.java
File metadata and controls
125 lines (111 loc) · 3.47 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
import java.awt.*;
import java.util.StringTokenizer;
// contributed by Edward Calver
class AMElm extends CircuitElm {
static final int FLAG_COS = 2;
double carrierfreq,signalfreq, maxVoltage, freqTimeZero;
public AMElm(int xx, int yy) {
super(xx, yy);
maxVoltage = 5;
carrierfreq = 1000;
signalfreq=40;
reset();
}
public AMElm(int xa, int ya, int xb, int yb, int f,
StringTokenizer st) {
super(xa, ya, xb, yb, f);
carrierfreq = new Double(st.nextToken()).doubleValue();
signalfreq= new Double(st.nextToken()).doubleValue();
maxVoltage = new Double(st.nextToken()).doubleValue();
if ((flags & FLAG_COS) != 0) {
flags &= ~FLAG_COS;
}
reset();
}
int getDumpType() { return 200; }
String dump() {
return super.dump() + " " +carrierfreq+" " + signalfreq + " " +maxVoltage;
}
/*void setCurrent(double c) {
current = c;
System.out.print("v current set to " + c + "\n");
}*/
void reset() {
freqTimeZero = 0;
curcount = 0;
}
int getPostCount() { return 1; }
void stamp() {
sim.stampVoltageSource(0, nodes[0], voltSource);
}
void doStep() {
sim.updateVoltageSource(0, nodes[0], voltSource, getVoltage());
}
double getVoltage() {
double w = 2*pi*(sim.t-freqTimeZero);
return ((Math.sin(w*signalfreq)+1)/2)*Math.sin(w*carrierfreq)*maxVoltage;
}
final int circleSize = 17;
void draw(Graphics g) {
setBbox(point1, point2, circleSize);
setVoltageColor(g, volts[0]);
drawThickLine(g, point1, lead1);
Font f = new Font("SansSerif", 0, 12);
g.setFont(f);
g.setColor(needsHighlight() ? selectColor : whiteColor);
setPowerColor(g, false);
double v = getVoltage();
String s = "AM";
drawCenteredText(g, s, x2, y2, true);
drawWaveform(g, point2);
drawPosts(g);
curcount = updateDotCount(-current, curcount);
if (sim.dragElm != this)
drawDots(g, point1, lead1, curcount);
}
void drawWaveform(Graphics g, Point center) {
g.setColor(needsHighlight() ? selectColor : Color.gray);
setPowerColor(g, false);
int xc = center.x; int yc = center.y;
drawThickCircle(g, xc, yc, circleSize);
int wl = 8;
adjustBbox(xc-circleSize, yc-circleSize,
xc+circleSize, yc+circleSize);
}
void setPoints() {
super.setPoints();
lead1 = interpPoint(point1, point2, 1-circleSize/dn);
}
double getVoltageDiff() { return volts[0]; }
boolean hasGroundConnection(int n1) { return true; }
int getVoltageSourceCount() {
return 1;
}
double getPower() { return -getVoltageDiff()*current; }
void getInfo(String arr[]) {
arr[0] = "AM Source";
arr[1] = "I = " + getCurrentText(getCurrent());
arr[2] = "V = " +
getVoltageText(getVoltageDiff());
arr[3] = "cf = " + getUnitText(carrierfreq, "Hz");
arr[4] = "sf = " + getUnitText(signalfreq, "Hz");
arr[5] = "Vmax = " + getVoltageText(maxVoltage);
}
public EditInfo getEditInfo(int n) {
if (n == 0)
return new EditInfo("Max Voltage", maxVoltage, -20, 20);
if (n == 1)
return new EditInfo("Carrier Frequency (Hz)", carrierfreq, 4, 500);
if (n == 2)
return new EditInfo("Signal Frequency (Hz)", signalfreq, 4, 500);
return null;
}
public void setEditValue(int n, EditInfo ei) {
if (n == 0)
maxVoltage = ei.value;
if (n == 1)
carrierfreq = ei.value;
if (n == 2)
signalfreq=ei.value;
}
}